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/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dpcm.h>
32 #include <sound/initval.h>
34 #define DPCM_MAX_BE_USERS 8
36 /* DPCM stream event, send event to FE and all active BEs. */
37 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
40 struct snd_soc_dpcm *dpcm;
42 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
44 struct snd_soc_pcm_runtime *be = dpcm->be;
46 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
47 be->dai_link->name, event, dir);
49 snd_soc_dapm_stream_event(be, dir, event);
52 snd_soc_dapm_stream_event(fe, dir, event);
57 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58 struct snd_soc_dai *soc_dai)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
63 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
64 rtd->dai_link->symmetric_rates)) {
65 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
68 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
69 SNDRV_PCM_HW_PARAM_RATE,
70 soc_dai->rate, soc_dai->rate);
73 "ASoC: Unable to apply rate constraint: %d\n",
79 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
80 rtd->dai_link->symmetric_channels)) {
81 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
84 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
85 SNDRV_PCM_HW_PARAM_CHANNELS,
90 "ASoC: Unable to apply channel symmetry constraint: %d\n",
96 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
97 rtd->dai_link->symmetric_samplebits)) {
98 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
99 soc_dai->sample_bits);
101 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
102 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
103 soc_dai->sample_bits,
104 soc_dai->sample_bits);
106 dev_err(soc_dai->dev,
107 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
116 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
117 struct snd_pcm_hw_params *params)
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
121 struct snd_soc_dai *codec_dai = rtd->codec_dai;
122 unsigned int rate, channels, sample_bits, symmetry;
124 rate = params_rate(params);
125 channels = params_channels(params);
126 sample_bits = snd_pcm_format_physical_width(params_format(params));
128 /* reject unmatched parameters when applying symmetry */
129 symmetry = cpu_dai->driver->symmetric_rates ||
130 codec_dai->driver->symmetric_rates ||
131 rtd->dai_link->symmetric_rates;
132 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
133 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
134 cpu_dai->rate, rate);
138 symmetry = cpu_dai->driver->symmetric_channels ||
139 codec_dai->driver->symmetric_channels ||
140 rtd->dai_link->symmetric_channels;
141 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
142 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
143 cpu_dai->channels, channels);
147 symmetry = cpu_dai->driver->symmetric_samplebits ||
148 codec_dai->driver->symmetric_samplebits ||
149 rtd->dai_link->symmetric_samplebits;
150 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
151 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
152 cpu_dai->sample_bits, sample_bits);
160 * List of sample sizes that might go over the bus for parameter
161 * application. There ought to be a wildcard sample size for things
162 * like the DAC/ADC resolution to use but there isn't right now.
164 static int sample_sizes[] = {
168 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
169 struct snd_soc_dai *dai)
173 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
174 bits = dai->driver->playback.sig_bits;
176 bits = dai->driver->capture.sig_bits;
181 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
182 if (bits >= sample_sizes[i])
185 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
186 sample_sizes[i], bits);
189 "ASoC: Failed to set MSB %d/%d: %d\n",
190 bits, sample_sizes[i], ret);
195 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
196 * then initialized and any private data can be allocated. This also calls
197 * startup for the cpu DAI, platform, machine and codec DAI.
199 static int soc_pcm_open(struct snd_pcm_substream *substream)
201 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 struct snd_pcm_runtime *runtime = substream->runtime;
203 struct snd_soc_platform *platform = rtd->platform;
204 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
205 struct snd_soc_dai *codec_dai = rtd->codec_dai;
206 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
207 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
210 pm_runtime_get_sync(cpu_dai->dev);
211 pm_runtime_get_sync(codec_dai->dev);
212 pm_runtime_get_sync(platform->dev);
214 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
216 /* startup the audio subsystem */
217 if (cpu_dai->driver->ops->startup) {
218 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
220 dev_err(cpu_dai->dev, "ASoC: can't open interface"
221 " %s: %d\n", cpu_dai->name, ret);
226 if (platform->driver->ops && platform->driver->ops->open) {
227 ret = platform->driver->ops->open(substream);
229 dev_err(platform->dev, "ASoC: can't open platform"
230 " %s: %d\n", platform->name, ret);
235 if (codec_dai->driver->ops->startup) {
236 ret = codec_dai->driver->ops->startup(substream, codec_dai);
238 dev_err(codec_dai->dev, "ASoC: can't open codec"
239 " %s: %d\n", codec_dai->name, ret);
244 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
245 ret = rtd->dai_link->ops->startup(substream);
247 pr_err("ASoC: %s startup failed: %d\n",
248 rtd->dai_link->name, ret);
253 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
254 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
257 /* Check that the codec and cpu DAIs are compatible */
258 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
259 runtime->hw.rate_min =
260 max(codec_dai_drv->playback.rate_min,
261 cpu_dai_drv->playback.rate_min);
262 runtime->hw.rate_max =
263 min(codec_dai_drv->playback.rate_max,
264 cpu_dai_drv->playback.rate_max);
265 runtime->hw.channels_min =
266 max(codec_dai_drv->playback.channels_min,
267 cpu_dai_drv->playback.channels_min);
268 runtime->hw.channels_max =
269 min(codec_dai_drv->playback.channels_max,
270 cpu_dai_drv->playback.channels_max);
271 runtime->hw.formats =
272 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
274 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
275 if (codec_dai_drv->playback.rates
276 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
277 runtime->hw.rates |= cpu_dai_drv->playback.rates;
278 if (cpu_dai_drv->playback.rates
279 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
280 runtime->hw.rates |= codec_dai_drv->playback.rates;
282 runtime->hw.rate_min =
283 max(codec_dai_drv->capture.rate_min,
284 cpu_dai_drv->capture.rate_min);
285 runtime->hw.rate_max =
286 min(codec_dai_drv->capture.rate_max,
287 cpu_dai_drv->capture.rate_max);
288 runtime->hw.channels_min =
289 max(codec_dai_drv->capture.channels_min,
290 cpu_dai_drv->capture.channels_min);
291 runtime->hw.channels_max =
292 min(codec_dai_drv->capture.channels_max,
293 cpu_dai_drv->capture.channels_max);
294 runtime->hw.formats =
295 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
297 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
298 if (codec_dai_drv->capture.rates
299 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
300 runtime->hw.rates |= cpu_dai_drv->capture.rates;
301 if (cpu_dai_drv->capture.rates
302 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
303 runtime->hw.rates |= codec_dai_drv->capture.rates;
307 snd_pcm_limit_hw_rates(runtime);
308 if (!runtime->hw.rates) {
309 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
310 codec_dai->name, cpu_dai->name);
313 if (!runtime->hw.formats) {
314 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
315 codec_dai->name, cpu_dai->name);
318 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
319 runtime->hw.channels_min > runtime->hw.channels_max) {
320 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
321 codec_dai->name, cpu_dai->name);
325 soc_pcm_apply_msb(substream, codec_dai);
326 soc_pcm_apply_msb(substream, cpu_dai);
328 /* Symmetry only applies if we've already got an active stream. */
329 if (cpu_dai->active) {
330 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
335 if (codec_dai->active) {
336 ret = soc_pcm_apply_symmetry(substream, codec_dai);
341 pr_debug("ASoC: %s <-> %s info:\n",
342 codec_dai->name, cpu_dai->name);
343 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
344 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
345 runtime->hw.channels_max);
346 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
347 runtime->hw.rate_max);
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
351 cpu_dai->playback_active++;
352 codec_dai->playback_active++;
354 cpu_dai->capture_active++;
355 codec_dai->capture_active++;
359 rtd->codec->active++;
360 mutex_unlock(&rtd->pcm_mutex);
364 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
365 rtd->dai_link->ops->shutdown(substream);
368 if (codec_dai->driver->ops->shutdown)
369 codec_dai->driver->ops->shutdown(substream, codec_dai);
372 if (platform->driver->ops && platform->driver->ops->close)
373 platform->driver->ops->close(substream);
376 if (cpu_dai->driver->ops->shutdown)
377 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
379 mutex_unlock(&rtd->pcm_mutex);
381 pm_runtime_put(platform->dev);
382 pm_runtime_put(codec_dai->dev);
383 pm_runtime_put(cpu_dai->dev);
389 * Power down the audio subsystem pmdown_time msecs after close is called.
390 * This is to ensure there are no pops or clicks in between any music tracks
391 * due to DAPM power cycling.
393 static void close_delayed_work(struct work_struct *work)
395 struct snd_soc_pcm_runtime *rtd =
396 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
397 struct snd_soc_dai *codec_dai = rtd->codec_dai;
399 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
401 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
402 codec_dai->driver->playback.stream_name,
403 codec_dai->playback_active ? "active" : "inactive",
404 rtd->pop_wait ? "yes" : "no");
406 /* are we waiting on this codec DAI stream */
407 if (rtd->pop_wait == 1) {
409 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
410 SND_SOC_DAPM_STREAM_STOP);
413 mutex_unlock(&rtd->pcm_mutex);
417 * Called by ALSA when a PCM substream is closed. Private data can be
418 * freed here. The cpu DAI, codec DAI, machine and platform are also
421 static int soc_pcm_close(struct snd_pcm_substream *substream)
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;
427 struct snd_soc_codec *codec = rtd->codec;
429 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432 cpu_dai->playback_active--;
433 codec_dai->playback_active--;
435 cpu_dai->capture_active--;
436 codec_dai->capture_active--;
443 /* Muting the DAC suppresses artifacts caused during digital
444 * shutdown, for example from stopping clocks.
446 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
448 if (cpu_dai->driver->ops->shutdown)
449 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
451 if (codec_dai->driver->ops->shutdown)
452 codec_dai->driver->ops->shutdown(substream, codec_dai);
454 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
455 rtd->dai_link->ops->shutdown(substream);
457 if (platform->driver->ops && platform->driver->ops->close)
458 platform->driver->ops->close(substream);
459 cpu_dai->runtime = NULL;
461 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
462 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
463 rtd->dai_link->ignore_pmdown_time) {
464 /* powered down playback stream now */
465 snd_soc_dapm_stream_event(rtd,
466 SNDRV_PCM_STREAM_PLAYBACK,
467 SND_SOC_DAPM_STREAM_STOP);
469 /* start delayed pop wq here for playback streams */
471 schedule_delayed_work(&rtd->delayed_work,
472 msecs_to_jiffies(rtd->pmdown_time));
475 /* capture streams can be powered down now */
476 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
477 SND_SOC_DAPM_STREAM_STOP);
480 mutex_unlock(&rtd->pcm_mutex);
482 pm_runtime_put(platform->dev);
483 pm_runtime_put(codec_dai->dev);
484 pm_runtime_put(cpu_dai->dev);
490 * Called by ALSA when the PCM substream is prepared, can set format, sample
491 * rate, etc. This function is non atomic and can be called multiple times,
492 * it can refer to the runtime info.
494 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
496 struct snd_soc_pcm_runtime *rtd = substream->private_data;
497 struct snd_soc_platform *platform = rtd->platform;
498 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
499 struct snd_soc_dai *codec_dai = rtd->codec_dai;
502 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
504 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
505 ret = rtd->dai_link->ops->prepare(substream);
507 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
513 if (platform->driver->ops && platform->driver->ops->prepare) {
514 ret = platform->driver->ops->prepare(substream);
516 dev_err(platform->dev, "ASoC: platform prepare error:"
522 if (codec_dai->driver->ops->prepare) {
523 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
525 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
531 if (cpu_dai->driver->ops->prepare) {
532 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
534 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
540 /* cancel any delayed stream shutdown that is pending */
541 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
544 cancel_delayed_work(&rtd->delayed_work);
547 snd_soc_dapm_stream_event(rtd, substream->stream,
548 SND_SOC_DAPM_STREAM_START);
550 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
553 mutex_unlock(&rtd->pcm_mutex);
558 * Called by ALSA when the hardware params are set by application. This
559 * function can also be called multiple times and can allocate buffers
560 * (using snd_pcm_lib_* ). It's non-atomic.
562 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
563 struct snd_pcm_hw_params *params)
565 struct snd_soc_pcm_runtime *rtd = substream->private_data;
566 struct snd_soc_platform *platform = rtd->platform;
567 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
568 struct snd_soc_dai *codec_dai = rtd->codec_dai;
571 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
573 ret = soc_pcm_params_symmetry(substream, params);
577 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
578 ret = rtd->dai_link->ops->hw_params(substream, params);
580 dev_err(rtd->card->dev, "ASoC: machine hw_params"
581 " failed: %d\n", ret);
586 if (codec_dai->driver->ops->hw_params) {
587 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
589 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
590 " %d\n", codec_dai->name, ret);
595 if (cpu_dai->driver->ops->hw_params) {
596 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
598 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
604 if (platform->driver->ops && platform->driver->ops->hw_params) {
605 ret = platform->driver->ops->hw_params(substream, params);
607 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
608 platform->name, ret);
613 /* store the parameters for each DAIs */
614 cpu_dai->rate = params_rate(params);
615 cpu_dai->channels = params_channels(params);
616 cpu_dai->sample_bits =
617 snd_pcm_format_physical_width(params_format(params));
619 codec_dai->rate = params_rate(params);
620 codec_dai->channels = params_channels(params);
621 codec_dai->sample_bits =
622 snd_pcm_format_physical_width(params_format(params));
625 mutex_unlock(&rtd->pcm_mutex);
629 if (cpu_dai->driver->ops->hw_free)
630 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
633 if (codec_dai->driver->ops->hw_free)
634 codec_dai->driver->ops->hw_free(substream, codec_dai);
637 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
638 rtd->dai_link->ops->hw_free(substream);
640 mutex_unlock(&rtd->pcm_mutex);
645 * Frees resources allocated by hw_params, can be called multiple times
647 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
649 struct snd_soc_pcm_runtime *rtd = substream->private_data;
650 struct snd_soc_platform *platform = rtd->platform;
651 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
652 struct snd_soc_dai *codec_dai = rtd->codec_dai;
653 struct snd_soc_codec *codec = rtd->codec;
655 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
657 /* clear the corresponding DAIs parameters when going to be inactive */
658 if (cpu_dai->active == 1) {
660 cpu_dai->channels = 0;
661 cpu_dai->sample_bits = 0;
664 if (codec_dai->active == 1) {
666 codec_dai->channels = 0;
667 codec_dai->sample_bits = 0;
670 /* apply codec digital mute */
672 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
674 /* free any machine hw params */
675 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
676 rtd->dai_link->ops->hw_free(substream);
678 /* free any DMA resources */
679 if (platform->driver->ops && platform->driver->ops->hw_free)
680 platform->driver->ops->hw_free(substream);
682 /* now free hw params for the DAIs */
683 if (codec_dai->driver->ops->hw_free)
684 codec_dai->driver->ops->hw_free(substream, codec_dai);
686 if (cpu_dai->driver->ops->hw_free)
687 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
689 mutex_unlock(&rtd->pcm_mutex);
693 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
695 struct snd_soc_pcm_runtime *rtd = substream->private_data;
696 struct snd_soc_platform *platform = rtd->platform;
697 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
698 struct snd_soc_dai *codec_dai = rtd->codec_dai;
701 if (codec_dai->driver->ops->trigger) {
702 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
707 if (platform->driver->ops && platform->driver->ops->trigger) {
708 ret = platform->driver->ops->trigger(substream, cmd);
713 if (cpu_dai->driver->ops->trigger) {
714 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
721 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
724 struct snd_soc_pcm_runtime *rtd = substream->private_data;
725 struct snd_soc_platform *platform = rtd->platform;
726 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
727 struct snd_soc_dai *codec_dai = rtd->codec_dai;
730 if (codec_dai->driver->ops->bespoke_trigger) {
731 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
736 if (platform->driver->bespoke_trigger) {
737 ret = platform->driver->bespoke_trigger(substream, cmd);
742 if (cpu_dai->driver->ops->bespoke_trigger) {
743 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
750 * soc level wrapper for pointer callback
751 * If cpu_dai, codec_dai, platform driver has the delay callback, than
752 * the runtime->delay will be updated accordingly.
754 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
756 struct snd_soc_pcm_runtime *rtd = substream->private_data;
757 struct snd_soc_platform *platform = rtd->platform;
758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
759 struct snd_soc_dai *codec_dai = rtd->codec_dai;
760 struct snd_pcm_runtime *runtime = substream->runtime;
761 snd_pcm_uframes_t offset = 0;
762 snd_pcm_sframes_t delay = 0;
764 if (platform->driver->ops && platform->driver->ops->pointer)
765 offset = platform->driver->ops->pointer(substream);
767 if (cpu_dai->driver->ops->delay)
768 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
770 if (codec_dai->driver->ops->delay)
771 delay += codec_dai->driver->ops->delay(substream, codec_dai);
773 if (platform->driver->delay)
774 delay += platform->driver->delay(substream, codec_dai);
776 runtime->delay = delay;
781 /* connect a FE and BE */
782 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
783 struct snd_soc_pcm_runtime *be, int stream)
785 struct snd_soc_dpcm *dpcm;
787 /* only add new dpcms */
788 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
789 if (dpcm->be == be && dpcm->fe == fe)
793 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
799 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
800 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
801 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
802 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
804 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
805 stream ? "capture" : "playback", fe->dai_link->name,
806 stream ? "<-" : "->", be->dai_link->name);
808 #ifdef CONFIG_DEBUG_FS
809 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
810 fe->debugfs_dpcm_root, &dpcm->state);
815 /* reparent a BE onto another FE */
816 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
817 struct snd_soc_pcm_runtime *be, int stream)
819 struct snd_soc_dpcm *dpcm;
820 struct snd_pcm_substream *fe_substream, *be_substream;
822 /* reparent if BE is connected to other FEs */
823 if (!be->dpcm[stream].users)
826 be_substream = snd_soc_dpcm_get_substream(be, stream);
828 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
832 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
833 stream ? "capture" : "playback",
834 dpcm->fe->dai_link->name,
835 stream ? "<-" : "->", dpcm->be->dai_link->name);
837 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
838 be_substream->runtime = fe_substream->runtime;
843 /* disconnect a BE and FE */
844 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
846 struct snd_soc_dpcm *dpcm, *d;
848 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
849 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
850 stream ? "capture" : "playback",
851 dpcm->be->dai_link->name);
853 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
856 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
857 stream ? "capture" : "playback", fe->dai_link->name,
858 stream ? "<-" : "->", dpcm->be->dai_link->name);
860 /* BEs still alive need new FE */
861 dpcm_be_reparent(fe, dpcm->be, stream);
863 #ifdef CONFIG_DEBUG_FS
864 debugfs_remove(dpcm->debugfs_state);
866 list_del(&dpcm->list_be);
867 list_del(&dpcm->list_fe);
872 /* get BE for DAI widget and stream */
873 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
874 struct snd_soc_dapm_widget *widget, int stream)
876 struct snd_soc_pcm_runtime *be;
879 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
880 for (i = 0; i < card->num_links; i++) {
883 if (!be->dai_link->no_pcm)
886 if (be->cpu_dai->playback_widget == widget ||
887 be->codec_dai->playback_widget == widget)
892 for (i = 0; i < card->num_links; i++) {
895 if (!be->dai_link->no_pcm)
898 if (be->cpu_dai->capture_widget == widget ||
899 be->codec_dai->capture_widget == widget)
904 dev_dbg(card->dev, "ASoC: can't get %s BE for %s\n",
905 stream ? "capture" : "playback", widget->name);
909 static inline struct snd_soc_dapm_widget *
910 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
912 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
913 return rtd->cpu_dai->playback_widget;
915 return rtd->cpu_dai->capture_widget;
918 static inline struct snd_soc_dapm_widget *
919 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
921 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
922 return rtd->codec_dai->playback_widget;
924 return rtd->codec_dai->capture_widget;
927 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
928 struct snd_soc_dapm_widget *widget)
932 for (i = 0; i < list->num_widgets; i++) {
933 if (widget == list->widgets[i])
940 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
941 int stream, struct snd_soc_dapm_widget_list **list_)
943 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
944 struct snd_soc_dapm_widget_list *list;
947 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
948 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
952 /* get number of valid DAI paths and their widgets */
953 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
955 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
956 stream ? "capture" : "playback");
962 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
967 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
968 struct snd_soc_dapm_widget_list **list_)
970 struct snd_soc_dpcm *dpcm;
971 struct snd_soc_dapm_widget_list *list = *list_;
972 struct snd_soc_dapm_widget *widget;
975 /* Destroy any old FE <--> BE connections */
976 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
978 /* is there a valid CPU DAI widget for this BE */
979 widget = rtd_get_cpu_widget(dpcm->be, stream);
981 /* prune the BE if it's no longer in our active list */
982 if (widget && widget_in_list(list, widget))
985 /* is there a valid CODEC DAI widget for this BE */
986 widget = rtd_get_codec_widget(dpcm->be, stream);
988 /* prune the BE if it's no longer in our active list */
989 if (widget && widget_in_list(list, widget))
992 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
993 stream ? "capture" : "playback",
994 dpcm->be->dai_link->name, fe->dai_link->name);
995 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
996 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1000 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1004 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1005 struct snd_soc_dapm_widget_list **list_)
1007 struct snd_soc_card *card = fe->card;
1008 struct snd_soc_dapm_widget_list *list = *list_;
1009 struct snd_soc_pcm_runtime *be;
1010 int i, new = 0, err;
1012 /* Create any new FE <--> BE connections */
1013 for (i = 0; i < list->num_widgets; i++) {
1015 switch (list->widgets[i]->id) {
1016 case snd_soc_dapm_dai_in:
1017 case snd_soc_dapm_dai_out:
1023 /* is there a valid BE rtd for this widget */
1024 be = dpcm_get_be(card, list->widgets[i], stream);
1026 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1027 list->widgets[i]->name);
1031 /* make sure BE is a real BE */
1032 if (!be->dai_link->no_pcm)
1035 /* don't connect if FE is not running */
1036 if (!fe->dpcm[stream].runtime)
1039 /* newly connected FE and BE */
1040 err = dpcm_be_connect(fe, be, stream);
1042 dev_err(fe->dev, "ASoC: can't connect %s\n",
1043 list->widgets[i]->name);
1045 } else if (err == 0) /* already connected */
1049 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1053 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1058 * Find the corresponding BE DAIs that source or sink audio to this
1061 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1062 int stream, struct snd_soc_dapm_widget_list **list, int new)
1065 return dpcm_add_paths(fe, stream, list);
1067 return dpcm_prune_paths(fe, stream, list);
1070 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1072 struct snd_soc_dpcm *dpcm;
1074 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1075 dpcm->be->dpcm[stream].runtime_update =
1076 SND_SOC_DPCM_UPDATE_NO;
1079 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1082 struct snd_soc_dpcm *dpcm;
1084 /* disable any enabled and non active backends */
1085 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1087 struct snd_soc_pcm_runtime *be = dpcm->be;
1088 struct snd_pcm_substream *be_substream =
1089 snd_soc_dpcm_get_substream(be, stream);
1091 if (be->dpcm[stream].users == 0)
1092 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1093 stream ? "capture" : "playback",
1094 be->dpcm[stream].state);
1096 if (--be->dpcm[stream].users != 0)
1099 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1102 soc_pcm_close(be_substream);
1103 be_substream->runtime = NULL;
1104 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1108 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1110 struct snd_soc_dpcm *dpcm;
1113 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1114 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1116 struct snd_soc_pcm_runtime *be = dpcm->be;
1117 struct snd_pcm_substream *be_substream =
1118 snd_soc_dpcm_get_substream(be, stream);
1120 /* is this op for this BE ? */
1121 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1124 /* first time the dpcm is open ? */
1125 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1126 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1127 stream ? "capture" : "playback",
1128 be->dpcm[stream].state);
1130 if (be->dpcm[stream].users++ != 0)
1133 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1134 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1137 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1139 be_substream->runtime = be->dpcm[stream].runtime;
1140 err = soc_pcm_open(be_substream);
1142 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1143 be->dpcm[stream].users--;
1144 if (be->dpcm[stream].users < 0)
1145 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1146 stream ? "capture" : "playback",
1147 be->dpcm[stream].state);
1149 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1153 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1160 /* disable any enabled and non active backends */
1161 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1162 struct snd_soc_pcm_runtime *be = dpcm->be;
1163 struct snd_pcm_substream *be_substream =
1164 snd_soc_dpcm_get_substream(be, stream);
1166 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1169 if (be->dpcm[stream].users == 0)
1170 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1171 stream ? "capture" : "playback",
1172 be->dpcm[stream].state);
1174 if (--be->dpcm[stream].users != 0)
1177 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1180 soc_pcm_close(be_substream);
1181 be_substream->runtime = NULL;
1182 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1188 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1190 struct snd_pcm_runtime *runtime = substream->runtime;
1191 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1192 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1193 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1195 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1196 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1197 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1198 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1199 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1200 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1201 runtime->hw.rates = cpu_dai_drv->playback.rates;
1203 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1204 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1205 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1206 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1207 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1208 runtime->hw.rates = cpu_dai_drv->capture.rates;
1212 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1214 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1215 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1216 int stream = fe_substream->stream, ret = 0;
1218 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1220 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1222 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1226 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1228 /* start the DAI frontend */
1229 ret = soc_pcm_open(fe_substream);
1231 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1235 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1237 dpcm_set_fe_runtime(fe_substream);
1238 snd_pcm_limit_hw_rates(runtime);
1240 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1244 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1246 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1250 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1252 struct snd_soc_dpcm *dpcm;
1254 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1255 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1257 struct snd_soc_pcm_runtime *be = dpcm->be;
1258 struct snd_pcm_substream *be_substream =
1259 snd_soc_dpcm_get_substream(be, stream);
1261 /* is this op for this BE ? */
1262 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1265 if (be->dpcm[stream].users == 0)
1266 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1267 stream ? "capture" : "playback",
1268 be->dpcm[stream].state);
1270 if (--be->dpcm[stream].users != 0)
1273 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1274 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1277 dev_dbg(be->dev, "ASoC: close BE %s\n",
1278 dpcm->fe->dai_link->name);
1280 soc_pcm_close(be_substream);
1281 be_substream->runtime = NULL;
1283 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1288 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1290 struct snd_soc_pcm_runtime *fe = substream->private_data;
1291 int stream = substream->stream;
1293 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1295 /* shutdown the BEs */
1296 dpcm_be_dai_shutdown(fe, substream->stream);
1298 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1300 /* now shutdown the frontend */
1301 soc_pcm_close(substream);
1303 /* run the stream event for each BE */
1304 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1306 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1307 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1311 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1313 struct snd_soc_dpcm *dpcm;
1315 /* only hw_params backends that are either sinks or sources
1316 * to this frontend DAI */
1317 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1319 struct snd_soc_pcm_runtime *be = dpcm->be;
1320 struct snd_pcm_substream *be_substream =
1321 snd_soc_dpcm_get_substream(be, stream);
1323 /* is this op for this BE ? */
1324 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1327 /* only free hw when no longer used - check all FEs */
1328 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1331 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1332 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1333 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1334 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1335 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1338 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1339 dpcm->fe->dai_link->name);
1341 soc_pcm_hw_free(be_substream);
1343 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1349 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1351 struct snd_soc_pcm_runtime *fe = substream->private_data;
1352 int err, stream = substream->stream;
1354 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1355 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1357 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1359 /* call hw_free on the frontend */
1360 err = soc_pcm_hw_free(substream);
1362 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1363 fe->dai_link->name);
1365 /* only hw_params backends that are either sinks or sources
1366 * to this frontend DAI */
1367 err = dpcm_be_dai_hw_free(fe, stream);
1369 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1370 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1372 mutex_unlock(&fe->card->mutex);
1376 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1378 struct snd_soc_dpcm *dpcm;
1381 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1383 struct snd_soc_pcm_runtime *be = dpcm->be;
1384 struct snd_pcm_substream *be_substream =
1385 snd_soc_dpcm_get_substream(be, stream);
1387 /* is this op for this BE ? */
1388 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1391 /* only allow hw_params() if no connected FEs are running */
1392 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1395 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1396 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1397 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1400 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1401 dpcm->fe->dai_link->name);
1403 /* copy params for each dpcm */
1404 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1405 sizeof(struct snd_pcm_hw_params));
1407 /* perform any hw_params fixups */
1408 if (be->dai_link->be_hw_params_fixup) {
1409 ret = be->dai_link->be_hw_params_fixup(be,
1413 "ASoC: hw_params BE fixup failed %d\n",
1419 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1421 dev_err(dpcm->be->dev,
1422 "ASoC: hw_params BE failed %d\n", ret);
1426 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1431 /* disable any enabled and non active backends */
1432 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1433 struct snd_soc_pcm_runtime *be = dpcm->be;
1434 struct snd_pcm_substream *be_substream =
1435 snd_soc_dpcm_get_substream(be, stream);
1437 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1440 /* only allow hw_free() if no connected FEs are running */
1441 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1444 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1447 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1450 soc_pcm_hw_free(be_substream);
1456 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1457 struct snd_pcm_hw_params *params)
1459 struct snd_soc_pcm_runtime *fe = substream->private_data;
1460 int ret, stream = substream->stream;
1462 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1463 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1465 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1466 sizeof(struct snd_pcm_hw_params));
1467 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1469 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1473 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1474 fe->dai_link->name, params_rate(params),
1475 params_channels(params), params_format(params));
1477 /* call hw_params on the frontend */
1478 ret = soc_pcm_hw_params(substream, params);
1480 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1481 dpcm_be_dai_hw_free(fe, stream);
1483 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1486 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1487 mutex_unlock(&fe->card->mutex);
1491 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1492 struct snd_pcm_substream *substream, int cmd)
1496 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1497 dpcm->fe->dai_link->name, cmd);
1499 ret = soc_pcm_trigger(substream, cmd);
1501 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1506 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1509 struct snd_soc_dpcm *dpcm;
1512 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1514 struct snd_soc_pcm_runtime *be = dpcm->be;
1515 struct snd_pcm_substream *be_substream =
1516 snd_soc_dpcm_get_substream(be, stream);
1518 /* is this op for this BE ? */
1519 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1523 case SNDRV_PCM_TRIGGER_START:
1524 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1525 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1528 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1532 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1534 case SNDRV_PCM_TRIGGER_RESUME:
1535 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1538 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1542 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1544 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1545 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1548 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1552 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1554 case SNDRV_PCM_TRIGGER_STOP:
1555 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1558 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1561 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1565 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1567 case SNDRV_PCM_TRIGGER_SUSPEND:
1568 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1571 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1574 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1578 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1580 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1581 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1584 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1587 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1591 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1598 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1600 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1602 struct snd_soc_pcm_runtime *fe = substream->private_data;
1603 int stream = substream->stream, ret;
1604 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1606 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1609 case SND_SOC_DPCM_TRIGGER_PRE:
1610 /* call trigger on the frontend before the backend. */
1612 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1613 fe->dai_link->name, cmd);
1615 ret = soc_pcm_trigger(substream, cmd);
1617 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1621 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1623 case SND_SOC_DPCM_TRIGGER_POST:
1624 /* call trigger on the frontend after the backend. */
1626 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1628 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1632 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1633 fe->dai_link->name, cmd);
1635 ret = soc_pcm_trigger(substream, cmd);
1637 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1638 /* bespoke trigger() - handles both FE and BEs */
1640 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1641 fe->dai_link->name, cmd);
1643 ret = soc_pcm_bespoke_trigger(substream, cmd);
1645 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1650 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1651 fe->dai_link->name);
1657 case SNDRV_PCM_TRIGGER_START:
1658 case SNDRV_PCM_TRIGGER_RESUME:
1659 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1660 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1662 case SNDRV_PCM_TRIGGER_STOP:
1663 case SNDRV_PCM_TRIGGER_SUSPEND:
1664 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1665 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1670 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1674 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1676 struct snd_soc_dpcm *dpcm;
1679 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1681 struct snd_soc_pcm_runtime *be = dpcm->be;
1682 struct snd_pcm_substream *be_substream =
1683 snd_soc_dpcm_get_substream(be, stream);
1685 /* is this op for this BE ? */
1686 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1689 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1690 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1693 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1694 dpcm->fe->dai_link->name);
1696 ret = soc_pcm_prepare(be_substream);
1698 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1703 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1708 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1710 struct snd_soc_pcm_runtime *fe = substream->private_data;
1711 int stream = substream->stream, ret = 0;
1713 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1715 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1717 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1719 /* there is no point preparing this FE if there are no BEs */
1720 if (list_empty(&fe->dpcm[stream].be_clients)) {
1721 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1722 fe->dai_link->name);
1727 ret = dpcm_be_dai_prepare(fe, substream->stream);
1731 /* call prepare on the frontend */
1732 ret = soc_pcm_prepare(substream);
1734 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1735 fe->dai_link->name);
1739 /* run the stream event for each BE */
1740 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1741 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1744 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1745 mutex_unlock(&fe->card->mutex);
1750 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1751 unsigned int cmd, void *arg)
1753 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1754 struct snd_soc_platform *platform = rtd->platform;
1756 if (platform->driver->ops->ioctl)
1757 return platform->driver->ops->ioctl(substream, cmd, arg);
1758 return snd_pcm_lib_ioctl(substream, cmd, arg);
1761 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1763 struct snd_pcm_substream *substream =
1764 snd_soc_dpcm_get_substream(fe, stream);
1765 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1768 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1769 stream ? "capture" : "playback", fe->dai_link->name);
1771 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1772 /* call bespoke trigger - FE takes care of all BE triggers */
1773 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1774 fe->dai_link->name);
1776 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1778 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1780 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1781 fe->dai_link->name);
1783 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1785 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1788 err = dpcm_be_dai_hw_free(fe, stream);
1790 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1792 err = dpcm_be_dai_shutdown(fe, stream);
1794 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1796 /* run the stream event for each BE */
1797 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1802 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1804 struct snd_pcm_substream *substream =
1805 snd_soc_dpcm_get_substream(fe, stream);
1806 struct snd_soc_dpcm *dpcm;
1807 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1810 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1811 stream ? "capture" : "playback", fe->dai_link->name);
1813 /* Only start the BE if the FE is ready */
1814 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1815 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1818 /* startup must always be called for new BEs */
1819 ret = dpcm_be_dai_startup(fe, stream);
1823 /* keep going if FE state is > open */
1824 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1827 ret = dpcm_be_dai_hw_params(fe, stream);
1831 /* keep going if FE state is > hw_params */
1832 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1836 ret = dpcm_be_dai_prepare(fe, stream);
1840 /* run the stream event for each BE */
1841 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1843 /* keep going if FE state is > prepare */
1844 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1845 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1848 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1849 /* call trigger on the frontend - FE takes care of all BE triggers */
1850 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1851 fe->dai_link->name);
1853 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1855 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1859 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1860 fe->dai_link->name);
1862 ret = dpcm_be_dai_trigger(fe, stream,
1863 SNDRV_PCM_TRIGGER_START);
1865 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1873 dpcm_be_dai_hw_free(fe, stream);
1875 dpcm_be_dai_shutdown(fe, stream);
1877 /* disconnect any non started BEs */
1878 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1879 struct snd_soc_pcm_runtime *be = dpcm->be;
1880 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1881 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1887 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1891 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1892 ret = dpcm_run_update_startup(fe, stream);
1894 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1895 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1900 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1904 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1905 ret = dpcm_run_update_shutdown(fe, stream);
1907 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1908 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1913 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1916 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1918 struct snd_soc_card *card;
1919 int i, old, new, paths;
1922 card = widget->codec->card;
1923 else if (widget->platform)
1924 card = widget->platform->card;
1928 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1929 for (i = 0; i < card->num_rtd; i++) {
1930 struct snd_soc_dapm_widget_list *list;
1931 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1933 /* make sure link is FE */
1934 if (!fe->dai_link->dynamic)
1937 /* only check active links */
1938 if (!fe->cpu_dai->active)
1941 /* DAPM sync will call this to update DSP paths */
1942 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1943 fe->dai_link->name);
1945 /* skip if FE doesn't have playback capability */
1946 if (!fe->cpu_dai->driver->playback.channels_min)
1949 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1951 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1952 fe->dai_link->name, "playback");
1953 mutex_unlock(&card->mutex);
1957 /* update any new playback paths */
1958 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1960 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1961 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1962 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1965 /* update any old playback paths */
1966 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1968 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1969 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1970 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1974 /* skip if FE doesn't have capture capability */
1975 if (!fe->cpu_dai->driver->capture.channels_min)
1978 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1980 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1981 fe->dai_link->name, "capture");
1982 mutex_unlock(&card->mutex);
1986 /* update any new capture paths */
1987 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1989 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1990 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1991 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1994 /* update any old capture paths */
1995 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1997 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1998 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1999 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2002 dpcm_path_put(&list);
2005 mutex_unlock(&card->mutex);
2008 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2010 struct snd_soc_dpcm *dpcm;
2011 struct list_head *clients =
2012 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2014 list_for_each_entry(dpcm, clients, list_be) {
2016 struct snd_soc_pcm_runtime *be = dpcm->be;
2017 struct snd_soc_dai *dai = be->codec_dai;
2018 struct snd_soc_dai_driver *drv = dai->driver;
2020 if (be->dai_link->ignore_suspend)
2023 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
2025 if (drv->ops->digital_mute && dai->playback_active)
2026 drv->ops->digital_mute(dai, mute);
2032 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2034 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2035 struct snd_soc_dpcm *dpcm;
2036 struct snd_soc_dapm_widget_list *list;
2038 int stream = fe_substream->stream;
2040 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2041 fe->dpcm[stream].runtime = fe_substream->runtime;
2043 if (dpcm_path_get(fe, stream, &list) <= 0) {
2044 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2045 fe->dai_link->name, stream ? "capture" : "playback");
2048 /* calculate valid and active FE <-> BE dpcms */
2049 dpcm_process_paths(fe, stream, &list, 1);
2051 ret = dpcm_fe_dai_startup(fe_substream);
2053 /* clean up all links */
2054 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2055 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2057 dpcm_be_disconnect(fe, stream);
2058 fe->dpcm[stream].runtime = NULL;
2061 dpcm_clear_pending_state(fe, stream);
2062 dpcm_path_put(&list);
2063 mutex_unlock(&fe->card->mutex);
2067 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2069 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2070 struct snd_soc_dpcm *dpcm;
2071 int stream = fe_substream->stream, ret;
2073 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2074 ret = dpcm_fe_dai_shutdown(fe_substream);
2076 /* mark FE's links ready to prune */
2077 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2078 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2080 dpcm_be_disconnect(fe, stream);
2082 fe->dpcm[stream].runtime = NULL;
2083 mutex_unlock(&fe->card->mutex);
2087 /* create a new pcm */
2088 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2090 struct snd_soc_platform *platform = rtd->platform;
2091 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2092 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2093 struct snd_pcm *pcm;
2095 int ret = 0, playback = 0, capture = 0;
2097 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2098 if (cpu_dai->driver->playback.channels_min)
2100 if (cpu_dai->driver->capture.channels_min)
2103 if (codec_dai->driver->playback.channels_min &&
2104 cpu_dai->driver->playback.channels_min)
2106 if (codec_dai->driver->capture.channels_min &&
2107 cpu_dai->driver->capture.channels_min)
2111 /* create the PCM */
2112 if (rtd->dai_link->no_pcm) {
2113 snprintf(new_name, sizeof(new_name), "(%s)",
2114 rtd->dai_link->stream_name);
2116 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2117 playback, capture, &pcm);
2119 if (rtd->dai_link->dynamic)
2120 snprintf(new_name, sizeof(new_name), "%s (*)",
2121 rtd->dai_link->stream_name);
2123 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2124 rtd->dai_link->stream_name, codec_dai->name, num);
2126 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2130 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2131 rtd->dai_link->name);
2134 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2136 /* DAPM dai link stream work */
2137 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2140 pcm->private_data = rtd;
2142 if (rtd->dai_link->no_pcm) {
2144 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2146 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2150 /* ASoC PCM operations */
2151 if (rtd->dai_link->dynamic) {
2152 rtd->ops.open = dpcm_fe_dai_open;
2153 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2154 rtd->ops.prepare = dpcm_fe_dai_prepare;
2155 rtd->ops.trigger = dpcm_fe_dai_trigger;
2156 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2157 rtd->ops.close = dpcm_fe_dai_close;
2158 rtd->ops.pointer = soc_pcm_pointer;
2159 rtd->ops.ioctl = soc_pcm_ioctl;
2161 rtd->ops.open = soc_pcm_open;
2162 rtd->ops.hw_params = soc_pcm_hw_params;
2163 rtd->ops.prepare = soc_pcm_prepare;
2164 rtd->ops.trigger = soc_pcm_trigger;
2165 rtd->ops.hw_free = soc_pcm_hw_free;
2166 rtd->ops.close = soc_pcm_close;
2167 rtd->ops.pointer = soc_pcm_pointer;
2168 rtd->ops.ioctl = soc_pcm_ioctl;
2171 if (platform->driver->ops) {
2172 rtd->ops.ack = platform->driver->ops->ack;
2173 rtd->ops.copy = platform->driver->ops->copy;
2174 rtd->ops.silence = platform->driver->ops->silence;
2175 rtd->ops.page = platform->driver->ops->page;
2176 rtd->ops.mmap = platform->driver->ops->mmap;
2180 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2183 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2185 if (platform->driver->pcm_new) {
2186 ret = platform->driver->pcm_new(rtd);
2188 dev_err(platform->dev,
2189 "ASoC: pcm constructor failed: %d\n",
2195 pcm->private_free = platform->driver->pcm_free;
2197 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2202 /* is the current PCM operation for this FE ? */
2203 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2205 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2209 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2211 /* is the current PCM operation for this BE ? */
2212 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2213 struct snd_soc_pcm_runtime *be, int stream)
2215 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2216 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2217 be->dpcm[stream].runtime_update))
2221 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2223 /* get the substream for this BE */
2224 struct snd_pcm_substream *
2225 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2227 return be->pcm->streams[stream].substream;
2229 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2231 /* get the BE runtime state */
2232 enum snd_soc_dpcm_state
2233 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2235 return be->dpcm[stream].state;
2237 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2239 /* set the BE runtime state */
2240 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2241 int stream, enum snd_soc_dpcm_state state)
2243 be->dpcm[stream].state = state;
2245 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2248 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2249 * are not running, paused or suspended for the specified stream direction.
2251 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2252 struct snd_soc_pcm_runtime *be, int stream)
2254 struct snd_soc_dpcm *dpcm;
2257 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2262 state = dpcm->fe->dpcm[stream].state;
2263 if (state == SND_SOC_DPCM_STATE_START ||
2264 state == SND_SOC_DPCM_STATE_PAUSED ||
2265 state == SND_SOC_DPCM_STATE_SUSPEND)
2269 /* it's safe to free/stop this BE DAI */
2272 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2275 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2276 * running, paused or suspended for the specified stream direction.
2278 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2279 struct snd_soc_pcm_runtime *be, int stream)
2281 struct snd_soc_dpcm *dpcm;
2284 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2289 state = dpcm->fe->dpcm[stream].state;
2290 if (state == SND_SOC_DPCM_STATE_START ||
2291 state == SND_SOC_DPCM_STATE_PAUSED ||
2292 state == SND_SOC_DPCM_STATE_SUSPEND ||
2293 state == SND_SOC_DPCM_STATE_PREPARE)
2297 /* it's safe to change hw_params */
2300 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2302 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2303 int cmd, struct snd_soc_platform *platform)
2305 if (platform->driver->ops->trigger)
2306 return platform->driver->ops->trigger(substream, cmd);
2309 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2311 #ifdef CONFIG_DEBUG_FS
2312 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2315 case SND_SOC_DPCM_STATE_NEW:
2317 case SND_SOC_DPCM_STATE_OPEN:
2319 case SND_SOC_DPCM_STATE_HW_PARAMS:
2321 case SND_SOC_DPCM_STATE_PREPARE:
2323 case SND_SOC_DPCM_STATE_START:
2325 case SND_SOC_DPCM_STATE_STOP:
2327 case SND_SOC_DPCM_STATE_SUSPEND:
2329 case SND_SOC_DPCM_STATE_PAUSED:
2331 case SND_SOC_DPCM_STATE_HW_FREE:
2333 case SND_SOC_DPCM_STATE_CLOSE:
2340 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2341 int stream, char *buf, size_t size)
2343 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2344 struct snd_soc_dpcm *dpcm;
2348 offset += snprintf(buf + offset, size - offset,
2349 "[%s - %s]\n", fe->dai_link->name,
2350 stream ? "Capture" : "Playback");
2352 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2353 dpcm_state_string(fe->dpcm[stream].state));
2355 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2356 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2357 offset += snprintf(buf + offset, size - offset,
2359 "Format = %s, Channels = %d, Rate = %d\n",
2360 snd_pcm_format_name(params_format(params)),
2361 params_channels(params),
2362 params_rate(params));
2365 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2367 if (list_empty(&fe->dpcm[stream].be_clients)) {
2368 offset += snprintf(buf + offset, size - offset,
2369 " No active DSP links\n");
2373 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2374 struct snd_soc_pcm_runtime *be = dpcm->be;
2375 params = &dpcm->hw_params;
2377 offset += snprintf(buf + offset, size - offset,
2378 "- %s\n", be->dai_link->name);
2380 offset += snprintf(buf + offset, size - offset,
2382 dpcm_state_string(be->dpcm[stream].state));
2384 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2385 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2386 offset += snprintf(buf + offset, size - offset,
2387 " Hardware Params: "
2388 "Format = %s, Channels = %d, Rate = %d\n",
2389 snd_pcm_format_name(params_format(params)),
2390 params_channels(params),
2391 params_rate(params));
2398 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2399 size_t count, loff_t *ppos)
2401 struct snd_soc_pcm_runtime *fe = file->private_data;
2402 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2405 buf = kmalloc(out_count, GFP_KERNEL);
2409 if (fe->cpu_dai->driver->playback.channels_min)
2410 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2411 buf + offset, out_count - offset);
2413 if (fe->cpu_dai->driver->capture.channels_min)
2414 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2415 buf + offset, out_count - offset);
2417 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2423 static const struct file_operations dpcm_state_fops = {
2424 .open = simple_open,
2425 .read = dpcm_state_read_file,
2426 .llseek = default_llseek,
2429 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2434 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2435 rtd->card->debugfs_card_root);
2436 if (!rtd->debugfs_dpcm_root) {
2438 "ASoC: Failed to create dpcm debugfs directory %s\n",
2439 rtd->dai_link->name);
2443 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2444 rtd->debugfs_dpcm_root,
2445 rtd, &dpcm_state_fops);