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_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
45 * Must be called with the rtd->pcm_mutex being held
47 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
52 lockdep_assert_held(&rtd->pcm_mutex);
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
59 cpu_dai->capture_active++;
60 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
65 cpu_dai->component->active++;
66 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
80 * Must be called with the rtd->pcm_mutex being held
82 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87 lockdep_assert_held(&rtd->pcm_mutex);
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
91 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
94 cpu_dai->capture_active--;
95 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
100 cpu_dai->component->active--;
101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
116 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
135 * Sets the substream runtime hardware parameters.
137 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
151 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
153 /* DPCM stream event, send event to FE and all active BEs. */
154 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
157 struct snd_soc_dpcm *dpcm;
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
161 struct snd_soc_pcm_runtime *be = dpcm->be;
163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
164 be->dai_link->name, event, dir);
166 snd_soc_dapm_stream_event(be, dir, event);
169 snd_soc_dapm_stream_event(fe, dir, event);
174 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
185 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186 SNDRV_PCM_HW_PARAM_RATE,
187 soc_dai->rate, soc_dai->rate);
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
201 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
202 SNDRV_PCM_HW_PARAM_CHANNELS,
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
218 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits,
221 soc_dai->sample_bits);
223 dev_err(soc_dai->dev,
224 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
233 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
236 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
238 unsigned int rate, channels, sample_bits, symmetry, i;
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
246 rtd->dai_link->symmetric_rates;
248 for (i = 0; i < rtd->num_codecs; i++)
249 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
257 symmetry = cpu_dai->driver->symmetric_channels ||
258 rtd->dai_link->symmetric_channels;
260 for (i = 0; i < rtd->num_codecs; i++)
261 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
270 rtd->dai_link->symmetric_samplebits;
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
284 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
288 struct snd_soc_dai_link *link = rtd->dai_link;
289 unsigned int symmetry, i;
291 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
292 cpu_driver->symmetric_channels || link->symmetric_channels ||
293 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry = symmetry ||
297 rtd->codec_dais[i]->driver->symmetric_rates ||
298 rtd->codec_dais[i]->driver->symmetric_channels ||
299 rtd->codec_dais[i]->driver->symmetric_samplebits;
304 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
312 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
314 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
318 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
322 struct snd_soc_dai *codec_dai;
324 unsigned int bits = 0, cpu_bits;
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
327 for (i = 0; i < rtd->num_codecs; i++) {
328 codec_dai = rtd->codec_dais[i];
329 if (codec_dai->driver->playback.sig_bits == 0) {
333 bits = max(codec_dai->driver->playback.sig_bits, bits);
335 cpu_bits = cpu_dai->driver->playback.sig_bits;
337 for (i = 0; i < rtd->num_codecs; i++) {
338 codec_dai = rtd->codec_dais[i];
339 if (codec_dai->driver->capture.sig_bits == 0) {
343 bits = max(codec_dai->driver->capture.sig_bits, bits);
345 cpu_bits = cpu_dai->driver->capture.sig_bits;
348 soc_pcm_set_msb(substream, bits);
349 soc_pcm_set_msb(substream, cpu_bits);
352 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
354 struct snd_pcm_runtime *runtime = substream->runtime;
355 struct snd_pcm_hardware *hw = &runtime->hw;
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv;
359 struct snd_soc_pcm_stream *codec_stream;
360 struct snd_soc_pcm_stream *cpu_stream;
361 unsigned int chan_min = 0, chan_max = UINT_MAX;
362 unsigned int rate_min = 0, rate_max = UINT_MAX;
363 unsigned int rates = UINT_MAX;
364 u64 formats = ULLONG_MAX;
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 cpu_stream = &cpu_dai_drv->playback;
370 cpu_stream = &cpu_dai_drv->capture;
372 /* first calculate min/max only for CODECs in the DAI link */
373 for (i = 0; i < rtd->num_codecs; i++) {
374 codec_dai_drv = rtd->codec_dais[i]->driver;
375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
376 codec_stream = &codec_dai_drv->playback;
378 codec_stream = &codec_dai_drv->capture;
379 chan_min = max(chan_min, codec_stream->channels_min);
380 chan_max = min(chan_max, codec_stream->channels_max);
381 rate_min = max(rate_min, codec_stream->rate_min);
382 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
383 formats &= codec_stream->formats;
384 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
388 * chan min/max cannot be enforced if there are multiple CODEC DAIs
389 * connected to a single CPU DAI, use CPU DAI's directly and let
390 * channel allocation be fixed up later
392 if (rtd->num_codecs > 1) {
393 chan_min = cpu_stream->channels_min;
394 chan_max = cpu_stream->channels_max;
397 hw->channels_min = max(chan_min, cpu_stream->channels_min);
398 hw->channels_max = min(chan_max, cpu_stream->channels_max);
400 hw->formats &= formats & cpu_stream->formats;
402 hw->formats = formats & cpu_stream->formats;
403 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
405 snd_pcm_limit_hw_rates(runtime);
407 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
408 hw->rate_min = max(hw->rate_min, rate_min);
409 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
410 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
414 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
415 * then initialized and any private data can be allocated. This also calls
416 * startup for the cpu DAI, platform, machine and codec DAI.
418 static int soc_pcm_open(struct snd_pcm_substream *substream)
420 struct snd_soc_pcm_runtime *rtd = substream->private_data;
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct snd_soc_platform *platform = rtd->platform;
423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
424 struct snd_soc_dai *codec_dai;
425 const char *codec_dai_name = "multicodec";
428 pinctrl_pm_select_default_state(cpu_dai->dev);
429 for (i = 0; i < rtd->num_codecs; i++)
430 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
431 pm_runtime_get_sync(cpu_dai->dev);
432 for (i = 0; i < rtd->num_codecs; i++)
433 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
434 pm_runtime_get_sync(platform->dev);
436 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
438 /* startup the audio subsystem */
439 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
440 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
442 dev_err(cpu_dai->dev, "ASoC: can't open interface"
443 " %s: %d\n", cpu_dai->name, ret);
448 if (platform->driver->ops && platform->driver->ops->open) {
449 ret = platform->driver->ops->open(substream);
451 dev_err(platform->dev, "ASoC: can't open platform"
452 " %s: %d\n", platform->component.name, ret);
457 for (i = 0; i < rtd->num_codecs; i++) {
458 codec_dai = rtd->codec_dais[i];
459 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
460 ret = codec_dai->driver->ops->startup(substream,
463 dev_err(codec_dai->dev,
464 "ASoC: can't open codec %s: %d\n",
465 codec_dai->name, ret);
470 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
471 codec_dai->tx_mask = 0;
473 codec_dai->rx_mask = 0;
476 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
477 ret = rtd->dai_link->ops->startup(substream);
479 pr_err("ASoC: %s startup failed: %d\n",
480 rtd->dai_link->name, ret);
485 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
486 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
489 /* Check that the codec and cpu DAIs are compatible */
490 soc_pcm_init_runtime_hw(substream);
492 if (rtd->num_codecs == 1)
493 codec_dai_name = rtd->codec_dai->name;
495 if (soc_pcm_has_symmetry(substream))
496 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
499 if (!runtime->hw.rates) {
500 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
501 codec_dai_name, cpu_dai->name);
504 if (!runtime->hw.formats) {
505 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
506 codec_dai_name, cpu_dai->name);
509 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
510 runtime->hw.channels_min > runtime->hw.channels_max) {
511 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
512 codec_dai_name, cpu_dai->name);
516 soc_pcm_apply_msb(substream);
518 /* Symmetry only applies if we've already got an active stream. */
519 if (cpu_dai->active) {
520 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
525 for (i = 0; i < rtd->num_codecs; i++) {
526 if (rtd->codec_dais[i]->active) {
527 ret = soc_pcm_apply_symmetry(substream,
534 pr_debug("ASoC: %s <-> %s info:\n",
535 codec_dai_name, cpu_dai->name);
536 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
537 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
538 runtime->hw.channels_max);
539 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
540 runtime->hw.rate_max);
544 snd_soc_runtime_activate(rtd, substream->stream);
546 mutex_unlock(&rtd->pcm_mutex);
550 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
551 rtd->dai_link->ops->shutdown(substream);
558 codec_dai = rtd->codec_dais[i];
559 if (codec_dai->driver->ops->shutdown)
560 codec_dai->driver->ops->shutdown(substream, codec_dai);
563 if (platform->driver->ops && platform->driver->ops->close)
564 platform->driver->ops->close(substream);
567 if (cpu_dai->driver->ops->shutdown)
568 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
570 mutex_unlock(&rtd->pcm_mutex);
572 pm_runtime_put(platform->dev);
573 for (i = 0; i < rtd->num_codecs; i++)
574 pm_runtime_put(rtd->codec_dais[i]->dev);
575 pm_runtime_put(cpu_dai->dev);
576 for (i = 0; i < rtd->num_codecs; i++) {
577 if (!rtd->codec_dais[i]->active)
578 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
580 if (!cpu_dai->active)
581 pinctrl_pm_select_sleep_state(cpu_dai->dev);
587 * Power down the audio subsystem pmdown_time msecs after close is called.
588 * This is to ensure there are no pops or clicks in between any music tracks
589 * due to DAPM power cycling.
591 static void close_delayed_work(struct work_struct *work)
593 struct snd_soc_pcm_runtime *rtd =
594 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
595 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
597 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
599 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
600 codec_dai->driver->playback.stream_name,
601 codec_dai->playback_active ? "active" : "inactive",
602 rtd->pop_wait ? "yes" : "no");
604 /* are we waiting on this codec DAI stream */
605 if (rtd->pop_wait == 1) {
607 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
608 SND_SOC_DAPM_STREAM_STOP);
611 mutex_unlock(&rtd->pcm_mutex);
615 * Called by ALSA when a PCM substream is closed. Private data can be
616 * freed here. The cpu DAI, codec DAI, machine and platform are also
619 static int soc_pcm_close(struct snd_pcm_substream *substream)
621 struct snd_soc_pcm_runtime *rtd = substream->private_data;
622 struct snd_soc_platform *platform = rtd->platform;
623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
624 struct snd_soc_dai *codec_dai;
627 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
629 snd_soc_runtime_deactivate(rtd, substream->stream);
631 /* clear the corresponding DAIs rate when inactive */
632 if (!cpu_dai->active)
635 for (i = 0; i < rtd->num_codecs; i++) {
636 codec_dai = rtd->codec_dais[i];
637 if (!codec_dai->active)
641 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
643 if (cpu_dai->driver->ops->shutdown)
644 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
646 for (i = 0; i < rtd->num_codecs; i++) {
647 codec_dai = rtd->codec_dais[i];
648 if (codec_dai->driver->ops->shutdown)
649 codec_dai->driver->ops->shutdown(substream, codec_dai);
652 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
653 rtd->dai_link->ops->shutdown(substream);
655 if (platform->driver->ops && platform->driver->ops->close)
656 platform->driver->ops->close(substream);
658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
659 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
660 /* powered down playback stream now */
661 snd_soc_dapm_stream_event(rtd,
662 SNDRV_PCM_STREAM_PLAYBACK,
663 SND_SOC_DAPM_STREAM_STOP);
665 /* start delayed pop wq here for playback streams */
667 queue_delayed_work(system_power_efficient_wq,
669 msecs_to_jiffies(rtd->pmdown_time));
672 /* capture streams can be powered down now */
673 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
674 SND_SOC_DAPM_STREAM_STOP);
677 mutex_unlock(&rtd->pcm_mutex);
679 pm_runtime_put(platform->dev);
680 for (i = 0; i < rtd->num_codecs; i++)
681 pm_runtime_put(rtd->codec_dais[i]->dev);
682 pm_runtime_put(cpu_dai->dev);
683 for (i = 0; i < rtd->num_codecs; i++) {
684 if (!rtd->codec_dais[i]->active)
685 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
687 if (!cpu_dai->active)
688 pinctrl_pm_select_sleep_state(cpu_dai->dev);
694 * Called by ALSA when the PCM substream is prepared, can set format, sample
695 * rate, etc. This function is non atomic and can be called multiple times,
696 * it can refer to the runtime info.
698 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
703 struct snd_soc_dai *codec_dai;
706 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
708 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
709 ret = rtd->dai_link->ops->prepare(substream);
711 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
717 if (platform->driver->ops && platform->driver->ops->prepare) {
718 ret = platform->driver->ops->prepare(substream);
720 dev_err(platform->dev, "ASoC: platform prepare error:"
726 for (i = 0; i < rtd->num_codecs; i++) {
727 codec_dai = rtd->codec_dais[i];
728 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
729 ret = codec_dai->driver->ops->prepare(substream,
732 dev_err(codec_dai->dev,
733 "ASoC: DAI prepare error: %d\n", ret);
739 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
740 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
742 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
748 /* cancel any delayed stream shutdown that is pending */
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
752 cancel_delayed_work(&rtd->delayed_work);
755 snd_soc_dapm_stream_event(rtd, substream->stream,
756 SND_SOC_DAPM_STREAM_START);
758 for (i = 0; i < rtd->num_codecs; i++)
759 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
761 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
764 mutex_unlock(&rtd->pcm_mutex);
768 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
771 struct snd_interval *interval;
772 int channels = hweight_long(mask);
774 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
775 interval->min = channels;
776 interval->max = channels;
779 int soc_dai_hw_params(struct snd_pcm_substream *substream,
780 struct snd_pcm_hw_params *params,
781 struct snd_soc_dai *dai)
785 if (dai->driver->ops && dai->driver->ops->hw_params) {
786 ret = dai->driver->ops->hw_params(substream, params, dai);
788 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
798 * Called by ALSA when the hardware params are set by application. This
799 * function can also be called multiple times and can allocate buffers
800 * (using snd_pcm_lib_* ). It's non-atomic.
802 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
803 struct snd_pcm_hw_params *params)
805 struct snd_soc_pcm_runtime *rtd = substream->private_data;
806 struct snd_soc_platform *platform = rtd->platform;
807 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
810 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
812 ret = soc_pcm_params_symmetry(substream, params);
816 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
817 ret = rtd->dai_link->ops->hw_params(substream, params);
819 dev_err(rtd->card->dev, "ASoC: machine hw_params"
820 " failed: %d\n", ret);
825 for (i = 0; i < rtd->num_codecs; i++) {
826 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
827 struct snd_pcm_hw_params codec_params;
829 /* copy params for each codec */
830 codec_params = *params;
832 /* fixup params based on TDM slot masks */
833 if (codec_dai->tx_mask)
834 soc_pcm_codec_params_fixup(&codec_params,
836 if (codec_dai->rx_mask)
837 soc_pcm_codec_params_fixup(&codec_params,
840 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
844 codec_dai->rate = params_rate(&codec_params);
845 codec_dai->channels = params_channels(&codec_params);
846 codec_dai->sample_bits = snd_pcm_format_physical_width(
847 params_format(&codec_params));
850 ret = soc_dai_hw_params(substream, params, cpu_dai);
854 if (platform->driver->ops && platform->driver->ops->hw_params) {
855 ret = platform->driver->ops->hw_params(substream, params);
857 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
858 platform->component.name, ret);
863 /* store the parameters for each DAIs */
864 cpu_dai->rate = params_rate(params);
865 cpu_dai->channels = params_channels(params);
866 cpu_dai->sample_bits =
867 snd_pcm_format_physical_width(params_format(params));
870 mutex_unlock(&rtd->pcm_mutex);
874 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
875 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
882 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
883 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
884 codec_dai->driver->ops->hw_free(substream, codec_dai);
888 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
889 rtd->dai_link->ops->hw_free(substream);
891 mutex_unlock(&rtd->pcm_mutex);
896 * Frees resources allocated by hw_params, can be called multiple times
898 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
901 struct snd_soc_platform *platform = rtd->platform;
902 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
903 struct snd_soc_dai *codec_dai;
904 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
907 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
909 /* clear the corresponding DAIs parameters when going to be inactive */
910 if (cpu_dai->active == 1) {
912 cpu_dai->channels = 0;
913 cpu_dai->sample_bits = 0;
916 for (i = 0; i < rtd->num_codecs; i++) {
917 codec_dai = rtd->codec_dais[i];
918 if (codec_dai->active == 1) {
920 codec_dai->channels = 0;
921 codec_dai->sample_bits = 0;
925 /* apply codec digital mute */
926 for (i = 0; i < rtd->num_codecs; i++) {
927 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
928 (!playback && rtd->codec_dais[i]->capture_active == 1))
929 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
933 /* free any machine hw params */
934 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
935 rtd->dai_link->ops->hw_free(substream);
937 /* free any DMA resources */
938 if (platform->driver->ops && platform->driver->ops->hw_free)
939 platform->driver->ops->hw_free(substream);
941 /* now free hw params for the DAIs */
942 for (i = 0; i < rtd->num_codecs; i++) {
943 codec_dai = rtd->codec_dais[i];
944 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
945 codec_dai->driver->ops->hw_free(substream, codec_dai);
948 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
949 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
951 mutex_unlock(&rtd->pcm_mutex);
955 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
957 struct snd_soc_pcm_runtime *rtd = substream->private_data;
958 struct snd_soc_platform *platform = rtd->platform;
959 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
960 struct snd_soc_dai *codec_dai;
963 for (i = 0; i < rtd->num_codecs; i++) {
964 codec_dai = rtd->codec_dais[i];
965 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
966 ret = codec_dai->driver->ops->trigger(substream,
973 if (platform->driver->ops && platform->driver->ops->trigger) {
974 ret = platform->driver->ops->trigger(substream, cmd);
979 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
980 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
985 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
986 ret = rtd->dai_link->ops->trigger(substream, cmd);
994 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
997 struct snd_soc_pcm_runtime *rtd = substream->private_data;
998 struct snd_soc_platform *platform = rtd->platform;
999 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1000 struct snd_soc_dai *codec_dai;
1003 for (i = 0; i < rtd->num_codecs; i++) {
1004 codec_dai = rtd->codec_dais[i];
1005 if (codec_dai->driver->ops &&
1006 codec_dai->driver->ops->bespoke_trigger) {
1007 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1014 if (platform->driver->bespoke_trigger) {
1015 ret = platform->driver->bespoke_trigger(substream, cmd);
1020 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1021 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1028 * soc level wrapper for pointer callback
1029 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1030 * the runtime->delay will be updated accordingly.
1032 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1034 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1035 struct snd_soc_platform *platform = rtd->platform;
1036 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1037 struct snd_soc_dai *codec_dai;
1038 struct snd_pcm_runtime *runtime = substream->runtime;
1039 snd_pcm_uframes_t offset = 0;
1040 snd_pcm_sframes_t delay = 0;
1041 snd_pcm_sframes_t codec_delay = 0;
1044 if (platform->driver->ops && platform->driver->ops->pointer)
1045 offset = platform->driver->ops->pointer(substream);
1047 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1048 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1050 for (i = 0; i < rtd->num_codecs; i++) {
1051 codec_dai = rtd->codec_dais[i];
1052 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1053 codec_delay = max(codec_delay,
1054 codec_dai->driver->ops->delay(substream,
1057 delay += codec_delay;
1060 * None of the existing platform drivers implement delay(), so
1061 * for now the codec_dai of first multicodec entry is used
1063 if (platform->driver->delay)
1064 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1066 runtime->delay = delay;
1071 /* connect a FE and BE */
1072 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1073 struct snd_soc_pcm_runtime *be, int stream)
1075 struct snd_soc_dpcm *dpcm;
1077 /* only add new dpcms */
1078 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1079 if (dpcm->be == be && dpcm->fe == fe)
1083 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1089 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1090 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1091 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1092 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1094 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1095 stream ? "capture" : "playback", fe->dai_link->name,
1096 stream ? "<-" : "->", be->dai_link->name);
1098 #ifdef CONFIG_DEBUG_FS
1099 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1100 fe->debugfs_dpcm_root, &dpcm->state);
1105 /* reparent a BE onto another FE */
1106 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1107 struct snd_soc_pcm_runtime *be, int stream)
1109 struct snd_soc_dpcm *dpcm;
1110 struct snd_pcm_substream *fe_substream, *be_substream;
1112 /* reparent if BE is connected to other FEs */
1113 if (!be->dpcm[stream].users)
1116 be_substream = snd_soc_dpcm_get_substream(be, stream);
1118 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1122 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1123 stream ? "capture" : "playback",
1124 dpcm->fe->dai_link->name,
1125 stream ? "<-" : "->", dpcm->be->dai_link->name);
1127 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1128 be_substream->runtime = fe_substream->runtime;
1133 /* disconnect a BE and FE */
1134 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1136 struct snd_soc_dpcm *dpcm, *d;
1138 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1139 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1140 stream ? "capture" : "playback",
1141 dpcm->be->dai_link->name);
1143 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1146 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1147 stream ? "capture" : "playback", fe->dai_link->name,
1148 stream ? "<-" : "->", dpcm->be->dai_link->name);
1150 /* BEs still alive need new FE */
1151 dpcm_be_reparent(fe, dpcm->be, stream);
1153 #ifdef CONFIG_DEBUG_FS
1154 debugfs_remove(dpcm->debugfs_state);
1156 list_del(&dpcm->list_be);
1157 list_del(&dpcm->list_fe);
1162 /* get BE for DAI widget and stream */
1163 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1164 struct snd_soc_dapm_widget *widget, int stream)
1166 struct snd_soc_pcm_runtime *be;
1169 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1170 for (i = 0; i < card->num_links; i++) {
1173 if (!be->dai_link->no_pcm)
1176 if (be->cpu_dai->playback_widget == widget)
1179 for (j = 0; j < be->num_codecs; j++) {
1180 struct snd_soc_dai *dai = be->codec_dais[j];
1181 if (dai->playback_widget == widget)
1187 for (i = 0; i < card->num_links; i++) {
1190 if (!be->dai_link->no_pcm)
1193 if (be->cpu_dai->capture_widget == widget)
1196 for (j = 0; j < be->num_codecs; j++) {
1197 struct snd_soc_dai *dai = be->codec_dais[j];
1198 if (dai->capture_widget == widget)
1204 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1205 stream ? "capture" : "playback", widget->name);
1209 static inline struct snd_soc_dapm_widget *
1210 dai_get_widget(struct snd_soc_dai *dai, int stream)
1212 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1213 return dai->playback_widget;
1215 return dai->capture_widget;
1218 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1219 struct snd_soc_dapm_widget *widget)
1223 for (i = 0; i < list->num_widgets; i++) {
1224 if (widget == list->widgets[i])
1231 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1232 int stream, struct snd_soc_dapm_widget_list **list_)
1234 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1235 struct snd_soc_dapm_widget_list *list;
1238 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1239 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1243 /* get number of valid DAI paths and their widgets */
1244 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1246 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1247 stream ? "capture" : "playback");
1253 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1254 struct snd_soc_dapm_widget_list **list_)
1256 struct snd_soc_dpcm *dpcm;
1257 struct snd_soc_dapm_widget_list *list = *list_;
1258 struct snd_soc_dapm_widget *widget;
1261 /* Destroy any old FE <--> BE connections */
1262 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1265 /* is there a valid CPU DAI widget for this BE */
1266 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1268 /* prune the BE if it's no longer in our active list */
1269 if (widget && widget_in_list(list, widget))
1272 /* is there a valid CODEC DAI widget for this BE */
1273 for (i = 0; i < dpcm->be->num_codecs; i++) {
1274 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1275 widget = dai_get_widget(dai, stream);
1277 /* prune the BE if it's no longer in our active list */
1278 if (widget && widget_in_list(list, widget))
1282 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1283 stream ? "capture" : "playback",
1284 dpcm->be->dai_link->name, fe->dai_link->name);
1285 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1286 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1290 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1294 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1295 struct snd_soc_dapm_widget_list **list_)
1297 struct snd_soc_card *card = fe->card;
1298 struct snd_soc_dapm_widget_list *list = *list_;
1299 struct snd_soc_pcm_runtime *be;
1300 int i, new = 0, err;
1302 /* Create any new FE <--> BE connections */
1303 for (i = 0; i < list->num_widgets; i++) {
1305 switch (list->widgets[i]->id) {
1306 case snd_soc_dapm_dai_in:
1307 case snd_soc_dapm_dai_out:
1313 /* is there a valid BE rtd for this widget */
1314 be = dpcm_get_be(card, list->widgets[i], stream);
1316 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1317 list->widgets[i]->name);
1321 /* make sure BE is a real BE */
1322 if (!be->dai_link->no_pcm)
1325 /* don't connect if FE is not running */
1326 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1329 /* newly connected FE and BE */
1330 err = dpcm_be_connect(fe, be, stream);
1332 dev_err(fe->dev, "ASoC: can't connect %s\n",
1333 list->widgets[i]->name);
1335 } else if (err == 0) /* already connected */
1339 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1343 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1348 * Find the corresponding BE DAIs that source or sink audio to this
1351 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1352 int stream, struct snd_soc_dapm_widget_list **list, int new)
1355 return dpcm_add_paths(fe, stream, list);
1357 return dpcm_prune_paths(fe, stream, list);
1360 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1362 struct snd_soc_dpcm *dpcm;
1364 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1365 dpcm->be->dpcm[stream].runtime_update =
1366 SND_SOC_DPCM_UPDATE_NO;
1369 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1372 struct snd_soc_dpcm *dpcm;
1374 /* disable any enabled and non active backends */
1375 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1377 struct snd_soc_pcm_runtime *be = dpcm->be;
1378 struct snd_pcm_substream *be_substream =
1379 snd_soc_dpcm_get_substream(be, stream);
1381 if (be->dpcm[stream].users == 0)
1382 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1383 stream ? "capture" : "playback",
1384 be->dpcm[stream].state);
1386 if (--be->dpcm[stream].users != 0)
1389 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1392 soc_pcm_close(be_substream);
1393 be_substream->runtime = NULL;
1394 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1398 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1400 struct snd_soc_dpcm *dpcm;
1403 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1404 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1406 struct snd_soc_pcm_runtime *be = dpcm->be;
1407 struct snd_pcm_substream *be_substream =
1408 snd_soc_dpcm_get_substream(be, stream);
1410 if (!be_substream) {
1411 dev_err(be->dev, "ASoC: no backend %s stream\n",
1412 stream ? "capture" : "playback");
1416 /* is this op for this BE ? */
1417 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1420 /* first time the dpcm is open ? */
1421 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1422 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1423 stream ? "capture" : "playback",
1424 be->dpcm[stream].state);
1426 if (be->dpcm[stream].users++ != 0)
1429 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1430 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1433 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1434 stream ? "capture" : "playback", be->dai_link->name);
1436 be_substream->runtime = be->dpcm[stream].runtime;
1437 err = soc_pcm_open(be_substream);
1439 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1440 be->dpcm[stream].users--;
1441 if (be->dpcm[stream].users < 0)
1442 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1443 stream ? "capture" : "playback",
1444 be->dpcm[stream].state);
1446 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1457 /* disable any enabled and non active backends */
1458 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1459 struct snd_soc_pcm_runtime *be = dpcm->be;
1460 struct snd_pcm_substream *be_substream =
1461 snd_soc_dpcm_get_substream(be, stream);
1463 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1466 if (be->dpcm[stream].users == 0)
1467 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1468 stream ? "capture" : "playback",
1469 be->dpcm[stream].state);
1471 if (--be->dpcm[stream].users != 0)
1474 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1477 soc_pcm_close(be_substream);
1478 be_substream->runtime = NULL;
1479 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1485 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1486 struct snd_soc_pcm_stream *stream)
1488 runtime->hw.rate_min = stream->rate_min;
1489 runtime->hw.rate_max = stream->rate_max;
1490 runtime->hw.channels_min = stream->channels_min;
1491 runtime->hw.channels_max = stream->channels_max;
1492 if (runtime->hw.formats)
1493 runtime->hw.formats &= stream->formats;
1495 runtime->hw.formats = stream->formats;
1496 runtime->hw.rates = stream->rates;
1499 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1503 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1504 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1506 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1507 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1509 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1512 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1514 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1515 * for avoiding the race with trigger callback.
1516 * If the state is unset and a trigger is pending while the previous operation,
1517 * process the pending trigger action here.
1519 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1520 int stream, enum snd_soc_dpcm_update state)
1522 struct snd_pcm_substream *substream =
1523 snd_soc_dpcm_get_substream(fe, stream);
1525 snd_pcm_stream_lock_irq(substream);
1526 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1527 dpcm_fe_dai_do_trigger(substream,
1528 fe->dpcm[stream].trigger_pending - 1);
1529 fe->dpcm[stream].trigger_pending = 0;
1531 fe->dpcm[stream].runtime_update = state;
1532 snd_pcm_stream_unlock_irq(substream);
1535 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1537 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1538 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1539 int stream = fe_substream->stream, ret = 0;
1541 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1543 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1545 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1549 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1551 /* start the DAI frontend */
1552 ret = soc_pcm_open(fe_substream);
1554 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1558 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1560 dpcm_set_fe_runtime(fe_substream);
1561 snd_pcm_limit_hw_rates(runtime);
1563 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1567 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1569 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1573 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1575 struct snd_soc_dpcm *dpcm;
1577 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1578 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1580 struct snd_soc_pcm_runtime *be = dpcm->be;
1581 struct snd_pcm_substream *be_substream =
1582 snd_soc_dpcm_get_substream(be, stream);
1584 /* is this op for this BE ? */
1585 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1588 if (be->dpcm[stream].users == 0)
1589 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1590 stream ? "capture" : "playback",
1591 be->dpcm[stream].state);
1593 if (--be->dpcm[stream].users != 0)
1596 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1597 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1600 dev_dbg(be->dev, "ASoC: close BE %s\n",
1601 dpcm->fe->dai_link->name);
1603 soc_pcm_close(be_substream);
1604 be_substream->runtime = NULL;
1606 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1611 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1613 struct snd_soc_pcm_runtime *fe = substream->private_data;
1614 int stream = substream->stream;
1616 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1618 /* shutdown the BEs */
1619 dpcm_be_dai_shutdown(fe, substream->stream);
1621 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1623 /* now shutdown the frontend */
1624 soc_pcm_close(substream);
1626 /* run the stream event for each BE */
1627 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1629 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1630 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1634 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1636 struct snd_soc_dpcm *dpcm;
1638 /* only hw_params backends that are either sinks or sources
1639 * to this frontend DAI */
1640 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1642 struct snd_soc_pcm_runtime *be = dpcm->be;
1643 struct snd_pcm_substream *be_substream =
1644 snd_soc_dpcm_get_substream(be, stream);
1646 /* is this op for this BE ? */
1647 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1650 /* only free hw when no longer used - check all FEs */
1651 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1654 /* do not free hw if this BE is used by other FE */
1655 if (be->dpcm[stream].users > 1)
1658 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1659 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1660 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1661 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1662 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1665 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1666 dpcm->fe->dai_link->name);
1668 soc_pcm_hw_free(be_substream);
1670 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1676 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1678 struct snd_soc_pcm_runtime *fe = substream->private_data;
1679 int err, stream = substream->stream;
1681 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1682 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1684 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1686 /* call hw_free on the frontend */
1687 err = soc_pcm_hw_free(substream);
1689 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1690 fe->dai_link->name);
1692 /* only hw_params backends that are either sinks or sources
1693 * to this frontend DAI */
1694 err = dpcm_be_dai_hw_free(fe, stream);
1696 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1697 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1699 mutex_unlock(&fe->card->mutex);
1703 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1705 struct snd_soc_dpcm *dpcm;
1708 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1710 struct snd_soc_pcm_runtime *be = dpcm->be;
1711 struct snd_pcm_substream *be_substream =
1712 snd_soc_dpcm_get_substream(be, stream);
1714 /* is this op for this BE ? */
1715 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1718 /* only allow hw_params() if no connected FEs are running */
1719 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1722 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1723 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1724 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1727 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1728 dpcm->fe->dai_link->name);
1730 /* copy params for each dpcm */
1731 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1732 sizeof(struct snd_pcm_hw_params));
1734 /* perform any hw_params fixups */
1735 if (be->dai_link->be_hw_params_fixup) {
1736 ret = be->dai_link->be_hw_params_fixup(be,
1740 "ASoC: hw_params BE fixup failed %d\n",
1746 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1748 dev_err(dpcm->be->dev,
1749 "ASoC: hw_params BE failed %d\n", ret);
1753 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1758 /* disable any enabled and non active backends */
1759 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1760 struct snd_soc_pcm_runtime *be = dpcm->be;
1761 struct snd_pcm_substream *be_substream =
1762 snd_soc_dpcm_get_substream(be, stream);
1764 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1767 /* only allow hw_free() if no connected FEs are running */
1768 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1771 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1772 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1773 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1774 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1777 soc_pcm_hw_free(be_substream);
1783 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1784 struct snd_pcm_hw_params *params)
1786 struct snd_soc_pcm_runtime *fe = substream->private_data;
1787 int ret, stream = substream->stream;
1789 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1790 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1792 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1793 sizeof(struct snd_pcm_hw_params));
1794 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1796 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1800 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1801 fe->dai_link->name, params_rate(params),
1802 params_channels(params), params_format(params));
1804 /* call hw_params on the frontend */
1805 ret = soc_pcm_hw_params(substream, params);
1807 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1808 dpcm_be_dai_hw_free(fe, stream);
1810 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1813 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1814 mutex_unlock(&fe->card->mutex);
1818 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1819 struct snd_pcm_substream *substream, int cmd)
1823 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1824 dpcm->fe->dai_link->name, cmd);
1826 ret = soc_pcm_trigger(substream, cmd);
1828 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1833 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1836 struct snd_soc_dpcm *dpcm;
1839 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1841 struct snd_soc_pcm_runtime *be = dpcm->be;
1842 struct snd_pcm_substream *be_substream =
1843 snd_soc_dpcm_get_substream(be, stream);
1845 /* is this op for this BE ? */
1846 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1850 case SNDRV_PCM_TRIGGER_START:
1851 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1852 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1855 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1859 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1861 case SNDRV_PCM_TRIGGER_RESUME:
1862 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1865 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1869 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1871 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1872 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1875 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1879 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1881 case SNDRV_PCM_TRIGGER_STOP:
1882 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1885 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1888 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1892 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1894 case SNDRV_PCM_TRIGGER_SUSPEND:
1895 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1898 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1901 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1905 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1907 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1908 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1911 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1914 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1918 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1925 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1927 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
1929 struct snd_soc_pcm_runtime *fe = substream->private_data;
1930 int stream = substream->stream, ret;
1931 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1933 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1936 case SND_SOC_DPCM_TRIGGER_PRE:
1937 /* call trigger on the frontend before the backend. */
1939 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1940 fe->dai_link->name, cmd);
1942 ret = soc_pcm_trigger(substream, cmd);
1944 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1948 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1950 case SND_SOC_DPCM_TRIGGER_POST:
1951 /* call trigger on the frontend after the backend. */
1953 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1955 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1959 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1960 fe->dai_link->name, cmd);
1962 ret = soc_pcm_trigger(substream, cmd);
1964 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1965 /* bespoke trigger() - handles both FE and BEs */
1967 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1968 fe->dai_link->name, cmd);
1970 ret = soc_pcm_bespoke_trigger(substream, cmd);
1972 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1977 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1978 fe->dai_link->name);
1984 case SNDRV_PCM_TRIGGER_START:
1985 case SNDRV_PCM_TRIGGER_RESUME:
1986 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1987 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1989 case SNDRV_PCM_TRIGGER_STOP:
1990 case SNDRV_PCM_TRIGGER_SUSPEND:
1991 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1992 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1997 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2001 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2003 struct snd_soc_pcm_runtime *fe = substream->private_data;
2004 int stream = substream->stream;
2006 /* if FE's runtime_update is already set, we're in race;
2007 * process this trigger later at exit
2009 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2010 fe->dpcm[stream].trigger_pending = cmd + 1;
2011 return 0; /* delayed, assuming it's successful */
2014 /* we're alone, let's trigger */
2015 return dpcm_fe_dai_do_trigger(substream, cmd);
2018 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2020 struct snd_soc_dpcm *dpcm;
2023 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2025 struct snd_soc_pcm_runtime *be = dpcm->be;
2026 struct snd_pcm_substream *be_substream =
2027 snd_soc_dpcm_get_substream(be, stream);
2029 /* is this op for this BE ? */
2030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2033 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2034 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2037 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2038 dpcm->fe->dai_link->name);
2040 ret = soc_pcm_prepare(be_substream);
2042 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2047 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2052 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2054 struct snd_soc_pcm_runtime *fe = substream->private_data;
2055 int stream = substream->stream, ret = 0;
2057 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2059 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2061 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2063 /* there is no point preparing this FE if there are no BEs */
2064 if (list_empty(&fe->dpcm[stream].be_clients)) {
2065 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2066 fe->dai_link->name);
2071 ret = dpcm_be_dai_prepare(fe, substream->stream);
2075 /* call prepare on the frontend */
2076 ret = soc_pcm_prepare(substream);
2078 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2079 fe->dai_link->name);
2083 /* run the stream event for each BE */
2084 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2085 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2088 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2089 mutex_unlock(&fe->card->mutex);
2094 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2095 unsigned int cmd, void *arg)
2097 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2098 struct snd_soc_platform *platform = rtd->platform;
2100 if (platform->driver->ops && platform->driver->ops->ioctl)
2101 return platform->driver->ops->ioctl(substream, cmd, arg);
2102 return snd_pcm_lib_ioctl(substream, cmd, arg);
2105 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2107 struct snd_pcm_substream *substream =
2108 snd_soc_dpcm_get_substream(fe, stream);
2109 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2112 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2113 stream ? "capture" : "playback", fe->dai_link->name);
2115 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2116 /* call bespoke trigger - FE takes care of all BE triggers */
2117 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2118 fe->dai_link->name);
2120 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2122 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2124 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2125 fe->dai_link->name);
2127 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2129 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2132 err = dpcm_be_dai_hw_free(fe, stream);
2134 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2136 err = dpcm_be_dai_shutdown(fe, stream);
2138 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2140 /* run the stream event for each BE */
2141 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2146 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2148 struct snd_pcm_substream *substream =
2149 snd_soc_dpcm_get_substream(fe, stream);
2150 struct snd_soc_dpcm *dpcm;
2151 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2154 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2155 stream ? "capture" : "playback", fe->dai_link->name);
2157 /* Only start the BE if the FE is ready */
2158 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2159 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2162 /* startup must always be called for new BEs */
2163 ret = dpcm_be_dai_startup(fe, stream);
2167 /* keep going if FE state is > open */
2168 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2171 ret = dpcm_be_dai_hw_params(fe, stream);
2175 /* keep going if FE state is > hw_params */
2176 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2180 ret = dpcm_be_dai_prepare(fe, stream);
2184 /* run the stream event for each BE */
2185 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2187 /* keep going if FE state is > prepare */
2188 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2189 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2192 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2193 /* call trigger on the frontend - FE takes care of all BE triggers */
2194 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2195 fe->dai_link->name);
2197 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2199 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2203 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2204 fe->dai_link->name);
2206 ret = dpcm_be_dai_trigger(fe, stream,
2207 SNDRV_PCM_TRIGGER_START);
2209 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2217 dpcm_be_dai_hw_free(fe, stream);
2219 dpcm_be_dai_shutdown(fe, stream);
2221 /* disconnect any non started BEs */
2222 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2223 struct snd_soc_pcm_runtime *be = dpcm->be;
2224 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2225 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2231 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2235 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2236 ret = dpcm_run_update_startup(fe, stream);
2238 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2239 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2244 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2249 ret = dpcm_run_update_shutdown(fe, stream);
2251 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2252 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2257 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2260 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2262 int i, old, new, paths;
2264 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2265 for (i = 0; i < card->num_rtd; i++) {
2266 struct snd_soc_dapm_widget_list *list;
2267 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2269 /* make sure link is FE */
2270 if (!fe->dai_link->dynamic)
2273 /* only check active links */
2274 if (!fe->cpu_dai->active)
2277 /* DAPM sync will call this to update DSP paths */
2278 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2279 fe->dai_link->name);
2281 /* skip if FE doesn't have playback capability */
2282 if (!fe->cpu_dai->driver->playback.channels_min
2283 || !fe->codec_dai->driver->playback.channels_min)
2286 /* skip if FE isn't currently playing */
2287 if (!fe->cpu_dai->playback_active
2288 || !fe->codec_dai->playback_active)
2291 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2293 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2294 fe->dai_link->name, "playback");
2295 mutex_unlock(&card->mutex);
2299 /* update any new playback paths */
2300 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2302 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2303 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2304 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2307 /* update any old playback paths */
2308 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2310 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2311 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2312 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2315 dpcm_path_put(&list);
2317 /* skip if FE doesn't have capture capability */
2318 if (!fe->cpu_dai->driver->capture.channels_min
2319 || !fe->codec_dai->driver->capture.channels_min)
2322 /* skip if FE isn't currently capturing */
2323 if (!fe->cpu_dai->capture_active
2324 || !fe->codec_dai->capture_active)
2327 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2329 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2330 fe->dai_link->name, "capture");
2331 mutex_unlock(&card->mutex);
2335 /* update any new capture paths */
2336 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2338 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2339 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2340 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2343 /* update any old capture paths */
2344 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2346 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2347 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2348 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2351 dpcm_path_put(&list);
2354 mutex_unlock(&card->mutex);
2357 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2359 struct snd_soc_dpcm *dpcm;
2360 struct list_head *clients =
2361 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2363 list_for_each_entry(dpcm, clients, list_be) {
2365 struct snd_soc_pcm_runtime *be = dpcm->be;
2368 if (be->dai_link->ignore_suspend)
2371 for (i = 0; i < be->num_codecs; i++) {
2372 struct snd_soc_dai *dai = be->codec_dais[i];
2373 struct snd_soc_dai_driver *drv = dai->driver;
2375 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2376 be->dai_link->name);
2378 if (drv->ops && drv->ops->digital_mute &&
2379 dai->playback_active)
2380 drv->ops->digital_mute(dai, mute);
2387 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2389 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2390 struct snd_soc_dpcm *dpcm;
2391 struct snd_soc_dapm_widget_list *list;
2393 int stream = fe_substream->stream;
2395 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2396 fe->dpcm[stream].runtime = fe_substream->runtime;
2398 ret = dpcm_path_get(fe, stream, &list);
2400 mutex_unlock(&fe->card->mutex);
2402 } else if (ret == 0) {
2403 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2404 fe->dai_link->name, stream ? "capture" : "playback");
2407 /* calculate valid and active FE <-> BE dpcms */
2408 dpcm_process_paths(fe, stream, &list, 1);
2410 ret = dpcm_fe_dai_startup(fe_substream);
2412 /* clean up all links */
2413 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2414 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2416 dpcm_be_disconnect(fe, stream);
2417 fe->dpcm[stream].runtime = NULL;
2420 dpcm_clear_pending_state(fe, stream);
2421 dpcm_path_put(&list);
2422 mutex_unlock(&fe->card->mutex);
2426 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2428 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2429 struct snd_soc_dpcm *dpcm;
2430 int stream = fe_substream->stream, ret;
2432 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2433 ret = dpcm_fe_dai_shutdown(fe_substream);
2435 /* mark FE's links ready to prune */
2436 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2437 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2439 dpcm_be_disconnect(fe, stream);
2441 fe->dpcm[stream].runtime = NULL;
2442 mutex_unlock(&fe->card->mutex);
2446 /* create a new pcm */
2447 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2449 struct snd_soc_platform *platform = rtd->platform;
2450 struct snd_soc_dai *codec_dai;
2451 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2452 struct snd_pcm *pcm;
2454 int ret = 0, playback = 0, capture = 0;
2457 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2458 playback = rtd->dai_link->dpcm_playback;
2459 capture = rtd->dai_link->dpcm_capture;
2461 for (i = 0; i < rtd->num_codecs; i++) {
2462 codec_dai = rtd->codec_dais[i];
2463 if (codec_dai->driver->playback.channels_min)
2465 if (codec_dai->driver->capture.channels_min)
2469 capture = capture && cpu_dai->driver->capture.channels_min;
2470 playback = playback && cpu_dai->driver->playback.channels_min;
2473 if (rtd->dai_link->playback_only) {
2478 if (rtd->dai_link->capture_only) {
2483 /* create the PCM */
2484 if (rtd->dai_link->no_pcm) {
2485 snprintf(new_name, sizeof(new_name), "(%s)",
2486 rtd->dai_link->stream_name);
2488 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2489 playback, capture, &pcm);
2491 if (rtd->dai_link->dynamic)
2492 snprintf(new_name, sizeof(new_name), "%s (*)",
2493 rtd->dai_link->stream_name);
2495 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2496 rtd->dai_link->stream_name,
2497 (rtd->num_codecs > 1) ?
2498 "multicodec" : rtd->codec_dai->name, num);
2500 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2504 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2505 rtd->dai_link->name);
2508 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2510 /* DAPM dai link stream work */
2511 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2514 pcm->private_data = rtd;
2516 if (rtd->dai_link->no_pcm) {
2518 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2520 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2524 /* ASoC PCM operations */
2525 if (rtd->dai_link->dynamic) {
2526 rtd->ops.open = dpcm_fe_dai_open;
2527 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2528 rtd->ops.prepare = dpcm_fe_dai_prepare;
2529 rtd->ops.trigger = dpcm_fe_dai_trigger;
2530 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2531 rtd->ops.close = dpcm_fe_dai_close;
2532 rtd->ops.pointer = soc_pcm_pointer;
2533 rtd->ops.ioctl = soc_pcm_ioctl;
2535 rtd->ops.open = soc_pcm_open;
2536 rtd->ops.hw_params = soc_pcm_hw_params;
2537 rtd->ops.prepare = soc_pcm_prepare;
2538 rtd->ops.trigger = soc_pcm_trigger;
2539 rtd->ops.hw_free = soc_pcm_hw_free;
2540 rtd->ops.close = soc_pcm_close;
2541 rtd->ops.pointer = soc_pcm_pointer;
2542 rtd->ops.ioctl = soc_pcm_ioctl;
2545 if (platform->driver->ops) {
2546 rtd->ops.ack = platform->driver->ops->ack;
2547 rtd->ops.copy = platform->driver->ops->copy;
2548 rtd->ops.silence = platform->driver->ops->silence;
2549 rtd->ops.page = platform->driver->ops->page;
2550 rtd->ops.mmap = platform->driver->ops->mmap;
2554 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2557 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2559 if (platform->driver->pcm_new) {
2560 ret = platform->driver->pcm_new(rtd);
2562 dev_err(platform->dev,
2563 "ASoC: pcm constructor failed: %d\n",
2569 pcm->private_free = platform->driver->pcm_free;
2571 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2572 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2577 /* is the current PCM operation for this FE ? */
2578 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2580 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2584 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2586 /* is the current PCM operation for this BE ? */
2587 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2588 struct snd_soc_pcm_runtime *be, int stream)
2590 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2591 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2592 be->dpcm[stream].runtime_update))
2596 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2598 /* get the substream for this BE */
2599 struct snd_pcm_substream *
2600 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2602 return be->pcm->streams[stream].substream;
2604 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2606 /* get the BE runtime state */
2607 enum snd_soc_dpcm_state
2608 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2610 return be->dpcm[stream].state;
2612 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2614 /* set the BE runtime state */
2615 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2616 int stream, enum snd_soc_dpcm_state state)
2618 be->dpcm[stream].state = state;
2620 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2623 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2624 * are not running, paused or suspended for the specified stream direction.
2626 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2627 struct snd_soc_pcm_runtime *be, int stream)
2629 struct snd_soc_dpcm *dpcm;
2632 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2637 state = dpcm->fe->dpcm[stream].state;
2638 if (state == SND_SOC_DPCM_STATE_START ||
2639 state == SND_SOC_DPCM_STATE_PAUSED ||
2640 state == SND_SOC_DPCM_STATE_SUSPEND)
2644 /* it's safe to free/stop this BE DAI */
2647 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2650 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2651 * running, paused or suspended for the specified stream direction.
2653 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2654 struct snd_soc_pcm_runtime *be, int stream)
2656 struct snd_soc_dpcm *dpcm;
2659 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2664 state = dpcm->fe->dpcm[stream].state;
2665 if (state == SND_SOC_DPCM_STATE_START ||
2666 state == SND_SOC_DPCM_STATE_PAUSED ||
2667 state == SND_SOC_DPCM_STATE_SUSPEND ||
2668 state == SND_SOC_DPCM_STATE_PREPARE)
2672 /* it's safe to change hw_params */
2675 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2677 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2678 int cmd, struct snd_soc_platform *platform)
2680 if (platform->driver->ops && platform->driver->ops->trigger)
2681 return platform->driver->ops->trigger(substream, cmd);
2684 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2686 #ifdef CONFIG_DEBUG_FS
2687 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2690 case SND_SOC_DPCM_STATE_NEW:
2692 case SND_SOC_DPCM_STATE_OPEN:
2694 case SND_SOC_DPCM_STATE_HW_PARAMS:
2696 case SND_SOC_DPCM_STATE_PREPARE:
2698 case SND_SOC_DPCM_STATE_START:
2700 case SND_SOC_DPCM_STATE_STOP:
2702 case SND_SOC_DPCM_STATE_SUSPEND:
2704 case SND_SOC_DPCM_STATE_PAUSED:
2706 case SND_SOC_DPCM_STATE_HW_FREE:
2708 case SND_SOC_DPCM_STATE_CLOSE:
2715 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2716 int stream, char *buf, size_t size)
2718 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2719 struct snd_soc_dpcm *dpcm;
2723 offset += snprintf(buf + offset, size - offset,
2724 "[%s - %s]\n", fe->dai_link->name,
2725 stream ? "Capture" : "Playback");
2727 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2728 dpcm_state_string(fe->dpcm[stream].state));
2730 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2731 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2732 offset += snprintf(buf + offset, size - offset,
2734 "Format = %s, Channels = %d, Rate = %d\n",
2735 snd_pcm_format_name(params_format(params)),
2736 params_channels(params),
2737 params_rate(params));
2740 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2742 if (list_empty(&fe->dpcm[stream].be_clients)) {
2743 offset += snprintf(buf + offset, size - offset,
2744 " No active DSP links\n");
2748 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2749 struct snd_soc_pcm_runtime *be = dpcm->be;
2750 params = &dpcm->hw_params;
2752 offset += snprintf(buf + offset, size - offset,
2753 "- %s\n", be->dai_link->name);
2755 offset += snprintf(buf + offset, size - offset,
2757 dpcm_state_string(be->dpcm[stream].state));
2759 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2760 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2761 offset += snprintf(buf + offset, size - offset,
2762 " Hardware Params: "
2763 "Format = %s, Channels = %d, Rate = %d\n",
2764 snd_pcm_format_name(params_format(params)),
2765 params_channels(params),
2766 params_rate(params));
2773 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2774 size_t count, loff_t *ppos)
2776 struct snd_soc_pcm_runtime *fe = file->private_data;
2777 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2780 buf = kmalloc(out_count, GFP_KERNEL);
2784 if (fe->cpu_dai->driver->playback.channels_min)
2785 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2786 buf + offset, out_count - offset);
2788 if (fe->cpu_dai->driver->capture.channels_min)
2789 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2790 buf + offset, out_count - offset);
2792 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2798 static const struct file_operations dpcm_state_fops = {
2799 .open = simple_open,
2800 .read = dpcm_state_read_file,
2801 .llseek = default_llseek,
2804 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2809 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2810 rtd->card->debugfs_card_root);
2811 if (!rtd->debugfs_dpcm_root) {
2813 "ASoC: Failed to create dpcm debugfs directory %s\n",
2814 rtd->dai_link->name);
2818 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2819 rtd->debugfs_dpcm_root,
2820 rtd, &dpcm_state_fops);