2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
45 * Must be called with the rtd->pcm_mutex being held
47 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
52 lockdep_assert_held(&rtd->pcm_mutex);
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
59 cpu_dai->capture_active++;
60 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
65 cpu_dai->component->active++;
66 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
80 * Must be called with the rtd->pcm_mutex being held
82 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87 lockdep_assert_held(&rtd->pcm_mutex);
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
91 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
94 cpu_dai->capture_active--;
95 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
100 cpu_dai->component->active--;
101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
116 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
135 * Sets the substream runtime hardware parameters.
137 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
151 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
153 /* DPCM stream event, send event to FE and all active BEs. */
154 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
157 struct snd_soc_dpcm *dpcm;
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
161 struct snd_soc_pcm_runtime *be = dpcm->be;
163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
164 be->dai_link->name, event, dir);
166 snd_soc_dapm_stream_event(be, dir, event);
169 snd_soc_dapm_stream_event(fe, dir, event);
174 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
185 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186 SNDRV_PCM_HW_PARAM_RATE,
187 soc_dai->rate, soc_dai->rate);
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
201 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
202 SNDRV_PCM_HW_PARAM_CHANNELS,
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
218 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits,
221 soc_dai->sample_bits);
223 dev_err(soc_dai->dev,
224 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
233 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
236 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
238 unsigned int rate, channels, sample_bits, symmetry, i;
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
246 rtd->dai_link->symmetric_rates;
248 for (i = 0; i < rtd->num_codecs; i++)
249 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
257 symmetry = cpu_dai->driver->symmetric_channels ||
258 rtd->dai_link->symmetric_channels;
260 for (i = 0; i < rtd->num_codecs; i++)
261 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
270 rtd->dai_link->symmetric_samplebits;
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
284 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
288 struct snd_soc_dai_link *link = rtd->dai_link;
289 unsigned int symmetry, i;
291 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
292 cpu_driver->symmetric_channels || link->symmetric_channels ||
293 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry = symmetry ||
297 rtd->codec_dais[i]->driver->symmetric_rates ||
298 rtd->codec_dais[i]->driver->symmetric_channels ||
299 rtd->codec_dais[i]->driver->symmetric_samplebits;
305 * List of sample sizes that might go over the bus for parameter
306 * application. There ought to be a wildcard sample size for things
307 * like the DAC/ADC resolution to use but there isn't right now.
309 static int sample_sizes[] = {
313 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
322 if (bits >= sample_sizes[i])
325 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
326 sample_sizes[i], bits);
329 "ASoC: Failed to set MSB %d/%d: %d\n",
330 bits, sample_sizes[i], ret);
334 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
336 struct snd_soc_pcm_runtime *rtd = substream->private_data;
337 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
338 struct snd_soc_dai *codec_dai;
340 unsigned int bits = 0, cpu_bits;
342 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
343 for (i = 0; i < rtd->num_codecs; i++) {
344 codec_dai = rtd->codec_dais[i];
345 if (codec_dai->driver->playback.sig_bits == 0) {
349 bits = max(codec_dai->driver->playback.sig_bits, bits);
351 cpu_bits = cpu_dai->driver->playback.sig_bits;
353 for (i = 0; i < rtd->num_codecs; i++) {
354 codec_dai = rtd->codec_dais[i];
355 if (codec_dai->driver->capture.sig_bits == 0) {
359 bits = max(codec_dai->driver->capture.sig_bits, bits);
361 cpu_bits = cpu_dai->driver->capture.sig_bits;
364 soc_pcm_set_msb(substream, bits);
365 soc_pcm_set_msb(substream, cpu_bits);
368 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
370 struct snd_pcm_runtime *runtime = substream->runtime;
371 struct snd_pcm_hardware *hw = &runtime->hw;
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
374 struct snd_soc_dai_driver *codec_dai_drv;
375 struct snd_soc_pcm_stream *codec_stream;
376 struct snd_soc_pcm_stream *cpu_stream;
377 unsigned int chan_min = 0, chan_max = UINT_MAX;
378 unsigned int rate_min = 0, rate_max = UINT_MAX;
379 unsigned int rates = UINT_MAX;
380 u64 formats = ULLONG_MAX;
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
384 cpu_stream = &cpu_dai_drv->playback;
386 cpu_stream = &cpu_dai_drv->capture;
388 /* first calculate min/max only for CODECs in the DAI link */
389 for (i = 0; i < rtd->num_codecs; i++) {
390 codec_dai_drv = rtd->codec_dais[i]->driver;
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 codec_stream = &codec_dai_drv->playback;
394 codec_stream = &codec_dai_drv->capture;
395 chan_min = max(chan_min, codec_stream->channels_min);
396 chan_max = min(chan_max, codec_stream->channels_max);
397 rate_min = max(rate_min, codec_stream->rate_min);
398 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
399 formats &= codec_stream->formats;
400 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
404 * chan min/max cannot be enforced if there are multiple CODEC DAIs
405 * connected to a single CPU DAI, use CPU DAI's directly and let
406 * channel allocation be fixed up later
408 if (rtd->num_codecs > 1) {
409 chan_min = cpu_stream->channels_min;
410 chan_max = cpu_stream->channels_max;
413 hw->channels_min = max(chan_min, cpu_stream->channels_min);
414 hw->channels_max = min(chan_max, cpu_stream->channels_max);
416 hw->formats &= formats & cpu_stream->formats;
418 hw->formats = formats & cpu_stream->formats;
419 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
421 snd_pcm_limit_hw_rates(runtime);
423 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
424 hw->rate_min = max(hw->rate_min, rate_min);
425 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
426 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
430 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
431 * then initialized and any private data can be allocated. This also calls
432 * startup for the cpu DAI, platform, machine and codec DAI.
434 static int soc_pcm_open(struct snd_pcm_substream *substream)
436 struct snd_soc_pcm_runtime *rtd = substream->private_data;
437 struct snd_pcm_runtime *runtime = substream->runtime;
438 struct snd_soc_platform *platform = rtd->platform;
439 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
440 struct snd_soc_dai *codec_dai;
441 const char *codec_dai_name = "multicodec";
444 pinctrl_pm_select_default_state(cpu_dai->dev);
445 for (i = 0; i < rtd->num_codecs; i++)
446 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
447 pm_runtime_get_sync(cpu_dai->dev);
448 for (i = 0; i < rtd->num_codecs; i++)
449 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
450 pm_runtime_get_sync(platform->dev);
452 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
454 /* startup the audio subsystem */
455 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
456 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
458 dev_err(cpu_dai->dev, "ASoC: can't open interface"
459 " %s: %d\n", cpu_dai->name, ret);
464 if (platform->driver->ops && platform->driver->ops->open) {
465 ret = platform->driver->ops->open(substream);
467 dev_err(platform->dev, "ASoC: can't open platform"
468 " %s: %d\n", platform->component.name, ret);
473 for (i = 0; i < rtd->num_codecs; i++) {
474 codec_dai = rtd->codec_dais[i];
475 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
476 ret = codec_dai->driver->ops->startup(substream,
479 dev_err(codec_dai->dev,
480 "ASoC: can't open codec %s: %d\n",
481 codec_dai->name, ret);
486 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
487 codec_dai->tx_mask = 0;
489 codec_dai->rx_mask = 0;
492 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
493 ret = rtd->dai_link->ops->startup(substream);
495 pr_err("ASoC: %s startup failed: %d\n",
496 rtd->dai_link->name, ret);
501 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
502 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
505 /* Check that the codec and cpu DAIs are compatible */
506 soc_pcm_init_runtime_hw(substream);
508 if (rtd->num_codecs == 1)
509 codec_dai_name = rtd->codec_dai->name;
511 if (soc_pcm_has_symmetry(substream))
512 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
515 if (!runtime->hw.rates) {
516 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
517 codec_dai_name, cpu_dai->name);
520 if (!runtime->hw.formats) {
521 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
522 codec_dai_name, cpu_dai->name);
525 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
526 runtime->hw.channels_min > runtime->hw.channels_max) {
527 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
528 codec_dai_name, cpu_dai->name);
532 soc_pcm_apply_msb(substream);
534 /* Symmetry only applies if we've already got an active stream. */
535 if (cpu_dai->active) {
536 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
541 for (i = 0; i < rtd->num_codecs; i++) {
542 if (rtd->codec_dais[i]->active) {
543 ret = soc_pcm_apply_symmetry(substream,
550 pr_debug("ASoC: %s <-> %s info:\n",
551 codec_dai_name, cpu_dai->name);
552 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
553 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
554 runtime->hw.channels_max);
555 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
556 runtime->hw.rate_max);
560 snd_soc_runtime_activate(rtd, substream->stream);
562 mutex_unlock(&rtd->pcm_mutex);
566 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
567 rtd->dai_link->ops->shutdown(substream);
574 codec_dai = rtd->codec_dais[i];
575 if (codec_dai->driver->ops->shutdown)
576 codec_dai->driver->ops->shutdown(substream, codec_dai);
579 if (platform->driver->ops && platform->driver->ops->close)
580 platform->driver->ops->close(substream);
583 if (cpu_dai->driver->ops->shutdown)
584 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
586 mutex_unlock(&rtd->pcm_mutex);
588 pm_runtime_put(platform->dev);
589 for (i = 0; i < rtd->num_codecs; i++)
590 pm_runtime_put(rtd->codec_dais[i]->dev);
591 pm_runtime_put(cpu_dai->dev);
592 for (i = 0; i < rtd->num_codecs; i++) {
593 if (!rtd->codec_dais[i]->active)
594 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
596 if (!cpu_dai->active)
597 pinctrl_pm_select_sleep_state(cpu_dai->dev);
603 * Power down the audio subsystem pmdown_time msecs after close is called.
604 * This is to ensure there are no pops or clicks in between any music tracks
605 * due to DAPM power cycling.
607 static void close_delayed_work(struct work_struct *work)
609 struct snd_soc_pcm_runtime *rtd =
610 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
611 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
613 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
615 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
616 codec_dai->driver->playback.stream_name,
617 codec_dai->playback_active ? "active" : "inactive",
618 rtd->pop_wait ? "yes" : "no");
620 /* are we waiting on this codec DAI stream */
621 if (rtd->pop_wait == 1) {
623 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
624 SND_SOC_DAPM_STREAM_STOP);
627 mutex_unlock(&rtd->pcm_mutex);
631 * Called by ALSA when a PCM substream is closed. Private data can be
632 * freed here. The cpu DAI, codec DAI, machine and platform are also
635 static int soc_pcm_close(struct snd_pcm_substream *substream)
637 struct snd_soc_pcm_runtime *rtd = substream->private_data;
638 struct snd_soc_platform *platform = rtd->platform;
639 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
640 struct snd_soc_dai *codec_dai;
643 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
645 snd_soc_runtime_deactivate(rtd, substream->stream);
647 /* clear the corresponding DAIs rate when inactive */
648 if (!cpu_dai->active)
651 for (i = 0; i < rtd->num_codecs; i++) {
652 codec_dai = rtd->codec_dais[i];
653 if (!codec_dai->active)
657 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
659 if (cpu_dai->driver->ops->shutdown)
660 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
662 for (i = 0; i < rtd->num_codecs; i++) {
663 codec_dai = rtd->codec_dais[i];
664 if (codec_dai->driver->ops->shutdown)
665 codec_dai->driver->ops->shutdown(substream, codec_dai);
668 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
669 rtd->dai_link->ops->shutdown(substream);
671 if (platform->driver->ops && platform->driver->ops->close)
672 platform->driver->ops->close(substream);
674 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
675 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
676 /* powered down playback stream now */
677 snd_soc_dapm_stream_event(rtd,
678 SNDRV_PCM_STREAM_PLAYBACK,
679 SND_SOC_DAPM_STREAM_STOP);
681 /* start delayed pop wq here for playback streams */
683 queue_delayed_work(system_power_efficient_wq,
685 msecs_to_jiffies(rtd->pmdown_time));
688 /* capture streams can be powered down now */
689 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
690 SND_SOC_DAPM_STREAM_STOP);
693 mutex_unlock(&rtd->pcm_mutex);
695 pm_runtime_put(platform->dev);
696 for (i = 0; i < rtd->num_codecs; i++)
697 pm_runtime_put(rtd->codec_dais[i]->dev);
698 pm_runtime_put(cpu_dai->dev);
699 for (i = 0; i < rtd->num_codecs; i++) {
700 if (!rtd->codec_dais[i]->active)
701 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
703 if (!cpu_dai->active)
704 pinctrl_pm_select_sleep_state(cpu_dai->dev);
710 * Called by ALSA when the PCM substream is prepared, can set format, sample
711 * rate, etc. This function is non atomic and can be called multiple times,
712 * it can refer to the runtime info.
714 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
716 struct snd_soc_pcm_runtime *rtd = substream->private_data;
717 struct snd_soc_platform *platform = rtd->platform;
718 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
719 struct snd_soc_dai *codec_dai;
722 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
724 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
725 ret = rtd->dai_link->ops->prepare(substream);
727 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
733 if (platform->driver->ops && platform->driver->ops->prepare) {
734 ret = platform->driver->ops->prepare(substream);
736 dev_err(platform->dev, "ASoC: platform prepare error:"
742 for (i = 0; i < rtd->num_codecs; i++) {
743 codec_dai = rtd->codec_dais[i];
744 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
745 ret = codec_dai->driver->ops->prepare(substream,
748 dev_err(codec_dai->dev,
749 "ASoC: DAI prepare error: %d\n", ret);
755 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
756 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
758 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
764 /* cancel any delayed stream shutdown that is pending */
765 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
768 cancel_delayed_work(&rtd->delayed_work);
771 snd_soc_dapm_stream_event(rtd, substream->stream,
772 SND_SOC_DAPM_STREAM_START);
774 for (i = 0; i < rtd->num_codecs; i++)
775 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
777 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
780 mutex_unlock(&rtd->pcm_mutex);
784 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
787 struct snd_interval *interval;
788 int channels = hweight_long(mask);
790 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
791 interval->min = channels;
792 interval->max = channels;
795 int soc_dai_hw_params(struct snd_pcm_substream *substream,
796 struct snd_pcm_hw_params *params,
797 struct snd_soc_dai *dai)
801 if (dai->driver->ops && dai->driver->ops->hw_params) {
802 ret = dai->driver->ops->hw_params(substream, params, dai);
804 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
814 * Called by ALSA when the hardware params are set by application. This
815 * function can also be called multiple times and can allocate buffers
816 * (using snd_pcm_lib_* ). It's non-atomic.
818 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
819 struct snd_pcm_hw_params *params)
821 struct snd_soc_pcm_runtime *rtd = substream->private_data;
822 struct snd_soc_platform *platform = rtd->platform;
823 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
826 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
828 ret = soc_pcm_params_symmetry(substream, params);
832 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
833 ret = rtd->dai_link->ops->hw_params(substream, params);
835 dev_err(rtd->card->dev, "ASoC: machine hw_params"
836 " failed: %d\n", ret);
841 for (i = 0; i < rtd->num_codecs; i++) {
842 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
843 struct snd_pcm_hw_params codec_params;
845 /* copy params for each codec */
846 codec_params = *params;
848 /* fixup params based on TDM slot masks */
849 if (codec_dai->tx_mask)
850 soc_pcm_codec_params_fixup(&codec_params,
852 if (codec_dai->rx_mask)
853 soc_pcm_codec_params_fixup(&codec_params,
856 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
860 codec_dai->rate = params_rate(&codec_params);
861 codec_dai->channels = params_channels(&codec_params);
862 codec_dai->sample_bits = snd_pcm_format_physical_width(
863 params_format(&codec_params));
866 ret = soc_dai_hw_params(substream, params, cpu_dai);
870 if (platform->driver->ops && platform->driver->ops->hw_params) {
871 ret = platform->driver->ops->hw_params(substream, params);
873 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
874 platform->component.name, ret);
879 /* store the parameters for each DAIs */
880 cpu_dai->rate = params_rate(params);
881 cpu_dai->channels = params_channels(params);
882 cpu_dai->sample_bits =
883 snd_pcm_format_physical_width(params_format(params));
886 mutex_unlock(&rtd->pcm_mutex);
890 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
891 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
898 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
899 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
900 codec_dai->driver->ops->hw_free(substream, codec_dai);
904 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
905 rtd->dai_link->ops->hw_free(substream);
907 mutex_unlock(&rtd->pcm_mutex);
912 * Frees resources allocated by hw_params, can be called multiple times
914 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
916 struct snd_soc_pcm_runtime *rtd = substream->private_data;
917 struct snd_soc_platform *platform = rtd->platform;
918 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
919 struct snd_soc_dai *codec_dai;
920 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
923 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
925 /* clear the corresponding DAIs parameters when going to be inactive */
926 if (cpu_dai->active == 1) {
928 cpu_dai->channels = 0;
929 cpu_dai->sample_bits = 0;
932 for (i = 0; i < rtd->num_codecs; i++) {
933 codec_dai = rtd->codec_dais[i];
934 if (codec_dai->active == 1) {
936 codec_dai->channels = 0;
937 codec_dai->sample_bits = 0;
941 /* apply codec digital mute */
942 for (i = 0; i < rtd->num_codecs; i++) {
943 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
944 (!playback && rtd->codec_dais[i]->capture_active == 1))
945 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
949 /* free any machine hw params */
950 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
951 rtd->dai_link->ops->hw_free(substream);
953 /* free any DMA resources */
954 if (platform->driver->ops && platform->driver->ops->hw_free)
955 platform->driver->ops->hw_free(substream);
957 /* now free hw params for the DAIs */
958 for (i = 0; i < rtd->num_codecs; i++) {
959 codec_dai = rtd->codec_dais[i];
960 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
961 codec_dai->driver->ops->hw_free(substream, codec_dai);
964 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
965 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
967 mutex_unlock(&rtd->pcm_mutex);
971 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
973 struct snd_soc_pcm_runtime *rtd = substream->private_data;
974 struct snd_soc_platform *platform = rtd->platform;
975 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
976 struct snd_soc_dai *codec_dai;
979 for (i = 0; i < rtd->num_codecs; i++) {
980 codec_dai = rtd->codec_dais[i];
981 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
982 ret = codec_dai->driver->ops->trigger(substream,
989 if (platform->driver->ops && platform->driver->ops->trigger) {
990 ret = platform->driver->ops->trigger(substream, cmd);
995 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
996 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1001 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1002 ret = rtd->dai_link->ops->trigger(substream, cmd);
1010 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1013 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1014 struct snd_soc_platform *platform = rtd->platform;
1015 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1016 struct snd_soc_dai *codec_dai;
1019 for (i = 0; i < rtd->num_codecs; i++) {
1020 codec_dai = rtd->codec_dais[i];
1021 if (codec_dai->driver->ops &&
1022 codec_dai->driver->ops->bespoke_trigger) {
1023 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1030 if (platform->driver->bespoke_trigger) {
1031 ret = platform->driver->bespoke_trigger(substream, cmd);
1036 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1037 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1044 * soc level wrapper for pointer callback
1045 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1046 * the runtime->delay will be updated accordingly.
1048 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1050 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1051 struct snd_soc_platform *platform = rtd->platform;
1052 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1053 struct snd_soc_dai *codec_dai;
1054 struct snd_pcm_runtime *runtime = substream->runtime;
1055 snd_pcm_uframes_t offset = 0;
1056 snd_pcm_sframes_t delay = 0;
1057 snd_pcm_sframes_t codec_delay = 0;
1060 if (platform->driver->ops && platform->driver->ops->pointer)
1061 offset = platform->driver->ops->pointer(substream);
1063 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1064 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1066 for (i = 0; i < rtd->num_codecs; i++) {
1067 codec_dai = rtd->codec_dais[i];
1068 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1069 codec_delay = max(codec_delay,
1070 codec_dai->driver->ops->delay(substream,
1073 delay += codec_delay;
1076 * None of the existing platform drivers implement delay(), so
1077 * for now the codec_dai of first multicodec entry is used
1079 if (platform->driver->delay)
1080 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1082 runtime->delay = delay;
1087 /* connect a FE and BE */
1088 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1089 struct snd_soc_pcm_runtime *be, int stream)
1091 struct snd_soc_dpcm *dpcm;
1093 /* only add new dpcms */
1094 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1095 if (dpcm->be == be && dpcm->fe == fe)
1099 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1105 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1106 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1107 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1108 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1110 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1111 stream ? "capture" : "playback", fe->dai_link->name,
1112 stream ? "<-" : "->", be->dai_link->name);
1114 #ifdef CONFIG_DEBUG_FS
1115 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1116 fe->debugfs_dpcm_root, &dpcm->state);
1121 /* reparent a BE onto another FE */
1122 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1123 struct snd_soc_pcm_runtime *be, int stream)
1125 struct snd_soc_dpcm *dpcm;
1126 struct snd_pcm_substream *fe_substream, *be_substream;
1128 /* reparent if BE is connected to other FEs */
1129 if (!be->dpcm[stream].users)
1132 be_substream = snd_soc_dpcm_get_substream(be, stream);
1134 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1138 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1139 stream ? "capture" : "playback",
1140 dpcm->fe->dai_link->name,
1141 stream ? "<-" : "->", dpcm->be->dai_link->name);
1143 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1144 be_substream->runtime = fe_substream->runtime;
1149 /* disconnect a BE and FE */
1150 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1152 struct snd_soc_dpcm *dpcm, *d;
1154 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1155 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1156 stream ? "capture" : "playback",
1157 dpcm->be->dai_link->name);
1159 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1162 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1163 stream ? "capture" : "playback", fe->dai_link->name,
1164 stream ? "<-" : "->", dpcm->be->dai_link->name);
1166 /* BEs still alive need new FE */
1167 dpcm_be_reparent(fe, dpcm->be, stream);
1169 #ifdef CONFIG_DEBUG_FS
1170 debugfs_remove(dpcm->debugfs_state);
1172 list_del(&dpcm->list_be);
1173 list_del(&dpcm->list_fe);
1178 /* get BE for DAI widget and stream */
1179 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1180 struct snd_soc_dapm_widget *widget, int stream)
1182 struct snd_soc_pcm_runtime *be;
1185 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1186 for (i = 0; i < card->num_links; i++) {
1189 if (!be->dai_link->no_pcm)
1192 if (be->cpu_dai->playback_widget == widget)
1195 for (j = 0; j < be->num_codecs; j++) {
1196 struct snd_soc_dai *dai = be->codec_dais[j];
1197 if (dai->playback_widget == widget)
1203 for (i = 0; i < card->num_links; i++) {
1206 if (!be->dai_link->no_pcm)
1209 if (be->cpu_dai->capture_widget == widget)
1212 for (j = 0; j < be->num_codecs; j++) {
1213 struct snd_soc_dai *dai = be->codec_dais[j];
1214 if (dai->capture_widget == widget)
1220 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1221 stream ? "capture" : "playback", widget->name);
1225 static inline struct snd_soc_dapm_widget *
1226 dai_get_widget(struct snd_soc_dai *dai, int stream)
1228 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1229 return dai->playback_widget;
1231 return dai->capture_widget;
1234 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1235 struct snd_soc_dapm_widget *widget)
1239 for (i = 0; i < list->num_widgets; i++) {
1240 if (widget == list->widgets[i])
1247 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1248 int stream, struct snd_soc_dapm_widget_list **list_)
1250 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1251 struct snd_soc_dapm_widget_list *list;
1254 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1255 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1259 /* get number of valid DAI paths and their widgets */
1260 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1262 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1263 stream ? "capture" : "playback");
1269 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1270 struct snd_soc_dapm_widget_list **list_)
1272 struct snd_soc_dpcm *dpcm;
1273 struct snd_soc_dapm_widget_list *list = *list_;
1274 struct snd_soc_dapm_widget *widget;
1277 /* Destroy any old FE <--> BE connections */
1278 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1281 /* is there a valid CPU DAI widget for this BE */
1282 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1284 /* prune the BE if it's no longer in our active list */
1285 if (widget && widget_in_list(list, widget))
1288 /* is there a valid CODEC DAI widget for this BE */
1289 for (i = 0; i < dpcm->be->num_codecs; i++) {
1290 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1291 widget = dai_get_widget(dai, stream);
1293 /* prune the BE if it's no longer in our active list */
1294 if (widget && widget_in_list(list, widget))
1298 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1299 stream ? "capture" : "playback",
1300 dpcm->be->dai_link->name, fe->dai_link->name);
1301 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1302 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1306 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1310 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1311 struct snd_soc_dapm_widget_list **list_)
1313 struct snd_soc_card *card = fe->card;
1314 struct snd_soc_dapm_widget_list *list = *list_;
1315 struct snd_soc_pcm_runtime *be;
1316 int i, new = 0, err;
1318 /* Create any new FE <--> BE connections */
1319 for (i = 0; i < list->num_widgets; i++) {
1321 switch (list->widgets[i]->id) {
1322 case snd_soc_dapm_dai_in:
1323 case snd_soc_dapm_dai_out:
1329 /* is there a valid BE rtd for this widget */
1330 be = dpcm_get_be(card, list->widgets[i], stream);
1332 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1333 list->widgets[i]->name);
1337 /* make sure BE is a real BE */
1338 if (!be->dai_link->no_pcm)
1341 /* don't connect if FE is not running */
1342 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1345 /* newly connected FE and BE */
1346 err = dpcm_be_connect(fe, be, stream);
1348 dev_err(fe->dev, "ASoC: can't connect %s\n",
1349 list->widgets[i]->name);
1351 } else if (err == 0) /* already connected */
1355 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1359 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1364 * Find the corresponding BE DAIs that source or sink audio to this
1367 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1368 int stream, struct snd_soc_dapm_widget_list **list, int new)
1371 return dpcm_add_paths(fe, stream, list);
1373 return dpcm_prune_paths(fe, stream, list);
1376 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1378 struct snd_soc_dpcm *dpcm;
1380 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1381 dpcm->be->dpcm[stream].runtime_update =
1382 SND_SOC_DPCM_UPDATE_NO;
1385 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1388 struct snd_soc_dpcm *dpcm;
1390 /* disable any enabled and non active backends */
1391 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1393 struct snd_soc_pcm_runtime *be = dpcm->be;
1394 struct snd_pcm_substream *be_substream =
1395 snd_soc_dpcm_get_substream(be, stream);
1397 if (be->dpcm[stream].users == 0)
1398 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1399 stream ? "capture" : "playback",
1400 be->dpcm[stream].state);
1402 if (--be->dpcm[stream].users != 0)
1405 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1408 soc_pcm_close(be_substream);
1409 be_substream->runtime = NULL;
1410 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1414 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1416 struct snd_soc_dpcm *dpcm;
1419 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1420 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1422 struct snd_soc_pcm_runtime *be = dpcm->be;
1423 struct snd_pcm_substream *be_substream =
1424 snd_soc_dpcm_get_substream(be, stream);
1426 if (!be_substream) {
1427 dev_err(be->dev, "ASoC: no backend %s stream\n",
1428 stream ? "capture" : "playback");
1432 /* is this op for this BE ? */
1433 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1436 /* first time the dpcm is open ? */
1437 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1438 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1439 stream ? "capture" : "playback",
1440 be->dpcm[stream].state);
1442 if (be->dpcm[stream].users++ != 0)
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1449 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1450 stream ? "capture" : "playback", be->dai_link->name);
1452 be_substream->runtime = be->dpcm[stream].runtime;
1453 err = soc_pcm_open(be_substream);
1455 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1456 be->dpcm[stream].users--;
1457 if (be->dpcm[stream].users < 0)
1458 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1459 stream ? "capture" : "playback",
1460 be->dpcm[stream].state);
1462 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1466 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1473 /* disable any enabled and non active backends */
1474 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1475 struct snd_soc_pcm_runtime *be = dpcm->be;
1476 struct snd_pcm_substream *be_substream =
1477 snd_soc_dpcm_get_substream(be, stream);
1479 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1482 if (be->dpcm[stream].users == 0)
1483 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1484 stream ? "capture" : "playback",
1485 be->dpcm[stream].state);
1487 if (--be->dpcm[stream].users != 0)
1490 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1493 soc_pcm_close(be_substream);
1494 be_substream->runtime = NULL;
1495 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1501 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1502 struct snd_soc_pcm_stream *stream)
1504 runtime->hw.rate_min = stream->rate_min;
1505 runtime->hw.rate_max = stream->rate_max;
1506 runtime->hw.channels_min = stream->channels_min;
1507 runtime->hw.channels_max = stream->channels_max;
1508 if (runtime->hw.formats)
1509 runtime->hw.formats &= stream->formats;
1511 runtime->hw.formats = stream->formats;
1512 runtime->hw.rates = stream->rates;
1515 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1517 struct snd_pcm_runtime *runtime = substream->runtime;
1518 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1519 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1520 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1522 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1523 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1525 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1528 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1530 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1531 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1532 int stream = fe_substream->stream, ret = 0;
1534 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1536 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1538 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1542 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1544 /* start the DAI frontend */
1545 ret = soc_pcm_open(fe_substream);
1547 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1551 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1553 dpcm_set_fe_runtime(fe_substream);
1554 snd_pcm_limit_hw_rates(runtime);
1556 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1560 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1562 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1566 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1568 struct snd_soc_dpcm *dpcm;
1570 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1571 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1573 struct snd_soc_pcm_runtime *be = dpcm->be;
1574 struct snd_pcm_substream *be_substream =
1575 snd_soc_dpcm_get_substream(be, stream);
1577 /* is this op for this BE ? */
1578 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1581 if (be->dpcm[stream].users == 0)
1582 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1583 stream ? "capture" : "playback",
1584 be->dpcm[stream].state);
1586 if (--be->dpcm[stream].users != 0)
1589 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1590 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1593 dev_dbg(be->dev, "ASoC: close BE %s\n",
1594 dpcm->fe->dai_link->name);
1596 soc_pcm_close(be_substream);
1597 be_substream->runtime = NULL;
1599 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1604 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1606 struct snd_soc_pcm_runtime *fe = substream->private_data;
1607 int stream = substream->stream;
1609 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1611 /* shutdown the BEs */
1612 dpcm_be_dai_shutdown(fe, substream->stream);
1614 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1616 /* now shutdown the frontend */
1617 soc_pcm_close(substream);
1619 /* run the stream event for each BE */
1620 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1622 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1623 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1627 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1629 struct snd_soc_dpcm *dpcm;
1631 /* only hw_params backends that are either sinks or sources
1632 * to this frontend DAI */
1633 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1635 struct snd_soc_pcm_runtime *be = dpcm->be;
1636 struct snd_pcm_substream *be_substream =
1637 snd_soc_dpcm_get_substream(be, stream);
1639 /* is this op for this BE ? */
1640 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1643 /* only free hw when no longer used - check all FEs */
1644 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1647 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1648 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1649 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1650 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1651 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1654 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1655 dpcm->fe->dai_link->name);
1657 soc_pcm_hw_free(be_substream);
1659 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1665 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1667 struct snd_soc_pcm_runtime *fe = substream->private_data;
1668 int err, stream = substream->stream;
1670 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1671 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1673 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1675 /* call hw_free on the frontend */
1676 err = soc_pcm_hw_free(substream);
1678 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1679 fe->dai_link->name);
1681 /* only hw_params backends that are either sinks or sources
1682 * to this frontend DAI */
1683 err = dpcm_be_dai_hw_free(fe, stream);
1685 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1686 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1688 mutex_unlock(&fe->card->mutex);
1692 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1694 struct snd_soc_dpcm *dpcm;
1697 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1699 struct snd_soc_pcm_runtime *be = dpcm->be;
1700 struct snd_pcm_substream *be_substream =
1701 snd_soc_dpcm_get_substream(be, stream);
1703 /* is this op for this BE ? */
1704 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1707 /* only allow hw_params() if no connected FEs are running */
1708 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1711 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1712 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1713 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1716 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1717 dpcm->fe->dai_link->name);
1719 /* copy params for each dpcm */
1720 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1721 sizeof(struct snd_pcm_hw_params));
1723 /* perform any hw_params fixups */
1724 if (be->dai_link->be_hw_params_fixup) {
1725 ret = be->dai_link->be_hw_params_fixup(be,
1729 "ASoC: hw_params BE fixup failed %d\n",
1735 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1737 dev_err(dpcm->be->dev,
1738 "ASoC: hw_params BE failed %d\n", ret);
1742 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1747 /* disable any enabled and non active backends */
1748 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1749 struct snd_soc_pcm_runtime *be = dpcm->be;
1750 struct snd_pcm_substream *be_substream =
1751 snd_soc_dpcm_get_substream(be, stream);
1753 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1756 /* only allow hw_free() if no connected FEs are running */
1757 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1760 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1761 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1762 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1763 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1766 soc_pcm_hw_free(be_substream);
1772 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1773 struct snd_pcm_hw_params *params)
1775 struct snd_soc_pcm_runtime *fe = substream->private_data;
1776 int ret, stream = substream->stream;
1778 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1779 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1781 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1782 sizeof(struct snd_pcm_hw_params));
1783 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1785 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1789 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1790 fe->dai_link->name, params_rate(params),
1791 params_channels(params), params_format(params));
1793 /* call hw_params on the frontend */
1794 ret = soc_pcm_hw_params(substream, params);
1796 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1797 dpcm_be_dai_hw_free(fe, stream);
1799 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1802 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1803 mutex_unlock(&fe->card->mutex);
1807 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1808 struct snd_pcm_substream *substream, int cmd)
1812 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1813 dpcm->fe->dai_link->name, cmd);
1815 ret = soc_pcm_trigger(substream, cmd);
1817 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1822 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1825 struct snd_soc_dpcm *dpcm;
1828 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1830 struct snd_soc_pcm_runtime *be = dpcm->be;
1831 struct snd_pcm_substream *be_substream =
1832 snd_soc_dpcm_get_substream(be, stream);
1834 /* is this op for this BE ? */
1835 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1839 case SNDRV_PCM_TRIGGER_START:
1840 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1841 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1844 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1848 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1850 case SNDRV_PCM_TRIGGER_RESUME:
1851 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1854 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1858 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1860 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1861 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1864 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1868 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1870 case SNDRV_PCM_TRIGGER_STOP:
1871 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1874 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1877 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1881 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1883 case SNDRV_PCM_TRIGGER_SUSPEND:
1884 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1887 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1890 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1894 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1896 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1897 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1900 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1903 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1907 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1914 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1916 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1918 struct snd_soc_pcm_runtime *fe = substream->private_data;
1919 int stream = substream->stream, ret;
1920 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1922 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1925 case SND_SOC_DPCM_TRIGGER_PRE:
1926 /* call trigger on the frontend before the backend. */
1928 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1929 fe->dai_link->name, cmd);
1931 ret = soc_pcm_trigger(substream, cmd);
1933 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1937 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1939 case SND_SOC_DPCM_TRIGGER_POST:
1940 /* call trigger on the frontend after the backend. */
1942 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1944 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1948 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1949 fe->dai_link->name, cmd);
1951 ret = soc_pcm_trigger(substream, cmd);
1953 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1954 /* bespoke trigger() - handles both FE and BEs */
1956 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1957 fe->dai_link->name, cmd);
1959 ret = soc_pcm_bespoke_trigger(substream, cmd);
1961 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1966 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1967 fe->dai_link->name);
1973 case SNDRV_PCM_TRIGGER_START:
1974 case SNDRV_PCM_TRIGGER_RESUME:
1975 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1976 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1978 case SNDRV_PCM_TRIGGER_STOP:
1979 case SNDRV_PCM_TRIGGER_SUSPEND:
1980 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1981 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1986 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1990 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1992 struct snd_soc_dpcm *dpcm;
1995 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1997 struct snd_soc_pcm_runtime *be = dpcm->be;
1998 struct snd_pcm_substream *be_substream =
1999 snd_soc_dpcm_get_substream(be, stream);
2001 /* is this op for this BE ? */
2002 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2005 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2006 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2009 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2010 dpcm->fe->dai_link->name);
2012 ret = soc_pcm_prepare(be_substream);
2014 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2019 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2024 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2026 struct snd_soc_pcm_runtime *fe = substream->private_data;
2027 int stream = substream->stream, ret = 0;
2029 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2031 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2033 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2035 /* there is no point preparing this FE if there are no BEs */
2036 if (list_empty(&fe->dpcm[stream].be_clients)) {
2037 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2038 fe->dai_link->name);
2043 ret = dpcm_be_dai_prepare(fe, substream->stream);
2047 /* call prepare on the frontend */
2048 ret = soc_pcm_prepare(substream);
2050 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2051 fe->dai_link->name);
2055 /* run the stream event for each BE */
2056 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2057 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2060 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2061 mutex_unlock(&fe->card->mutex);
2066 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2067 unsigned int cmd, void *arg)
2069 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2070 struct snd_soc_platform *platform = rtd->platform;
2072 if (platform->driver->ops && platform->driver->ops->ioctl)
2073 return platform->driver->ops->ioctl(substream, cmd, arg);
2074 return snd_pcm_lib_ioctl(substream, cmd, arg);
2077 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2079 struct snd_pcm_substream *substream =
2080 snd_soc_dpcm_get_substream(fe, stream);
2081 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2084 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2085 stream ? "capture" : "playback", fe->dai_link->name);
2087 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2088 /* call bespoke trigger - FE takes care of all BE triggers */
2089 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2090 fe->dai_link->name);
2092 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2094 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2096 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2097 fe->dai_link->name);
2099 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2101 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2104 err = dpcm_be_dai_hw_free(fe, stream);
2106 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2108 err = dpcm_be_dai_shutdown(fe, stream);
2110 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2112 /* run the stream event for each BE */
2113 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2118 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2120 struct snd_pcm_substream *substream =
2121 snd_soc_dpcm_get_substream(fe, stream);
2122 struct snd_soc_dpcm *dpcm;
2123 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2126 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2127 stream ? "capture" : "playback", fe->dai_link->name);
2129 /* Only start the BE if the FE is ready */
2130 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2131 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2134 /* startup must always be called for new BEs */
2135 ret = dpcm_be_dai_startup(fe, stream);
2139 /* keep going if FE state is > open */
2140 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2143 ret = dpcm_be_dai_hw_params(fe, stream);
2147 /* keep going if FE state is > hw_params */
2148 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2152 ret = dpcm_be_dai_prepare(fe, stream);
2156 /* run the stream event for each BE */
2157 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2159 /* keep going if FE state is > prepare */
2160 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2161 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2164 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2165 /* call trigger on the frontend - FE takes care of all BE triggers */
2166 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2167 fe->dai_link->name);
2169 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2171 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2175 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2176 fe->dai_link->name);
2178 ret = dpcm_be_dai_trigger(fe, stream,
2179 SNDRV_PCM_TRIGGER_START);
2181 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2189 dpcm_be_dai_hw_free(fe, stream);
2191 dpcm_be_dai_shutdown(fe, stream);
2193 /* disconnect any non started BEs */
2194 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2195 struct snd_soc_pcm_runtime *be = dpcm->be;
2196 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2197 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2203 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2207 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2208 ret = dpcm_run_update_startup(fe, stream);
2210 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2211 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2216 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2220 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2221 ret = dpcm_run_update_shutdown(fe, stream);
2223 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2224 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2229 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2232 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2234 int i, old, new, paths;
2236 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2237 for (i = 0; i < card->num_rtd; i++) {
2238 struct snd_soc_dapm_widget_list *list;
2239 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2241 /* make sure link is FE */
2242 if (!fe->dai_link->dynamic)
2245 /* only check active links */
2246 if (!fe->cpu_dai->active)
2249 /* DAPM sync will call this to update DSP paths */
2250 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2251 fe->dai_link->name);
2253 /* skip if FE doesn't have playback capability */
2254 if (!fe->cpu_dai->driver->playback.channels_min)
2257 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2259 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2260 fe->dai_link->name, "playback");
2261 mutex_unlock(&card->mutex);
2265 /* update any new playback paths */
2266 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2268 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2269 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2270 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2273 /* update any old playback paths */
2274 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2276 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2277 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2278 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2281 dpcm_path_put(&list);
2283 /* skip if FE doesn't have capture capability */
2284 if (!fe->cpu_dai->driver->capture.channels_min)
2287 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2289 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2290 fe->dai_link->name, "capture");
2291 mutex_unlock(&card->mutex);
2295 /* update any new capture paths */
2296 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2298 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2299 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2300 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2303 /* update any old capture paths */
2304 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2306 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2307 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2308 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2311 dpcm_path_put(&list);
2314 mutex_unlock(&card->mutex);
2317 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2319 struct snd_soc_dpcm *dpcm;
2320 struct list_head *clients =
2321 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2323 list_for_each_entry(dpcm, clients, list_be) {
2325 struct snd_soc_pcm_runtime *be = dpcm->be;
2328 if (be->dai_link->ignore_suspend)
2331 for (i = 0; i < be->num_codecs; i++) {
2332 struct snd_soc_dai *dai = be->codec_dais[i];
2333 struct snd_soc_dai_driver *drv = dai->driver;
2335 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2336 be->dai_link->name);
2338 if (drv->ops && drv->ops->digital_mute &&
2339 dai->playback_active)
2340 drv->ops->digital_mute(dai, mute);
2347 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2349 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2350 struct snd_soc_dpcm *dpcm;
2351 struct snd_soc_dapm_widget_list *list;
2353 int stream = fe_substream->stream;
2355 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2356 fe->dpcm[stream].runtime = fe_substream->runtime;
2358 ret = dpcm_path_get(fe, stream, &list);
2360 mutex_unlock(&fe->card->mutex);
2362 } else if (ret == 0) {
2363 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2364 fe->dai_link->name, stream ? "capture" : "playback");
2367 /* calculate valid and active FE <-> BE dpcms */
2368 dpcm_process_paths(fe, stream, &list, 1);
2370 ret = dpcm_fe_dai_startup(fe_substream);
2372 /* clean up all links */
2373 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2374 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2376 dpcm_be_disconnect(fe, stream);
2377 fe->dpcm[stream].runtime = NULL;
2380 dpcm_clear_pending_state(fe, stream);
2381 dpcm_path_put(&list);
2382 mutex_unlock(&fe->card->mutex);
2386 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2388 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2389 struct snd_soc_dpcm *dpcm;
2390 int stream = fe_substream->stream, ret;
2392 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2393 ret = dpcm_fe_dai_shutdown(fe_substream);
2395 /* mark FE's links ready to prune */
2396 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2397 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2399 dpcm_be_disconnect(fe, stream);
2401 fe->dpcm[stream].runtime = NULL;
2402 mutex_unlock(&fe->card->mutex);
2406 /* create a new pcm */
2407 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2409 struct snd_soc_platform *platform = rtd->platform;
2410 struct snd_soc_dai *codec_dai;
2411 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2412 struct snd_pcm *pcm;
2414 int ret = 0, playback = 0, capture = 0;
2417 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2418 playback = rtd->dai_link->dpcm_playback;
2419 capture = rtd->dai_link->dpcm_capture;
2421 for (i = 0; i < rtd->num_codecs; i++) {
2422 codec_dai = rtd->codec_dais[i];
2423 if (codec_dai->driver->playback.channels_min)
2425 if (codec_dai->driver->capture.channels_min)
2429 capture = capture && cpu_dai->driver->capture.channels_min;
2430 playback = playback && cpu_dai->driver->playback.channels_min;
2433 if (rtd->dai_link->playback_only) {
2438 if (rtd->dai_link->capture_only) {
2443 /* create the PCM */
2444 if (rtd->dai_link->no_pcm) {
2445 snprintf(new_name, sizeof(new_name), "(%s)",
2446 rtd->dai_link->stream_name);
2448 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2449 playback, capture, &pcm);
2451 if (rtd->dai_link->dynamic)
2452 snprintf(new_name, sizeof(new_name), "%s (*)",
2453 rtd->dai_link->stream_name);
2455 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2456 rtd->dai_link->stream_name,
2457 (rtd->num_codecs > 1) ?
2458 "multicodec" : rtd->codec_dai->name, num);
2460 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2464 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2465 rtd->dai_link->name);
2468 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2470 /* DAPM dai link stream work */
2471 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2474 pcm->private_data = rtd;
2476 if (rtd->dai_link->no_pcm) {
2478 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2480 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2484 /* ASoC PCM operations */
2485 if (rtd->dai_link->dynamic) {
2486 rtd->ops.open = dpcm_fe_dai_open;
2487 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2488 rtd->ops.prepare = dpcm_fe_dai_prepare;
2489 rtd->ops.trigger = dpcm_fe_dai_trigger;
2490 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2491 rtd->ops.close = dpcm_fe_dai_close;
2492 rtd->ops.pointer = soc_pcm_pointer;
2493 rtd->ops.ioctl = soc_pcm_ioctl;
2495 rtd->ops.open = soc_pcm_open;
2496 rtd->ops.hw_params = soc_pcm_hw_params;
2497 rtd->ops.prepare = soc_pcm_prepare;
2498 rtd->ops.trigger = soc_pcm_trigger;
2499 rtd->ops.hw_free = soc_pcm_hw_free;
2500 rtd->ops.close = soc_pcm_close;
2501 rtd->ops.pointer = soc_pcm_pointer;
2502 rtd->ops.ioctl = soc_pcm_ioctl;
2505 if (platform->driver->ops) {
2506 rtd->ops.ack = platform->driver->ops->ack;
2507 rtd->ops.copy = platform->driver->ops->copy;
2508 rtd->ops.silence = platform->driver->ops->silence;
2509 rtd->ops.page = platform->driver->ops->page;
2510 rtd->ops.mmap = platform->driver->ops->mmap;
2514 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2517 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2519 if (platform->driver->pcm_new) {
2520 ret = platform->driver->pcm_new(rtd);
2522 dev_err(platform->dev,
2523 "ASoC: pcm constructor failed: %d\n",
2529 pcm->private_free = platform->driver->pcm_free;
2531 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2532 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2537 /* is the current PCM operation for this FE ? */
2538 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2540 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2544 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2546 /* is the current PCM operation for this BE ? */
2547 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2548 struct snd_soc_pcm_runtime *be, int stream)
2550 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2551 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2552 be->dpcm[stream].runtime_update))
2556 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2558 /* get the substream for this BE */
2559 struct snd_pcm_substream *
2560 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2562 return be->pcm->streams[stream].substream;
2564 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2566 /* get the BE runtime state */
2567 enum snd_soc_dpcm_state
2568 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2570 return be->dpcm[stream].state;
2572 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2574 /* set the BE runtime state */
2575 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2576 int stream, enum snd_soc_dpcm_state state)
2578 be->dpcm[stream].state = state;
2580 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2583 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2584 * are not running, paused or suspended for the specified stream direction.
2586 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2587 struct snd_soc_pcm_runtime *be, int stream)
2589 struct snd_soc_dpcm *dpcm;
2592 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2597 state = dpcm->fe->dpcm[stream].state;
2598 if (state == SND_SOC_DPCM_STATE_START ||
2599 state == SND_SOC_DPCM_STATE_PAUSED ||
2600 state == SND_SOC_DPCM_STATE_SUSPEND)
2604 /* it's safe to free/stop this BE DAI */
2607 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2610 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2611 * running, paused or suspended for the specified stream direction.
2613 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2614 struct snd_soc_pcm_runtime *be, int stream)
2616 struct snd_soc_dpcm *dpcm;
2619 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2624 state = dpcm->fe->dpcm[stream].state;
2625 if (state == SND_SOC_DPCM_STATE_START ||
2626 state == SND_SOC_DPCM_STATE_PAUSED ||
2627 state == SND_SOC_DPCM_STATE_SUSPEND ||
2628 state == SND_SOC_DPCM_STATE_PREPARE)
2632 /* it's safe to change hw_params */
2635 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2637 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2638 int cmd, struct snd_soc_platform *platform)
2640 if (platform->driver->ops && platform->driver->ops->trigger)
2641 return platform->driver->ops->trigger(substream, cmd);
2644 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2646 #ifdef CONFIG_DEBUG_FS
2647 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2650 case SND_SOC_DPCM_STATE_NEW:
2652 case SND_SOC_DPCM_STATE_OPEN:
2654 case SND_SOC_DPCM_STATE_HW_PARAMS:
2656 case SND_SOC_DPCM_STATE_PREPARE:
2658 case SND_SOC_DPCM_STATE_START:
2660 case SND_SOC_DPCM_STATE_STOP:
2662 case SND_SOC_DPCM_STATE_SUSPEND:
2664 case SND_SOC_DPCM_STATE_PAUSED:
2666 case SND_SOC_DPCM_STATE_HW_FREE:
2668 case SND_SOC_DPCM_STATE_CLOSE:
2675 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2676 int stream, char *buf, size_t size)
2678 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2679 struct snd_soc_dpcm *dpcm;
2683 offset += snprintf(buf + offset, size - offset,
2684 "[%s - %s]\n", fe->dai_link->name,
2685 stream ? "Capture" : "Playback");
2687 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2688 dpcm_state_string(fe->dpcm[stream].state));
2690 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2691 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2692 offset += snprintf(buf + offset, size - offset,
2694 "Format = %s, Channels = %d, Rate = %d\n",
2695 snd_pcm_format_name(params_format(params)),
2696 params_channels(params),
2697 params_rate(params));
2700 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2702 if (list_empty(&fe->dpcm[stream].be_clients)) {
2703 offset += snprintf(buf + offset, size - offset,
2704 " No active DSP links\n");
2708 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2709 struct snd_soc_pcm_runtime *be = dpcm->be;
2710 params = &dpcm->hw_params;
2712 offset += snprintf(buf + offset, size - offset,
2713 "- %s\n", be->dai_link->name);
2715 offset += snprintf(buf + offset, size - offset,
2717 dpcm_state_string(be->dpcm[stream].state));
2719 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2720 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2721 offset += snprintf(buf + offset, size - offset,
2722 " Hardware Params: "
2723 "Format = %s, Channels = %d, Rate = %d\n",
2724 snd_pcm_format_name(params_format(params)),
2725 params_channels(params),
2726 params_rate(params));
2733 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2734 size_t count, loff_t *ppos)
2736 struct snd_soc_pcm_runtime *fe = file->private_data;
2737 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2740 buf = kmalloc(out_count, GFP_KERNEL);
2744 if (fe->cpu_dai->driver->playback.channels_min)
2745 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2746 buf + offset, out_count - offset);
2748 if (fe->cpu_dai->driver->capture.channels_min)
2749 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2750 buf + offset, out_count - offset);
2752 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2758 static const struct file_operations dpcm_state_fops = {
2759 .open = simple_open,
2760 .read = dpcm_state_read_file,
2761 .llseek = default_llseek,
2764 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2769 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2770 rtd->card->debugfs_card_root);
2771 if (!rtd->debugfs_dpcm_root) {
2773 "ASoC: Failed to create dpcm debugfs directory %s\n",
2774 rtd->dai_link->name);
2778 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2779 rtd->debugfs_dpcm_root,
2780 rtd, &dpcm_state_fops);