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/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
42 * Sets the substream runtime hardware parameters.
44 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
58 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
60 /* DPCM stream event, send event to FE and all active BEs. */
61 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
64 struct snd_soc_dpcm *dpcm;
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
68 struct snd_soc_pcm_runtime *be = dpcm->be;
70 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
71 be->dai_link->name, event, dir);
73 snd_soc_dapm_stream_event(be, dir, event);
76 snd_soc_dapm_stream_event(fe, dir, event);
81 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
87 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
88 rtd->dai_link->symmetric_rates)) {
89 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
92 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
93 SNDRV_PCM_HW_PARAM_RATE,
94 soc_dai->rate, soc_dai->rate);
97 "ASoC: Unable to apply rate constraint: %d\n",
103 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
104 rtd->dai_link->symmetric_channels)) {
105 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
108 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
109 SNDRV_PCM_HW_PARAM_CHANNELS,
113 dev_err(soc_dai->dev,
114 "ASoC: Unable to apply channel symmetry constraint: %d\n",
120 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
121 rtd->dai_link->symmetric_samplebits)) {
122 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
123 soc_dai->sample_bits);
125 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
126 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
127 soc_dai->sample_bits,
128 soc_dai->sample_bits);
130 dev_err(soc_dai->dev,
131 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
140 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *params)
143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
144 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
145 struct snd_soc_dai *codec_dai = rtd->codec_dai;
146 unsigned int rate, channels, sample_bits, symmetry;
148 rate = params_rate(params);
149 channels = params_channels(params);
150 sample_bits = snd_pcm_format_physical_width(params_format(params));
152 /* reject unmatched parameters when applying symmetry */
153 symmetry = cpu_dai->driver->symmetric_rates ||
154 codec_dai->driver->symmetric_rates ||
155 rtd->dai_link->symmetric_rates;
156 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
157 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
158 cpu_dai->rate, rate);
162 symmetry = cpu_dai->driver->symmetric_channels ||
163 codec_dai->driver->symmetric_channels ||
164 rtd->dai_link->symmetric_channels;
165 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
166 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
167 cpu_dai->channels, channels);
171 symmetry = cpu_dai->driver->symmetric_samplebits ||
172 codec_dai->driver->symmetric_samplebits ||
173 rtd->dai_link->symmetric_samplebits;
174 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
175 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176 cpu_dai->sample_bits, sample_bits);
183 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
185 struct snd_soc_pcm_runtime *rtd = substream->private_data;
186 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
187 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
188 struct snd_soc_dai_link *link = rtd->dai_link;
190 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
191 link->symmetric_rates || cpu_driver->symmetric_channels ||
192 codec_driver->symmetric_channels || link->symmetric_channels ||
193 cpu_driver->symmetric_samplebits ||
194 codec_driver->symmetric_samplebits ||
195 link->symmetric_samplebits;
199 * List of sample sizes that might go over the bus for parameter
200 * application. There ought to be a wildcard sample size for things
201 * like the DAC/ADC resolution to use but there isn't right now.
203 static int sample_sizes[] = {
207 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
208 struct snd_soc_dai *dai)
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213 bits = dai->driver->playback.sig_bits;
215 bits = dai->driver->capture.sig_bits;
220 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
221 if (bits >= sample_sizes[i])
224 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
225 sample_sizes[i], bits);
228 "ASoC: Failed to set MSB %d/%d: %d\n",
229 bits, sample_sizes[i], ret);
233 static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
234 struct snd_soc_pcm_stream *codec_stream,
235 struct snd_soc_pcm_stream *cpu_stream)
237 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
238 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
239 hw->channels_min = max(codec_stream->channels_min,
240 cpu_stream->channels_min);
241 hw->channels_max = min(codec_stream->channels_max,
242 cpu_stream->channels_max);
243 hw->formats = codec_stream->formats & cpu_stream->formats;
244 hw->rates = codec_stream->rates & cpu_stream->rates;
245 if (codec_stream->rates
246 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
247 hw->rates |= cpu_stream->rates;
248 if (cpu_stream->rates
249 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
250 hw->rates |= codec_stream->rates;
254 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
255 * then initialized and any private data can be allocated. This also calls
256 * startup for the cpu DAI, platform, machine and codec DAI.
258 static int soc_pcm_open(struct snd_pcm_substream *substream)
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_pcm_runtime *runtime = substream->runtime;
262 struct snd_soc_platform *platform = rtd->platform;
263 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
264 struct snd_soc_dai *codec_dai = rtd->codec_dai;
265 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
266 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
269 pinctrl_pm_select_default_state(cpu_dai->dev);
270 pinctrl_pm_select_default_state(codec_dai->dev);
271 pm_runtime_get_sync(cpu_dai->dev);
272 pm_runtime_get_sync(codec_dai->dev);
273 pm_runtime_get_sync(platform->dev);
275 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
277 /* startup the audio subsystem */
278 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
279 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
281 dev_err(cpu_dai->dev, "ASoC: can't open interface"
282 " %s: %d\n", cpu_dai->name, ret);
287 if (platform->driver->ops && platform->driver->ops->open) {
288 ret = platform->driver->ops->open(substream);
290 dev_err(platform->dev, "ASoC: can't open platform"
291 " %s: %d\n", platform->name, ret);
296 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
297 ret = codec_dai->driver->ops->startup(substream, codec_dai);
299 dev_err(codec_dai->dev, "ASoC: can't open codec"
300 " %s: %d\n", codec_dai->name, ret);
305 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
306 ret = rtd->dai_link->ops->startup(substream);
308 pr_err("ASoC: %s startup failed: %d\n",
309 rtd->dai_link->name, ret);
314 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
315 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
318 /* Check that the codec and cpu DAIs are compatible */
319 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
320 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
321 &cpu_dai_drv->playback);
323 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
324 &cpu_dai_drv->capture);
327 if (soc_pcm_has_symmetry(substream))
328 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
331 snd_pcm_limit_hw_rates(runtime);
332 if (!runtime->hw.rates) {
333 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
334 codec_dai->name, cpu_dai->name);
337 if (!runtime->hw.formats) {
338 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
339 codec_dai->name, cpu_dai->name);
342 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
343 runtime->hw.channels_min > runtime->hw.channels_max) {
344 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
345 codec_dai->name, cpu_dai->name);
349 soc_pcm_apply_msb(substream, codec_dai);
350 soc_pcm_apply_msb(substream, cpu_dai);
352 /* Symmetry only applies if we've already got an active stream. */
353 if (cpu_dai->active) {
354 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
359 if (codec_dai->active) {
360 ret = soc_pcm_apply_symmetry(substream, codec_dai);
365 pr_debug("ASoC: %s <-> %s info:\n",
366 codec_dai->name, cpu_dai->name);
367 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
368 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
369 runtime->hw.channels_max);
370 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
371 runtime->hw.rate_max);
374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 cpu_dai->playback_active++;
376 codec_dai->playback_active++;
378 cpu_dai->capture_active++;
379 codec_dai->capture_active++;
383 rtd->codec->active++;
384 mutex_unlock(&rtd->pcm_mutex);
388 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
389 rtd->dai_link->ops->shutdown(substream);
392 if (codec_dai->driver->ops->shutdown)
393 codec_dai->driver->ops->shutdown(substream, codec_dai);
396 if (platform->driver->ops && platform->driver->ops->close)
397 platform->driver->ops->close(substream);
400 if (cpu_dai->driver->ops->shutdown)
401 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
403 mutex_unlock(&rtd->pcm_mutex);
405 pm_runtime_put(platform->dev);
406 pm_runtime_put(codec_dai->dev);
407 pm_runtime_put(cpu_dai->dev);
408 if (!codec_dai->active)
409 pinctrl_pm_select_sleep_state(codec_dai->dev);
410 if (!cpu_dai->active)
411 pinctrl_pm_select_sleep_state(cpu_dai->dev);
417 * Power down the audio subsystem pmdown_time msecs after close is called.
418 * This is to ensure there are no pops or clicks in between any music tracks
419 * due to DAPM power cycling.
421 static void close_delayed_work(struct work_struct *work)
423 struct snd_soc_pcm_runtime *rtd =
424 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
425 struct snd_soc_dai *codec_dai = rtd->codec_dai;
427 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
429 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
430 codec_dai->driver->playback.stream_name,
431 codec_dai->playback_active ? "active" : "inactive",
432 rtd->pop_wait ? "yes" : "no");
434 /* are we waiting on this codec DAI stream */
435 if (rtd->pop_wait == 1) {
437 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
438 SND_SOC_DAPM_STREAM_STOP);
441 mutex_unlock(&rtd->pcm_mutex);
445 * Called by ALSA when a PCM substream is closed. Private data can be
446 * freed here. The cpu DAI, codec DAI, machine and platform are also
449 static int soc_pcm_close(struct snd_pcm_substream *substream)
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 struct snd_soc_codec *codec = rtd->codec;
457 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
459 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
460 cpu_dai->playback_active--;
461 codec_dai->playback_active--;
463 cpu_dai->capture_active--;
464 codec_dai->capture_active--;
471 /* clear the corresponding DAIs rate when inactive */
472 if (!cpu_dai->active)
475 if (!codec_dai->active)
478 if (cpu_dai->driver->ops->shutdown)
479 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
481 if (codec_dai->driver->ops->shutdown)
482 codec_dai->driver->ops->shutdown(substream, codec_dai);
484 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
485 rtd->dai_link->ops->shutdown(substream);
487 if (platform->driver->ops && platform->driver->ops->close)
488 platform->driver->ops->close(substream);
489 cpu_dai->runtime = NULL;
491 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
492 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
493 rtd->dai_link->ignore_pmdown_time) {
494 /* powered down playback stream now */
495 snd_soc_dapm_stream_event(rtd,
496 SNDRV_PCM_STREAM_PLAYBACK,
497 SND_SOC_DAPM_STREAM_STOP);
499 /* start delayed pop wq here for playback streams */
501 queue_delayed_work(system_power_efficient_wq,
503 msecs_to_jiffies(rtd->pmdown_time));
506 /* capture streams can be powered down now */
507 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
508 SND_SOC_DAPM_STREAM_STOP);
511 mutex_unlock(&rtd->pcm_mutex);
513 pm_runtime_put(platform->dev);
514 pm_runtime_put(codec_dai->dev);
515 pm_runtime_put(cpu_dai->dev);
516 if (!codec_dai->active)
517 pinctrl_pm_select_sleep_state(codec_dai->dev);
518 if (!cpu_dai->active)
519 pinctrl_pm_select_sleep_state(cpu_dai->dev);
525 * Called by ALSA when the PCM substream is prepared, can set format, sample
526 * rate, etc. This function is non atomic and can be called multiple times,
527 * it can refer to the runtime info.
529 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
531 struct snd_soc_pcm_runtime *rtd = substream->private_data;
532 struct snd_soc_platform *platform = rtd->platform;
533 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
534 struct snd_soc_dai *codec_dai = rtd->codec_dai;
537 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
539 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
540 ret = rtd->dai_link->ops->prepare(substream);
542 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
548 if (platform->driver->ops && platform->driver->ops->prepare) {
549 ret = platform->driver->ops->prepare(substream);
551 dev_err(platform->dev, "ASoC: platform prepare error:"
557 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
558 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
560 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
566 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
567 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
569 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
575 /* cancel any delayed stream shutdown that is pending */
576 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
579 cancel_delayed_work(&rtd->delayed_work);
582 snd_soc_dapm_stream_event(rtd, substream->stream,
583 SND_SOC_DAPM_STREAM_START);
585 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
588 mutex_unlock(&rtd->pcm_mutex);
593 * Called by ALSA when the hardware params are set by application. This
594 * function can also be called multiple times and can allocate buffers
595 * (using snd_pcm_lib_* ). It's non-atomic.
597 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
598 struct snd_pcm_hw_params *params)
600 struct snd_soc_pcm_runtime *rtd = substream->private_data;
601 struct snd_soc_platform *platform = rtd->platform;
602 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
603 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
608 ret = soc_pcm_params_symmetry(substream, params);
612 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
613 ret = rtd->dai_link->ops->hw_params(substream, params);
615 dev_err(rtd->card->dev, "ASoC: machine hw_params"
616 " failed: %d\n", ret);
621 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
622 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
624 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
625 " %d\n", codec_dai->name, ret);
630 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
631 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
633 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
639 if (platform->driver->ops && platform->driver->ops->hw_params) {
640 ret = platform->driver->ops->hw_params(substream, params);
642 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
643 platform->name, ret);
648 /* store the parameters for each DAIs */
649 cpu_dai->rate = params_rate(params);
650 cpu_dai->channels = params_channels(params);
651 cpu_dai->sample_bits =
652 snd_pcm_format_physical_width(params_format(params));
654 codec_dai->rate = params_rate(params);
655 codec_dai->channels = params_channels(params);
656 codec_dai->sample_bits =
657 snd_pcm_format_physical_width(params_format(params));
660 mutex_unlock(&rtd->pcm_mutex);
664 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
665 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
668 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
669 codec_dai->driver->ops->hw_free(substream, codec_dai);
672 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
673 rtd->dai_link->ops->hw_free(substream);
675 mutex_unlock(&rtd->pcm_mutex);
680 * Frees resources allocated by hw_params, can be called multiple times
682 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
684 struct snd_soc_pcm_runtime *rtd = substream->private_data;
685 struct snd_soc_platform *platform = rtd->platform;
686 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
687 struct snd_soc_dai *codec_dai = rtd->codec_dai;
688 struct snd_soc_codec *codec = rtd->codec;
690 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
692 /* clear the corresponding DAIs parameters when going to be inactive */
693 if (cpu_dai->active == 1) {
695 cpu_dai->channels = 0;
696 cpu_dai->sample_bits = 0;
699 if (codec_dai->active == 1) {
701 codec_dai->channels = 0;
702 codec_dai->sample_bits = 0;
705 /* apply codec digital mute */
707 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
709 /* free any machine hw params */
710 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
711 rtd->dai_link->ops->hw_free(substream);
713 /* free any DMA resources */
714 if (platform->driver->ops && platform->driver->ops->hw_free)
715 platform->driver->ops->hw_free(substream);
717 /* now free hw params for the DAIs */
718 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
719 codec_dai->driver->ops->hw_free(substream, codec_dai);
721 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
722 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
724 mutex_unlock(&rtd->pcm_mutex);
728 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
730 struct snd_soc_pcm_runtime *rtd = substream->private_data;
731 struct snd_soc_platform *platform = rtd->platform;
732 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
733 struct snd_soc_dai *codec_dai = rtd->codec_dai;
736 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
737 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
742 if (platform->driver->ops && platform->driver->ops->trigger) {
743 ret = platform->driver->ops->trigger(substream, cmd);
748 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
749 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
756 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
759 struct snd_soc_pcm_runtime *rtd = substream->private_data;
760 struct snd_soc_platform *platform = rtd->platform;
761 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
762 struct snd_soc_dai *codec_dai = rtd->codec_dai;
765 if (codec_dai->driver->ops &&
766 codec_dai->driver->ops->bespoke_trigger) {
767 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
772 if (platform->driver->ops && platform->driver->bespoke_trigger) {
773 ret = platform->driver->bespoke_trigger(substream, cmd);
778 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
779 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
786 * soc level wrapper for pointer callback
787 * If cpu_dai, codec_dai, platform driver has the delay callback, than
788 * the runtime->delay will be updated accordingly.
790 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
793 struct snd_soc_platform *platform = rtd->platform;
794 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
795 struct snd_soc_dai *codec_dai = rtd->codec_dai;
796 struct snd_pcm_runtime *runtime = substream->runtime;
797 snd_pcm_uframes_t offset = 0;
798 snd_pcm_sframes_t delay = 0;
800 if (platform->driver->ops && platform->driver->ops->pointer)
801 offset = platform->driver->ops->pointer(substream);
803 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
804 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
806 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
807 delay += codec_dai->driver->ops->delay(substream, codec_dai);
809 if (platform->driver->delay)
810 delay += platform->driver->delay(substream, codec_dai);
812 runtime->delay = delay;
817 /* connect a FE and BE */
818 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
819 struct snd_soc_pcm_runtime *be, int stream)
821 struct snd_soc_dpcm *dpcm;
823 /* only add new dpcms */
824 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
825 if (dpcm->be == be && dpcm->fe == fe)
829 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
835 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
836 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
837 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
838 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
840 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
841 stream ? "capture" : "playback", fe->dai_link->name,
842 stream ? "<-" : "->", be->dai_link->name);
844 #ifdef CONFIG_DEBUG_FS
845 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
846 fe->debugfs_dpcm_root, &dpcm->state);
851 /* reparent a BE onto another FE */
852 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
853 struct snd_soc_pcm_runtime *be, int stream)
855 struct snd_soc_dpcm *dpcm;
856 struct snd_pcm_substream *fe_substream, *be_substream;
858 /* reparent if BE is connected to other FEs */
859 if (!be->dpcm[stream].users)
862 be_substream = snd_soc_dpcm_get_substream(be, stream);
864 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
868 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
869 stream ? "capture" : "playback",
870 dpcm->fe->dai_link->name,
871 stream ? "<-" : "->", dpcm->be->dai_link->name);
873 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
874 be_substream->runtime = fe_substream->runtime;
879 /* disconnect a BE and FE */
880 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
882 struct snd_soc_dpcm *dpcm, *d;
884 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
885 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
886 stream ? "capture" : "playback",
887 dpcm->be->dai_link->name);
889 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
892 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
893 stream ? "capture" : "playback", fe->dai_link->name,
894 stream ? "<-" : "->", dpcm->be->dai_link->name);
896 /* BEs still alive need new FE */
897 dpcm_be_reparent(fe, dpcm->be, stream);
899 #ifdef CONFIG_DEBUG_FS
900 debugfs_remove(dpcm->debugfs_state);
902 list_del(&dpcm->list_be);
903 list_del(&dpcm->list_fe);
908 /* get BE for DAI widget and stream */
909 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
910 struct snd_soc_dapm_widget *widget, int stream)
912 struct snd_soc_pcm_runtime *be;
915 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
916 for (i = 0; i < card->num_links; i++) {
919 if (!be->dai_link->no_pcm)
922 if (be->cpu_dai->playback_widget == widget ||
923 be->codec_dai->playback_widget == widget)
928 for (i = 0; i < card->num_links; i++) {
931 if (!be->dai_link->no_pcm)
934 if (be->cpu_dai->capture_widget == widget ||
935 be->codec_dai->capture_widget == widget)
940 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
941 stream ? "capture" : "playback", widget->name);
945 static inline struct snd_soc_dapm_widget *
946 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
948 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
949 return rtd->cpu_dai->playback_widget;
951 return rtd->cpu_dai->capture_widget;
954 static inline struct snd_soc_dapm_widget *
955 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
957 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
958 return rtd->codec_dai->playback_widget;
960 return rtd->codec_dai->capture_widget;
963 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
964 struct snd_soc_dapm_widget *widget)
968 for (i = 0; i < list->num_widgets; i++) {
969 if (widget == list->widgets[i])
976 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
977 int stream, struct snd_soc_dapm_widget_list **list_)
979 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
980 struct snd_soc_dapm_widget_list *list;
983 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
984 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
988 /* get number of valid DAI paths and their widgets */
989 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
991 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
992 stream ? "capture" : "playback");
998 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1003 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1004 struct snd_soc_dapm_widget_list **list_)
1006 struct snd_soc_dpcm *dpcm;
1007 struct snd_soc_dapm_widget_list *list = *list_;
1008 struct snd_soc_dapm_widget *widget;
1011 /* Destroy any old FE <--> BE connections */
1012 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1014 /* is there a valid CPU DAI widget for this BE */
1015 widget = rtd_get_cpu_widget(dpcm->be, stream);
1017 /* prune the BE if it's no longer in our active list */
1018 if (widget && widget_in_list(list, widget))
1021 /* is there a valid CODEC DAI widget for this BE */
1022 widget = rtd_get_codec_widget(dpcm->be, stream);
1024 /* prune the BE if it's no longer in our active list */
1025 if (widget && widget_in_list(list, widget))
1028 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1029 stream ? "capture" : "playback",
1030 dpcm->be->dai_link->name, fe->dai_link->name);
1031 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1032 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1036 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1040 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1041 struct snd_soc_dapm_widget_list **list_)
1043 struct snd_soc_card *card = fe->card;
1044 struct snd_soc_dapm_widget_list *list = *list_;
1045 struct snd_soc_pcm_runtime *be;
1046 int i, new = 0, err;
1048 /* Create any new FE <--> BE connections */
1049 for (i = 0; i < list->num_widgets; i++) {
1051 switch (list->widgets[i]->id) {
1052 case snd_soc_dapm_dai_in:
1053 case snd_soc_dapm_dai_out:
1059 /* is there a valid BE rtd for this widget */
1060 be = dpcm_get_be(card, list->widgets[i], stream);
1062 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1063 list->widgets[i]->name);
1067 /* make sure BE is a real BE */
1068 if (!be->dai_link->no_pcm)
1071 /* don't connect if FE is not running */
1072 if (!fe->dpcm[stream].runtime)
1075 /* newly connected FE and BE */
1076 err = dpcm_be_connect(fe, be, stream);
1078 dev_err(fe->dev, "ASoC: can't connect %s\n",
1079 list->widgets[i]->name);
1081 } else if (err == 0) /* already connected */
1085 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1089 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1094 * Find the corresponding BE DAIs that source or sink audio to this
1097 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1098 int stream, struct snd_soc_dapm_widget_list **list, int new)
1101 return dpcm_add_paths(fe, stream, list);
1103 return dpcm_prune_paths(fe, stream, list);
1106 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1108 struct snd_soc_dpcm *dpcm;
1110 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1111 dpcm->be->dpcm[stream].runtime_update =
1112 SND_SOC_DPCM_UPDATE_NO;
1115 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1118 struct snd_soc_dpcm *dpcm;
1120 /* disable any enabled and non active backends */
1121 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1123 struct snd_soc_pcm_runtime *be = dpcm->be;
1124 struct snd_pcm_substream *be_substream =
1125 snd_soc_dpcm_get_substream(be, stream);
1127 if (be->dpcm[stream].users == 0)
1128 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1129 stream ? "capture" : "playback",
1130 be->dpcm[stream].state);
1132 if (--be->dpcm[stream].users != 0)
1135 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1138 soc_pcm_close(be_substream);
1139 be_substream->runtime = NULL;
1140 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1144 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1146 struct snd_soc_dpcm *dpcm;
1149 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1150 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1152 struct snd_soc_pcm_runtime *be = dpcm->be;
1153 struct snd_pcm_substream *be_substream =
1154 snd_soc_dpcm_get_substream(be, stream);
1156 if (!be_substream) {
1157 dev_err(be->dev, "ASoC: no backend %s stream\n",
1158 stream ? "capture" : "playback");
1162 /* is this op for this BE ? */
1163 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1166 /* first time the dpcm is open ? */
1167 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1168 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1169 stream ? "capture" : "playback",
1170 be->dpcm[stream].state);
1172 if (be->dpcm[stream].users++ != 0)
1175 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1176 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1179 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1180 stream ? "capture" : "playback", be->dai_link->name);
1182 be_substream->runtime = be->dpcm[stream].runtime;
1183 err = soc_pcm_open(be_substream);
1185 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1186 be->dpcm[stream].users--;
1187 if (be->dpcm[stream].users < 0)
1188 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1189 stream ? "capture" : "playback",
1190 be->dpcm[stream].state);
1192 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1203 /* disable any enabled and non active backends */
1204 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1205 struct snd_soc_pcm_runtime *be = dpcm->be;
1206 struct snd_pcm_substream *be_substream =
1207 snd_soc_dpcm_get_substream(be, stream);
1209 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1212 if (be->dpcm[stream].users == 0)
1213 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1214 stream ? "capture" : "playback",
1215 be->dpcm[stream].state);
1217 if (--be->dpcm[stream].users != 0)
1220 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1223 soc_pcm_close(be_substream);
1224 be_substream->runtime = NULL;
1225 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1231 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1232 struct snd_soc_pcm_stream *stream)
1234 runtime->hw.rate_min = stream->rate_min;
1235 runtime->hw.rate_max = stream->rate_max;
1236 runtime->hw.channels_min = stream->channels_min;
1237 runtime->hw.channels_max = stream->channels_max;
1238 runtime->hw.formats &= stream->formats;
1239 runtime->hw.rates = stream->rates;
1242 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1244 struct snd_pcm_runtime *runtime = substream->runtime;
1245 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1246 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1247 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1249 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1250 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1252 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1255 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1257 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1258 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1259 int stream = fe_substream->stream, ret = 0;
1261 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1263 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1265 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1269 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1271 /* start the DAI frontend */
1272 ret = soc_pcm_open(fe_substream);
1274 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1278 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1280 dpcm_set_fe_runtime(fe_substream);
1281 snd_pcm_limit_hw_rates(runtime);
1283 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1287 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1289 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1293 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1295 struct snd_soc_dpcm *dpcm;
1297 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1298 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1300 struct snd_soc_pcm_runtime *be = dpcm->be;
1301 struct snd_pcm_substream *be_substream =
1302 snd_soc_dpcm_get_substream(be, stream);
1304 /* is this op for this BE ? */
1305 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1308 if (be->dpcm[stream].users == 0)
1309 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1310 stream ? "capture" : "playback",
1311 be->dpcm[stream].state);
1313 if (--be->dpcm[stream].users != 0)
1316 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1317 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1320 dev_dbg(be->dev, "ASoC: close BE %s\n",
1321 dpcm->fe->dai_link->name);
1323 soc_pcm_close(be_substream);
1324 be_substream->runtime = NULL;
1326 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1331 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1333 struct snd_soc_pcm_runtime *fe = substream->private_data;
1334 int stream = substream->stream;
1336 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1338 /* shutdown the BEs */
1339 dpcm_be_dai_shutdown(fe, substream->stream);
1341 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1343 /* now shutdown the frontend */
1344 soc_pcm_close(substream);
1346 /* run the stream event for each BE */
1347 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1349 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1350 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1354 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1356 struct snd_soc_dpcm *dpcm;
1358 /* only hw_params backends that are either sinks or sources
1359 * to this frontend DAI */
1360 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1362 struct snd_soc_pcm_runtime *be = dpcm->be;
1363 struct snd_pcm_substream *be_substream =
1364 snd_soc_dpcm_get_substream(be, stream);
1366 /* is this op for this BE ? */
1367 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1370 /* only free hw when no longer used - check all FEs */
1371 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1374 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1375 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1376 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1377 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1378 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1381 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1382 dpcm->fe->dai_link->name);
1384 soc_pcm_hw_free(be_substream);
1386 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1392 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1394 struct snd_soc_pcm_runtime *fe = substream->private_data;
1395 int err, stream = substream->stream;
1397 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1398 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1400 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1402 /* call hw_free on the frontend */
1403 err = soc_pcm_hw_free(substream);
1405 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1406 fe->dai_link->name);
1408 /* only hw_params backends that are either sinks or sources
1409 * to this frontend DAI */
1410 err = dpcm_be_dai_hw_free(fe, stream);
1412 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1413 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1415 mutex_unlock(&fe->card->mutex);
1419 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1421 struct snd_soc_dpcm *dpcm;
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1430 /* is this op for this BE ? */
1431 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1434 /* only allow hw_params() if no connected FEs are running */
1435 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1438 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1439 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1440 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1443 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1444 dpcm->fe->dai_link->name);
1446 /* copy params for each dpcm */
1447 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1448 sizeof(struct snd_pcm_hw_params));
1450 /* perform any hw_params fixups */
1451 if (be->dai_link->be_hw_params_fixup) {
1452 ret = be->dai_link->be_hw_params_fixup(be,
1456 "ASoC: hw_params BE fixup failed %d\n",
1462 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1464 dev_err(dpcm->be->dev,
1465 "ASoC: hw_params BE failed %d\n", ret);
1469 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1474 /* disable any enabled and non active backends */
1475 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1476 struct snd_soc_pcm_runtime *be = dpcm->be;
1477 struct snd_pcm_substream *be_substream =
1478 snd_soc_dpcm_get_substream(be, stream);
1480 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1483 /* only allow hw_free() if no connected FEs are running */
1484 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1487 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1488 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1489 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1490 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1493 soc_pcm_hw_free(be_substream);
1499 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1500 struct snd_pcm_hw_params *params)
1502 struct snd_soc_pcm_runtime *fe = substream->private_data;
1503 int ret, stream = substream->stream;
1505 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1506 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1508 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1509 sizeof(struct snd_pcm_hw_params));
1510 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1512 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1516 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1517 fe->dai_link->name, params_rate(params),
1518 params_channels(params), params_format(params));
1520 /* call hw_params on the frontend */
1521 ret = soc_pcm_hw_params(substream, params);
1523 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1524 dpcm_be_dai_hw_free(fe, stream);
1526 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1529 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1530 mutex_unlock(&fe->card->mutex);
1534 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1535 struct snd_pcm_substream *substream, int cmd)
1539 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1540 dpcm->fe->dai_link->name, cmd);
1542 ret = soc_pcm_trigger(substream, cmd);
1544 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1549 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1552 struct snd_soc_dpcm *dpcm;
1555 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1557 struct snd_soc_pcm_runtime *be = dpcm->be;
1558 struct snd_pcm_substream *be_substream =
1559 snd_soc_dpcm_get_substream(be, stream);
1561 /* is this op for this BE ? */
1562 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1566 case SNDRV_PCM_TRIGGER_START:
1567 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1568 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1571 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1575 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1577 case SNDRV_PCM_TRIGGER_RESUME:
1578 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1581 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1585 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1587 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1588 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1591 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1595 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1597 case SNDRV_PCM_TRIGGER_STOP:
1598 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1601 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1604 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1608 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1610 case SNDRV_PCM_TRIGGER_SUSPEND:
1611 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1614 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1617 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1621 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1623 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1624 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1627 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1630 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1634 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1641 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1643 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1645 struct snd_soc_pcm_runtime *fe = substream->private_data;
1646 int stream = substream->stream, ret;
1647 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1649 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1652 case SND_SOC_DPCM_TRIGGER_PRE:
1653 /* call trigger on the frontend before the backend. */
1655 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1656 fe->dai_link->name, cmd);
1658 ret = soc_pcm_trigger(substream, cmd);
1660 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1664 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1666 case SND_SOC_DPCM_TRIGGER_POST:
1667 /* call trigger on the frontend after the backend. */
1669 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1671 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1675 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1676 fe->dai_link->name, cmd);
1678 ret = soc_pcm_trigger(substream, cmd);
1680 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1681 /* bespoke trigger() - handles both FE and BEs */
1683 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1684 fe->dai_link->name, cmd);
1686 ret = soc_pcm_bespoke_trigger(substream, cmd);
1688 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1693 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1694 fe->dai_link->name);
1700 case SNDRV_PCM_TRIGGER_START:
1701 case SNDRV_PCM_TRIGGER_RESUME:
1702 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1703 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1705 case SNDRV_PCM_TRIGGER_STOP:
1706 case SNDRV_PCM_TRIGGER_SUSPEND:
1707 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1708 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1713 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1717 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1719 struct snd_soc_dpcm *dpcm;
1722 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1724 struct snd_soc_pcm_runtime *be = dpcm->be;
1725 struct snd_pcm_substream *be_substream =
1726 snd_soc_dpcm_get_substream(be, stream);
1728 /* is this op for this BE ? */
1729 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1732 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1733 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1736 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1737 dpcm->fe->dai_link->name);
1739 ret = soc_pcm_prepare(be_substream);
1741 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1746 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1751 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1753 struct snd_soc_pcm_runtime *fe = substream->private_data;
1754 int stream = substream->stream, ret = 0;
1756 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1758 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1760 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1762 /* there is no point preparing this FE if there are no BEs */
1763 if (list_empty(&fe->dpcm[stream].be_clients)) {
1764 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1765 fe->dai_link->name);
1770 ret = dpcm_be_dai_prepare(fe, substream->stream);
1774 /* call prepare on the frontend */
1775 ret = soc_pcm_prepare(substream);
1777 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1778 fe->dai_link->name);
1782 /* run the stream event for each BE */
1783 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1784 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1787 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1788 mutex_unlock(&fe->card->mutex);
1793 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1794 unsigned int cmd, void *arg)
1796 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1797 struct snd_soc_platform *platform = rtd->platform;
1799 if (platform->driver->ops && platform->driver->ops->ioctl)
1800 return platform->driver->ops->ioctl(substream, cmd, arg);
1801 return snd_pcm_lib_ioctl(substream, cmd, arg);
1804 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1806 struct snd_pcm_substream *substream =
1807 snd_soc_dpcm_get_substream(fe, stream);
1808 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1811 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1812 stream ? "capture" : "playback", fe->dai_link->name);
1814 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1815 /* call bespoke trigger - FE takes care of all BE triggers */
1816 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1817 fe->dai_link->name);
1819 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1821 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1823 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1824 fe->dai_link->name);
1826 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1828 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1831 err = dpcm_be_dai_hw_free(fe, stream);
1833 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1835 err = dpcm_be_dai_shutdown(fe, stream);
1837 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1839 /* run the stream event for each BE */
1840 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1845 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1847 struct snd_pcm_substream *substream =
1848 snd_soc_dpcm_get_substream(fe, stream);
1849 struct snd_soc_dpcm *dpcm;
1850 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1853 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1854 stream ? "capture" : "playback", fe->dai_link->name);
1856 /* Only start the BE if the FE is ready */
1857 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1858 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1861 /* startup must always be called for new BEs */
1862 ret = dpcm_be_dai_startup(fe, stream);
1866 /* keep going if FE state is > open */
1867 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1870 ret = dpcm_be_dai_hw_params(fe, stream);
1874 /* keep going if FE state is > hw_params */
1875 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1879 ret = dpcm_be_dai_prepare(fe, stream);
1883 /* run the stream event for each BE */
1884 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1886 /* keep going if FE state is > prepare */
1887 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1888 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1891 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1892 /* call trigger on the frontend - FE takes care of all BE triggers */
1893 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1894 fe->dai_link->name);
1896 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1898 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1902 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1903 fe->dai_link->name);
1905 ret = dpcm_be_dai_trigger(fe, stream,
1906 SNDRV_PCM_TRIGGER_START);
1908 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1916 dpcm_be_dai_hw_free(fe, stream);
1918 dpcm_be_dai_shutdown(fe, stream);
1920 /* disconnect any non started BEs */
1921 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1922 struct snd_soc_pcm_runtime *be = dpcm->be;
1923 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1924 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1930 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1934 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1935 ret = dpcm_run_update_startup(fe, stream);
1937 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1938 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1943 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1947 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1948 ret = dpcm_run_update_shutdown(fe, stream);
1950 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1951 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1956 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1959 int soc_dpcm_runtime_update(struct snd_soc_card *card)
1961 int i, old, new, paths;
1963 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1964 for (i = 0; i < card->num_rtd; i++) {
1965 struct snd_soc_dapm_widget_list *list;
1966 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1968 /* make sure link is FE */
1969 if (!fe->dai_link->dynamic)
1972 /* only check active links */
1973 if (!fe->cpu_dai->active)
1976 /* DAPM sync will call this to update DSP paths */
1977 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1978 fe->dai_link->name);
1980 /* skip if FE doesn't have playback capability */
1981 if (!fe->cpu_dai->driver->playback.channels_min)
1984 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1986 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1987 fe->dai_link->name, "playback");
1988 mutex_unlock(&card->mutex);
1992 /* update any new playback paths */
1993 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1995 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1996 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1997 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2000 /* update any old playback paths */
2001 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2003 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2004 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2005 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2009 /* skip if FE doesn't have capture capability */
2010 if (!fe->cpu_dai->driver->capture.channels_min)
2013 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2015 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2016 fe->dai_link->name, "capture");
2017 mutex_unlock(&card->mutex);
2021 /* update any new capture paths */
2022 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2024 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2025 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2026 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2029 /* update any old capture paths */
2030 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2032 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2033 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2034 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2037 dpcm_path_put(&list);
2040 mutex_unlock(&card->mutex);
2043 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2045 struct snd_soc_dpcm *dpcm;
2046 struct list_head *clients =
2047 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2049 list_for_each_entry(dpcm, clients, list_be) {
2051 struct snd_soc_pcm_runtime *be = dpcm->be;
2052 struct snd_soc_dai *dai = be->codec_dai;
2053 struct snd_soc_dai_driver *drv = dai->driver;
2055 if (be->dai_link->ignore_suspend)
2058 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
2060 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2061 drv->ops->digital_mute(dai, mute);
2067 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2069 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2070 struct snd_soc_dpcm *dpcm;
2071 struct snd_soc_dapm_widget_list *list;
2073 int stream = fe_substream->stream;
2075 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2076 fe->dpcm[stream].runtime = fe_substream->runtime;
2078 if (dpcm_path_get(fe, stream, &list) <= 0) {
2079 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2080 fe->dai_link->name, stream ? "capture" : "playback");
2083 /* calculate valid and active FE <-> BE dpcms */
2084 dpcm_process_paths(fe, stream, &list, 1);
2086 ret = dpcm_fe_dai_startup(fe_substream);
2088 /* clean up all links */
2089 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2090 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2092 dpcm_be_disconnect(fe, stream);
2093 fe->dpcm[stream].runtime = NULL;
2096 dpcm_clear_pending_state(fe, stream);
2097 dpcm_path_put(&list);
2098 mutex_unlock(&fe->card->mutex);
2102 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2104 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2105 struct snd_soc_dpcm *dpcm;
2106 int stream = fe_substream->stream, ret;
2108 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2109 ret = dpcm_fe_dai_shutdown(fe_substream);
2111 /* mark FE's links ready to prune */
2112 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2113 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2115 dpcm_be_disconnect(fe, stream);
2117 fe->dpcm[stream].runtime = NULL;
2118 mutex_unlock(&fe->card->mutex);
2122 /* create a new pcm */
2123 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2125 struct snd_soc_platform *platform = rtd->platform;
2126 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2127 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2128 struct snd_pcm *pcm;
2130 int ret = 0, playback = 0, capture = 0;
2132 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2133 if (cpu_dai->driver->playback.channels_min)
2135 if (cpu_dai->driver->capture.channels_min)
2138 if (codec_dai->driver->playback.channels_min &&
2139 cpu_dai->driver->playback.channels_min)
2141 if (codec_dai->driver->capture.channels_min &&
2142 cpu_dai->driver->capture.channels_min)
2146 if (rtd->dai_link->playback_only) {
2151 if (rtd->dai_link->capture_only) {
2156 /* create the PCM */
2157 if (rtd->dai_link->no_pcm) {
2158 snprintf(new_name, sizeof(new_name), "(%s)",
2159 rtd->dai_link->stream_name);
2161 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2162 playback, capture, &pcm);
2164 if (rtd->dai_link->dynamic)
2165 snprintf(new_name, sizeof(new_name), "%s (*)",
2166 rtd->dai_link->stream_name);
2168 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2169 rtd->dai_link->stream_name, codec_dai->name, num);
2171 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2175 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2176 rtd->dai_link->name);
2179 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2181 /* DAPM dai link stream work */
2182 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2185 pcm->private_data = rtd;
2187 if (rtd->dai_link->no_pcm) {
2189 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2191 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2195 /* ASoC PCM operations */
2196 if (rtd->dai_link->dynamic) {
2197 rtd->ops.open = dpcm_fe_dai_open;
2198 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2199 rtd->ops.prepare = dpcm_fe_dai_prepare;
2200 rtd->ops.trigger = dpcm_fe_dai_trigger;
2201 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2202 rtd->ops.close = dpcm_fe_dai_close;
2203 rtd->ops.pointer = soc_pcm_pointer;
2204 rtd->ops.ioctl = soc_pcm_ioctl;
2206 rtd->ops.open = soc_pcm_open;
2207 rtd->ops.hw_params = soc_pcm_hw_params;
2208 rtd->ops.prepare = soc_pcm_prepare;
2209 rtd->ops.trigger = soc_pcm_trigger;
2210 rtd->ops.hw_free = soc_pcm_hw_free;
2211 rtd->ops.close = soc_pcm_close;
2212 rtd->ops.pointer = soc_pcm_pointer;
2213 rtd->ops.ioctl = soc_pcm_ioctl;
2216 if (platform->driver->ops) {
2217 rtd->ops.ack = platform->driver->ops->ack;
2218 rtd->ops.copy = platform->driver->ops->copy;
2219 rtd->ops.silence = platform->driver->ops->silence;
2220 rtd->ops.page = platform->driver->ops->page;
2221 rtd->ops.mmap = platform->driver->ops->mmap;
2225 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2228 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2230 if (platform->driver->pcm_new) {
2231 ret = platform->driver->pcm_new(rtd);
2233 dev_err(platform->dev,
2234 "ASoC: pcm constructor failed: %d\n",
2240 pcm->private_free = platform->driver->pcm_free;
2242 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2247 /* is the current PCM operation for this FE ? */
2248 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2250 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2254 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2256 /* is the current PCM operation for this BE ? */
2257 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2258 struct snd_soc_pcm_runtime *be, int stream)
2260 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2261 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2262 be->dpcm[stream].runtime_update))
2266 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2268 /* get the substream for this BE */
2269 struct snd_pcm_substream *
2270 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2272 return be->pcm->streams[stream].substream;
2274 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2276 /* get the BE runtime state */
2277 enum snd_soc_dpcm_state
2278 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2280 return be->dpcm[stream].state;
2282 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2284 /* set the BE runtime state */
2285 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2286 int stream, enum snd_soc_dpcm_state state)
2288 be->dpcm[stream].state = state;
2290 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2293 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2294 * are not running, paused or suspended for the specified stream direction.
2296 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2297 struct snd_soc_pcm_runtime *be, int stream)
2299 struct snd_soc_dpcm *dpcm;
2302 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2307 state = dpcm->fe->dpcm[stream].state;
2308 if (state == SND_SOC_DPCM_STATE_START ||
2309 state == SND_SOC_DPCM_STATE_PAUSED ||
2310 state == SND_SOC_DPCM_STATE_SUSPEND)
2314 /* it's safe to free/stop this BE DAI */
2317 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2320 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2321 * running, paused or suspended for the specified stream direction.
2323 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2324 struct snd_soc_pcm_runtime *be, int stream)
2326 struct snd_soc_dpcm *dpcm;
2329 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2334 state = dpcm->fe->dpcm[stream].state;
2335 if (state == SND_SOC_DPCM_STATE_START ||
2336 state == SND_SOC_DPCM_STATE_PAUSED ||
2337 state == SND_SOC_DPCM_STATE_SUSPEND ||
2338 state == SND_SOC_DPCM_STATE_PREPARE)
2342 /* it's safe to change hw_params */
2345 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2347 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2348 int cmd, struct snd_soc_platform *platform)
2350 if (platform->driver->ops && platform->driver->ops->trigger)
2351 return platform->driver->ops->trigger(substream, cmd);
2354 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2356 #ifdef CONFIG_DEBUG_FS
2357 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2360 case SND_SOC_DPCM_STATE_NEW:
2362 case SND_SOC_DPCM_STATE_OPEN:
2364 case SND_SOC_DPCM_STATE_HW_PARAMS:
2366 case SND_SOC_DPCM_STATE_PREPARE:
2368 case SND_SOC_DPCM_STATE_START:
2370 case SND_SOC_DPCM_STATE_STOP:
2372 case SND_SOC_DPCM_STATE_SUSPEND:
2374 case SND_SOC_DPCM_STATE_PAUSED:
2376 case SND_SOC_DPCM_STATE_HW_FREE:
2378 case SND_SOC_DPCM_STATE_CLOSE:
2385 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2386 int stream, char *buf, size_t size)
2388 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2389 struct snd_soc_dpcm *dpcm;
2393 offset += snprintf(buf + offset, size - offset,
2394 "[%s - %s]\n", fe->dai_link->name,
2395 stream ? "Capture" : "Playback");
2397 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2398 dpcm_state_string(fe->dpcm[stream].state));
2400 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2401 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2402 offset += snprintf(buf + offset, size - offset,
2404 "Format = %s, Channels = %d, Rate = %d\n",
2405 snd_pcm_format_name(params_format(params)),
2406 params_channels(params),
2407 params_rate(params));
2410 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2412 if (list_empty(&fe->dpcm[stream].be_clients)) {
2413 offset += snprintf(buf + offset, size - offset,
2414 " No active DSP links\n");
2418 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2419 struct snd_soc_pcm_runtime *be = dpcm->be;
2420 params = &dpcm->hw_params;
2422 offset += snprintf(buf + offset, size - offset,
2423 "- %s\n", be->dai_link->name);
2425 offset += snprintf(buf + offset, size - offset,
2427 dpcm_state_string(be->dpcm[stream].state));
2429 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2430 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2431 offset += snprintf(buf + offset, size - offset,
2432 " Hardware Params: "
2433 "Format = %s, Channels = %d, Rate = %d\n",
2434 snd_pcm_format_name(params_format(params)),
2435 params_channels(params),
2436 params_rate(params));
2443 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2444 size_t count, loff_t *ppos)
2446 struct snd_soc_pcm_runtime *fe = file->private_data;
2447 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2450 buf = kmalloc(out_count, GFP_KERNEL);
2454 if (fe->cpu_dai->driver->playback.channels_min)
2455 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2456 buf + offset, out_count - offset);
2458 if (fe->cpu_dai->driver->capture.channels_min)
2459 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2460 buf + offset, out_count - offset);
2462 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2468 static const struct file_operations dpcm_state_fops = {
2469 .open = simple_open,
2470 .read = dpcm_state_read_file,
2471 .llseek = default_llseek,
2474 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2479 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2480 rtd->card->debugfs_card_root);
2481 if (!rtd->debugfs_dpcm_root) {
2483 "ASoC: Failed to create dpcm debugfs directory %s\n",
2484 rtd->dai_link->name);
2488 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2489 rtd->debugfs_dpcm_root,
2490 rtd, &dpcm_state_fops);