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_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
136 struct snd_soc_rtdcom_list *rtdcom;
137 struct snd_soc_component *component;
141 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
144 for_each_rtdcom(rtd, rtdcom) {
145 component = rtdcom->component;
147 ignore &= !component->driver->use_pmdown_time;
150 /* this will be removed */
151 for (i = 0; i < rtd->num_codecs; i++)
152 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
158 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
159 * @substream: the pcm substream
160 * @hw: the hardware parameters
162 * Sets the substream runtime hardware parameters.
164 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
165 const struct snd_pcm_hardware *hw)
167 struct snd_pcm_runtime *runtime = substream->runtime;
168 runtime->hw.info = hw->info;
169 runtime->hw.formats = hw->formats;
170 runtime->hw.period_bytes_min = hw->period_bytes_min;
171 runtime->hw.period_bytes_max = hw->period_bytes_max;
172 runtime->hw.periods_min = hw->periods_min;
173 runtime->hw.periods_max = hw->periods_max;
174 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
175 runtime->hw.fifo_size = hw->fifo_size;
178 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
180 /* DPCM stream event, send event to FE and all active BEs. */
181 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
184 struct snd_soc_dpcm *dpcm;
186 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
188 struct snd_soc_pcm_runtime *be = dpcm->be;
190 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
191 be->dai_link->name, event, dir);
193 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
194 (be->dpcm[dir].users >= 1))
197 snd_soc_dapm_stream_event(be, dir, event);
200 snd_soc_dapm_stream_event(fe, dir, event);
205 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
206 struct snd_soc_dai *soc_dai)
208 struct snd_soc_pcm_runtime *rtd = substream->private_data;
211 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
212 rtd->dai_link->symmetric_rates)) {
213 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
216 ret = snd_pcm_hw_constraint_single(substream->runtime,
217 SNDRV_PCM_HW_PARAM_RATE,
220 dev_err(soc_dai->dev,
221 "ASoC: Unable to apply rate constraint: %d\n",
227 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
228 rtd->dai_link->symmetric_channels)) {
229 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
232 ret = snd_pcm_hw_constraint_single(substream->runtime,
233 SNDRV_PCM_HW_PARAM_CHANNELS,
236 dev_err(soc_dai->dev,
237 "ASoC: Unable to apply channel symmetry constraint: %d\n",
243 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
244 rtd->dai_link->symmetric_samplebits)) {
245 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
246 soc_dai->sample_bits);
248 ret = snd_pcm_hw_constraint_single(substream->runtime,
249 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
250 soc_dai->sample_bits);
252 dev_err(soc_dai->dev,
253 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
262 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
263 struct snd_pcm_hw_params *params)
265 struct snd_soc_pcm_runtime *rtd = substream->private_data;
266 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
267 unsigned int rate, channels, sample_bits, symmetry, i;
269 rate = params_rate(params);
270 channels = params_channels(params);
271 sample_bits = snd_pcm_format_physical_width(params_format(params));
273 /* reject unmatched parameters when applying symmetry */
274 symmetry = cpu_dai->driver->symmetric_rates ||
275 rtd->dai_link->symmetric_rates;
277 for (i = 0; i < rtd->num_codecs; i++)
278 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
280 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
281 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
282 cpu_dai->rate, rate);
286 symmetry = cpu_dai->driver->symmetric_channels ||
287 rtd->dai_link->symmetric_channels;
289 for (i = 0; i < rtd->num_codecs; i++)
290 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
292 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
293 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
294 cpu_dai->channels, channels);
298 symmetry = cpu_dai->driver->symmetric_samplebits ||
299 rtd->dai_link->symmetric_samplebits;
301 for (i = 0; i < rtd->num_codecs; i++)
302 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
304 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
305 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
306 cpu_dai->sample_bits, sample_bits);
313 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
316 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
317 struct snd_soc_dai_link *link = rtd->dai_link;
318 unsigned int symmetry, i;
320 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
321 cpu_driver->symmetric_channels || link->symmetric_channels ||
322 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
324 for (i = 0; i < rtd->num_codecs; i++)
325 symmetry = symmetry ||
326 rtd->codec_dais[i]->driver->symmetric_rates ||
327 rtd->codec_dais[i]->driver->symmetric_channels ||
328 rtd->codec_dais[i]->driver->symmetric_samplebits;
333 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
335 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
343 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
347 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
349 struct snd_soc_pcm_runtime *rtd = substream->private_data;
350 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
351 struct snd_soc_dai *codec_dai;
353 unsigned int bits = 0, cpu_bits;
355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
356 for (i = 0; i < rtd->num_codecs; i++) {
357 codec_dai = rtd->codec_dais[i];
358 if (codec_dai->driver->playback.sig_bits == 0) {
362 bits = max(codec_dai->driver->playback.sig_bits, bits);
364 cpu_bits = cpu_dai->driver->playback.sig_bits;
366 for (i = 0; i < rtd->num_codecs; i++) {
367 codec_dai = rtd->codec_dais[i];
368 if (codec_dai->driver->capture.sig_bits == 0) {
372 bits = max(codec_dai->driver->capture.sig_bits, bits);
374 cpu_bits = cpu_dai->driver->capture.sig_bits;
377 soc_pcm_set_msb(substream, bits);
378 soc_pcm_set_msb(substream, cpu_bits);
381 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
383 struct snd_pcm_runtime *runtime = substream->runtime;
384 struct snd_pcm_hardware *hw = &runtime->hw;
385 struct snd_soc_pcm_runtime *rtd = substream->private_data;
386 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
387 struct snd_soc_dai_driver *codec_dai_drv;
388 struct snd_soc_pcm_stream *codec_stream;
389 struct snd_soc_pcm_stream *cpu_stream;
390 unsigned int chan_min = 0, chan_max = UINT_MAX;
391 unsigned int rate_min = 0, rate_max = UINT_MAX;
392 unsigned int rates = UINT_MAX;
393 u64 formats = ULLONG_MAX;
396 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
397 cpu_stream = &cpu_dai_drv->playback;
399 cpu_stream = &cpu_dai_drv->capture;
401 /* first calculate min/max only for CODECs in the DAI link */
402 for (i = 0; i < rtd->num_codecs; i++) {
405 * Skip CODECs which don't support the current stream type.
406 * Otherwise, since the rate, channel, and format values will
407 * zero in that case, we would have no usable settings left,
408 * causing the resulting setup to fail.
409 * At least one CODEC should match, otherwise we should have
410 * bailed out on a higher level, since there would be no
411 * CODEC to support the transfer direction in that case.
413 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
417 codec_dai_drv = rtd->codec_dais[i]->driver;
418 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
419 codec_stream = &codec_dai_drv->playback;
421 codec_stream = &codec_dai_drv->capture;
422 chan_min = max(chan_min, codec_stream->channels_min);
423 chan_max = min(chan_max, codec_stream->channels_max);
424 rate_min = max(rate_min, codec_stream->rate_min);
425 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
426 formats &= codec_stream->formats;
427 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
431 * chan min/max cannot be enforced if there are multiple CODEC DAIs
432 * connected to a single CPU DAI, use CPU DAI's directly and let
433 * channel allocation be fixed up later
435 if (rtd->num_codecs > 1) {
436 chan_min = cpu_stream->channels_min;
437 chan_max = cpu_stream->channels_max;
440 hw->channels_min = max(chan_min, cpu_stream->channels_min);
441 hw->channels_max = min(chan_max, cpu_stream->channels_max);
443 hw->formats &= formats & cpu_stream->formats;
445 hw->formats = formats & cpu_stream->formats;
446 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
448 snd_pcm_limit_hw_rates(runtime);
450 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
451 hw->rate_min = max(hw->rate_min, rate_min);
452 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
453 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
457 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
458 * then initialized and any private data can be allocated. This also calls
459 * startup for the cpu DAI, component, machine and codec DAI.
461 static int soc_pcm_open(struct snd_pcm_substream *substream)
463 struct snd_soc_pcm_runtime *rtd = substream->private_data;
464 struct snd_pcm_runtime *runtime = substream->runtime;
465 struct snd_soc_component *component;
466 struct snd_soc_rtdcom_list *rtdcom;
467 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
468 struct snd_soc_dai *codec_dai;
469 const char *codec_dai_name = "multicodec";
470 int i, ret = 0, __ret;
472 pinctrl_pm_select_default_state(cpu_dai->dev);
473 for (i = 0; i < rtd->num_codecs; i++)
474 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
476 for_each_rtdcom(rtd, rtdcom) {
477 component = rtdcom->component;
479 pm_runtime_get_sync(component->dev);
482 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
484 /* startup the audio subsystem */
485 if (cpu_dai->driver->ops->startup) {
486 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
488 dev_err(cpu_dai->dev, "ASoC: can't open interface"
489 " %s: %d\n", cpu_dai->name, ret);
495 for_each_rtdcom(rtd, rtdcom) {
496 component = rtdcom->component;
498 if (!component->driver->ops ||
499 !component->driver->ops->open)
502 __ret = component->driver->ops->open(substream);
504 dev_err(component->dev,
505 "ASoC: can't open component %s: %d\n",
506 component->name, ret);
513 for (i = 0; i < rtd->num_codecs; i++) {
514 codec_dai = rtd->codec_dais[i];
515 if (codec_dai->driver->ops->startup) {
516 ret = codec_dai->driver->ops->startup(substream,
519 dev_err(codec_dai->dev,
520 "ASoC: can't open codec %s: %d\n",
521 codec_dai->name, ret);
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
527 codec_dai->tx_mask = 0;
529 codec_dai->rx_mask = 0;
532 if (rtd->dai_link->ops->startup) {
533 ret = rtd->dai_link->ops->startup(substream);
535 pr_err("ASoC: %s startup failed: %d\n",
536 rtd->dai_link->name, ret);
541 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
542 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
545 /* Check that the codec and cpu DAIs are compatible */
546 soc_pcm_init_runtime_hw(substream);
548 if (rtd->num_codecs == 1)
549 codec_dai_name = rtd->codec_dai->name;
551 if (soc_pcm_has_symmetry(substream))
552 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
555 if (!runtime->hw.rates) {
556 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
557 codec_dai_name, cpu_dai->name);
560 if (!runtime->hw.formats) {
561 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
562 codec_dai_name, cpu_dai->name);
565 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
566 runtime->hw.channels_min > runtime->hw.channels_max) {
567 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
568 codec_dai_name, cpu_dai->name);
572 soc_pcm_apply_msb(substream);
574 /* Symmetry only applies if we've already got an active stream. */
575 if (cpu_dai->active) {
576 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
581 for (i = 0; i < rtd->num_codecs; i++) {
582 if (rtd->codec_dais[i]->active) {
583 ret = soc_pcm_apply_symmetry(substream,
590 pr_debug("ASoC: %s <-> %s info:\n",
591 codec_dai_name, cpu_dai->name);
592 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
593 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
594 runtime->hw.channels_max);
595 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
596 runtime->hw.rate_max);
600 snd_soc_runtime_activate(rtd, substream->stream);
602 mutex_unlock(&rtd->pcm_mutex);
606 if (rtd->dai_link->ops->shutdown)
607 rtd->dai_link->ops->shutdown(substream);
614 codec_dai = rtd->codec_dais[i];
615 if (codec_dai->driver->ops->shutdown)
616 codec_dai->driver->ops->shutdown(substream, codec_dai);
620 for_each_rtdcom(rtd, rtdcom) {
621 component = rtdcom->component;
623 if (!component->driver->ops ||
624 !component->driver->ops->close)
627 component->driver->ops->close(substream);
630 if (cpu_dai->driver->ops->shutdown)
631 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
633 mutex_unlock(&rtd->pcm_mutex);
635 for_each_rtdcom(rtd, rtdcom) {
636 component = rtdcom->component;
638 pm_runtime_mark_last_busy(component->dev);
639 pm_runtime_put_autosuspend(component->dev);
642 for (i = 0; i < rtd->num_codecs; i++) {
643 if (!rtd->codec_dais[i]->active)
644 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
646 if (!cpu_dai->active)
647 pinctrl_pm_select_sleep_state(cpu_dai->dev);
653 * Power down the audio subsystem pmdown_time msecs after close is called.
654 * This is to ensure there are no pops or clicks in between any music tracks
655 * due to DAPM power cycling.
657 static void close_delayed_work(struct work_struct *work)
659 struct snd_soc_pcm_runtime *rtd =
660 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
661 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
663 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
665 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
666 codec_dai->driver->playback.stream_name,
667 codec_dai->playback_active ? "active" : "inactive",
668 rtd->pop_wait ? "yes" : "no");
670 /* are we waiting on this codec DAI stream */
671 if (rtd->pop_wait == 1) {
673 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
674 SND_SOC_DAPM_STREAM_STOP);
677 mutex_unlock(&rtd->pcm_mutex);
681 * Called by ALSA when a PCM substream is closed. Private data can be
682 * freed here. The cpu DAI, codec DAI, machine and components are also
685 static int soc_pcm_close(struct snd_pcm_substream *substream)
687 struct snd_soc_pcm_runtime *rtd = substream->private_data;
688 struct snd_soc_component *component;
689 struct snd_soc_rtdcom_list *rtdcom;
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
691 struct snd_soc_dai *codec_dai;
694 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
696 snd_soc_runtime_deactivate(rtd, substream->stream);
698 /* clear the corresponding DAIs rate when inactive */
699 if (!cpu_dai->active)
702 for (i = 0; i < rtd->num_codecs; i++) {
703 codec_dai = rtd->codec_dais[i];
704 if (!codec_dai->active)
708 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
710 if (cpu_dai->driver->ops->shutdown)
711 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
713 for (i = 0; i < rtd->num_codecs; i++) {
714 codec_dai = rtd->codec_dais[i];
715 if (codec_dai->driver->ops->shutdown)
716 codec_dai->driver->ops->shutdown(substream, codec_dai);
719 if (rtd->dai_link->ops->shutdown)
720 rtd->dai_link->ops->shutdown(substream);
722 for_each_rtdcom(rtd, rtdcom) {
723 component = rtdcom->component;
725 if (!component->driver->ops ||
726 !component->driver->ops->close)
729 component->driver->ops->close(substream);
732 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
733 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
734 /* powered down playback stream now */
735 snd_soc_dapm_stream_event(rtd,
736 SNDRV_PCM_STREAM_PLAYBACK,
737 SND_SOC_DAPM_STREAM_STOP);
739 /* start delayed pop wq here for playback streams */
741 queue_delayed_work(system_power_efficient_wq,
743 msecs_to_jiffies(rtd->pmdown_time));
746 /* capture streams can be powered down now */
747 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
748 SND_SOC_DAPM_STREAM_STOP);
751 mutex_unlock(&rtd->pcm_mutex);
753 for_each_rtdcom(rtd, rtdcom) {
754 component = rtdcom->component;
756 pm_runtime_mark_last_busy(component->dev);
757 pm_runtime_put_autosuspend(component->dev);
760 for (i = 0; i < rtd->num_codecs; i++) {
761 if (!rtd->codec_dais[i]->active)
762 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
764 if (!cpu_dai->active)
765 pinctrl_pm_select_sleep_state(cpu_dai->dev);
771 * Called by ALSA when the PCM substream is prepared, can set format, sample
772 * rate, etc. This function is non atomic and can be called multiple times,
773 * it can refer to the runtime info.
775 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_component *component;
779 struct snd_soc_rtdcom_list *rtdcom;
780 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
781 struct snd_soc_dai *codec_dai;
784 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
786 if (rtd->dai_link->ops->prepare) {
787 ret = rtd->dai_link->ops->prepare(substream);
789 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
795 for_each_rtdcom(rtd, rtdcom) {
796 component = rtdcom->component;
798 if (!component->driver->ops ||
799 !component->driver->ops->prepare)
802 ret = component->driver->ops->prepare(substream);
804 dev_err(component->dev,
805 "ASoC: platform prepare error: %d\n", ret);
810 for (i = 0; i < rtd->num_codecs; i++) {
811 codec_dai = rtd->codec_dais[i];
812 if (codec_dai->driver->ops->prepare) {
813 ret = codec_dai->driver->ops->prepare(substream,
816 dev_err(codec_dai->dev,
817 "ASoC: codec DAI prepare error: %d\n",
824 if (cpu_dai->driver->ops->prepare) {
825 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
827 dev_err(cpu_dai->dev,
828 "ASoC: cpu DAI prepare error: %d\n", ret);
833 /* cancel any delayed stream shutdown that is pending */
834 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
837 cancel_delayed_work(&rtd->delayed_work);
840 snd_soc_dapm_stream_event(rtd, substream->stream,
841 SND_SOC_DAPM_STREAM_START);
843 for (i = 0; i < rtd->num_codecs; i++)
844 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
846 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
849 mutex_unlock(&rtd->pcm_mutex);
853 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
856 struct snd_interval *interval;
857 int channels = hweight_long(mask);
859 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
860 interval->min = channels;
861 interval->max = channels;
864 int soc_dai_hw_params(struct snd_pcm_substream *substream,
865 struct snd_pcm_hw_params *params,
866 struct snd_soc_dai *dai)
870 if (dai->driver->ops->hw_params) {
871 ret = dai->driver->ops->hw_params(substream, params, dai);
873 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
883 * Called by ALSA when the hardware params are set by application. This
884 * function can also be called multiple times and can allocate buffers
885 * (using snd_pcm_lib_* ). It's non-atomic.
887 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
888 struct snd_pcm_hw_params *params)
890 struct snd_soc_pcm_runtime *rtd = substream->private_data;
891 struct snd_soc_component *component;
892 struct snd_soc_rtdcom_list *rtdcom;
893 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
894 int i, ret = 0, __ret;
896 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
897 if (rtd->dai_link->ops->hw_params) {
898 ret = rtd->dai_link->ops->hw_params(substream, params);
900 dev_err(rtd->card->dev, "ASoC: machine hw_params"
901 " failed: %d\n", ret);
906 for (i = 0; i < rtd->num_codecs; i++) {
907 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
908 struct snd_pcm_hw_params codec_params;
911 * Skip CODECs which don't support the current stream type,
912 * the idea being that if a CODEC is not used for the currently
913 * set up transfer direction, it should not need to be
914 * configured, especially since the configuration used might
915 * not even be supported by that CODEC. There may be cases
916 * however where a CODEC needs to be set up although it is
917 * actually not being used for the transfer, e.g. if a
918 * capture-only CODEC is acting as an LRCLK and/or BCLK master
919 * for the DAI link including a playback-only CODEC.
920 * If this becomes necessary, we will have to augment the
921 * machine driver setup with information on how to act, so
922 * we can do the right thing here.
924 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
927 /* copy params for each codec */
928 codec_params = *params;
930 /* fixup params based on TDM slot masks */
931 if (codec_dai->tx_mask)
932 soc_pcm_codec_params_fixup(&codec_params,
934 if (codec_dai->rx_mask)
935 soc_pcm_codec_params_fixup(&codec_params,
938 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
942 codec_dai->rate = params_rate(&codec_params);
943 codec_dai->channels = params_channels(&codec_params);
944 codec_dai->sample_bits = snd_pcm_format_physical_width(
945 params_format(&codec_params));
948 ret = soc_dai_hw_params(substream, params, cpu_dai);
953 for_each_rtdcom(rtd, rtdcom) {
954 component = rtdcom->component;
956 if (!component->driver->ops ||
957 !component->driver->ops->hw_params)
960 __ret = component->driver->ops->hw_params(substream, params);
962 dev_err(component->dev,
963 "ASoC: %s hw params failed: %d\n",
964 component->name, ret);
971 /* store the parameters for each DAIs */
972 cpu_dai->rate = params_rate(params);
973 cpu_dai->channels = params_channels(params);
974 cpu_dai->sample_bits =
975 snd_pcm_format_physical_width(params_format(params));
977 ret = soc_pcm_params_symmetry(substream, params);
981 mutex_unlock(&rtd->pcm_mutex);
985 for_each_rtdcom(rtd, rtdcom) {
986 component = rtdcom->component;
988 if (!component->driver->ops ||
989 !component->driver->ops->hw_free)
992 component->driver->ops->hw_free(substream);
995 if (cpu_dai->driver->ops->hw_free)
996 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1003 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1004 if (codec_dai->driver->ops->hw_free)
1005 codec_dai->driver->ops->hw_free(substream, codec_dai);
1006 codec_dai->rate = 0;
1009 if (rtd->dai_link->ops->hw_free)
1010 rtd->dai_link->ops->hw_free(substream);
1012 mutex_unlock(&rtd->pcm_mutex);
1017 * Frees resources allocated by hw_params, can be called multiple times
1019 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1021 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1022 struct snd_soc_component *component;
1023 struct snd_soc_rtdcom_list *rtdcom;
1024 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1025 struct snd_soc_dai *codec_dai;
1026 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1029 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1031 /* clear the corresponding DAIs parameters when going to be inactive */
1032 if (cpu_dai->active == 1) {
1034 cpu_dai->channels = 0;
1035 cpu_dai->sample_bits = 0;
1038 for (i = 0; i < rtd->num_codecs; i++) {
1039 codec_dai = rtd->codec_dais[i];
1040 if (codec_dai->active == 1) {
1041 codec_dai->rate = 0;
1042 codec_dai->channels = 0;
1043 codec_dai->sample_bits = 0;
1047 /* apply codec digital mute */
1048 for (i = 0; i < rtd->num_codecs; i++) {
1049 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1050 (!playback && rtd->codec_dais[i]->capture_active == 1))
1051 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1055 /* free any machine hw params */
1056 if (rtd->dai_link->ops->hw_free)
1057 rtd->dai_link->ops->hw_free(substream);
1059 /* free any component resources */
1060 for_each_rtdcom(rtd, rtdcom) {
1061 component = rtdcom->component;
1063 if (!component->driver->ops ||
1064 !component->driver->ops->hw_free)
1067 component->driver->ops->hw_free(substream);
1070 /* now free hw params for the DAIs */
1071 for (i = 0; i < rtd->num_codecs; i++) {
1072 codec_dai = rtd->codec_dais[i];
1073 if (codec_dai->driver->ops->hw_free)
1074 codec_dai->driver->ops->hw_free(substream, codec_dai);
1077 if (cpu_dai->driver->ops->hw_free)
1078 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1080 mutex_unlock(&rtd->pcm_mutex);
1084 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1086 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1087 struct snd_soc_component *component;
1088 struct snd_soc_rtdcom_list *rtdcom;
1089 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1090 struct snd_soc_dai *codec_dai;
1093 for (i = 0; i < rtd->num_codecs; i++) {
1094 codec_dai = rtd->codec_dais[i];
1095 if (codec_dai->driver->ops->trigger) {
1096 ret = codec_dai->driver->ops->trigger(substream,
1103 for_each_rtdcom(rtd, rtdcom) {
1104 component = rtdcom->component;
1106 if (!component->driver->ops ||
1107 !component->driver->ops->trigger)
1110 ret = component->driver->ops->trigger(substream, cmd);
1115 if (cpu_dai->driver->ops->trigger) {
1116 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1121 if (rtd->dai_link->ops->trigger) {
1122 ret = rtd->dai_link->ops->trigger(substream, cmd);
1130 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1134 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1135 struct snd_soc_dai *codec_dai;
1138 for (i = 0; i < rtd->num_codecs; i++) {
1139 codec_dai = rtd->codec_dais[i];
1140 if (codec_dai->driver->ops->bespoke_trigger) {
1141 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1148 if (cpu_dai->driver->ops->bespoke_trigger) {
1149 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1156 * soc level wrapper for pointer callback
1157 * If cpu_dai, codec_dai, component driver has the delay callback, then
1158 * the runtime->delay will be updated accordingly.
1160 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1162 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1163 struct snd_soc_component *component;
1164 struct snd_soc_rtdcom_list *rtdcom;
1165 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1166 struct snd_soc_dai *codec_dai;
1167 struct snd_pcm_runtime *runtime = substream->runtime;
1168 snd_pcm_uframes_t offset = 0;
1169 snd_pcm_sframes_t delay = 0;
1170 snd_pcm_sframes_t codec_delay = 0;
1173 for_each_rtdcom(rtd, rtdcom) {
1174 component = rtdcom->component;
1176 if (!component->driver->ops ||
1177 !component->driver->ops->pointer)
1180 /* FIXME: use 1st pointer */
1181 offset = component->driver->ops->pointer(substream);
1185 if (cpu_dai->driver->ops->delay)
1186 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1188 for (i = 0; i < rtd->num_codecs; i++) {
1189 codec_dai = rtd->codec_dais[i];
1190 if (codec_dai->driver->ops->delay)
1191 codec_delay = max(codec_delay,
1192 codec_dai->driver->ops->delay(substream,
1195 delay += codec_delay;
1197 runtime->delay = delay;
1202 /* connect a FE and BE */
1203 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1204 struct snd_soc_pcm_runtime *be, int stream)
1206 struct snd_soc_dpcm *dpcm;
1208 /* only add new dpcms */
1209 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1210 if (dpcm->be == be && dpcm->fe == fe)
1214 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1220 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1221 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1222 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1223 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1225 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1226 stream ? "capture" : "playback", fe->dai_link->name,
1227 stream ? "<-" : "->", be->dai_link->name);
1229 #ifdef CONFIG_DEBUG_FS
1230 if (fe->debugfs_dpcm_root)
1231 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1232 fe->debugfs_dpcm_root, &dpcm->state);
1237 /* reparent a BE onto another FE */
1238 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1239 struct snd_soc_pcm_runtime *be, int stream)
1241 struct snd_soc_dpcm *dpcm;
1242 struct snd_pcm_substream *fe_substream, *be_substream;
1244 /* reparent if BE is connected to other FEs */
1245 if (!be->dpcm[stream].users)
1248 be_substream = snd_soc_dpcm_get_substream(be, stream);
1250 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1254 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1255 stream ? "capture" : "playback",
1256 dpcm->fe->dai_link->name,
1257 stream ? "<-" : "->", dpcm->be->dai_link->name);
1259 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1260 be_substream->runtime = fe_substream->runtime;
1265 /* disconnect a BE and FE */
1266 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1268 struct snd_soc_dpcm *dpcm, *d;
1270 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1271 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1272 stream ? "capture" : "playback",
1273 dpcm->be->dai_link->name);
1275 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1278 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1279 stream ? "capture" : "playback", fe->dai_link->name,
1280 stream ? "<-" : "->", dpcm->be->dai_link->name);
1282 /* BEs still alive need new FE */
1283 dpcm_be_reparent(fe, dpcm->be, stream);
1285 #ifdef CONFIG_DEBUG_FS
1286 debugfs_remove(dpcm->debugfs_state);
1288 list_del(&dpcm->list_be);
1289 list_del(&dpcm->list_fe);
1294 /* get BE for DAI widget and stream */
1295 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1296 struct snd_soc_dapm_widget *widget, int stream)
1298 struct snd_soc_pcm_runtime *be;
1301 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1303 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1304 list_for_each_entry(be, &card->rtd_list, list) {
1306 if (!be->dai_link->no_pcm)
1309 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1310 be->cpu_dai->playback_widget ?
1311 be->cpu_dai->playback_widget->name : "(not set)");
1313 if (be->cpu_dai->playback_widget == widget)
1316 for (i = 0; i < be->num_codecs; i++) {
1317 struct snd_soc_dai *dai = be->codec_dais[i];
1318 if (dai->playback_widget == widget)
1324 list_for_each_entry(be, &card->rtd_list, list) {
1326 if (!be->dai_link->no_pcm)
1329 dev_dbg(card->dev, "ASoC: try BE %s\n",
1330 be->cpu_dai->capture_widget ?
1331 be->cpu_dai->capture_widget->name : "(not set)");
1333 if (be->cpu_dai->capture_widget == widget)
1336 for (i = 0; i < be->num_codecs; i++) {
1337 struct snd_soc_dai *dai = be->codec_dais[i];
1338 if (dai->capture_widget == widget)
1344 /* dai link name and stream name set correctly ? */
1345 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1346 stream ? "capture" : "playback", widget->name);
1350 static inline struct snd_soc_dapm_widget *
1351 dai_get_widget(struct snd_soc_dai *dai, int stream)
1353 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1354 return dai->playback_widget;
1356 return dai->capture_widget;
1359 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1360 struct snd_soc_dapm_widget *widget)
1364 for (i = 0; i < list->num_widgets; i++) {
1365 if (widget == list->widgets[i])
1372 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1373 enum snd_soc_dapm_direction dir)
1375 struct snd_soc_card *card = widget->dapm->card;
1376 struct snd_soc_pcm_runtime *rtd;
1379 if (dir == SND_SOC_DAPM_DIR_OUT) {
1380 list_for_each_entry(rtd, &card->rtd_list, list) {
1381 if (!rtd->dai_link->no_pcm)
1384 if (rtd->cpu_dai->playback_widget == widget)
1387 for (i = 0; i < rtd->num_codecs; ++i) {
1388 struct snd_soc_dai *dai = rtd->codec_dais[i];
1389 if (dai->playback_widget == widget)
1393 } else { /* SND_SOC_DAPM_DIR_IN */
1394 list_for_each_entry(rtd, &card->rtd_list, list) {
1395 if (!rtd->dai_link->no_pcm)
1398 if (rtd->cpu_dai->capture_widget == widget)
1401 for (i = 0; i < rtd->num_codecs; ++i) {
1402 struct snd_soc_dai *dai = rtd->codec_dais[i];
1403 if (dai->capture_widget == widget)
1412 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1413 int stream, struct snd_soc_dapm_widget_list **list)
1415 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1418 /* get number of valid DAI paths and their widgets */
1419 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1420 dpcm_end_walk_at_be);
1422 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1423 stream ? "capture" : "playback");
1428 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1429 struct snd_soc_dapm_widget_list **list_)
1431 struct snd_soc_dpcm *dpcm;
1432 struct snd_soc_dapm_widget_list *list = *list_;
1433 struct snd_soc_dapm_widget *widget;
1436 /* Destroy any old FE <--> BE connections */
1437 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1440 /* is there a valid CPU DAI widget for this BE */
1441 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1443 /* prune the BE if it's no longer in our active list */
1444 if (widget && widget_in_list(list, widget))
1447 /* is there a valid CODEC DAI widget for this BE */
1448 for (i = 0; i < dpcm->be->num_codecs; i++) {
1449 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1450 widget = dai_get_widget(dai, stream);
1452 /* prune the BE if it's no longer in our active list */
1453 if (widget && widget_in_list(list, widget))
1457 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1458 stream ? "capture" : "playback",
1459 dpcm->be->dai_link->name, fe->dai_link->name);
1460 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1461 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1465 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1469 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1470 struct snd_soc_dapm_widget_list **list_)
1472 struct snd_soc_card *card = fe->card;
1473 struct snd_soc_dapm_widget_list *list = *list_;
1474 struct snd_soc_pcm_runtime *be;
1475 int i, new = 0, err;
1477 /* Create any new FE <--> BE connections */
1478 for (i = 0; i < list->num_widgets; i++) {
1480 switch (list->widgets[i]->id) {
1481 case snd_soc_dapm_dai_in:
1482 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1485 case snd_soc_dapm_dai_out:
1486 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1493 /* is there a valid BE rtd for this widget */
1494 be = dpcm_get_be(card, list->widgets[i], stream);
1496 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1497 list->widgets[i]->name);
1501 /* make sure BE is a real BE */
1502 if (!be->dai_link->no_pcm)
1505 /* don't connect if FE is not running */
1506 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1509 /* newly connected FE and BE */
1510 err = dpcm_be_connect(fe, be, stream);
1512 dev_err(fe->dev, "ASoC: can't connect %s\n",
1513 list->widgets[i]->name);
1515 } else if (err == 0) /* already connected */
1519 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1523 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1528 * Find the corresponding BE DAIs that source or sink audio to this
1531 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1532 int stream, struct snd_soc_dapm_widget_list **list, int new)
1535 return dpcm_add_paths(fe, stream, list);
1537 return dpcm_prune_paths(fe, stream, list);
1540 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1542 struct snd_soc_dpcm *dpcm;
1544 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1545 dpcm->be->dpcm[stream].runtime_update =
1546 SND_SOC_DPCM_UPDATE_NO;
1549 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1552 struct snd_soc_dpcm *dpcm;
1554 /* disable any enabled and non active backends */
1555 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1557 struct snd_soc_pcm_runtime *be = dpcm->be;
1558 struct snd_pcm_substream *be_substream =
1559 snd_soc_dpcm_get_substream(be, stream);
1561 if (be->dpcm[stream].users == 0)
1562 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1563 stream ? "capture" : "playback",
1564 be->dpcm[stream].state);
1566 if (--be->dpcm[stream].users != 0)
1569 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1572 soc_pcm_close(be_substream);
1573 be_substream->runtime = NULL;
1574 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1578 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1580 struct snd_soc_dpcm *dpcm;
1583 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1584 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1586 struct snd_soc_pcm_runtime *be = dpcm->be;
1587 struct snd_pcm_substream *be_substream =
1588 snd_soc_dpcm_get_substream(be, stream);
1590 if (!be_substream) {
1591 dev_err(be->dev, "ASoC: no backend %s stream\n",
1592 stream ? "capture" : "playback");
1596 /* is this op for this BE ? */
1597 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1600 /* first time the dpcm is open ? */
1601 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1602 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1603 stream ? "capture" : "playback",
1604 be->dpcm[stream].state);
1606 if (be->dpcm[stream].users++ != 0)
1609 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1610 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1613 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1614 stream ? "capture" : "playback", be->dai_link->name);
1616 be_substream->runtime = be->dpcm[stream].runtime;
1617 err = soc_pcm_open(be_substream);
1619 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1620 be->dpcm[stream].users--;
1621 if (be->dpcm[stream].users < 0)
1622 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1623 stream ? "capture" : "playback",
1624 be->dpcm[stream].state);
1626 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1630 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1637 /* disable any enabled and non active backends */
1638 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1639 struct snd_soc_pcm_runtime *be = dpcm->be;
1640 struct snd_pcm_substream *be_substream =
1641 snd_soc_dpcm_get_substream(be, stream);
1643 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1646 if (be->dpcm[stream].users == 0)
1647 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1648 stream ? "capture" : "playback",
1649 be->dpcm[stream].state);
1651 if (--be->dpcm[stream].users != 0)
1654 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1657 soc_pcm_close(be_substream);
1658 be_substream->runtime = NULL;
1659 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1665 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1666 struct snd_soc_pcm_stream *stream,
1669 runtime->hw.rate_min = stream->rate_min;
1670 runtime->hw.rate_max = stream->rate_max;
1671 runtime->hw.channels_min = stream->channels_min;
1672 runtime->hw.channels_max = stream->channels_max;
1673 if (runtime->hw.formats)
1674 runtime->hw.formats &= formats & stream->formats;
1676 runtime->hw.formats = formats & stream->formats;
1677 runtime->hw.rates = stream->rates;
1680 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1682 struct snd_soc_pcm_runtime *fe = substream->private_data;
1683 struct snd_soc_dpcm *dpcm;
1684 u64 formats = ULLONG_MAX;
1685 int stream = substream->stream;
1687 if (!fe->dai_link->dpcm_merged_format)
1691 * It returns merged BE codec format
1692 * if FE want to use it (= dpcm_merged_format)
1695 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1696 struct snd_soc_pcm_runtime *be = dpcm->be;
1697 struct snd_soc_dai_driver *codec_dai_drv;
1698 struct snd_soc_pcm_stream *codec_stream;
1701 for (i = 0; i < be->num_codecs; i++) {
1702 codec_dai_drv = be->codec_dais[i]->driver;
1703 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1704 codec_stream = &codec_dai_drv->playback;
1706 codec_stream = &codec_dai_drv->capture;
1708 formats &= codec_stream->formats;
1715 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1717 struct snd_pcm_runtime *runtime = substream->runtime;
1718 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1719 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1720 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1721 u64 format = dpcm_runtime_base_format(substream);
1723 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1724 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1726 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1729 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1731 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1732 * for avoiding the race with trigger callback.
1733 * If the state is unset and a trigger is pending while the previous operation,
1734 * process the pending trigger action here.
1736 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1737 int stream, enum snd_soc_dpcm_update state)
1739 struct snd_pcm_substream *substream =
1740 snd_soc_dpcm_get_substream(fe, stream);
1742 snd_pcm_stream_lock_irq(substream);
1743 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1744 dpcm_fe_dai_do_trigger(substream,
1745 fe->dpcm[stream].trigger_pending - 1);
1746 fe->dpcm[stream].trigger_pending = 0;
1748 fe->dpcm[stream].runtime_update = state;
1749 snd_pcm_stream_unlock_irq(substream);
1752 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1755 struct snd_soc_dpcm *dpcm;
1756 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1757 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1760 /* apply symmetry for FE */
1761 if (soc_pcm_has_symmetry(fe_substream))
1762 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1764 /* Symmetry only applies if we've got an active stream. */
1765 if (fe_cpu_dai->active) {
1766 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1771 /* apply symmetry for BE */
1772 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1773 struct snd_soc_pcm_runtime *be = dpcm->be;
1774 struct snd_pcm_substream *be_substream =
1775 snd_soc_dpcm_get_substream(be, stream);
1776 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1779 if (rtd->dai_link->be_hw_params_fixup)
1782 if (soc_pcm_has_symmetry(be_substream))
1783 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1785 /* Symmetry only applies if we've got an active stream. */
1786 if (rtd->cpu_dai->active) {
1787 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1792 for (i = 0; i < rtd->num_codecs; i++) {
1793 if (rtd->codec_dais[i]->active) {
1794 err = soc_pcm_apply_symmetry(be_substream,
1795 rtd->codec_dais[i]);
1805 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1807 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1808 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1809 int stream = fe_substream->stream, ret = 0;
1811 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1813 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1815 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1819 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1821 /* start the DAI frontend */
1822 ret = soc_pcm_open(fe_substream);
1824 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1828 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1830 dpcm_set_fe_runtime(fe_substream);
1831 snd_pcm_limit_hw_rates(runtime);
1833 ret = dpcm_apply_symmetry(fe_substream, stream);
1835 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1840 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1844 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1846 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1850 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1852 struct snd_soc_dpcm *dpcm;
1854 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1855 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1857 struct snd_soc_pcm_runtime *be = dpcm->be;
1858 struct snd_pcm_substream *be_substream =
1859 snd_soc_dpcm_get_substream(be, stream);
1861 /* is this op for this BE ? */
1862 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1865 if (be->dpcm[stream].users == 0)
1866 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1867 stream ? "capture" : "playback",
1868 be->dpcm[stream].state);
1870 if (--be->dpcm[stream].users != 0)
1873 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1874 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1877 dev_dbg(be->dev, "ASoC: close BE %s\n",
1878 be->dai_link->name);
1880 soc_pcm_close(be_substream);
1881 be_substream->runtime = NULL;
1883 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1888 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1890 struct snd_soc_pcm_runtime *fe = substream->private_data;
1891 int stream = substream->stream;
1893 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1895 /* shutdown the BEs */
1896 dpcm_be_dai_shutdown(fe, substream->stream);
1898 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1900 /* now shutdown the frontend */
1901 soc_pcm_close(substream);
1903 /* run the stream event for each BE */
1904 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1906 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1907 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1911 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1913 struct snd_soc_dpcm *dpcm;
1915 /* only hw_params backends that are either sinks or sources
1916 * to this frontend DAI */
1917 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1919 struct snd_soc_pcm_runtime *be = dpcm->be;
1920 struct snd_pcm_substream *be_substream =
1921 snd_soc_dpcm_get_substream(be, stream);
1923 /* is this op for this BE ? */
1924 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1927 /* only free hw when no longer used - check all FEs */
1928 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1931 /* do not free hw if this BE is used by other FE */
1932 if (be->dpcm[stream].users > 1)
1935 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1936 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1937 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1943 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1944 be->dai_link->name);
1946 soc_pcm_hw_free(be_substream);
1948 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1954 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1956 struct snd_soc_pcm_runtime *fe = substream->private_data;
1957 int err, stream = substream->stream;
1959 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1960 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1962 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1964 /* call hw_free on the frontend */
1965 err = soc_pcm_hw_free(substream);
1967 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1968 fe->dai_link->name);
1970 /* only hw_params backends that are either sinks or sources
1971 * to this frontend DAI */
1972 err = dpcm_be_dai_hw_free(fe, stream);
1974 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1975 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1977 mutex_unlock(&fe->card->mutex);
1981 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1983 struct snd_soc_dpcm *dpcm;
1986 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1988 struct snd_soc_pcm_runtime *be = dpcm->be;
1989 struct snd_pcm_substream *be_substream =
1990 snd_soc_dpcm_get_substream(be, stream);
1992 /* is this op for this BE ? */
1993 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1996 /* copy params for each dpcm */
1997 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1998 sizeof(struct snd_pcm_hw_params));
2000 /* perform any hw_params fixups */
2001 if (be->dai_link->be_hw_params_fixup) {
2002 ret = be->dai_link->be_hw_params_fixup(be,
2006 "ASoC: hw_params BE fixup failed %d\n",
2012 /* only allow hw_params() if no connected FEs are running */
2013 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2016 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2017 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2018 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2021 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2022 be->dai_link->name);
2024 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2026 dev_err(dpcm->be->dev,
2027 "ASoC: hw_params BE failed %d\n", ret);
2031 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2036 /* disable any enabled and non active backends */
2037 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2038 struct snd_soc_pcm_runtime *be = dpcm->be;
2039 struct snd_pcm_substream *be_substream =
2040 snd_soc_dpcm_get_substream(be, stream);
2042 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2045 /* only allow hw_free() if no connected FEs are running */
2046 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2049 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2050 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2052 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2055 soc_pcm_hw_free(be_substream);
2061 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2062 struct snd_pcm_hw_params *params)
2064 struct snd_soc_pcm_runtime *fe = substream->private_data;
2065 int ret, stream = substream->stream;
2067 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2068 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2070 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2071 sizeof(struct snd_pcm_hw_params));
2072 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2074 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2078 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2079 fe->dai_link->name, params_rate(params),
2080 params_channels(params), params_format(params));
2082 /* call hw_params on the frontend */
2083 ret = soc_pcm_hw_params(substream, params);
2085 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2086 dpcm_be_dai_hw_free(fe, stream);
2088 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2091 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2092 mutex_unlock(&fe->card->mutex);
2096 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2097 struct snd_pcm_substream *substream, int cmd)
2101 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2102 dpcm->be->dai_link->name, cmd);
2104 ret = soc_pcm_trigger(substream, cmd);
2106 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2111 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2114 struct snd_soc_dpcm *dpcm;
2117 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2119 struct snd_soc_pcm_runtime *be = dpcm->be;
2120 struct snd_pcm_substream *be_substream =
2121 snd_soc_dpcm_get_substream(be, stream);
2123 /* is this op for this BE ? */
2124 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2128 case SNDRV_PCM_TRIGGER_START:
2129 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2130 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2133 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2137 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2139 case SNDRV_PCM_TRIGGER_RESUME:
2140 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2143 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2147 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2149 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2150 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2153 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2157 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2159 case SNDRV_PCM_TRIGGER_STOP:
2160 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2163 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2166 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2170 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2172 case SNDRV_PCM_TRIGGER_SUSPEND:
2173 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2176 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2179 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2183 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2185 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2186 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2189 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2192 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2203 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2205 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2207 struct snd_soc_pcm_runtime *fe = substream->private_data;
2208 int stream = substream->stream, ret;
2209 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2211 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2214 case SND_SOC_DPCM_TRIGGER_PRE:
2215 /* call trigger on the frontend before the backend. */
2217 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2218 fe->dai_link->name, cmd);
2220 ret = soc_pcm_trigger(substream, cmd);
2222 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2226 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2228 case SND_SOC_DPCM_TRIGGER_POST:
2229 /* call trigger on the frontend after the backend. */
2231 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2233 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2237 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2238 fe->dai_link->name, cmd);
2240 ret = soc_pcm_trigger(substream, cmd);
2242 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2243 /* bespoke trigger() - handles both FE and BEs */
2245 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2246 fe->dai_link->name, cmd);
2248 ret = soc_pcm_bespoke_trigger(substream, cmd);
2250 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2255 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2256 fe->dai_link->name);
2262 case SNDRV_PCM_TRIGGER_START:
2263 case SNDRV_PCM_TRIGGER_RESUME:
2264 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2265 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2267 case SNDRV_PCM_TRIGGER_STOP:
2268 case SNDRV_PCM_TRIGGER_SUSPEND:
2269 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2272 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2277 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2281 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2283 struct snd_soc_pcm_runtime *fe = substream->private_data;
2284 int stream = substream->stream;
2286 /* if FE's runtime_update is already set, we're in race;
2287 * process this trigger later at exit
2289 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2290 fe->dpcm[stream].trigger_pending = cmd + 1;
2291 return 0; /* delayed, assuming it's successful */
2294 /* we're alone, let's trigger */
2295 return dpcm_fe_dai_do_trigger(substream, cmd);
2298 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2300 struct snd_soc_dpcm *dpcm;
2303 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2305 struct snd_soc_pcm_runtime *be = dpcm->be;
2306 struct snd_pcm_substream *be_substream =
2307 snd_soc_dpcm_get_substream(be, stream);
2309 /* is this op for this BE ? */
2310 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2313 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2314 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2315 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2318 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2319 be->dai_link->name);
2321 ret = soc_pcm_prepare(be_substream);
2323 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2328 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2333 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2335 struct snd_soc_pcm_runtime *fe = substream->private_data;
2336 int stream = substream->stream, ret = 0;
2338 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2340 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2342 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2344 /* there is no point preparing this FE if there are no BEs */
2345 if (list_empty(&fe->dpcm[stream].be_clients)) {
2346 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2347 fe->dai_link->name);
2352 ret = dpcm_be_dai_prepare(fe, substream->stream);
2356 /* call prepare on the frontend */
2357 ret = soc_pcm_prepare(substream);
2359 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2360 fe->dai_link->name);
2364 /* run the stream event for each BE */
2365 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2366 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2369 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2370 mutex_unlock(&fe->card->mutex);
2375 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2376 unsigned int cmd, void *arg)
2378 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2379 struct snd_soc_component *component;
2380 struct snd_soc_rtdcom_list *rtdcom;
2382 for_each_rtdcom(rtd, rtdcom) {
2383 component = rtdcom->component;
2385 if (!component->driver->ops ||
2386 !component->driver->ops->ioctl)
2389 /* FIXME: use 1st ioctl */
2390 return component->driver->ops->ioctl(substream, cmd, arg);
2393 return snd_pcm_lib_ioctl(substream, cmd, arg);
2396 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2398 struct snd_pcm_substream *substream =
2399 snd_soc_dpcm_get_substream(fe, stream);
2400 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2403 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2404 stream ? "capture" : "playback", fe->dai_link->name);
2406 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2407 /* call bespoke trigger - FE takes care of all BE triggers */
2408 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2409 fe->dai_link->name);
2411 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2413 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2415 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2416 fe->dai_link->name);
2418 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2420 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2423 err = dpcm_be_dai_hw_free(fe, stream);
2425 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2427 err = dpcm_be_dai_shutdown(fe, stream);
2429 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2431 /* run the stream event for each BE */
2432 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2437 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2439 struct snd_pcm_substream *substream =
2440 snd_soc_dpcm_get_substream(fe, stream);
2441 struct snd_soc_dpcm *dpcm;
2442 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2445 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2446 stream ? "capture" : "playback", fe->dai_link->name);
2448 /* Only start the BE if the FE is ready */
2449 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2450 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2453 /* startup must always be called for new BEs */
2454 ret = dpcm_be_dai_startup(fe, stream);
2458 /* keep going if FE state is > open */
2459 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2462 ret = dpcm_be_dai_hw_params(fe, stream);
2466 /* keep going if FE state is > hw_params */
2467 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2471 ret = dpcm_be_dai_prepare(fe, stream);
2475 /* run the stream event for each BE */
2476 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2478 /* keep going if FE state is > prepare */
2479 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2480 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2483 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2484 /* call trigger on the frontend - FE takes care of all BE triggers */
2485 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2486 fe->dai_link->name);
2488 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2490 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2494 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2495 fe->dai_link->name);
2497 ret = dpcm_be_dai_trigger(fe, stream,
2498 SNDRV_PCM_TRIGGER_START);
2500 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2508 dpcm_be_dai_hw_free(fe, stream);
2510 dpcm_be_dai_shutdown(fe, stream);
2512 /* disconnect any non started BEs */
2513 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2514 struct snd_soc_pcm_runtime *be = dpcm->be;
2515 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2516 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2522 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2526 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2527 ret = dpcm_run_update_startup(fe, stream);
2529 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2530 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2535 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2539 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2540 ret = dpcm_run_update_shutdown(fe, stream);
2542 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2543 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2548 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2551 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2553 struct snd_soc_pcm_runtime *fe;
2554 int old, new, paths;
2556 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2557 list_for_each_entry(fe, &card->rtd_list, list) {
2558 struct snd_soc_dapm_widget_list *list;
2560 /* make sure link is FE */
2561 if (!fe->dai_link->dynamic)
2564 /* only check active links */
2565 if (!fe->cpu_dai->active)
2568 /* DAPM sync will call this to update DSP paths */
2569 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2570 fe->dai_link->name);
2572 /* skip if FE doesn't have playback capability */
2573 if (!fe->cpu_dai->driver->playback.channels_min
2574 || !fe->codec_dai->driver->playback.channels_min)
2577 /* skip if FE isn't currently playing */
2578 if (!fe->cpu_dai->playback_active
2579 || !fe->codec_dai->playback_active)
2582 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2584 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2585 fe->dai_link->name, "playback");
2586 mutex_unlock(&card->mutex);
2590 /* update any new playback paths */
2591 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2593 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2594 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2595 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2598 /* update any old playback paths */
2599 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2601 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2602 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2603 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2606 dpcm_path_put(&list);
2608 /* skip if FE doesn't have capture capability */
2609 if (!fe->cpu_dai->driver->capture.channels_min
2610 || !fe->codec_dai->driver->capture.channels_min)
2613 /* skip if FE isn't currently capturing */
2614 if (!fe->cpu_dai->capture_active
2615 || !fe->codec_dai->capture_active)
2618 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2620 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2621 fe->dai_link->name, "capture");
2622 mutex_unlock(&card->mutex);
2626 /* update any new capture paths */
2627 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2629 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2630 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2631 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2634 /* update any old capture paths */
2635 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2637 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2638 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2639 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2642 dpcm_path_put(&list);
2645 mutex_unlock(&card->mutex);
2648 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2650 struct snd_soc_dpcm *dpcm;
2651 struct list_head *clients =
2652 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2654 list_for_each_entry(dpcm, clients, list_be) {
2656 struct snd_soc_pcm_runtime *be = dpcm->be;
2659 if (be->dai_link->ignore_suspend)
2662 for (i = 0; i < be->num_codecs; i++) {
2663 struct snd_soc_dai *dai = be->codec_dais[i];
2664 struct snd_soc_dai_driver *drv = dai->driver;
2666 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2667 be->dai_link->name);
2669 if (drv->ops && drv->ops->digital_mute &&
2670 dai->playback_active)
2671 drv->ops->digital_mute(dai, mute);
2678 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2680 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2681 struct snd_soc_dpcm *dpcm;
2682 struct snd_soc_dapm_widget_list *list;
2684 int stream = fe_substream->stream;
2686 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2687 fe->dpcm[stream].runtime = fe_substream->runtime;
2689 ret = dpcm_path_get(fe, stream, &list);
2691 mutex_unlock(&fe->card->mutex);
2693 } else if (ret == 0) {
2694 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2695 fe->dai_link->name, stream ? "capture" : "playback");
2698 /* calculate valid and active FE <-> BE dpcms */
2699 dpcm_process_paths(fe, stream, &list, 1);
2701 ret = dpcm_fe_dai_startup(fe_substream);
2703 /* clean up all links */
2704 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2705 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2707 dpcm_be_disconnect(fe, stream);
2708 fe->dpcm[stream].runtime = NULL;
2711 dpcm_clear_pending_state(fe, stream);
2712 dpcm_path_put(&list);
2713 mutex_unlock(&fe->card->mutex);
2717 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2719 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2720 struct snd_soc_dpcm *dpcm;
2721 int stream = fe_substream->stream, ret;
2723 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2724 ret = dpcm_fe_dai_shutdown(fe_substream);
2726 /* mark FE's links ready to prune */
2727 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2728 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2730 dpcm_be_disconnect(fe, stream);
2732 fe->dpcm[stream].runtime = NULL;
2733 mutex_unlock(&fe->card->mutex);
2737 static void soc_pcm_private_free(struct snd_pcm *pcm)
2739 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2740 struct snd_soc_rtdcom_list *rtdcom;
2741 struct snd_soc_component *component;
2743 /* need to sync the delayed work before releasing resources */
2744 flush_delayed_work(&rtd->delayed_work);
2745 for_each_rtdcom(rtd, rtdcom) {
2746 component = rtdcom->component;
2748 if (component->pcm_free)
2749 component->pcm_free(component, pcm);
2753 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2755 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2756 struct snd_soc_rtdcom_list *rtdcom;
2757 struct snd_soc_component *component;
2759 for_each_rtdcom(rtd, rtdcom) {
2760 component = rtdcom->component;
2762 if (!component->driver->ops ||
2763 !component->driver->ops->ack)
2766 /* FIXME. it returns 1st ask now */
2767 return component->driver->ops->ack(substream);
2773 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2774 unsigned long pos, void __user *buf,
2775 unsigned long bytes)
2777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2778 struct snd_soc_rtdcom_list *rtdcom;
2779 struct snd_soc_component *component;
2781 for_each_rtdcom(rtd, rtdcom) {
2782 component = rtdcom->component;
2784 if (!component->driver->ops ||
2785 !component->driver->ops->copy_user)
2788 /* FIXME. it returns 1st copy now */
2789 return component->driver->ops->copy_user(substream, channel,
2796 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2797 unsigned long pos, void *buf, unsigned long bytes)
2799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2800 struct snd_soc_rtdcom_list *rtdcom;
2801 struct snd_soc_component *component;
2803 for_each_rtdcom(rtd, rtdcom) {
2804 component = rtdcom->component;
2806 if (!component->driver->ops ||
2807 !component->driver->ops->copy_kernel)
2810 /* FIXME. it returns 1st copy now */
2811 return component->driver->ops->copy_kernel(substream, channel,
2818 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2819 unsigned long pos, unsigned long bytes)
2821 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2822 struct snd_soc_rtdcom_list *rtdcom;
2823 struct snd_soc_component *component;
2825 for_each_rtdcom(rtd, rtdcom) {
2826 component = rtdcom->component;
2828 if (!component->driver->ops ||
2829 !component->driver->ops->fill_silence)
2832 /* FIXME. it returns 1st silence now */
2833 return component->driver->ops->fill_silence(substream, channel,
2840 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2841 unsigned long offset)
2843 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2844 struct snd_soc_rtdcom_list *rtdcom;
2845 struct snd_soc_component *component;
2848 for_each_rtdcom(rtd, rtdcom) {
2849 component = rtdcom->component;
2851 if (!component->driver->ops ||
2852 !component->driver->ops->page)
2855 /* FIXME. it returns 1st page now */
2856 page = component->driver->ops->page(substream, offset);
2864 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2865 struct vm_area_struct *vma)
2867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2868 struct snd_soc_rtdcom_list *rtdcom;
2869 struct snd_soc_component *component;
2871 for_each_rtdcom(rtd, rtdcom) {
2872 component = rtdcom->component;
2874 if (!component->driver->ops ||
2875 !component->driver->ops->mmap)
2878 /* FIXME. it returns 1st mmap now */
2879 return component->driver->ops->mmap(substream, vma);
2885 /* create a new pcm */
2886 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2888 struct snd_soc_dai *codec_dai;
2889 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2890 struct snd_soc_component *component;
2891 struct snd_soc_rtdcom_list *rtdcom;
2892 struct snd_pcm *pcm;
2894 int ret = 0, playback = 0, capture = 0;
2897 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2898 playback = rtd->dai_link->dpcm_playback;
2899 capture = rtd->dai_link->dpcm_capture;
2901 for (i = 0; i < rtd->num_codecs; i++) {
2902 codec_dai = rtd->codec_dais[i];
2903 if (codec_dai->driver->playback.channels_min)
2905 if (codec_dai->driver->capture.channels_min)
2909 capture = capture && cpu_dai->driver->capture.channels_min;
2910 playback = playback && cpu_dai->driver->playback.channels_min;
2913 if (rtd->dai_link->playback_only) {
2918 if (rtd->dai_link->capture_only) {
2923 /* create the PCM */
2924 if (rtd->dai_link->no_pcm) {
2925 snprintf(new_name, sizeof(new_name), "(%s)",
2926 rtd->dai_link->stream_name);
2928 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2929 playback, capture, &pcm);
2931 if (rtd->dai_link->dynamic)
2932 snprintf(new_name, sizeof(new_name), "%s (*)",
2933 rtd->dai_link->stream_name);
2935 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2936 rtd->dai_link->stream_name,
2937 (rtd->num_codecs > 1) ?
2938 "multicodec" : rtd->codec_dai->name, num);
2940 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2944 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2945 rtd->dai_link->name);
2948 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2950 /* DAPM dai link stream work */
2951 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2953 pcm->nonatomic = rtd->dai_link->nonatomic;
2955 pcm->private_data = rtd;
2957 if (rtd->dai_link->no_pcm) {
2959 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2961 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2965 /* ASoC PCM operations */
2966 if (rtd->dai_link->dynamic) {
2967 rtd->ops.open = dpcm_fe_dai_open;
2968 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2969 rtd->ops.prepare = dpcm_fe_dai_prepare;
2970 rtd->ops.trigger = dpcm_fe_dai_trigger;
2971 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2972 rtd->ops.close = dpcm_fe_dai_close;
2973 rtd->ops.pointer = soc_pcm_pointer;
2974 rtd->ops.ioctl = soc_pcm_ioctl;
2976 rtd->ops.open = soc_pcm_open;
2977 rtd->ops.hw_params = soc_pcm_hw_params;
2978 rtd->ops.prepare = soc_pcm_prepare;
2979 rtd->ops.trigger = soc_pcm_trigger;
2980 rtd->ops.hw_free = soc_pcm_hw_free;
2981 rtd->ops.close = soc_pcm_close;
2982 rtd->ops.pointer = soc_pcm_pointer;
2983 rtd->ops.ioctl = soc_pcm_ioctl;
2986 for_each_rtdcom(rtd, rtdcom) {
2987 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
2993 rtd->ops.ack = soc_rtdcom_ack;
2995 rtd->ops.copy_user = soc_rtdcom_copy_user;
2996 if (ops->copy_kernel)
2997 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
2998 if (ops->fill_silence)
2999 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3001 rtd->ops.page = soc_rtdcom_page;
3003 rtd->ops.mmap = soc_rtdcom_mmap;
3007 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3010 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3012 for_each_rtdcom(rtd, rtdcom) {
3013 component = rtdcom->component;
3015 if (!component->pcm_new)
3018 ret = component->pcm_new(component, rtd);
3020 dev_err(component->dev,
3021 "ASoC: pcm constructor failed: %d\n",
3027 pcm->private_free = soc_pcm_private_free;
3029 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3030 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3035 /* is the current PCM operation for this FE ? */
3036 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3038 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3042 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3044 /* is the current PCM operation for this BE ? */
3045 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3046 struct snd_soc_pcm_runtime *be, int stream)
3048 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3049 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3050 be->dpcm[stream].runtime_update))
3054 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3056 /* get the substream for this BE */
3057 struct snd_pcm_substream *
3058 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3060 return be->pcm->streams[stream].substream;
3062 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3064 /* get the BE runtime state */
3065 enum snd_soc_dpcm_state
3066 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3068 return be->dpcm[stream].state;
3070 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3072 /* set the BE runtime state */
3073 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3074 int stream, enum snd_soc_dpcm_state state)
3076 be->dpcm[stream].state = state;
3078 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3081 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3082 * are not running, paused or suspended for the specified stream direction.
3084 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3085 struct snd_soc_pcm_runtime *be, int stream)
3087 struct snd_soc_dpcm *dpcm;
3090 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3095 state = dpcm->fe->dpcm[stream].state;
3096 if (state == SND_SOC_DPCM_STATE_START ||
3097 state == SND_SOC_DPCM_STATE_PAUSED ||
3098 state == SND_SOC_DPCM_STATE_SUSPEND)
3102 /* it's safe to free/stop this BE DAI */
3105 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3108 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3109 * running, paused or suspended for the specified stream direction.
3111 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3112 struct snd_soc_pcm_runtime *be, int stream)
3114 struct snd_soc_dpcm *dpcm;
3117 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3122 state = dpcm->fe->dpcm[stream].state;
3123 if (state == SND_SOC_DPCM_STATE_START ||
3124 state == SND_SOC_DPCM_STATE_PAUSED ||
3125 state == SND_SOC_DPCM_STATE_SUSPEND ||
3126 state == SND_SOC_DPCM_STATE_PREPARE)
3130 /* it's safe to change hw_params */
3133 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3135 #ifdef CONFIG_DEBUG_FS
3136 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3139 case SND_SOC_DPCM_STATE_NEW:
3141 case SND_SOC_DPCM_STATE_OPEN:
3143 case SND_SOC_DPCM_STATE_HW_PARAMS:
3145 case SND_SOC_DPCM_STATE_PREPARE:
3147 case SND_SOC_DPCM_STATE_START:
3149 case SND_SOC_DPCM_STATE_STOP:
3151 case SND_SOC_DPCM_STATE_SUSPEND:
3153 case SND_SOC_DPCM_STATE_PAUSED:
3155 case SND_SOC_DPCM_STATE_HW_FREE:
3157 case SND_SOC_DPCM_STATE_CLOSE:
3164 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3165 int stream, char *buf, size_t size)
3167 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3168 struct snd_soc_dpcm *dpcm;
3172 offset += snprintf(buf + offset, size - offset,
3173 "[%s - %s]\n", fe->dai_link->name,
3174 stream ? "Capture" : "Playback");
3176 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3177 dpcm_state_string(fe->dpcm[stream].state));
3179 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3180 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3181 offset += snprintf(buf + offset, size - offset,
3183 "Format = %s, Channels = %d, Rate = %d\n",
3184 snd_pcm_format_name(params_format(params)),
3185 params_channels(params),
3186 params_rate(params));
3189 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3191 if (list_empty(&fe->dpcm[stream].be_clients)) {
3192 offset += snprintf(buf + offset, size - offset,
3193 " No active DSP links\n");
3197 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3198 struct snd_soc_pcm_runtime *be = dpcm->be;
3199 params = &dpcm->hw_params;
3201 offset += snprintf(buf + offset, size - offset,
3202 "- %s\n", be->dai_link->name);
3204 offset += snprintf(buf + offset, size - offset,
3206 dpcm_state_string(be->dpcm[stream].state));
3208 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3209 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3210 offset += snprintf(buf + offset, size - offset,
3211 " Hardware Params: "
3212 "Format = %s, Channels = %d, Rate = %d\n",
3213 snd_pcm_format_name(params_format(params)),
3214 params_channels(params),
3215 params_rate(params));
3222 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3223 size_t count, loff_t *ppos)
3225 struct snd_soc_pcm_runtime *fe = file->private_data;
3226 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3229 buf = kmalloc(out_count, GFP_KERNEL);
3233 if (fe->cpu_dai->driver->playback.channels_min)
3234 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3235 buf + offset, out_count - offset);
3237 if (fe->cpu_dai->driver->capture.channels_min)
3238 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3239 buf + offset, out_count - offset);
3241 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3247 static const struct file_operations dpcm_state_fops = {
3248 .open = simple_open,
3249 .read = dpcm_state_read_file,
3250 .llseek = default_llseek,
3253 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3258 if (!rtd->card->debugfs_card_root)
3261 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3262 rtd->card->debugfs_card_root);
3263 if (!rtd->debugfs_dpcm_root) {
3265 "ASoC: Failed to create dpcm debugfs directory %s\n",
3266 rtd->dai_link->name);
3270 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3271 rtd, &dpcm_state_fops);