1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-pcm.c -- ALSA SoC PCM
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Authors: Liam Girdwood <lrg@ti.com>
11 // Mark Brown <broonie@opensource.wolfsonmicro.com>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/initval.h>
29 #define DPCM_MAX_BE_USERS 8
32 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
34 * Returns true if the DAI supports the indicated stream type.
36 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
38 struct snd_soc_pcm_stream *codec_stream;
40 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
41 codec_stream = &dai->driver->playback;
43 codec_stream = &dai->driver->capture;
45 /* If the codec specifies any rate at all, it supports the stream. */
46 return codec_stream->rates;
50 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
51 * @rtd: ASoC PCM runtime that is activated
52 * @stream: Direction of the PCM stream
54 * Increments the active count for all the DAIs and components attached to a PCM
55 * runtime. Should typically be called when a stream is opened.
57 * Must be called with the rtd->pcm_mutex being held
59 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
61 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
64 lockdep_assert_held(&rtd->pcm_mutex);
66 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
67 cpu_dai->playback_active++;
68 for (i = 0; i < rtd->num_codecs; i++)
69 rtd->codec_dais[i]->playback_active++;
71 cpu_dai->capture_active++;
72 for (i = 0; i < rtd->num_codecs; i++)
73 rtd->codec_dais[i]->capture_active++;
77 cpu_dai->component->active++;
78 for (i = 0; i < rtd->num_codecs; i++) {
79 rtd->codec_dais[i]->active++;
80 rtd->codec_dais[i]->component->active++;
85 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
86 * @rtd: ASoC PCM runtime that is deactivated
87 * @stream: Direction of the PCM stream
89 * Decrements the active count for all the DAIs and components attached to a PCM
90 * runtime. Should typically be called when a stream is closed.
92 * Must be called with the rtd->pcm_mutex being held
94 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
96 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99 lockdep_assert_held(&rtd->pcm_mutex);
101 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
102 cpu_dai->playback_active--;
103 for (i = 0; i < rtd->num_codecs; i++)
104 rtd->codec_dais[i]->playback_active--;
106 cpu_dai->capture_active--;
107 for (i = 0; i < rtd->num_codecs; i++)
108 rtd->codec_dais[i]->capture_active--;
112 cpu_dai->component->active--;
113 for (i = 0; i < rtd->num_codecs; i++) {
114 rtd->codec_dais[i]->component->active--;
115 rtd->codec_dais[i]->active--;
120 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
121 * @rtd: The ASoC PCM runtime that should be checked.
123 * This function checks whether the power down delay should be ignored for a
124 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
125 * been configured to ignore the delay, or if none of the components benefits
126 * from having the delay.
128 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
130 struct snd_soc_rtdcom_list *rtdcom;
131 struct snd_soc_component *component;
134 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
137 for_each_rtdcom(rtd, rtdcom) {
138 component = rtdcom->component;
140 ignore &= !component->driver->use_pmdown_time;
147 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
148 * @substream: the pcm substream
149 * @hw: the hardware parameters
151 * Sets the substream runtime hardware parameters.
153 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
154 const struct snd_pcm_hardware *hw)
156 struct snd_pcm_runtime *runtime = substream->runtime;
157 runtime->hw.info = hw->info;
158 runtime->hw.formats = hw->formats;
159 runtime->hw.period_bytes_min = hw->period_bytes_min;
160 runtime->hw.period_bytes_max = hw->period_bytes_max;
161 runtime->hw.periods_min = hw->periods_min;
162 runtime->hw.periods_max = hw->periods_max;
163 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
164 runtime->hw.fifo_size = hw->fifo_size;
167 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
169 /* DPCM stream event, send event to FE and all active BEs. */
170 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
173 struct snd_soc_dpcm *dpcm;
175 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
177 struct snd_soc_pcm_runtime *be = dpcm->be;
179 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
180 be->dai_link->name, event, dir);
182 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
183 (be->dpcm[dir].users >= 1))
186 snd_soc_dapm_stream_event(be, dir, event);
189 snd_soc_dapm_stream_event(fe, dir, event);
194 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *soc_dai)
197 struct snd_soc_pcm_runtime *rtd = substream->private_data;
200 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
201 rtd->dai_link->symmetric_rates)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
205 ret = snd_pcm_hw_constraint_single(substream->runtime,
206 SNDRV_PCM_HW_PARAM_RATE,
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply rate constraint: %d\n",
216 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
217 rtd->dai_link->symmetric_channels)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
221 ret = snd_pcm_hw_constraint_single(substream->runtime,
222 SNDRV_PCM_HW_PARAM_CHANNELS,
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply channel symmetry constraint: %d\n",
232 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
233 rtd->dai_link->symmetric_samplebits)) {
234 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
235 soc_dai->sample_bits);
237 ret = snd_pcm_hw_constraint_single(substream->runtime,
238 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
239 soc_dai->sample_bits);
241 dev_err(soc_dai->dev,
242 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
251 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
256 unsigned int rate, channels, sample_bits, symmetry, i;
258 rate = params_rate(params);
259 channels = params_channels(params);
260 sample_bits = snd_pcm_format_physical_width(params_format(params));
262 /* reject unmatched parameters when applying symmetry */
263 symmetry = cpu_dai->driver->symmetric_rates ||
264 rtd->dai_link->symmetric_rates;
266 for (i = 0; i < rtd->num_codecs; i++)
267 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
269 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271 cpu_dai->rate, rate);
275 symmetry = cpu_dai->driver->symmetric_channels ||
276 rtd->dai_link->symmetric_channels;
278 for (i = 0; i < rtd->num_codecs; i++)
279 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
281 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283 cpu_dai->channels, channels);
287 symmetry = cpu_dai->driver->symmetric_samplebits ||
288 rtd->dai_link->symmetric_samplebits;
290 for (i = 0; i < rtd->num_codecs; i++)
291 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
293 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295 cpu_dai->sample_bits, sample_bits);
302 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
306 struct snd_soc_dai_link *link = rtd->dai_link;
307 unsigned int symmetry, i;
309 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310 cpu_driver->symmetric_channels || link->symmetric_channels ||
311 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
313 for (i = 0; i < rtd->num_codecs; i++)
314 symmetry = symmetry ||
315 rtd->codec_dais[i]->driver->symmetric_rates ||
316 rtd->codec_dais[i]->driver->symmetric_channels ||
317 rtd->codec_dais[i]->driver->symmetric_samplebits;
322 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
330 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
332 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
336 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
340 struct snd_soc_dai *codec_dai;
342 unsigned int bits = 0, cpu_bits;
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
345 for (i = 0; i < rtd->num_codecs; i++) {
346 codec_dai = rtd->codec_dais[i];
347 if (codec_dai->driver->playback.sig_bits == 0) {
351 bits = max(codec_dai->driver->playback.sig_bits, bits);
353 cpu_bits = cpu_dai->driver->playback.sig_bits;
355 for (i = 0; i < rtd->num_codecs; i++) {
356 codec_dai = rtd->codec_dais[i];
357 if (codec_dai->driver->capture.sig_bits == 0) {
361 bits = max(codec_dai->driver->capture.sig_bits, bits);
363 cpu_bits = cpu_dai->driver->capture.sig_bits;
366 soc_pcm_set_msb(substream, bits);
367 soc_pcm_set_msb(substream, cpu_bits);
370 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
372 struct snd_pcm_runtime *runtime = substream->runtime;
373 struct snd_pcm_hardware *hw = &runtime->hw;
374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376 struct snd_soc_dai_driver *codec_dai_drv;
377 struct snd_soc_pcm_stream *codec_stream;
378 struct snd_soc_pcm_stream *cpu_stream;
379 unsigned int chan_min = 0, chan_max = UINT_MAX;
380 unsigned int rate_min = 0, rate_max = UINT_MAX;
381 unsigned int rates = UINT_MAX;
382 u64 formats = ULLONG_MAX;
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 cpu_stream = &cpu_dai_drv->playback;
388 cpu_stream = &cpu_dai_drv->capture;
390 /* first calculate min/max only for CODECs in the DAI link */
391 for (i = 0; i < rtd->num_codecs; i++) {
394 * Skip CODECs which don't support the current stream type.
395 * Otherwise, since the rate, channel, and format values will
396 * zero in that case, we would have no usable settings left,
397 * causing the resulting setup to fail.
398 * At least one CODEC should match, otherwise we should have
399 * bailed out on a higher level, since there would be no
400 * CODEC to support the transfer direction in that case.
402 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
406 codec_dai_drv = rtd->codec_dais[i]->driver;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 codec_stream = &codec_dai_drv->playback;
410 codec_stream = &codec_dai_drv->capture;
411 chan_min = max(chan_min, codec_stream->channels_min);
412 chan_max = min(chan_max, codec_stream->channels_max);
413 rate_min = max(rate_min, codec_stream->rate_min);
414 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415 formats &= codec_stream->formats;
416 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
420 * chan min/max cannot be enforced if there are multiple CODEC DAIs
421 * connected to a single CPU DAI, use CPU DAI's directly and let
422 * channel allocation be fixed up later
424 if (rtd->num_codecs > 1) {
425 chan_min = cpu_stream->channels_min;
426 chan_max = cpu_stream->channels_max;
429 hw->channels_min = max(chan_min, cpu_stream->channels_min);
430 hw->channels_max = min(chan_max, cpu_stream->channels_max);
432 hw->formats &= formats & cpu_stream->formats;
434 hw->formats = formats & cpu_stream->formats;
435 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
437 snd_pcm_limit_hw_rates(runtime);
439 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
440 hw->rate_min = max(hw->rate_min, rate_min);
441 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
442 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
445 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
446 struct snd_soc_component *last)
448 struct snd_soc_pcm_runtime *rtd = substream->private_data;
449 struct snd_soc_rtdcom_list *rtdcom;
450 struct snd_soc_component *component;
452 for_each_rtdcom(rtd, rtdcom) {
453 component = rtdcom->component;
455 if (component == last)
458 if (!component->driver->ops ||
459 !component->driver->ops->close)
462 component->driver->ops->close(substream);
469 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
470 * then initialized and any private data can be allocated. This also calls
471 * startup for the cpu DAI, component, machine and codec DAI.
473 static int soc_pcm_open(struct snd_pcm_substream *substream)
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_pcm_runtime *runtime = substream->runtime;
477 struct snd_soc_component *component;
478 struct snd_soc_rtdcom_list *rtdcom;
479 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
480 struct snd_soc_dai *codec_dai;
481 const char *codec_dai_name = "multicodec";
484 pinctrl_pm_select_default_state(cpu_dai->dev);
485 for (i = 0; i < rtd->num_codecs; i++)
486 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
488 for_each_rtdcom(rtd, rtdcom) {
489 component = rtdcom->component;
491 pm_runtime_get_sync(component->dev);
494 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
496 /* startup the audio subsystem */
497 if (cpu_dai->driver->ops->startup) {
498 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
500 dev_err(cpu_dai->dev, "ASoC: can't open interface"
501 " %s: %d\n", cpu_dai->name, ret);
506 for_each_rtdcom(rtd, rtdcom) {
507 component = rtdcom->component;
509 if (!component->driver->ops ||
510 !component->driver->ops->open)
513 ret = component->driver->ops->open(substream);
515 dev_err(component->dev,
516 "ASoC: can't open component %s: %d\n",
517 component->name, ret);
523 for (i = 0; i < rtd->num_codecs; i++) {
524 codec_dai = rtd->codec_dais[i];
525 if (codec_dai->driver->ops->startup) {
526 ret = codec_dai->driver->ops->startup(substream,
529 dev_err(codec_dai->dev,
530 "ASoC: can't open codec %s: %d\n",
531 codec_dai->name, ret);
536 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
537 codec_dai->tx_mask = 0;
539 codec_dai->rx_mask = 0;
542 if (rtd->dai_link->ops->startup) {
543 ret = rtd->dai_link->ops->startup(substream);
545 pr_err("ASoC: %s startup failed: %d\n",
546 rtd->dai_link->name, ret);
551 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
552 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
555 /* Check that the codec and cpu DAIs are compatible */
556 soc_pcm_init_runtime_hw(substream);
558 if (rtd->num_codecs == 1)
559 codec_dai_name = rtd->codec_dai->name;
561 if (soc_pcm_has_symmetry(substream))
562 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
565 if (!runtime->hw.rates) {
566 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
567 codec_dai_name, cpu_dai->name);
570 if (!runtime->hw.formats) {
571 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
572 codec_dai_name, cpu_dai->name);
575 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
576 runtime->hw.channels_min > runtime->hw.channels_max) {
577 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
578 codec_dai_name, cpu_dai->name);
582 soc_pcm_apply_msb(substream);
584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
591 for (i = 0; i < rtd->num_codecs; i++) {
592 if (rtd->codec_dais[i]->active) {
593 ret = soc_pcm_apply_symmetry(substream,
600 pr_debug("ASoC: %s <-> %s info:\n",
601 codec_dai_name, cpu_dai->name);
602 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
603 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
604 runtime->hw.channels_max);
605 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
606 runtime->hw.rate_max);
610 snd_soc_runtime_activate(rtd, substream->stream);
612 mutex_unlock(&rtd->pcm_mutex);
616 if (rtd->dai_link->ops->shutdown)
617 rtd->dai_link->ops->shutdown(substream);
624 codec_dai = rtd->codec_dais[i];
625 if (codec_dai->driver->ops->shutdown)
626 codec_dai->driver->ops->shutdown(substream, codec_dai);
630 soc_pcm_components_close(substream, component);
632 if (cpu_dai->driver->ops->shutdown)
633 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
635 mutex_unlock(&rtd->pcm_mutex);
637 for_each_rtdcom(rtd, rtdcom) {
638 component = rtdcom->component;
640 pm_runtime_mark_last_busy(component->dev);
641 pm_runtime_put_autosuspend(component->dev);
644 for (i = 0; i < rtd->num_codecs; i++) {
645 if (!rtd->codec_dais[i]->active)
646 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
648 if (!cpu_dai->active)
649 pinctrl_pm_select_sleep_state(cpu_dai->dev);
655 * Power down the audio subsystem pmdown_time msecs after close is called.
656 * This is to ensure there are no pops or clicks in between any music tracks
657 * due to DAPM power cycling.
659 static void close_delayed_work(struct work_struct *work)
661 struct snd_soc_pcm_runtime *rtd =
662 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
663 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
665 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
667 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
668 codec_dai->driver->playback.stream_name,
669 codec_dai->playback_active ? "active" : "inactive",
670 rtd->pop_wait ? "yes" : "no");
672 /* are we waiting on this codec DAI stream */
673 if (rtd->pop_wait == 1) {
675 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
676 SND_SOC_DAPM_STREAM_STOP);
679 mutex_unlock(&rtd->pcm_mutex);
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and components are also
687 static int soc_pcm_close(struct snd_pcm_substream *substream)
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
690 struct snd_soc_component *component;
691 struct snd_soc_rtdcom_list *rtdcom;
692 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
693 struct snd_soc_dai *codec_dai;
696 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
698 snd_soc_runtime_deactivate(rtd, substream->stream);
700 /* clear the corresponding DAIs rate when inactive */
701 if (!cpu_dai->active)
704 for (i = 0; i < rtd->num_codecs; i++) {
705 codec_dai = rtd->codec_dais[i];
706 if (!codec_dai->active)
710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
712 if (cpu_dai->driver->ops->shutdown)
713 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
715 for (i = 0; i < rtd->num_codecs; i++) {
716 codec_dai = rtd->codec_dais[i];
717 if (codec_dai->driver->ops->shutdown)
718 codec_dai->driver->ops->shutdown(substream, codec_dai);
721 if (rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
724 soc_pcm_components_close(substream, NULL);
726 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
727 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
728 /* powered down playback stream now */
729 snd_soc_dapm_stream_event(rtd,
730 SNDRV_PCM_STREAM_PLAYBACK,
731 SND_SOC_DAPM_STREAM_STOP);
733 /* start delayed pop wq here for playback streams */
735 queue_delayed_work(system_power_efficient_wq,
737 msecs_to_jiffies(rtd->pmdown_time));
740 /* capture streams can be powered down now */
741 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
742 SND_SOC_DAPM_STREAM_STOP);
745 mutex_unlock(&rtd->pcm_mutex);
747 for_each_rtdcom(rtd, rtdcom) {
748 component = rtdcom->component;
750 pm_runtime_mark_last_busy(component->dev);
751 pm_runtime_put_autosuspend(component->dev);
754 for (i = 0; i < rtd->num_codecs; i++) {
755 if (!rtd->codec_dais[i]->active)
756 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
758 if (!cpu_dai->active)
759 pinctrl_pm_select_sleep_state(cpu_dai->dev);
765 * Called by ALSA when the PCM substream is prepared, can set format, sample
766 * rate, etc. This function is non atomic and can be called multiple times,
767 * it can refer to the runtime info.
769 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
771 struct snd_soc_pcm_runtime *rtd = substream->private_data;
772 struct snd_soc_component *component;
773 struct snd_soc_rtdcom_list *rtdcom;
774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
775 struct snd_soc_dai *codec_dai;
778 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
780 if (rtd->dai_link->ops->prepare) {
781 ret = rtd->dai_link->ops->prepare(substream);
783 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
789 for_each_rtdcom(rtd, rtdcom) {
790 component = rtdcom->component;
792 if (!component->driver->ops ||
793 !component->driver->ops->prepare)
796 ret = component->driver->ops->prepare(substream);
798 dev_err(component->dev,
799 "ASoC: platform prepare error: %d\n", ret);
804 for (i = 0; i < rtd->num_codecs; i++) {
805 codec_dai = rtd->codec_dais[i];
806 if (codec_dai->driver->ops->prepare) {
807 ret = codec_dai->driver->ops->prepare(substream,
810 dev_err(codec_dai->dev,
811 "ASoC: codec DAI prepare error: %d\n",
818 if (cpu_dai->driver->ops->prepare) {
819 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
821 dev_err(cpu_dai->dev,
822 "ASoC: cpu DAI prepare error: %d\n", ret);
827 /* cancel any delayed stream shutdown that is pending */
828 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 cancel_delayed_work(&rtd->delayed_work);
834 snd_soc_dapm_stream_event(rtd, substream->stream,
835 SND_SOC_DAPM_STREAM_START);
837 for (i = 0; i < rtd->num_codecs; i++)
838 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
840 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
843 mutex_unlock(&rtd->pcm_mutex);
847 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
850 struct snd_interval *interval;
851 int channels = hweight_long(mask);
853 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
854 interval->min = channels;
855 interval->max = channels;
858 int soc_dai_hw_params(struct snd_pcm_substream *substream,
859 struct snd_pcm_hw_params *params,
860 struct snd_soc_dai *dai)
862 struct snd_soc_pcm_runtime *rtd = substream->private_data;
865 /* perform any topology hw_params fixups before DAI */
866 if (rtd->dai_link->be_hw_params_fixup) {
867 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
870 "ASoC: hw_params topology fixup failed %d\n",
876 if (dai->driver->ops->hw_params) {
877 ret = dai->driver->ops->hw_params(substream, params, dai);
879 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
888 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
889 struct snd_soc_component *last)
891 struct snd_soc_pcm_runtime *rtd = substream->private_data;
892 struct snd_soc_rtdcom_list *rtdcom;
893 struct snd_soc_component *component;
895 for_each_rtdcom(rtd, rtdcom) {
896 component = rtdcom->component;
898 if (component == last)
901 if (!component->driver->ops ||
902 !component->driver->ops->hw_free)
905 component->driver->ops->hw_free(substream);
912 * Called by ALSA when the hardware params are set by application. This
913 * function can also be called multiple times and can allocate buffers
914 * (using snd_pcm_lib_* ). It's non-atomic.
916 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
917 struct snd_pcm_hw_params *params)
919 struct snd_soc_pcm_runtime *rtd = substream->private_data;
920 struct snd_soc_component *component;
921 struct snd_soc_rtdcom_list *rtdcom;
922 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
925 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
926 if (rtd->dai_link->ops->hw_params) {
927 ret = rtd->dai_link->ops->hw_params(substream, params);
929 dev_err(rtd->card->dev, "ASoC: machine hw_params"
930 " failed: %d\n", ret);
935 for (i = 0; i < rtd->num_codecs; i++) {
936 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
937 struct snd_pcm_hw_params codec_params;
940 * Skip CODECs which don't support the current stream type,
941 * the idea being that if a CODEC is not used for the currently
942 * set up transfer direction, it should not need to be
943 * configured, especially since the configuration used might
944 * not even be supported by that CODEC. There may be cases
945 * however where a CODEC needs to be set up although it is
946 * actually not being used for the transfer, e.g. if a
947 * capture-only CODEC is acting as an LRCLK and/or BCLK master
948 * for the DAI link including a playback-only CODEC.
949 * If this becomes necessary, we will have to augment the
950 * machine driver setup with information on how to act, so
951 * we can do the right thing here.
953 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
956 /* copy params for each codec */
957 codec_params = *params;
959 /* fixup params based on TDM slot masks */
960 if (codec_dai->tx_mask)
961 soc_pcm_codec_params_fixup(&codec_params,
963 if (codec_dai->rx_mask)
964 soc_pcm_codec_params_fixup(&codec_params,
967 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
971 codec_dai->rate = params_rate(&codec_params);
972 codec_dai->channels = params_channels(&codec_params);
973 codec_dai->sample_bits = snd_pcm_format_physical_width(
974 params_format(&codec_params));
977 ret = soc_dai_hw_params(substream, params, cpu_dai);
981 for_each_rtdcom(rtd, rtdcom) {
982 component = rtdcom->component;
984 if (!component->driver->ops ||
985 !component->driver->ops->hw_params)
988 ret = component->driver->ops->hw_params(substream, params);
990 dev_err(component->dev,
991 "ASoC: %s hw params failed: %d\n",
992 component->name, ret);
998 /* store the parameters for each DAIs */
999 cpu_dai->rate = params_rate(params);
1000 cpu_dai->channels = params_channels(params);
1001 cpu_dai->sample_bits =
1002 snd_pcm_format_physical_width(params_format(params));
1004 ret = soc_pcm_params_symmetry(substream, params);
1008 mutex_unlock(&rtd->pcm_mutex);
1012 soc_pcm_components_hw_free(substream, component);
1014 if (cpu_dai->driver->ops->hw_free)
1015 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1018 i = rtd->num_codecs;
1022 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1023 if (codec_dai->driver->ops->hw_free)
1024 codec_dai->driver->ops->hw_free(substream, codec_dai);
1025 codec_dai->rate = 0;
1028 if (rtd->dai_link->ops->hw_free)
1029 rtd->dai_link->ops->hw_free(substream);
1031 mutex_unlock(&rtd->pcm_mutex);
1036 * Frees resources allocated by hw_params, can be called multiple times
1038 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1040 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1042 struct snd_soc_dai *codec_dai;
1043 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1046 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1048 /* clear the corresponding DAIs parameters when going to be inactive */
1049 if (cpu_dai->active == 1) {
1051 cpu_dai->channels = 0;
1052 cpu_dai->sample_bits = 0;
1055 for (i = 0; i < rtd->num_codecs; i++) {
1056 codec_dai = rtd->codec_dais[i];
1057 if (codec_dai->active == 1) {
1058 codec_dai->rate = 0;
1059 codec_dai->channels = 0;
1060 codec_dai->sample_bits = 0;
1064 /* apply codec digital mute */
1065 for (i = 0; i < rtd->num_codecs; i++) {
1066 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1067 (!playback && rtd->codec_dais[i]->capture_active == 1))
1068 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1072 /* free any machine hw params */
1073 if (rtd->dai_link->ops->hw_free)
1074 rtd->dai_link->ops->hw_free(substream);
1076 /* free any component resources */
1077 soc_pcm_components_hw_free(substream, NULL);
1079 /* now free hw params for the DAIs */
1080 for (i = 0; i < rtd->num_codecs; i++) {
1081 codec_dai = rtd->codec_dais[i];
1082 if (codec_dai->driver->ops->hw_free)
1083 codec_dai->driver->ops->hw_free(substream, codec_dai);
1086 if (cpu_dai->driver->ops->hw_free)
1087 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1089 mutex_unlock(&rtd->pcm_mutex);
1093 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1095 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1096 struct snd_soc_component *component;
1097 struct snd_soc_rtdcom_list *rtdcom;
1098 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1099 struct snd_soc_dai *codec_dai;
1102 for (i = 0; i < rtd->num_codecs; i++) {
1103 codec_dai = rtd->codec_dais[i];
1104 if (codec_dai->driver->ops->trigger) {
1105 ret = codec_dai->driver->ops->trigger(substream,
1112 for_each_rtdcom(rtd, rtdcom) {
1113 component = rtdcom->component;
1115 if (!component->driver->ops ||
1116 !component->driver->ops->trigger)
1119 ret = component->driver->ops->trigger(substream, cmd);
1124 if (cpu_dai->driver->ops->trigger) {
1125 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1130 if (rtd->dai_link->ops->trigger) {
1131 ret = rtd->dai_link->ops->trigger(substream, cmd);
1139 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1143 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1144 struct snd_soc_dai *codec_dai;
1147 for (i = 0; i < rtd->num_codecs; i++) {
1148 codec_dai = rtd->codec_dais[i];
1149 if (codec_dai->driver->ops->bespoke_trigger) {
1150 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1157 if (cpu_dai->driver->ops->bespoke_trigger) {
1158 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1165 * soc level wrapper for pointer callback
1166 * If cpu_dai, codec_dai, component driver has the delay callback, then
1167 * the runtime->delay will be updated accordingly.
1169 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1171 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1172 struct snd_soc_component *component;
1173 struct snd_soc_rtdcom_list *rtdcom;
1174 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1175 struct snd_soc_dai *codec_dai;
1176 struct snd_pcm_runtime *runtime = substream->runtime;
1177 snd_pcm_uframes_t offset = 0;
1178 snd_pcm_sframes_t delay = 0;
1179 snd_pcm_sframes_t codec_delay = 0;
1182 for_each_rtdcom(rtd, rtdcom) {
1183 component = rtdcom->component;
1185 if (!component->driver->ops ||
1186 !component->driver->ops->pointer)
1189 /* FIXME: use 1st pointer */
1190 offset = component->driver->ops->pointer(substream);
1194 if (cpu_dai->driver->ops->delay)
1195 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1197 for (i = 0; i < rtd->num_codecs; i++) {
1198 codec_dai = rtd->codec_dais[i];
1199 if (codec_dai->driver->ops->delay)
1200 codec_delay = max(codec_delay,
1201 codec_dai->driver->ops->delay(substream,
1204 delay += codec_delay;
1206 runtime->delay = delay;
1211 /* connect a FE and BE */
1212 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1213 struct snd_soc_pcm_runtime *be, int stream)
1215 struct snd_soc_dpcm *dpcm;
1217 /* only add new dpcms */
1218 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1219 if (dpcm->be == be && dpcm->fe == fe)
1223 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1229 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1230 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1231 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1232 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1234 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1235 stream ? "capture" : "playback", fe->dai_link->name,
1236 stream ? "<-" : "->", be->dai_link->name);
1238 #ifdef CONFIG_DEBUG_FS
1239 if (fe->debugfs_dpcm_root)
1240 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1241 fe->debugfs_dpcm_root, &dpcm->state);
1246 /* reparent a BE onto another FE */
1247 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1248 struct snd_soc_pcm_runtime *be, int stream)
1250 struct snd_soc_dpcm *dpcm;
1251 struct snd_pcm_substream *fe_substream, *be_substream;
1253 /* reparent if BE is connected to other FEs */
1254 if (!be->dpcm[stream].users)
1257 be_substream = snd_soc_dpcm_get_substream(be, stream);
1259 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1263 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1264 stream ? "capture" : "playback",
1265 dpcm->fe->dai_link->name,
1266 stream ? "<-" : "->", dpcm->be->dai_link->name);
1268 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1269 be_substream->runtime = fe_substream->runtime;
1274 /* disconnect a BE and FE */
1275 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1277 struct snd_soc_dpcm *dpcm, *d;
1279 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1280 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1281 stream ? "capture" : "playback",
1282 dpcm->be->dai_link->name);
1284 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1287 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1288 stream ? "capture" : "playback", fe->dai_link->name,
1289 stream ? "<-" : "->", dpcm->be->dai_link->name);
1291 /* BEs still alive need new FE */
1292 dpcm_be_reparent(fe, dpcm->be, stream);
1294 #ifdef CONFIG_DEBUG_FS
1295 debugfs_remove(dpcm->debugfs_state);
1297 list_del(&dpcm->list_be);
1298 list_del(&dpcm->list_fe);
1303 /* get BE for DAI widget and stream */
1304 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1305 struct snd_soc_dapm_widget *widget, int stream)
1307 struct snd_soc_pcm_runtime *be;
1310 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1312 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1313 list_for_each_entry(be, &card->rtd_list, list) {
1315 if (!be->dai_link->no_pcm)
1318 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1319 be->cpu_dai->playback_widget ?
1320 be->cpu_dai->playback_widget->name : "(not set)");
1322 if (be->cpu_dai->playback_widget == widget)
1325 for (i = 0; i < be->num_codecs; i++) {
1326 struct snd_soc_dai *dai = be->codec_dais[i];
1327 if (dai->playback_widget == widget)
1333 list_for_each_entry(be, &card->rtd_list, list) {
1335 if (!be->dai_link->no_pcm)
1338 dev_dbg(card->dev, "ASoC: try BE %s\n",
1339 be->cpu_dai->capture_widget ?
1340 be->cpu_dai->capture_widget->name : "(not set)");
1342 if (be->cpu_dai->capture_widget == widget)
1345 for (i = 0; i < be->num_codecs; i++) {
1346 struct snd_soc_dai *dai = be->codec_dais[i];
1347 if (dai->capture_widget == widget)
1353 /* dai link name and stream name set correctly ? */
1354 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1355 stream ? "capture" : "playback", widget->name);
1359 static inline struct snd_soc_dapm_widget *
1360 dai_get_widget(struct snd_soc_dai *dai, int stream)
1362 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1363 return dai->playback_widget;
1365 return dai->capture_widget;
1368 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1369 struct snd_soc_dapm_widget *widget)
1373 for (i = 0; i < list->num_widgets; i++) {
1374 if (widget == list->widgets[i])
1381 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1382 enum snd_soc_dapm_direction dir)
1384 struct snd_soc_card *card = widget->dapm->card;
1385 struct snd_soc_pcm_runtime *rtd;
1388 if (dir == SND_SOC_DAPM_DIR_OUT) {
1389 list_for_each_entry(rtd, &card->rtd_list, list) {
1390 if (!rtd->dai_link->no_pcm)
1393 if (rtd->cpu_dai->playback_widget == widget)
1396 for (i = 0; i < rtd->num_codecs; ++i) {
1397 struct snd_soc_dai *dai = rtd->codec_dais[i];
1398 if (dai->playback_widget == widget)
1402 } else { /* SND_SOC_DAPM_DIR_IN */
1403 list_for_each_entry(rtd, &card->rtd_list, list) {
1404 if (!rtd->dai_link->no_pcm)
1407 if (rtd->cpu_dai->capture_widget == widget)
1410 for (i = 0; i < rtd->num_codecs; ++i) {
1411 struct snd_soc_dai *dai = rtd->codec_dais[i];
1412 if (dai->capture_widget == widget)
1421 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1422 int stream, struct snd_soc_dapm_widget_list **list)
1424 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1427 /* get number of valid DAI paths and their widgets */
1428 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1429 dpcm_end_walk_at_be);
1431 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1432 stream ? "capture" : "playback");
1437 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1438 struct snd_soc_dapm_widget_list **list_)
1440 struct snd_soc_dpcm *dpcm;
1441 struct snd_soc_dapm_widget_list *list = *list_;
1442 struct snd_soc_dapm_widget *widget;
1445 /* Destroy any old FE <--> BE connections */
1446 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1449 /* is there a valid CPU DAI widget for this BE */
1450 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1452 /* prune the BE if it's no longer in our active list */
1453 if (widget && widget_in_list(list, widget))
1456 /* is there a valid CODEC DAI widget for this BE */
1457 for (i = 0; i < dpcm->be->num_codecs; i++) {
1458 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1459 widget = dai_get_widget(dai, stream);
1461 /* prune the BE if it's no longer in our active list */
1462 if (widget && widget_in_list(list, widget))
1466 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1467 stream ? "capture" : "playback",
1468 dpcm->be->dai_link->name, fe->dai_link->name);
1469 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1470 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1474 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1478 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1479 struct snd_soc_dapm_widget_list **list_)
1481 struct snd_soc_card *card = fe->card;
1482 struct snd_soc_dapm_widget_list *list = *list_;
1483 struct snd_soc_pcm_runtime *be;
1484 int i, new = 0, err;
1486 /* Create any new FE <--> BE connections */
1487 for (i = 0; i < list->num_widgets; i++) {
1489 switch (list->widgets[i]->id) {
1490 case snd_soc_dapm_dai_in:
1491 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1494 case snd_soc_dapm_dai_out:
1495 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1502 /* is there a valid BE rtd for this widget */
1503 be = dpcm_get_be(card, list->widgets[i], stream);
1505 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1506 list->widgets[i]->name);
1510 /* make sure BE is a real BE */
1511 if (!be->dai_link->no_pcm)
1514 /* don't connect if FE is not running */
1515 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1518 /* newly connected FE and BE */
1519 err = dpcm_be_connect(fe, be, stream);
1521 dev_err(fe->dev, "ASoC: can't connect %s\n",
1522 list->widgets[i]->name);
1524 } else if (err == 0) /* already connected */
1528 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1532 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1537 * Find the corresponding BE DAIs that source or sink audio to this
1540 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1541 int stream, struct snd_soc_dapm_widget_list **list, int new)
1544 return dpcm_add_paths(fe, stream, list);
1546 return dpcm_prune_paths(fe, stream, list);
1549 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1551 struct snd_soc_dpcm *dpcm;
1553 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1554 dpcm->be->dpcm[stream].runtime_update =
1555 SND_SOC_DPCM_UPDATE_NO;
1558 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1561 struct snd_soc_dpcm *dpcm;
1563 /* disable any enabled and non active backends */
1564 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1566 struct snd_soc_pcm_runtime *be = dpcm->be;
1567 struct snd_pcm_substream *be_substream =
1568 snd_soc_dpcm_get_substream(be, stream);
1570 if (be->dpcm[stream].users == 0)
1571 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1572 stream ? "capture" : "playback",
1573 be->dpcm[stream].state);
1575 if (--be->dpcm[stream].users != 0)
1578 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1581 soc_pcm_close(be_substream);
1582 be_substream->runtime = NULL;
1583 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1587 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1589 struct snd_soc_dpcm *dpcm;
1592 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1593 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1595 struct snd_soc_pcm_runtime *be = dpcm->be;
1596 struct snd_pcm_substream *be_substream =
1597 snd_soc_dpcm_get_substream(be, stream);
1599 if (!be_substream) {
1600 dev_err(be->dev, "ASoC: no backend %s stream\n",
1601 stream ? "capture" : "playback");
1605 /* is this op for this BE ? */
1606 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1609 /* first time the dpcm is open ? */
1610 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1611 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1612 stream ? "capture" : "playback",
1613 be->dpcm[stream].state);
1615 if (be->dpcm[stream].users++ != 0)
1618 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1619 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1622 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1623 stream ? "capture" : "playback", be->dai_link->name);
1625 be_substream->runtime = be->dpcm[stream].runtime;
1626 err = soc_pcm_open(be_substream);
1628 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1629 be->dpcm[stream].users--;
1630 if (be->dpcm[stream].users < 0)
1631 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1632 stream ? "capture" : "playback",
1633 be->dpcm[stream].state);
1635 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1639 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1646 /* disable any enabled and non active backends */
1647 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1648 struct snd_soc_pcm_runtime *be = dpcm->be;
1649 struct snd_pcm_substream *be_substream =
1650 snd_soc_dpcm_get_substream(be, stream);
1652 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1655 if (be->dpcm[stream].users == 0)
1656 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1657 stream ? "capture" : "playback",
1658 be->dpcm[stream].state);
1660 if (--be->dpcm[stream].users != 0)
1663 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1666 soc_pcm_close(be_substream);
1667 be_substream->runtime = NULL;
1668 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1674 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1675 struct snd_soc_pcm_stream *stream)
1677 runtime->hw.rate_min = stream->rate_min;
1678 runtime->hw.rate_max = stream->rate_max;
1679 runtime->hw.channels_min = stream->channels_min;
1680 runtime->hw.channels_max = stream->channels_max;
1681 if (runtime->hw.formats)
1682 runtime->hw.formats &= stream->formats;
1684 runtime->hw.formats = stream->formats;
1685 runtime->hw.rates = stream->rates;
1688 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1691 struct snd_soc_pcm_runtime *fe = substream->private_data;
1692 struct snd_soc_dpcm *dpcm;
1693 int stream = substream->stream;
1695 if (!fe->dai_link->dpcm_merged_format)
1699 * It returns merged BE codec format
1700 * if FE want to use it (= dpcm_merged_format)
1703 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1704 struct snd_soc_pcm_runtime *be = dpcm->be;
1705 struct snd_soc_dai_driver *codec_dai_drv;
1706 struct snd_soc_pcm_stream *codec_stream;
1709 for (i = 0; i < be->num_codecs; i++) {
1710 codec_dai_drv = be->codec_dais[i]->driver;
1711 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1712 codec_stream = &codec_dai_drv->playback;
1714 codec_stream = &codec_dai_drv->capture;
1716 *formats &= codec_stream->formats;
1721 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1722 unsigned int *channels_min,
1723 unsigned int *channels_max)
1725 struct snd_soc_pcm_runtime *fe = substream->private_data;
1726 struct snd_soc_dpcm *dpcm;
1727 int stream = substream->stream;
1729 if (!fe->dai_link->dpcm_merged_chan)
1733 * It returns merged BE codec channel;
1734 * if FE want to use it (= dpcm_merged_chan)
1737 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1738 struct snd_soc_pcm_runtime *be = dpcm->be;
1739 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1740 struct snd_soc_dai_driver *codec_dai_drv;
1741 struct snd_soc_pcm_stream *codec_stream;
1742 struct snd_soc_pcm_stream *cpu_stream;
1744 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1745 cpu_stream = &cpu_dai_drv->playback;
1747 cpu_stream = &cpu_dai_drv->capture;
1749 *channels_min = max(*channels_min, cpu_stream->channels_min);
1750 *channels_max = min(*channels_max, cpu_stream->channels_max);
1753 * chan min/max cannot be enforced if there are multiple CODEC
1754 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1756 if (be->num_codecs == 1) {
1757 codec_dai_drv = be->codec_dais[0]->driver;
1759 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1760 codec_stream = &codec_dai_drv->playback;
1762 codec_stream = &codec_dai_drv->capture;
1764 *channels_min = max(*channels_min,
1765 codec_stream->channels_min);
1766 *channels_max = min(*channels_max,
1767 codec_stream->channels_max);
1772 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1773 unsigned int *rates,
1774 unsigned int *rate_min,
1775 unsigned int *rate_max)
1777 struct snd_soc_pcm_runtime *fe = substream->private_data;
1778 struct snd_soc_dpcm *dpcm;
1779 int stream = substream->stream;
1781 if (!fe->dai_link->dpcm_merged_rate)
1785 * It returns merged BE codec channel;
1786 * if FE want to use it (= dpcm_merged_chan)
1789 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1790 struct snd_soc_pcm_runtime *be = dpcm->be;
1791 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1792 struct snd_soc_dai_driver *codec_dai_drv;
1793 struct snd_soc_pcm_stream *codec_stream;
1794 struct snd_soc_pcm_stream *cpu_stream;
1797 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1798 cpu_stream = &cpu_dai_drv->playback;
1800 cpu_stream = &cpu_dai_drv->capture;
1802 *rate_min = max(*rate_min, cpu_stream->rate_min);
1803 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1804 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1806 for (i = 0; i < be->num_codecs; i++) {
1808 * Skip CODECs which don't support the current stream
1809 * type. See soc_pcm_init_runtime_hw() for more details
1811 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1815 codec_dai_drv = be->codec_dais[i]->driver;
1816 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1817 codec_stream = &codec_dai_drv->playback;
1819 codec_stream = &codec_dai_drv->capture;
1821 *rate_min = max(*rate_min, codec_stream->rate_min);
1822 *rate_max = min_not_zero(*rate_max,
1823 codec_stream->rate_max);
1824 *rates = snd_pcm_rate_mask_intersect(*rates,
1825 codec_stream->rates);
1830 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1832 struct snd_pcm_runtime *runtime = substream->runtime;
1833 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1834 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1835 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1837 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1838 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1840 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1842 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1843 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1844 &runtime->hw.channels_max);
1845 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1846 &runtime->hw.rate_min, &runtime->hw.rate_max);
1849 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1851 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1852 * for avoiding the race with trigger callback.
1853 * If the state is unset and a trigger is pending while the previous operation,
1854 * process the pending trigger action here.
1856 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1857 int stream, enum snd_soc_dpcm_update state)
1859 struct snd_pcm_substream *substream =
1860 snd_soc_dpcm_get_substream(fe, stream);
1862 snd_pcm_stream_lock_irq(substream);
1863 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1864 dpcm_fe_dai_do_trigger(substream,
1865 fe->dpcm[stream].trigger_pending - 1);
1866 fe->dpcm[stream].trigger_pending = 0;
1868 fe->dpcm[stream].runtime_update = state;
1869 snd_pcm_stream_unlock_irq(substream);
1872 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1875 struct snd_soc_dpcm *dpcm;
1876 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1877 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1880 /* apply symmetry for FE */
1881 if (soc_pcm_has_symmetry(fe_substream))
1882 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1884 /* Symmetry only applies if we've got an active stream. */
1885 if (fe_cpu_dai->active) {
1886 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1891 /* apply symmetry for BE */
1892 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1893 struct snd_soc_pcm_runtime *be = dpcm->be;
1894 struct snd_pcm_substream *be_substream =
1895 snd_soc_dpcm_get_substream(be, stream);
1896 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1899 if (rtd->dai_link->be_hw_params_fixup)
1902 if (soc_pcm_has_symmetry(be_substream))
1903 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1905 /* Symmetry only applies if we've got an active stream. */
1906 if (rtd->cpu_dai->active) {
1907 err = soc_pcm_apply_symmetry(fe_substream,
1913 for (i = 0; i < rtd->num_codecs; i++) {
1914 if (rtd->codec_dais[i]->active) {
1915 err = soc_pcm_apply_symmetry(fe_substream,
1916 rtd->codec_dais[i]);
1926 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1928 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1929 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1930 int stream = fe_substream->stream, ret = 0;
1932 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1934 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1936 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1940 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1942 /* start the DAI frontend */
1943 ret = soc_pcm_open(fe_substream);
1945 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1949 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1951 dpcm_set_fe_runtime(fe_substream);
1952 snd_pcm_limit_hw_rates(runtime);
1954 ret = dpcm_apply_symmetry(fe_substream, stream);
1956 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1961 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1965 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1967 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1971 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1973 struct snd_soc_dpcm *dpcm;
1975 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1976 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1978 struct snd_soc_pcm_runtime *be = dpcm->be;
1979 struct snd_pcm_substream *be_substream =
1980 snd_soc_dpcm_get_substream(be, stream);
1982 /* is this op for this BE ? */
1983 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1986 if (be->dpcm[stream].users == 0)
1987 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1988 stream ? "capture" : "playback",
1989 be->dpcm[stream].state);
1991 if (--be->dpcm[stream].users != 0)
1994 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1995 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1996 soc_pcm_hw_free(be_substream);
1997 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2000 dev_dbg(be->dev, "ASoC: close BE %s\n",
2001 be->dai_link->name);
2003 soc_pcm_close(be_substream);
2004 be_substream->runtime = NULL;
2006 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2011 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2013 struct snd_soc_pcm_runtime *fe = substream->private_data;
2014 int stream = substream->stream;
2016 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2018 /* shutdown the BEs */
2019 dpcm_be_dai_shutdown(fe, substream->stream);
2021 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2023 /* now shutdown the frontend */
2024 soc_pcm_close(substream);
2026 /* run the stream event for each BE */
2027 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2029 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2030 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2034 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2036 struct snd_soc_dpcm *dpcm;
2038 /* only hw_params backends that are either sinks or sources
2039 * to this frontend DAI */
2040 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2042 struct snd_soc_pcm_runtime *be = dpcm->be;
2043 struct snd_pcm_substream *be_substream =
2044 snd_soc_dpcm_get_substream(be, stream);
2046 /* is this op for this BE ? */
2047 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2050 /* only free hw when no longer used - check all FEs */
2051 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2054 /* do not free hw if this BE is used by other FE */
2055 if (be->dpcm[stream].users > 1)
2058 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2059 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2060 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2061 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2062 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2063 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2066 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2067 be->dai_link->name);
2069 soc_pcm_hw_free(be_substream);
2071 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2077 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2079 struct snd_soc_pcm_runtime *fe = substream->private_data;
2080 int err, stream = substream->stream;
2082 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2083 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2085 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2087 /* call hw_free on the frontend */
2088 err = soc_pcm_hw_free(substream);
2090 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2091 fe->dai_link->name);
2093 /* only hw_params backends that are either sinks or sources
2094 * to this frontend DAI */
2095 err = dpcm_be_dai_hw_free(fe, stream);
2097 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2098 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2100 mutex_unlock(&fe->card->mutex);
2104 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2106 struct snd_soc_dpcm *dpcm;
2109 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2111 struct snd_soc_pcm_runtime *be = dpcm->be;
2112 struct snd_pcm_substream *be_substream =
2113 snd_soc_dpcm_get_substream(be, stream);
2115 /* is this op for this BE ? */
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2119 /* copy params for each dpcm */
2120 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2121 sizeof(struct snd_pcm_hw_params));
2123 /* perform any hw_params fixups */
2124 if (be->dai_link->be_hw_params_fixup) {
2125 ret = be->dai_link->be_hw_params_fixup(be,
2129 "ASoC: hw_params BE fixup failed %d\n",
2135 /* only allow hw_params() if no connected FEs are running */
2136 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2139 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2140 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2141 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2144 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2145 be->dai_link->name);
2147 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2149 dev_err(dpcm->be->dev,
2150 "ASoC: hw_params BE failed %d\n", ret);
2154 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2159 /* disable any enabled and non active backends */
2160 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2161 struct snd_soc_pcm_runtime *be = dpcm->be;
2162 struct snd_pcm_substream *be_substream =
2163 snd_soc_dpcm_get_substream(be, stream);
2165 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2168 /* only allow hw_free() if no connected FEs are running */
2169 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2172 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2173 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2174 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2175 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2178 soc_pcm_hw_free(be_substream);
2184 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2185 struct snd_pcm_hw_params *params)
2187 struct snd_soc_pcm_runtime *fe = substream->private_data;
2188 int ret, stream = substream->stream;
2190 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2191 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2193 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2194 sizeof(struct snd_pcm_hw_params));
2195 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2197 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2201 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2202 fe->dai_link->name, params_rate(params),
2203 params_channels(params), params_format(params));
2205 /* call hw_params on the frontend */
2206 ret = soc_pcm_hw_params(substream, params);
2208 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2209 dpcm_be_dai_hw_free(fe, stream);
2211 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2214 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2215 mutex_unlock(&fe->card->mutex);
2219 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2220 struct snd_pcm_substream *substream, int cmd)
2224 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2225 dpcm->be->dai_link->name, cmd);
2227 ret = soc_pcm_trigger(substream, cmd);
2229 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2234 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2237 struct snd_soc_dpcm *dpcm;
2240 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2242 struct snd_soc_pcm_runtime *be = dpcm->be;
2243 struct snd_pcm_substream *be_substream =
2244 snd_soc_dpcm_get_substream(be, stream);
2246 /* is this op for this BE ? */
2247 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2251 case SNDRV_PCM_TRIGGER_START:
2252 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2256 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2262 case SNDRV_PCM_TRIGGER_RESUME:
2263 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2266 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2272 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2273 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2276 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2280 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2282 case SNDRV_PCM_TRIGGER_STOP:
2283 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2286 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2289 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2293 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2295 case SNDRV_PCM_TRIGGER_SUSPEND:
2296 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2299 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2302 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2308 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2309 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2312 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2315 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2319 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2326 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2328 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2330 struct snd_soc_pcm_runtime *fe = substream->private_data;
2331 int stream = substream->stream, ret;
2332 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2334 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2337 case SND_SOC_DPCM_TRIGGER_PRE:
2338 /* call trigger on the frontend before the backend. */
2340 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2341 fe->dai_link->name, cmd);
2343 ret = soc_pcm_trigger(substream, cmd);
2345 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2349 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2351 case SND_SOC_DPCM_TRIGGER_POST:
2352 /* call trigger on the frontend after the backend. */
2354 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2356 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2360 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2361 fe->dai_link->name, cmd);
2363 ret = soc_pcm_trigger(substream, cmd);
2365 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2366 /* bespoke trigger() - handles both FE and BEs */
2368 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2369 fe->dai_link->name, cmd);
2371 ret = soc_pcm_bespoke_trigger(substream, cmd);
2373 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2378 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2379 fe->dai_link->name);
2385 case SNDRV_PCM_TRIGGER_START:
2386 case SNDRV_PCM_TRIGGER_RESUME:
2387 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2388 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2390 case SNDRV_PCM_TRIGGER_STOP:
2391 case SNDRV_PCM_TRIGGER_SUSPEND:
2392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2394 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2395 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2400 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2404 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2406 struct snd_soc_pcm_runtime *fe = substream->private_data;
2407 int stream = substream->stream;
2409 /* if FE's runtime_update is already set, we're in race;
2410 * process this trigger later at exit
2412 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2413 fe->dpcm[stream].trigger_pending = cmd + 1;
2414 return 0; /* delayed, assuming it's successful */
2417 /* we're alone, let's trigger */
2418 return dpcm_fe_dai_do_trigger(substream, cmd);
2421 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2423 struct snd_soc_dpcm *dpcm;
2426 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2428 struct snd_soc_pcm_runtime *be = dpcm->be;
2429 struct snd_pcm_substream *be_substream =
2430 snd_soc_dpcm_get_substream(be, stream);
2432 /* is this op for this BE ? */
2433 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2436 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2438 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2441 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2442 be->dai_link->name);
2444 ret = soc_pcm_prepare(be_substream);
2446 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2451 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2456 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2458 struct snd_soc_pcm_runtime *fe = substream->private_data;
2459 int stream = substream->stream, ret = 0;
2461 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2463 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2465 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2467 /* there is no point preparing this FE if there are no BEs */
2468 if (list_empty(&fe->dpcm[stream].be_clients)) {
2469 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2470 fe->dai_link->name);
2475 ret = dpcm_be_dai_prepare(fe, substream->stream);
2479 /* call prepare on the frontend */
2480 ret = soc_pcm_prepare(substream);
2482 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2483 fe->dai_link->name);
2487 /* run the stream event for each BE */
2488 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2489 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2492 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2493 mutex_unlock(&fe->card->mutex);
2498 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2499 unsigned int cmd, void *arg)
2501 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2502 struct snd_soc_component *component;
2503 struct snd_soc_rtdcom_list *rtdcom;
2505 for_each_rtdcom(rtd, rtdcom) {
2506 component = rtdcom->component;
2508 if (!component->driver->ops ||
2509 !component->driver->ops->ioctl)
2512 /* FIXME: use 1st ioctl */
2513 return component->driver->ops->ioctl(substream, cmd, arg);
2516 return snd_pcm_lib_ioctl(substream, cmd, arg);
2519 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2521 struct snd_pcm_substream *substream =
2522 snd_soc_dpcm_get_substream(fe, stream);
2523 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2526 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2527 stream ? "capture" : "playback", fe->dai_link->name);
2529 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2530 /* call bespoke trigger - FE takes care of all BE triggers */
2531 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2532 fe->dai_link->name);
2534 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2536 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2538 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2539 fe->dai_link->name);
2541 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2543 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2546 err = dpcm_be_dai_hw_free(fe, stream);
2548 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2550 err = dpcm_be_dai_shutdown(fe, stream);
2552 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2554 /* run the stream event for each BE */
2555 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2560 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2562 struct snd_pcm_substream *substream =
2563 snd_soc_dpcm_get_substream(fe, stream);
2564 struct snd_soc_dpcm *dpcm;
2565 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2568 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2569 stream ? "capture" : "playback", fe->dai_link->name);
2571 /* Only start the BE if the FE is ready */
2572 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2573 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2576 /* startup must always be called for new BEs */
2577 ret = dpcm_be_dai_startup(fe, stream);
2581 /* keep going if FE state is > open */
2582 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2585 ret = dpcm_be_dai_hw_params(fe, stream);
2589 /* keep going if FE state is > hw_params */
2590 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2594 ret = dpcm_be_dai_prepare(fe, stream);
2598 /* run the stream event for each BE */
2599 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2601 /* keep going if FE state is > prepare */
2602 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2603 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2606 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2607 /* call trigger on the frontend - FE takes care of all BE triggers */
2608 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2609 fe->dai_link->name);
2611 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2613 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2617 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2618 fe->dai_link->name);
2620 ret = dpcm_be_dai_trigger(fe, stream,
2621 SNDRV_PCM_TRIGGER_START);
2623 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2631 dpcm_be_dai_hw_free(fe, stream);
2633 dpcm_be_dai_shutdown(fe, stream);
2635 /* disconnect any non started BEs */
2636 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2637 struct snd_soc_pcm_runtime *be = dpcm->be;
2638 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2639 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2645 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2650 ret = dpcm_run_update_startup(fe, stream);
2652 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2658 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2662 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2663 ret = dpcm_run_update_shutdown(fe, stream);
2665 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2666 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2671 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2673 struct snd_soc_dapm_widget_list *list;
2676 if (!fe->dai_link->dynamic)
2679 /* only check active links */
2680 if (!fe->cpu_dai->active)
2683 /* DAPM sync will call this to update DSP paths */
2684 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2685 new ? "new" : "old", fe->dai_link->name);
2687 /* skip if FE doesn't have playback capability */
2688 if (!fe->cpu_dai->driver->playback.channels_min ||
2689 !fe->codec_dai->driver->playback.channels_min)
2692 /* skip if FE isn't currently playing */
2693 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2696 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2698 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2699 fe->dai_link->name, "playback");
2703 /* update any playback paths */
2704 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2707 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2709 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2711 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2712 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2715 dpcm_path_put(&list);
2718 /* skip if FE doesn't have capture capability */
2719 if (!fe->cpu_dai->driver->capture.channels_min ||
2720 !fe->codec_dai->driver->capture.channels_min)
2723 /* skip if FE isn't currently capturing */
2724 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2727 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2729 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2730 fe->dai_link->name, "capture");
2734 /* update any old capture paths */
2735 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2738 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2740 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2742 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2743 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2746 dpcm_path_put(&list);
2751 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2754 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2756 struct snd_soc_pcm_runtime *fe;
2759 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2760 /* shutdown all old paths first */
2761 list_for_each_entry(fe, &card->rtd_list, list) {
2762 ret = soc_dpcm_fe_runtime_update(fe, 0);
2767 /* bring new paths up */
2768 list_for_each_entry(fe, &card->rtd_list, list) {
2769 ret = soc_dpcm_fe_runtime_update(fe, 1);
2775 mutex_unlock(&card->mutex);
2778 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2780 struct snd_soc_dpcm *dpcm;
2781 struct list_head *clients =
2782 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2784 list_for_each_entry(dpcm, clients, list_be) {
2786 struct snd_soc_pcm_runtime *be = dpcm->be;
2789 if (be->dai_link->ignore_suspend)
2792 for (i = 0; i < be->num_codecs; i++) {
2793 struct snd_soc_dai *dai = be->codec_dais[i];
2794 struct snd_soc_dai_driver *drv = dai->driver;
2796 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2797 be->dai_link->name);
2799 if (drv->ops && drv->ops->digital_mute &&
2800 dai->playback_active)
2801 drv->ops->digital_mute(dai, mute);
2808 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2810 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2811 struct snd_soc_dpcm *dpcm;
2812 struct snd_soc_dapm_widget_list *list;
2814 int stream = fe_substream->stream;
2816 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2817 fe->dpcm[stream].runtime = fe_substream->runtime;
2819 ret = dpcm_path_get(fe, stream, &list);
2821 mutex_unlock(&fe->card->mutex);
2823 } else if (ret == 0) {
2824 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2825 fe->dai_link->name, stream ? "capture" : "playback");
2828 /* calculate valid and active FE <-> BE dpcms */
2829 dpcm_process_paths(fe, stream, &list, 1);
2831 ret = dpcm_fe_dai_startup(fe_substream);
2833 /* clean up all links */
2834 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2835 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2837 dpcm_be_disconnect(fe, stream);
2838 fe->dpcm[stream].runtime = NULL;
2841 dpcm_clear_pending_state(fe, stream);
2842 dpcm_path_put(&list);
2843 mutex_unlock(&fe->card->mutex);
2847 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2849 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2850 struct snd_soc_dpcm *dpcm;
2851 int stream = fe_substream->stream, ret;
2853 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2854 ret = dpcm_fe_dai_shutdown(fe_substream);
2856 /* mark FE's links ready to prune */
2857 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2858 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2860 dpcm_be_disconnect(fe, stream);
2862 fe->dpcm[stream].runtime = NULL;
2863 mutex_unlock(&fe->card->mutex);
2867 static void soc_pcm_private_free(struct snd_pcm *pcm)
2869 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2870 struct snd_soc_rtdcom_list *rtdcom;
2871 struct snd_soc_component *component;
2873 /* need to sync the delayed work before releasing resources */
2874 flush_delayed_work(&rtd->delayed_work);
2875 for_each_rtdcom(rtd, rtdcom) {
2876 component = rtdcom->component;
2878 if (component->driver->pcm_free)
2879 component->driver->pcm_free(pcm);
2883 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2886 struct snd_soc_rtdcom_list *rtdcom;
2887 struct snd_soc_component *component;
2889 for_each_rtdcom(rtd, rtdcom) {
2890 component = rtdcom->component;
2892 if (!component->driver->ops ||
2893 !component->driver->ops->ack)
2896 /* FIXME. it returns 1st ask now */
2897 return component->driver->ops->ack(substream);
2903 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2904 unsigned long pos, void __user *buf,
2905 unsigned long bytes)
2907 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2908 struct snd_soc_rtdcom_list *rtdcom;
2909 struct snd_soc_component *component;
2911 for_each_rtdcom(rtd, rtdcom) {
2912 component = rtdcom->component;
2914 if (!component->driver->ops ||
2915 !component->driver->ops->copy_user)
2918 /* FIXME. it returns 1st copy now */
2919 return component->driver->ops->copy_user(substream, channel,
2926 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2927 unsigned long pos, void *buf, unsigned long bytes)
2929 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2930 struct snd_soc_rtdcom_list *rtdcom;
2931 struct snd_soc_component *component;
2933 for_each_rtdcom(rtd, rtdcom) {
2934 component = rtdcom->component;
2936 if (!component->driver->ops ||
2937 !component->driver->ops->copy_kernel)
2940 /* FIXME. it returns 1st copy now */
2941 return component->driver->ops->copy_kernel(substream, channel,
2948 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2949 unsigned long pos, unsigned long bytes)
2951 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2952 struct snd_soc_rtdcom_list *rtdcom;
2953 struct snd_soc_component *component;
2955 for_each_rtdcom(rtd, rtdcom) {
2956 component = rtdcom->component;
2958 if (!component->driver->ops ||
2959 !component->driver->ops->fill_silence)
2962 /* FIXME. it returns 1st silence now */
2963 return component->driver->ops->fill_silence(substream, channel,
2970 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2971 unsigned long offset)
2973 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2974 struct snd_soc_rtdcom_list *rtdcom;
2975 struct snd_soc_component *component;
2978 for_each_rtdcom(rtd, rtdcom) {
2979 component = rtdcom->component;
2981 if (!component->driver->ops ||
2982 !component->driver->ops->page)
2985 /* FIXME. it returns 1st page now */
2986 page = component->driver->ops->page(substream, offset);
2994 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2995 struct vm_area_struct *vma)
2997 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2998 struct snd_soc_rtdcom_list *rtdcom;
2999 struct snd_soc_component *component;
3001 for_each_rtdcom(rtd, rtdcom) {
3002 component = rtdcom->component;
3004 if (!component->driver->ops ||
3005 !component->driver->ops->mmap)
3008 /* FIXME. it returns 1st mmap now */
3009 return component->driver->ops->mmap(substream, vma);
3015 /* create a new pcm */
3016 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3018 struct snd_soc_dai *codec_dai;
3019 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3020 struct snd_soc_component *component;
3021 struct snd_soc_rtdcom_list *rtdcom;
3022 struct snd_pcm *pcm;
3024 int ret = 0, playback = 0, capture = 0;
3027 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3028 playback = rtd->dai_link->dpcm_playback;
3029 capture = rtd->dai_link->dpcm_capture;
3031 for (i = 0; i < rtd->num_codecs; i++) {
3032 codec_dai = rtd->codec_dais[i];
3033 if (codec_dai->driver->playback.channels_min)
3035 if (codec_dai->driver->capture.channels_min)
3039 capture = capture && cpu_dai->driver->capture.channels_min;
3040 playback = playback && cpu_dai->driver->playback.channels_min;
3043 if (rtd->dai_link->playback_only) {
3048 if (rtd->dai_link->capture_only) {
3053 /* create the PCM */
3054 if (rtd->dai_link->no_pcm) {
3055 snprintf(new_name, sizeof(new_name), "(%s)",
3056 rtd->dai_link->stream_name);
3058 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3059 playback, capture, &pcm);
3061 if (rtd->dai_link->dynamic)
3062 snprintf(new_name, sizeof(new_name), "%s (*)",
3063 rtd->dai_link->stream_name);
3065 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3066 rtd->dai_link->stream_name,
3067 (rtd->num_codecs > 1) ?
3068 "multicodec" : rtd->codec_dai->name, num);
3070 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3074 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3075 rtd->dai_link->name);
3078 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3080 /* DAPM dai link stream work */
3081 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3083 pcm->nonatomic = rtd->dai_link->nonatomic;
3085 pcm->private_data = rtd;
3087 if (rtd->dai_link->no_pcm) {
3089 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3091 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3095 /* ASoC PCM operations */
3096 if (rtd->dai_link->dynamic) {
3097 rtd->ops.open = dpcm_fe_dai_open;
3098 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3099 rtd->ops.prepare = dpcm_fe_dai_prepare;
3100 rtd->ops.trigger = dpcm_fe_dai_trigger;
3101 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3102 rtd->ops.close = dpcm_fe_dai_close;
3103 rtd->ops.pointer = soc_pcm_pointer;
3104 rtd->ops.ioctl = soc_pcm_ioctl;
3106 rtd->ops.open = soc_pcm_open;
3107 rtd->ops.hw_params = soc_pcm_hw_params;
3108 rtd->ops.prepare = soc_pcm_prepare;
3109 rtd->ops.trigger = soc_pcm_trigger;
3110 rtd->ops.hw_free = soc_pcm_hw_free;
3111 rtd->ops.close = soc_pcm_close;
3112 rtd->ops.pointer = soc_pcm_pointer;
3113 rtd->ops.ioctl = soc_pcm_ioctl;
3116 for_each_rtdcom(rtd, rtdcom) {
3117 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3123 rtd->ops.ack = soc_rtdcom_ack;
3125 rtd->ops.copy_user = soc_rtdcom_copy_user;
3126 if (ops->copy_kernel)
3127 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3128 if (ops->fill_silence)
3129 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3131 rtd->ops.page = soc_rtdcom_page;
3133 rtd->ops.mmap = soc_rtdcom_mmap;
3137 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3140 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3142 for_each_rtdcom(rtd, rtdcom) {
3143 component = rtdcom->component;
3145 if (!component->driver->pcm_new)
3148 ret = component->driver->pcm_new(rtd);
3150 dev_err(component->dev,
3151 "ASoC: pcm constructor failed: %d\n",
3157 pcm->private_free = soc_pcm_private_free;
3159 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3160 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3165 /* is the current PCM operation for this FE ? */
3166 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3168 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3172 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3174 /* is the current PCM operation for this BE ? */
3175 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3176 struct snd_soc_pcm_runtime *be, int stream)
3178 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3179 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3180 be->dpcm[stream].runtime_update))
3184 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3186 /* get the substream for this BE */
3187 struct snd_pcm_substream *
3188 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3190 return be->pcm->streams[stream].substream;
3192 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3194 /* get the BE runtime state */
3195 enum snd_soc_dpcm_state
3196 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3198 return be->dpcm[stream].state;
3200 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3202 /* set the BE runtime state */
3203 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3204 int stream, enum snd_soc_dpcm_state state)
3206 be->dpcm[stream].state = state;
3208 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3211 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3212 * are not running, paused or suspended for the specified stream direction.
3214 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3215 struct snd_soc_pcm_runtime *be, int stream)
3217 struct snd_soc_dpcm *dpcm;
3220 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3225 state = dpcm->fe->dpcm[stream].state;
3226 if (state == SND_SOC_DPCM_STATE_START ||
3227 state == SND_SOC_DPCM_STATE_PAUSED ||
3228 state == SND_SOC_DPCM_STATE_SUSPEND)
3232 /* it's safe to free/stop this BE DAI */
3235 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3238 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3239 * running, paused or suspended for the specified stream direction.
3241 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3242 struct snd_soc_pcm_runtime *be, int stream)
3244 struct snd_soc_dpcm *dpcm;
3247 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3252 state = dpcm->fe->dpcm[stream].state;
3253 if (state == SND_SOC_DPCM_STATE_START ||
3254 state == SND_SOC_DPCM_STATE_PAUSED ||
3255 state == SND_SOC_DPCM_STATE_SUSPEND ||
3256 state == SND_SOC_DPCM_STATE_PREPARE)
3260 /* it's safe to change hw_params */
3263 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3265 #ifdef CONFIG_DEBUG_FS
3266 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3269 case SND_SOC_DPCM_STATE_NEW:
3271 case SND_SOC_DPCM_STATE_OPEN:
3273 case SND_SOC_DPCM_STATE_HW_PARAMS:
3275 case SND_SOC_DPCM_STATE_PREPARE:
3277 case SND_SOC_DPCM_STATE_START:
3279 case SND_SOC_DPCM_STATE_STOP:
3281 case SND_SOC_DPCM_STATE_SUSPEND:
3283 case SND_SOC_DPCM_STATE_PAUSED:
3285 case SND_SOC_DPCM_STATE_HW_FREE:
3287 case SND_SOC_DPCM_STATE_CLOSE:
3294 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3295 int stream, char *buf, size_t size)
3297 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3298 struct snd_soc_dpcm *dpcm;
3302 offset += snprintf(buf + offset, size - offset,
3303 "[%s - %s]\n", fe->dai_link->name,
3304 stream ? "Capture" : "Playback");
3306 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3307 dpcm_state_string(fe->dpcm[stream].state));
3309 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3310 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3311 offset += snprintf(buf + offset, size - offset,
3313 "Format = %s, Channels = %d, Rate = %d\n",
3314 snd_pcm_format_name(params_format(params)),
3315 params_channels(params),
3316 params_rate(params));
3319 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3321 if (list_empty(&fe->dpcm[stream].be_clients)) {
3322 offset += snprintf(buf + offset, size - offset,
3323 " No active DSP links\n");
3327 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3328 struct snd_soc_pcm_runtime *be = dpcm->be;
3329 params = &dpcm->hw_params;
3331 offset += snprintf(buf + offset, size - offset,
3332 "- %s\n", be->dai_link->name);
3334 offset += snprintf(buf + offset, size - offset,
3336 dpcm_state_string(be->dpcm[stream].state));
3338 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3339 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3340 offset += snprintf(buf + offset, size - offset,
3341 " Hardware Params: "
3342 "Format = %s, Channels = %d, Rate = %d\n",
3343 snd_pcm_format_name(params_format(params)),
3344 params_channels(params),
3345 params_rate(params));
3352 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3353 size_t count, loff_t *ppos)
3355 struct snd_soc_pcm_runtime *fe = file->private_data;
3356 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3359 buf = kmalloc(out_count, GFP_KERNEL);
3363 if (fe->cpu_dai->driver->playback.channels_min)
3364 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3365 buf + offset, out_count - offset);
3367 if (fe->cpu_dai->driver->capture.channels_min)
3368 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3369 buf + offset, out_count - offset);
3371 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3377 static const struct file_operations dpcm_state_fops = {
3378 .open = simple_open,
3379 .read = dpcm_state_read_file,
3380 .llseek = default_llseek,
3383 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3388 if (!rtd->card->debugfs_card_root)
3391 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3392 rtd->card->debugfs_card_root);
3393 if (!rtd->debugfs_dpcm_root) {
3395 "ASoC: Failed to create dpcm debugfs directory %s\n",
3396 rtd->dai_link->name);
3400 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3401 rtd, &dpcm_state_fops);