1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-pcm.c -- ALSA SoC PCM
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Authors: Liam Girdwood <lrg@ti.com>
11 // Mark Brown <broonie@opensource.wolfsonmicro.com>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/soc-link.h>
28 #include <sound/initval.h>
30 #define DPCM_MAX_BE_USERS 8
32 #ifdef CONFIG_DEBUG_FS
33 static const char *dpcm_state_string(enum snd_soc_dpcm_state state
)
36 case SND_SOC_DPCM_STATE_NEW
:
38 case SND_SOC_DPCM_STATE_OPEN
:
40 case SND_SOC_DPCM_STATE_HW_PARAMS
:
42 case SND_SOC_DPCM_STATE_PREPARE
:
44 case SND_SOC_DPCM_STATE_START
:
46 case SND_SOC_DPCM_STATE_STOP
:
48 case SND_SOC_DPCM_STATE_SUSPEND
:
50 case SND_SOC_DPCM_STATE_PAUSED
:
52 case SND_SOC_DPCM_STATE_HW_FREE
:
54 case SND_SOC_DPCM_STATE_CLOSE
:
61 static ssize_t
dpcm_show_state(struct snd_soc_pcm_runtime
*fe
,
62 int stream
, char *buf
, size_t size
)
64 struct snd_pcm_hw_params
*params
= &fe
->dpcm
[stream
].hw_params
;
65 struct snd_soc_dpcm
*dpcm
;
70 offset
+= scnprintf(buf
+ offset
, size
- offset
,
71 "[%s - %s]\n", fe
->dai_link
->name
,
72 stream
? "Capture" : "Playback");
74 offset
+= scnprintf(buf
+ offset
, size
- offset
, "State: %s\n",
75 dpcm_state_string(fe
->dpcm
[stream
].state
));
77 if ((fe
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
78 (fe
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
79 offset
+= scnprintf(buf
+ offset
, size
- offset
,
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params
)),
83 params_channels(params
),
87 offset
+= scnprintf(buf
+ offset
, size
- offset
, "Backends:\n");
89 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
90 offset
+= scnprintf(buf
+ offset
, size
- offset
,
91 " No active DSP links\n");
95 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
96 for_each_dpcm_be(fe
, stream
, dpcm
) {
97 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
98 params
= &dpcm
->hw_params
;
100 offset
+= scnprintf(buf
+ offset
, size
- offset
,
101 "- %s\n", be
->dai_link
->name
);
103 offset
+= scnprintf(buf
+ offset
, size
- offset
,
105 dpcm_state_string(be
->dpcm
[stream
].state
));
107 if ((be
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
108 (be
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
109 offset
+= scnprintf(buf
+ offset
, size
- offset
,
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params
)),
113 params_channels(params
),
114 params_rate(params
));
116 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
121 static ssize_t
dpcm_state_read_file(struct file
*file
, char __user
*user_buf
,
122 size_t count
, loff_t
*ppos
)
124 struct snd_soc_pcm_runtime
*fe
= file
->private_data
;
125 ssize_t out_count
= PAGE_SIZE
, offset
= 0, ret
= 0;
129 if (fe
->num_cpus
> 1) {
131 "%s doesn't support Multi CPU yet\n", __func__
);
135 buf
= kmalloc(out_count
, GFP_KERNEL
);
139 for_each_pcm_streams(stream
)
140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe
, 0), stream
))
141 offset
+= dpcm_show_state(fe
, stream
,
145 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, offset
);
151 static const struct file_operations dpcm_state_fops
= {
153 .read
= dpcm_state_read_file
,
154 .llseek
= default_llseek
,
157 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime
*rtd
)
162 if (!rtd
->dai_link
->dynamic
)
165 if (!rtd
->card
->debugfs_card_root
)
168 rtd
->debugfs_dpcm_root
= debugfs_create_dir(rtd
->dai_link
->name
,
169 rtd
->card
->debugfs_card_root
);
171 debugfs_create_file("state", 0444, rtd
->debugfs_dpcm_root
,
172 rtd
, &dpcm_state_fops
);
175 static void dpcm_create_debugfs_state(struct snd_soc_dpcm
*dpcm
, int stream
)
179 name
= kasprintf(GFP_KERNEL
, "%s:%s", dpcm
->be
->dai_link
->name
,
180 stream
? "capture" : "playback");
182 dpcm
->debugfs_state
= debugfs_create_dir(
183 name
, dpcm
->fe
->debugfs_dpcm_root
);
184 debugfs_create_u32("state", 0644, dpcm
->debugfs_state
,
190 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm
*dpcm
)
192 debugfs_remove_recursive(dpcm
->debugfs_state
);
196 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm
*dpcm
,
201 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm
*dpcm
)
207 * snd_soc_runtime_action() - Increment/Decrement active count for
208 * PCM runtime components
209 * @rtd: ASoC PCM runtime that is activated
210 * @stream: Direction of the PCM stream
211 * @action: Activate stream if 1. Deactivate if -1.
213 * Increments/Decrements the active count for all the DAIs and components
214 * attached to a PCM runtime.
215 * Should typically be called when a stream is opened.
217 * Must be called with the rtd->card->pcm_mutex being held
219 void snd_soc_runtime_action(struct snd_soc_pcm_runtime
*rtd
,
220 int stream
, int action
)
222 struct snd_soc_dai
*dai
;
225 lockdep_assert_held(&rtd
->card
->pcm_mutex
);
227 for_each_rtd_dais(rtd
, i
, dai
)
228 snd_soc_dai_action(dai
, stream
, action
);
230 EXPORT_SYMBOL_GPL(snd_soc_runtime_action
);
233 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
234 * @rtd: The ASoC PCM runtime that should be checked.
236 * This function checks whether the power down delay should be ignored for a
237 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
238 * been configured to ignore the delay, or if none of the components benefits
239 * from having the delay.
241 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime
*rtd
)
243 struct snd_soc_component
*component
;
247 if (!rtd
->pmdown_time
|| rtd
->dai_link
->ignore_pmdown_time
)
250 for_each_rtd_components(rtd
, i
, component
)
251 ignore
&= !component
->driver
->use_pmdown_time
;
257 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
258 * @substream: the pcm substream
259 * @hw: the hardware parameters
261 * Sets the substream runtime hardware parameters.
263 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
264 const struct snd_pcm_hardware
*hw
)
266 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
267 runtime
->hw
.info
= hw
->info
;
268 runtime
->hw
.formats
= hw
->formats
;
269 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
270 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
271 runtime
->hw
.periods_min
= hw
->periods_min
;
272 runtime
->hw
.periods_max
= hw
->periods_max
;
273 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
274 runtime
->hw
.fifo_size
= hw
->fifo_size
;
277 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
279 /* DPCM stream event, send event to FE and all active BEs. */
280 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime
*fe
, int dir
,
283 struct snd_soc_dpcm
*dpcm
;
285 for_each_dpcm_be(fe
, dir
, dpcm
) {
287 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
289 dev_dbg(be
->dev
, "ASoC: BE %s event %d dir %d\n",
290 be
->dai_link
->name
, event
, dir
);
292 if ((event
== SND_SOC_DAPM_STREAM_STOP
) &&
293 (be
->dpcm
[dir
].users
>= 1))
296 snd_soc_dapm_stream_event(be
, dir
, event
);
299 snd_soc_dapm_stream_event(fe
, dir
, event
);
304 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
,
305 struct snd_soc_dai
*soc_dai
)
307 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
310 if (soc_dai
->rate
&& (soc_dai
->driver
->symmetric_rates
||
311 rtd
->dai_link
->symmetric_rates
)) {
312 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %dHz rate\n",
315 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
316 SNDRV_PCM_HW_PARAM_RATE
,
319 dev_err(soc_dai
->dev
,
320 "ASoC: Unable to apply rate constraint: %d\n",
326 if (soc_dai
->channels
&& (soc_dai
->driver
->symmetric_channels
||
327 rtd
->dai_link
->symmetric_channels
)) {
328 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d channel(s)\n",
331 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
332 SNDRV_PCM_HW_PARAM_CHANNELS
,
335 dev_err(soc_dai
->dev
,
336 "ASoC: Unable to apply channel symmetry constraint: %d\n",
342 if (soc_dai
->sample_bits
&& (soc_dai
->driver
->symmetric_samplebits
||
343 rtd
->dai_link
->symmetric_samplebits
)) {
344 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d sample bits\n",
345 soc_dai
->sample_bits
);
347 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
348 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
349 soc_dai
->sample_bits
);
351 dev_err(soc_dai
->dev
,
352 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
361 static int soc_pcm_params_symmetry(struct snd_pcm_substream
*substream
,
362 struct snd_pcm_hw_params
*params
)
364 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
365 struct snd_soc_dai
*dai
;
366 struct snd_soc_dai
*cpu_dai
;
367 unsigned int rate
, channels
, sample_bits
, symmetry
, i
;
369 rate
= params_rate(params
);
370 channels
= params_channels(params
);
371 sample_bits
= snd_pcm_format_physical_width(params_format(params
));
373 /* reject unmatched parameters when applying symmetry */
374 symmetry
= rtd
->dai_link
->symmetric_rates
;
376 for_each_rtd_cpu_dais(rtd
, i
, dai
)
377 symmetry
|= dai
->driver
->symmetric_rates
;
380 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
381 if (cpu_dai
->rate
&& cpu_dai
->rate
!= rate
) {
382 dev_err(rtd
->dev
, "ASoC: unmatched rate symmetry: %d - %d\n",
383 cpu_dai
->rate
, rate
);
389 symmetry
= rtd
->dai_link
->symmetric_channels
;
391 for_each_rtd_dais(rtd
, i
, dai
)
392 symmetry
|= dai
->driver
->symmetric_channels
;
395 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
396 if (cpu_dai
->channels
&&
397 cpu_dai
->channels
!= channels
) {
398 dev_err(rtd
->dev
, "ASoC: unmatched channel symmetry: %d - %d\n",
399 cpu_dai
->channels
, channels
);
405 symmetry
= rtd
->dai_link
->symmetric_samplebits
;
407 for_each_rtd_dais(rtd
, i
, dai
)
408 symmetry
|= dai
->driver
->symmetric_samplebits
;
411 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
412 if (cpu_dai
->sample_bits
&&
413 cpu_dai
->sample_bits
!= sample_bits
) {
414 dev_err(rtd
->dev
, "ASoC: unmatched sample bits symmetry: %d - %d\n",
415 cpu_dai
->sample_bits
, sample_bits
);
424 static bool soc_pcm_has_symmetry(struct snd_pcm_substream
*substream
)
426 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
427 struct snd_soc_dai_link
*link
= rtd
->dai_link
;
428 struct snd_soc_dai
*dai
;
429 unsigned int symmetry
, i
;
431 symmetry
= link
->symmetric_rates
||
432 link
->symmetric_channels
||
433 link
->symmetric_samplebits
;
435 for_each_rtd_dais(rtd
, i
, dai
)
436 symmetry
= symmetry
||
437 dai
->driver
->symmetric_rates
||
438 dai
->driver
->symmetric_channels
||
439 dai
->driver
->symmetric_samplebits
;
444 static void soc_pcm_set_msb(struct snd_pcm_substream
*substream
, int bits
)
446 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
452 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0, 0, bits
);
454 dev_warn(rtd
->dev
, "ASoC: Failed to set MSB %d: %d\n",
458 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
)
460 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
461 struct snd_soc_dai
*cpu_dai
;
462 struct snd_soc_dai
*codec_dai
;
463 struct snd_soc_pcm_stream
*pcm_codec
, *pcm_cpu
;
464 int stream
= substream
->stream
;
466 unsigned int bits
= 0, cpu_bits
= 0;
468 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
469 pcm_codec
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
471 if (pcm_codec
->sig_bits
== 0) {
475 bits
= max(pcm_codec
->sig_bits
, bits
);
478 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
479 pcm_cpu
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
481 if (pcm_cpu
->sig_bits
== 0) {
485 cpu_bits
= max(pcm_cpu
->sig_bits
, cpu_bits
);
488 soc_pcm_set_msb(substream
, bits
);
489 soc_pcm_set_msb(substream
, cpu_bits
);
493 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
494 * @rtd: ASoC PCM runtime
495 * @hw: PCM hardware parameters (output)
496 * @stream: Direction of the PCM stream
498 * Calculates the subset of stream parameters supported by all DAIs
499 * associated with the PCM stream.
501 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime
*rtd
,
502 struct snd_pcm_hardware
*hw
, int stream
)
504 struct snd_soc_dai
*codec_dai
;
505 struct snd_soc_dai
*cpu_dai
;
506 struct snd_soc_pcm_stream
*codec_stream
;
507 struct snd_soc_pcm_stream
*cpu_stream
;
508 unsigned int chan_min
= 0, chan_max
= UINT_MAX
;
509 unsigned int cpu_chan_min
= 0, cpu_chan_max
= UINT_MAX
;
510 unsigned int rate_min
= 0, rate_max
= UINT_MAX
;
511 unsigned int cpu_rate_min
= 0, cpu_rate_max
= UINT_MAX
;
512 unsigned int rates
= UINT_MAX
, cpu_rates
= UINT_MAX
;
513 u64 formats
= ULLONG_MAX
;
516 /* first calculate min/max only for CPUs in the DAI link */
517 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
520 * Skip CPUs which don't support the current stream type.
521 * Otherwise, since the rate, channel, and format values will
522 * zero in that case, we would have no usable settings left,
523 * causing the resulting setup to fail.
525 if (!snd_soc_dai_stream_valid(cpu_dai
, stream
))
528 cpu_stream
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
530 cpu_chan_min
= max(cpu_chan_min
, cpu_stream
->channels_min
);
531 cpu_chan_max
= min(cpu_chan_max
, cpu_stream
->channels_max
);
532 cpu_rate_min
= max(cpu_rate_min
, cpu_stream
->rate_min
);
533 cpu_rate_max
= min_not_zero(cpu_rate_max
, cpu_stream
->rate_max
);
534 formats
&= cpu_stream
->formats
;
535 cpu_rates
= snd_pcm_rate_mask_intersect(cpu_stream
->rates
,
539 /* second calculate min/max only for CODECs in the DAI link */
540 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
543 * Skip CODECs which don't support the current stream type.
544 * Otherwise, since the rate, channel, and format values will
545 * zero in that case, we would have no usable settings left,
546 * causing the resulting setup to fail.
548 if (!snd_soc_dai_stream_valid(codec_dai
, stream
))
551 codec_stream
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
553 chan_min
= max(chan_min
, codec_stream
->channels_min
);
554 chan_max
= min(chan_max
, codec_stream
->channels_max
);
555 rate_min
= max(rate_min
, codec_stream
->rate_min
);
556 rate_max
= min_not_zero(rate_max
, codec_stream
->rate_max
);
557 formats
&= codec_stream
->formats
;
558 rates
= snd_pcm_rate_mask_intersect(codec_stream
->rates
, rates
);
561 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
562 if (!chan_min
|| !cpu_chan_min
)
566 * chan min/max cannot be enforced if there are multiple CODEC DAIs
567 * connected to CPU DAI(s), use CPU DAI's directly and let
568 * channel allocation be fixed up later
570 if (rtd
->num_codecs
> 1) {
571 chan_min
= cpu_chan_min
;
572 chan_max
= cpu_chan_max
;
575 /* finally find a intersection between CODECs and CPUs */
576 hw
->channels_min
= max(chan_min
, cpu_chan_min
);
577 hw
->channels_max
= min(chan_max
, cpu_chan_max
);
578 hw
->formats
= formats
;
579 hw
->rates
= snd_pcm_rate_mask_intersect(rates
, cpu_rates
);
581 snd_pcm_hw_limit_rates(hw
);
583 hw
->rate_min
= max(hw
->rate_min
, cpu_rate_min
);
584 hw
->rate_min
= max(hw
->rate_min
, rate_min
);
585 hw
->rate_max
= min_not_zero(hw
->rate_max
, cpu_rate_max
);
586 hw
->rate_max
= min_not_zero(hw
->rate_max
, rate_max
);
590 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw
);
592 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream
*substream
)
594 struct snd_pcm_hardware
*hw
= &substream
->runtime
->hw
;
595 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
596 u64 formats
= hw
->formats
;
599 * At least one CPU and one CODEC should match. Otherwise, we should
600 * have bailed out on a higher level, since there would be no CPU or
601 * CODEC to support the transfer direction in that case.
603 snd_soc_runtime_calc_hw(rtd
, hw
, substream
->stream
);
606 hw
->formats
&= formats
;
609 static int soc_pcm_components_open(struct snd_pcm_substream
*substream
)
611 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
612 struct snd_soc_component
*component
;
615 for_each_rtd_components(rtd
, i
, component
) {
616 ret
= snd_soc_component_module_get_when_open(component
, substream
);
620 ret
= snd_soc_component_open(component
, substream
);
628 static int soc_pcm_components_close(struct snd_pcm_substream
*substream
,
631 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
632 struct snd_soc_component
*component
;
635 for_each_rtd_components(rtd
, i
, component
) {
636 r
= snd_soc_component_close(component
, substream
, rollback
);
638 ret
= r
; /* use last ret */
640 snd_soc_component_module_put_when_close(component
, substream
, rollback
);
646 static int soc_pcm_clean(struct snd_pcm_substream
*substream
, int rollback
)
648 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
649 struct snd_soc_component
*component
;
650 struct snd_soc_dai
*dai
;
653 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
656 snd_soc_runtime_deactivate(rtd
, substream
->stream
);
658 for_each_rtd_dais(rtd
, i
, dai
)
659 snd_soc_dai_shutdown(dai
, substream
, rollback
);
661 snd_soc_link_shutdown(substream
, rollback
);
663 soc_pcm_components_close(substream
, rollback
);
666 mutex_unlock(&rtd
->card
->pcm_mutex
);
668 snd_soc_pcm_component_pm_runtime_put(rtd
, substream
, rollback
);
670 for_each_rtd_components(rtd
, i
, component
)
671 if (!snd_soc_component_active(component
))
672 pinctrl_pm_select_sleep_state(component
->dev
);
678 * Called by ALSA when a PCM substream is closed. Private data can be
679 * freed here. The cpu DAI, codec DAI, machine and components are also
682 static int soc_pcm_close(struct snd_pcm_substream
*substream
)
684 return soc_pcm_clean(substream
, 0);
688 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
689 * then initialized and any private data can be allocated. This also calls
690 * startup for the cpu DAI, component, machine and codec DAI.
692 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
694 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
695 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
696 struct snd_soc_component
*component
;
697 struct snd_soc_dai
*dai
;
698 const char *codec_dai_name
= "multicodec";
699 const char *cpu_dai_name
= "multicpu";
702 for_each_rtd_components(rtd
, i
, component
)
703 pinctrl_pm_select_default_state(component
->dev
);
705 ret
= snd_soc_pcm_component_pm_runtime_get(rtd
, substream
);
709 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
711 ret
= soc_pcm_components_open(substream
);
715 ret
= snd_soc_link_startup(substream
);
719 /* startup the audio subsystem */
720 for_each_rtd_dais(rtd
, i
, dai
) {
721 ret
= snd_soc_dai_startup(dai
, substream
);
725 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
731 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
732 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
735 /* Check that the codec and cpu DAIs are compatible */
736 soc_pcm_init_runtime_hw(substream
);
738 if (rtd
->num_codecs
== 1)
739 codec_dai_name
= asoc_rtd_to_codec(rtd
, 0)->name
;
741 if (rtd
->num_cpus
== 1)
742 cpu_dai_name
= asoc_rtd_to_cpu(rtd
, 0)->name
;
744 if (soc_pcm_has_symmetry(substream
))
745 runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
748 if (!runtime
->hw
.rates
) {
749 printk(KERN_ERR
"ASoC: %s <-> %s No matching rates\n",
750 codec_dai_name
, cpu_dai_name
);
753 if (!runtime
->hw
.formats
) {
754 printk(KERN_ERR
"ASoC: %s <-> %s No matching formats\n",
755 codec_dai_name
, cpu_dai_name
);
758 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
||
759 runtime
->hw
.channels_min
> runtime
->hw
.channels_max
) {
760 printk(KERN_ERR
"ASoC: %s <-> %s No matching channels\n",
761 codec_dai_name
, cpu_dai_name
);
765 soc_pcm_apply_msb(substream
);
767 /* Symmetry only applies if we've already got an active stream. */
768 for_each_rtd_dais(rtd
, i
, dai
) {
769 if (snd_soc_dai_active(dai
)) {
770 ret
= soc_pcm_apply_symmetry(substream
, dai
);
776 pr_debug("ASoC: %s <-> %s info:\n",
777 codec_dai_name
, cpu_dai_name
);
778 pr_debug("ASoC: rate mask 0x%x\n", runtime
->hw
.rates
);
779 pr_debug("ASoC: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
780 runtime
->hw
.channels_max
);
781 pr_debug("ASoC: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
782 runtime
->hw
.rate_max
);
784 snd_soc_runtime_activate(rtd
, substream
->stream
);
787 mutex_unlock(&rtd
->card
->pcm_mutex
);
790 soc_pcm_clean(substream
, 1);
795 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime
*rtd
)
798 * Currently nothing to do for c2c links
799 * Since c2c links are internal nodes in the DAPM graph and
800 * don't interface with the outside world or application layer
801 * we don't have to do any special handling on close.
806 * Called by ALSA when the PCM substream is prepared, can set format, sample
807 * rate, etc. This function is non atomic and can be called multiple times,
808 * it can refer to the runtime info.
810 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
812 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
813 struct snd_soc_dai
*dai
;
816 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
818 ret
= snd_soc_link_prepare(substream
);
822 ret
= snd_soc_pcm_component_prepare(substream
);
826 ret
= snd_soc_pcm_dai_prepare(substream
);
828 dev_err(rtd
->dev
, "ASoC: DAI prepare error: %d\n", ret
);
832 /* cancel any delayed stream shutdown that is pending */
833 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
836 cancel_delayed_work(&rtd
->delayed_work
);
839 snd_soc_dapm_stream_event(rtd
, substream
->stream
,
840 SND_SOC_DAPM_STREAM_START
);
842 for_each_rtd_dais(rtd
, i
, dai
)
843 snd_soc_dai_digital_mute(dai
, 0, substream
->stream
);
846 mutex_unlock(&rtd
->card
->pcm_mutex
);
850 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params
*params
,
853 struct snd_interval
*interval
;
854 int channels
= hweight_long(mask
);
856 interval
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
857 interval
->min
= channels
;
858 interval
->max
= channels
;
861 static int soc_pcm_hw_clean(struct snd_pcm_substream
*substream
, int rollback
)
863 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
864 struct snd_soc_dai
*dai
;
867 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
869 /* clear the corresponding DAIs parameters when going to be inactive */
870 for_each_rtd_dais(rtd
, i
, dai
) {
871 int active
= snd_soc_dai_stream_active(dai
, substream
->stream
);
873 if (snd_soc_dai_active(dai
) == 1) {
876 dai
->sample_bits
= 0;
880 snd_soc_dai_digital_mute(dai
, 1, substream
->stream
);
883 /* run the stream event */
884 snd_soc_dapm_stream_stop(rtd
, substream
->stream
);
886 /* free any machine hw params */
887 snd_soc_link_hw_free(substream
, rollback
);
889 /* free any component resources */
890 snd_soc_pcm_component_hw_free(substream
, rollback
);
892 /* now free hw params for the DAIs */
893 for_each_rtd_dais(rtd
, i
, dai
) {
894 if (!snd_soc_dai_stream_valid(dai
, substream
->stream
))
897 snd_soc_dai_hw_free(dai
, substream
, rollback
);
900 mutex_unlock(&rtd
->card
->pcm_mutex
);
905 * Frees resources allocated by hw_params, can be called multiple times
907 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
909 return soc_pcm_hw_clean(substream
, 0);
913 * Called by ALSA when the hardware params are set by application. This
914 * function can also be called multiple times and can allocate buffers
915 * (using snd_pcm_lib_* ). It's non-atomic.
917 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
918 struct snd_pcm_hw_params
*params
)
920 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
921 struct snd_soc_dai
*cpu_dai
;
922 struct snd_soc_dai
*codec_dai
;
925 mutex_lock_nested(&rtd
->card
->pcm_mutex
, rtd
->card
->pcm_subclass
);
927 ret
= soc_pcm_params_symmetry(substream
, params
);
931 ret
= snd_soc_link_hw_params(substream
, params
);
935 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
936 struct snd_pcm_hw_params codec_params
;
939 * Skip CODECs which don't support the current stream type,
940 * the idea being that if a CODEC is not used for the currently
941 * set up transfer direction, it should not need to be
942 * configured, especially since the configuration used might
943 * not even be supported by that CODEC. There may be cases
944 * however where a CODEC needs to be set up although it is
945 * actually not being used for the transfer, e.g. if a
946 * capture-only CODEC is acting as an LRCLK and/or BCLK master
947 * for the DAI link including a playback-only CODEC.
948 * If this becomes necessary, we will have to augment the
949 * machine driver setup with information on how to act, so
950 * we can do the right thing here.
952 if (!snd_soc_dai_stream_valid(codec_dai
, substream
->stream
))
955 /* copy params for each codec */
956 codec_params
= *params
;
958 /* fixup params based on TDM slot masks */
959 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
961 soc_pcm_codec_params_fixup(&codec_params
,
964 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
&&
966 soc_pcm_codec_params_fixup(&codec_params
,
969 ret
= snd_soc_dai_hw_params(codec_dai
, substream
,
974 codec_dai
->rate
= params_rate(&codec_params
);
975 codec_dai
->channels
= params_channels(&codec_params
);
976 codec_dai
->sample_bits
= snd_pcm_format_physical_width(
977 params_format(&codec_params
));
979 snd_soc_dapm_update_dai(substream
, &codec_params
, codec_dai
);
982 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
984 * Skip CPUs which don't support the current stream
985 * type. See soc_pcm_init_runtime_hw() for more details
987 if (!snd_soc_dai_stream_valid(cpu_dai
, substream
->stream
))
990 ret
= snd_soc_dai_hw_params(cpu_dai
, substream
, params
);
994 /* store the parameters for each DAI */
995 cpu_dai
->rate
= params_rate(params
);
996 cpu_dai
->channels
= params_channels(params
);
997 cpu_dai
->sample_bits
=
998 snd_pcm_format_physical_width(params_format(params
));
1000 snd_soc_dapm_update_dai(substream
, params
, cpu_dai
);
1003 ret
= snd_soc_pcm_component_hw_params(substream
, params
);
1005 mutex_unlock(&rtd
->card
->pcm_mutex
);
1008 soc_pcm_hw_clean(substream
, 1);
1013 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
1015 int ret
= -EINVAL
, _ret
= 0;
1019 case SNDRV_PCM_TRIGGER_START
:
1020 case SNDRV_PCM_TRIGGER_RESUME
:
1021 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1022 ret
= snd_soc_link_trigger(substream
, cmd
, 0);
1026 ret
= snd_soc_pcm_component_trigger(substream
, cmd
, 0);
1030 ret
= snd_soc_pcm_dai_trigger(substream
, cmd
, 0);
1039 case SNDRV_PCM_TRIGGER_START
:
1040 cmd
= SNDRV_PCM_TRIGGER_STOP
;
1042 case SNDRV_PCM_TRIGGER_RESUME
:
1043 cmd
= SNDRV_PCM_TRIGGER_SUSPEND
;
1045 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1046 cmd
= SNDRV_PCM_TRIGGER_PAUSE_PUSH
;
1052 case SNDRV_PCM_TRIGGER_STOP
:
1053 case SNDRV_PCM_TRIGGER_SUSPEND
:
1054 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1055 ret
= snd_soc_pcm_dai_trigger(substream
, cmd
, rollback
);
1059 ret
= snd_soc_pcm_component_trigger(substream
, cmd
, rollback
);
1063 ret
= snd_soc_link_trigger(substream
, cmd
, rollback
);
1074 * soc level wrapper for pointer callback
1075 * If cpu_dai, codec_dai, component driver has the delay callback, then
1076 * the runtime->delay will be updated accordingly.
1078 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
1080 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
1081 struct snd_soc_dai
*cpu_dai
;
1082 struct snd_soc_dai
*codec_dai
;
1083 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1084 snd_pcm_uframes_t offset
= 0;
1085 snd_pcm_sframes_t delay
= 0;
1086 snd_pcm_sframes_t codec_delay
= 0;
1087 snd_pcm_sframes_t cpu_delay
= 0;
1090 /* clearing the previous total delay */
1093 offset
= snd_soc_pcm_component_pointer(substream
);
1095 /* base delay if assigned in pointer callback */
1096 delay
= runtime
->delay
;
1098 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
1099 cpu_delay
= max(cpu_delay
,
1100 snd_soc_dai_delay(cpu_dai
, substream
));
1104 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
1105 codec_delay
= max(codec_delay
,
1106 snd_soc_dai_delay(codec_dai
, substream
));
1108 delay
+= codec_delay
;
1110 runtime
->delay
= delay
;
1115 /* connect a FE and BE */
1116 static int dpcm_be_connect(struct snd_soc_pcm_runtime
*fe
,
1117 struct snd_soc_pcm_runtime
*be
, int stream
)
1119 struct snd_soc_dpcm
*dpcm
;
1120 unsigned long flags
;
1122 /* only add new dpcms */
1123 for_each_dpcm_be(fe
, stream
, dpcm
) {
1124 if (dpcm
->be
== be
&& dpcm
->fe
== fe
)
1128 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
1134 be
->dpcm
[stream
].runtime
= fe
->dpcm
[stream
].runtime
;
1135 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_NEW
;
1136 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
1137 list_add(&dpcm
->list_be
, &fe
->dpcm
[stream
].be_clients
);
1138 list_add(&dpcm
->list_fe
, &be
->dpcm
[stream
].fe_clients
);
1139 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
1141 dev_dbg(fe
->dev
, "connected new DPCM %s path %s %s %s\n",
1142 stream
? "capture" : "playback", fe
->dai_link
->name
,
1143 stream
? "<-" : "->", be
->dai_link
->name
);
1145 dpcm_create_debugfs_state(dpcm
, stream
);
1150 /* reparent a BE onto another FE */
1151 static void dpcm_be_reparent(struct snd_soc_pcm_runtime
*fe
,
1152 struct snd_soc_pcm_runtime
*be
, int stream
)
1154 struct snd_soc_dpcm
*dpcm
;
1155 struct snd_pcm_substream
*fe_substream
, *be_substream
;
1157 /* reparent if BE is connected to other FEs */
1158 if (!be
->dpcm
[stream
].users
)
1161 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1163 for_each_dpcm_fe(be
, stream
, dpcm
) {
1167 dev_dbg(fe
->dev
, "reparent %s path %s %s %s\n",
1168 stream
? "capture" : "playback",
1169 dpcm
->fe
->dai_link
->name
,
1170 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1172 fe_substream
= snd_soc_dpcm_get_substream(dpcm
->fe
, stream
);
1173 be_substream
->runtime
= fe_substream
->runtime
;
1178 /* disconnect a BE and FE */
1179 void dpcm_be_disconnect(struct snd_soc_pcm_runtime
*fe
, int stream
)
1181 struct snd_soc_dpcm
*dpcm
, *d
;
1182 unsigned long flags
;
1184 for_each_dpcm_be_safe(fe
, stream
, dpcm
, d
) {
1185 dev_dbg(fe
->dev
, "ASoC: BE %s disconnect check for %s\n",
1186 stream
? "capture" : "playback",
1187 dpcm
->be
->dai_link
->name
);
1189 if (dpcm
->state
!= SND_SOC_DPCM_LINK_STATE_FREE
)
1192 dev_dbg(fe
->dev
, "freed DSP %s path %s %s %s\n",
1193 stream
? "capture" : "playback", fe
->dai_link
->name
,
1194 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1196 /* BEs still alive need new FE */
1197 dpcm_be_reparent(fe
, dpcm
->be
, stream
);
1199 dpcm_remove_debugfs_state(dpcm
);
1201 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
1202 list_del(&dpcm
->list_be
);
1203 list_del(&dpcm
->list_fe
);
1204 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
1209 /* get BE for DAI widget and stream */
1210 static struct snd_soc_pcm_runtime
*dpcm_get_be(struct snd_soc_card
*card
,
1211 struct snd_soc_dapm_widget
*widget
, int stream
)
1213 struct snd_soc_pcm_runtime
*be
;
1214 struct snd_soc_dapm_widget
*w
;
1215 struct snd_soc_dai
*dai
;
1218 dev_dbg(card
->dev
, "ASoC: find BE for widget %s\n", widget
->name
);
1220 for_each_card_rtds(card
, be
) {
1222 if (!be
->dai_link
->no_pcm
)
1225 for_each_rtd_dais(be
, i
, dai
) {
1226 w
= snd_soc_dai_get_widget(dai
, stream
);
1228 dev_dbg(card
->dev
, "ASoC: try BE : %s\n",
1229 w
? w
->name
: "(not set)");
1236 /* Widget provided is not a BE */
1240 static int widget_in_list(struct snd_soc_dapm_widget_list
*list
,
1241 struct snd_soc_dapm_widget
*widget
)
1243 struct snd_soc_dapm_widget
*w
;
1246 for_each_dapm_widgets(list
, i
, w
)
1253 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget
*widget
,
1254 enum snd_soc_dapm_direction dir
)
1256 struct snd_soc_card
*card
= widget
->dapm
->card
;
1257 struct snd_soc_pcm_runtime
*rtd
;
1260 /* adjust dir to stream */
1261 if (dir
== SND_SOC_DAPM_DIR_OUT
)
1262 stream
= SNDRV_PCM_STREAM_PLAYBACK
;
1264 stream
= SNDRV_PCM_STREAM_CAPTURE
;
1266 rtd
= dpcm_get_be(card
, widget
, stream
);
1273 int dpcm_path_get(struct snd_soc_pcm_runtime
*fe
,
1274 int stream
, struct snd_soc_dapm_widget_list
**list
)
1276 struct snd_soc_dai
*cpu_dai
= asoc_rtd_to_cpu(fe
, 0);
1279 if (fe
->num_cpus
> 1) {
1281 "%s doesn't support Multi CPU yet\n", __func__
);
1285 /* get number of valid DAI paths and their widgets */
1286 paths
= snd_soc_dapm_dai_get_connected_widgets(cpu_dai
, stream
, list
,
1287 fe
->card
->component_chaining
?
1288 NULL
: dpcm_end_walk_at_be
);
1290 dev_dbg(fe
->dev
, "ASoC: found %d audio %s paths\n", paths
,
1291 stream
? "capture" : "playback");
1296 void dpcm_path_put(struct snd_soc_dapm_widget_list
**list
)
1298 snd_soc_dapm_dai_free_widgets(list
);
1301 static bool dpcm_be_is_active(struct snd_soc_dpcm
*dpcm
, int stream
,
1302 struct snd_soc_dapm_widget_list
*list
)
1304 struct snd_soc_dapm_widget
*widget
;
1305 struct snd_soc_dai
*dai
;
1308 /* is there a valid DAI widget for this BE */
1309 for_each_rtd_dais(dpcm
->be
, i
, dai
) {
1310 widget
= snd_soc_dai_get_widget(dai
, stream
);
1313 * The BE is pruned only if none of the dai
1314 * widgets are in the active list.
1316 if (widget
&& widget_in_list(list
, widget
))
1323 static int dpcm_prune_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1324 struct snd_soc_dapm_widget_list
**list_
)
1326 struct snd_soc_dpcm
*dpcm
;
1329 /* Destroy any old FE <--> BE connections */
1330 for_each_dpcm_be(fe
, stream
, dpcm
) {
1331 if (dpcm_be_is_active(dpcm
, stream
, *list_
))
1334 dev_dbg(fe
->dev
, "ASoC: pruning %s BE %s for %s\n",
1335 stream
? "capture" : "playback",
1336 dpcm
->be
->dai_link
->name
, fe
->dai_link
->name
);
1337 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1338 dpcm
->be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1342 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", prune
);
1346 static int dpcm_add_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1347 struct snd_soc_dapm_widget_list
**list_
)
1349 struct snd_soc_card
*card
= fe
->card
;
1350 struct snd_soc_dapm_widget_list
*list
= *list_
;
1351 struct snd_soc_pcm_runtime
*be
;
1352 struct snd_soc_dapm_widget
*widget
;
1353 int i
, new = 0, err
;
1355 /* Create any new FE <--> BE connections */
1356 for_each_dapm_widgets(list
, i
, widget
) {
1358 switch (widget
->id
) {
1359 case snd_soc_dapm_dai_in
:
1360 if (stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1363 case snd_soc_dapm_dai_out
:
1364 if (stream
!= SNDRV_PCM_STREAM_CAPTURE
)
1371 /* is there a valid BE rtd for this widget */
1372 be
= dpcm_get_be(card
, widget
, stream
);
1374 dev_err(fe
->dev
, "ASoC: no BE found for %s\n",
1379 /* don't connect if FE is not running */
1380 if (!fe
->dpcm
[stream
].runtime
&& !fe
->fe_compr
)
1383 /* newly connected FE and BE */
1384 err
= dpcm_be_connect(fe
, be
, stream
);
1386 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
1389 } else if (err
== 0) /* already connected */
1393 be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1397 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
1402 * Find the corresponding BE DAIs that source or sink audio to this
1405 int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
1406 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
1409 return dpcm_add_paths(fe
, stream
, list
);
1411 return dpcm_prune_paths(fe
, stream
, list
);
1414 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime
*fe
, int stream
)
1416 struct snd_soc_dpcm
*dpcm
;
1417 unsigned long flags
;
1419 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
1420 for_each_dpcm_be(fe
, stream
, dpcm
)
1421 dpcm
->be
->dpcm
[stream
].runtime_update
=
1422 SND_SOC_DPCM_UPDATE_NO
;
1423 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
1426 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime
*fe
,
1429 struct snd_soc_dpcm
*dpcm
;
1431 /* disable any enabled and non active backends */
1432 for_each_dpcm_be(fe
, stream
, dpcm
) {
1434 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1435 struct snd_pcm_substream
*be_substream
=
1436 snd_soc_dpcm_get_substream(be
, stream
);
1438 if (be
->dpcm
[stream
].users
== 0)
1439 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1440 stream
? "capture" : "playback",
1441 be
->dpcm
[stream
].state
);
1443 if (--be
->dpcm
[stream
].users
!= 0)
1446 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1449 soc_pcm_close(be_substream
);
1450 be_substream
->runtime
= NULL
;
1451 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1455 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1457 struct snd_soc_dpcm
*dpcm
;
1460 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1461 for_each_dpcm_be(fe
, stream
, dpcm
) {
1463 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1464 struct snd_pcm_substream
*be_substream
=
1465 snd_soc_dpcm_get_substream(be
, stream
);
1467 if (!be_substream
) {
1468 dev_err(be
->dev
, "ASoC: no backend %s stream\n",
1469 stream
? "capture" : "playback");
1473 /* is this op for this BE ? */
1474 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1477 /* first time the dpcm is open ? */
1478 if (be
->dpcm
[stream
].users
== DPCM_MAX_BE_USERS
)
1479 dev_err(be
->dev
, "ASoC: too many users %s at open %d\n",
1480 stream
? "capture" : "playback",
1481 be
->dpcm
[stream
].state
);
1483 if (be
->dpcm
[stream
].users
++ != 0)
1486 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1487 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1490 dev_dbg(be
->dev
, "ASoC: open %s BE %s\n",
1491 stream
? "capture" : "playback", be
->dai_link
->name
);
1493 be_substream
->runtime
= be
->dpcm
[stream
].runtime
;
1494 err
= soc_pcm_open(be_substream
);
1496 dev_err(be
->dev
, "ASoC: BE open failed %d\n", err
);
1497 be
->dpcm
[stream
].users
--;
1498 if (be
->dpcm
[stream
].users
< 0)
1499 dev_err(be
->dev
, "ASoC: no users %s at unwind %d\n",
1500 stream
? "capture" : "playback",
1501 be
->dpcm
[stream
].state
);
1503 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1507 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1514 /* disable any enabled and non active backends */
1515 for_each_dpcm_be_rollback(fe
, stream
, dpcm
) {
1516 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1517 struct snd_pcm_substream
*be_substream
=
1518 snd_soc_dpcm_get_substream(be
, stream
);
1520 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1523 if (be
->dpcm
[stream
].users
== 0)
1524 dev_err(be
->dev
, "ASoC: no users %s at close %d\n",
1525 stream
? "capture" : "playback",
1526 be
->dpcm
[stream
].state
);
1528 if (--be
->dpcm
[stream
].users
!= 0)
1531 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1534 soc_pcm_close(be_substream
);
1535 be_substream
->runtime
= NULL
;
1536 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1542 static void dpcm_init_runtime_hw(struct snd_pcm_runtime
*runtime
,
1543 struct snd_soc_pcm_stream
*stream
)
1545 runtime
->hw
.rate_min
= stream
->rate_min
;
1546 runtime
->hw
.rate_max
= min_not_zero(stream
->rate_max
, UINT_MAX
);
1547 runtime
->hw
.channels_min
= stream
->channels_min
;
1548 runtime
->hw
.channels_max
= stream
->channels_max
;
1549 if (runtime
->hw
.formats
)
1550 runtime
->hw
.formats
&= stream
->formats
;
1552 runtime
->hw
.formats
= stream
->formats
;
1553 runtime
->hw
.rates
= stream
->rates
;
1556 static void dpcm_runtime_merge_format(struct snd_pcm_substream
*substream
,
1559 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
1560 struct snd_soc_dpcm
*dpcm
;
1561 struct snd_soc_dai
*dai
;
1562 int stream
= substream
->stream
;
1564 if (!fe
->dai_link
->dpcm_merged_format
)
1568 * It returns merged BE codec format
1569 * if FE want to use it (= dpcm_merged_format)
1572 for_each_dpcm_be(fe
, stream
, dpcm
) {
1573 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1574 struct snd_soc_pcm_stream
*codec_stream
;
1577 for_each_rtd_codec_dais(be
, i
, dai
) {
1579 * Skip CODECs which don't support the current stream
1580 * type. See soc_pcm_init_runtime_hw() for more details
1582 if (!snd_soc_dai_stream_valid(dai
, stream
))
1585 codec_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1587 *formats
&= codec_stream
->formats
;
1592 static void dpcm_runtime_merge_chan(struct snd_pcm_substream
*substream
,
1593 unsigned int *channels_min
,
1594 unsigned int *channels_max
)
1596 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
1597 struct snd_soc_dpcm
*dpcm
;
1598 int stream
= substream
->stream
;
1600 if (!fe
->dai_link
->dpcm_merged_chan
)
1604 * It returns merged BE codec channel;
1605 * if FE want to use it (= dpcm_merged_chan)
1608 for_each_dpcm_be(fe
, stream
, dpcm
) {
1609 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1610 struct snd_soc_pcm_stream
*codec_stream
;
1611 struct snd_soc_pcm_stream
*cpu_stream
;
1612 struct snd_soc_dai
*dai
;
1615 for_each_rtd_cpu_dais(be
, i
, dai
) {
1617 * Skip CPUs which don't support the current stream
1618 * type. See soc_pcm_init_runtime_hw() for more details
1620 if (!snd_soc_dai_stream_valid(dai
, stream
))
1623 cpu_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1625 *channels_min
= max(*channels_min
,
1626 cpu_stream
->channels_min
);
1627 *channels_max
= min(*channels_max
,
1628 cpu_stream
->channels_max
);
1632 * chan min/max cannot be enforced if there are multiple CODEC
1633 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1635 if (be
->num_codecs
== 1) {
1636 codec_stream
= snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be
, 0), stream
);
1638 *channels_min
= max(*channels_min
,
1639 codec_stream
->channels_min
);
1640 *channels_max
= min(*channels_max
,
1641 codec_stream
->channels_max
);
1646 static void dpcm_runtime_merge_rate(struct snd_pcm_substream
*substream
,
1647 unsigned int *rates
,
1648 unsigned int *rate_min
,
1649 unsigned int *rate_max
)
1651 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
1652 struct snd_soc_dpcm
*dpcm
;
1653 int stream
= substream
->stream
;
1655 if (!fe
->dai_link
->dpcm_merged_rate
)
1659 * It returns merged BE codec channel;
1660 * if FE want to use it (= dpcm_merged_chan)
1663 for_each_dpcm_be(fe
, stream
, dpcm
) {
1664 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1665 struct snd_soc_pcm_stream
*pcm
;
1666 struct snd_soc_dai
*dai
;
1669 for_each_rtd_dais(be
, i
, dai
) {
1671 * Skip DAIs which don't support the current stream
1672 * type. See soc_pcm_init_runtime_hw() for more details
1674 if (!snd_soc_dai_stream_valid(dai
, stream
))
1677 pcm
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1679 *rate_min
= max(*rate_min
, pcm
->rate_min
);
1680 *rate_max
= min_not_zero(*rate_max
, pcm
->rate_max
);
1681 *rates
= snd_pcm_rate_mask_intersect(*rates
, pcm
->rates
);
1686 static void dpcm_set_fe_runtime(struct snd_pcm_substream
*substream
)
1688 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1689 struct snd_soc_pcm_runtime
*rtd
= asoc_substream_to_rtd(substream
);
1690 struct snd_soc_dai
*cpu_dai
;
1693 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
1695 * Skip CPUs which don't support the current stream
1696 * type. See soc_pcm_init_runtime_hw() for more details
1698 if (!snd_soc_dai_stream_valid(cpu_dai
, substream
->stream
))
1701 dpcm_init_runtime_hw(runtime
,
1702 snd_soc_dai_get_pcm_stream(cpu_dai
,
1703 substream
->stream
));
1706 dpcm_runtime_merge_format(substream
, &runtime
->hw
.formats
);
1707 dpcm_runtime_merge_chan(substream
, &runtime
->hw
.channels_min
,
1708 &runtime
->hw
.channels_max
);
1709 dpcm_runtime_merge_rate(substream
, &runtime
->hw
.rates
,
1710 &runtime
->hw
.rate_min
, &runtime
->hw
.rate_max
);
1713 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
);
1715 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1716 * for avoiding the race with trigger callback.
1717 * If the state is unset and a trigger is pending while the previous operation,
1718 * process the pending trigger action here.
1720 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime
*fe
,
1721 int stream
, enum snd_soc_dpcm_update state
)
1723 struct snd_pcm_substream
*substream
=
1724 snd_soc_dpcm_get_substream(fe
, stream
);
1726 snd_pcm_stream_lock_irq(substream
);
1727 if (state
== SND_SOC_DPCM_UPDATE_NO
&& fe
->dpcm
[stream
].trigger_pending
) {
1728 dpcm_fe_dai_do_trigger(substream
,
1729 fe
->dpcm
[stream
].trigger_pending
- 1);
1730 fe
->dpcm
[stream
].trigger_pending
= 0;
1732 fe
->dpcm
[stream
].runtime_update
= state
;
1733 snd_pcm_stream_unlock_irq(substream
);
1736 static int dpcm_apply_symmetry(struct snd_pcm_substream
*fe_substream
,
1739 struct snd_soc_dpcm
*dpcm
;
1740 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(fe_substream
);
1741 struct snd_soc_dai
*fe_cpu_dai
;
1745 /* apply symmetry for FE */
1746 if (soc_pcm_has_symmetry(fe_substream
))
1747 fe_substream
->runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
1749 for_each_rtd_cpu_dais (fe
, i
, fe_cpu_dai
) {
1750 /* Symmetry only applies if we've got an active stream. */
1751 if (snd_soc_dai_active(fe_cpu_dai
)) {
1752 err
= soc_pcm_apply_symmetry(fe_substream
, fe_cpu_dai
);
1758 /* apply symmetry for BE */
1759 for_each_dpcm_be(fe
, stream
, dpcm
) {
1760 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1761 struct snd_pcm_substream
*be_substream
=
1762 snd_soc_dpcm_get_substream(be
, stream
);
1763 struct snd_soc_pcm_runtime
*rtd
;
1764 struct snd_soc_dai
*dai
;
1767 /* A backend may not have the requested substream */
1771 rtd
= asoc_substream_to_rtd(be_substream
);
1772 if (rtd
->dai_link
->be_hw_params_fixup
)
1775 if (soc_pcm_has_symmetry(be_substream
))
1776 be_substream
->runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
1778 /* Symmetry only applies if we've got an active stream. */
1779 for_each_rtd_dais(rtd
, i
, dai
) {
1780 if (snd_soc_dai_active(dai
)) {
1781 err
= soc_pcm_apply_symmetry(fe_substream
, dai
);
1791 static int dpcm_fe_dai_startup(struct snd_pcm_substream
*fe_substream
)
1793 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(fe_substream
);
1794 struct snd_pcm_runtime
*runtime
= fe_substream
->runtime
;
1795 int stream
= fe_substream
->stream
, ret
= 0;
1797 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1799 ret
= dpcm_be_dai_startup(fe
, stream
);
1801 dev_err(fe
->dev
,"ASoC: failed to start some BEs %d\n", ret
);
1805 dev_dbg(fe
->dev
, "ASoC: open FE %s\n", fe
->dai_link
->name
);
1807 /* start the DAI frontend */
1808 ret
= soc_pcm_open(fe_substream
);
1810 dev_err(fe
->dev
,"ASoC: failed to start FE %d\n", ret
);
1814 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1816 dpcm_set_fe_runtime(fe_substream
);
1817 snd_pcm_limit_hw_rates(runtime
);
1819 ret
= dpcm_apply_symmetry(fe_substream
, stream
);
1821 dev_err(fe
->dev
, "ASoC: failed to apply dpcm symmetry %d\n",
1826 dpcm_be_dai_startup_unwind(fe
, stream
);
1828 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1832 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
1834 struct snd_soc_dpcm
*dpcm
;
1836 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1837 for_each_dpcm_be(fe
, stream
, dpcm
) {
1839 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1840 struct snd_pcm_substream
*be_substream
=
1841 snd_soc_dpcm_get_substream(be
, stream
);
1843 /* is this op for this BE ? */
1844 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1847 if (be
->dpcm
[stream
].users
== 0)
1848 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1849 stream
? "capture" : "playback",
1850 be
->dpcm
[stream
].state
);
1852 if (--be
->dpcm
[stream
].users
!= 0)
1855 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1856 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)) {
1857 soc_pcm_hw_free(be_substream
);
1858 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1861 dev_dbg(be
->dev
, "ASoC: close BE %s\n",
1862 be
->dai_link
->name
);
1864 soc_pcm_close(be_substream
);
1865 be_substream
->runtime
= NULL
;
1867 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1872 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream
*substream
)
1874 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
1875 int stream
= substream
->stream
;
1877 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1879 /* shutdown the BEs */
1880 dpcm_be_dai_shutdown(fe
, stream
);
1882 dev_dbg(fe
->dev
, "ASoC: close FE %s\n", fe
->dai_link
->name
);
1884 /* now shutdown the frontend */
1885 soc_pcm_close(substream
);
1887 /* run the stream stop event */
1888 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_STOP
);
1890 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1891 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1895 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime
*fe
, int stream
)
1897 struct snd_soc_dpcm
*dpcm
;
1899 /* only hw_params backends that are either sinks or sources
1900 * to this frontend DAI */
1901 for_each_dpcm_be(fe
, stream
, dpcm
) {
1903 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1904 struct snd_pcm_substream
*be_substream
=
1905 snd_soc_dpcm_get_substream(be
, stream
);
1907 /* is this op for this BE ? */
1908 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1911 /* only free hw when no longer used - check all FEs */
1912 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1915 /* do not free hw if this BE is used by other FE */
1916 if (be
->dpcm
[stream
].users
> 1)
1919 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1920 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
1921 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1922 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
) &&
1923 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
1924 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
1927 dev_dbg(be
->dev
, "ASoC: hw_free BE %s\n",
1928 be
->dai_link
->name
);
1930 soc_pcm_hw_free(be_substream
);
1932 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1938 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream
*substream
)
1940 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
1941 int err
, stream
= substream
->stream
;
1943 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1944 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1946 dev_dbg(fe
->dev
, "ASoC: hw_free FE %s\n", fe
->dai_link
->name
);
1948 /* call hw_free on the frontend */
1949 err
= soc_pcm_hw_free(substream
);
1951 dev_err(fe
->dev
,"ASoC: hw_free FE %s failed\n",
1952 fe
->dai_link
->name
);
1954 /* only hw_params backends that are either sinks or sources
1955 * to this frontend DAI */
1956 err
= dpcm_be_dai_hw_free(fe
, stream
);
1958 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1959 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1961 mutex_unlock(&fe
->card
->mutex
);
1965 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
1967 struct snd_soc_dpcm
*dpcm
;
1970 for_each_dpcm_be(fe
, stream
, dpcm
) {
1972 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1973 struct snd_pcm_substream
*be_substream
=
1974 snd_soc_dpcm_get_substream(be
, stream
);
1976 /* is this op for this BE ? */
1977 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1980 /* copy params for each dpcm */
1981 memcpy(&dpcm
->hw_params
, &fe
->dpcm
[stream
].hw_params
,
1982 sizeof(struct snd_pcm_hw_params
));
1984 /* perform any hw_params fixups */
1985 ret
= snd_soc_link_be_hw_params_fixup(be
, &dpcm
->hw_params
);
1989 /* copy the fixed-up hw params for BE dai */
1990 memcpy(&be
->dpcm
[stream
].hw_params
, &dpcm
->hw_params
,
1991 sizeof(struct snd_pcm_hw_params
));
1993 /* only allow hw_params() if no connected FEs are running */
1994 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
1997 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
1998 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1999 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
))
2002 dev_dbg(be
->dev
, "ASoC: hw_params BE %s\n",
2003 be
->dai_link
->name
);
2005 ret
= soc_pcm_hw_params(be_substream
, &dpcm
->hw_params
);
2007 dev_err(dpcm
->be
->dev
,
2008 "ASoC: hw_params BE failed %d\n", ret
);
2012 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2017 /* disable any enabled and non active backends */
2018 for_each_dpcm_be_rollback(fe
, stream
, dpcm
) {
2019 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2020 struct snd_pcm_substream
*be_substream
=
2021 snd_soc_dpcm_get_substream(be
, stream
);
2023 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2026 /* only allow hw_free() if no connected FEs are running */
2027 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2030 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
2031 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2032 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
2033 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
2036 soc_pcm_hw_free(be_substream
);
2042 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream
*substream
,
2043 struct snd_pcm_hw_params
*params
)
2045 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
2046 int ret
, stream
= substream
->stream
;
2048 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2049 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2051 memcpy(&fe
->dpcm
[stream
].hw_params
, params
,
2052 sizeof(struct snd_pcm_hw_params
));
2053 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2055 dev_err(fe
->dev
,"ASoC: hw_params BE failed %d\n", ret
);
2059 dev_dbg(fe
->dev
, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2060 fe
->dai_link
->name
, params_rate(params
),
2061 params_channels(params
), params_format(params
));
2063 /* call hw_params on the frontend */
2064 ret
= soc_pcm_hw_params(substream
, params
);
2066 dev_err(fe
->dev
,"ASoC: hw_params FE failed %d\n", ret
);
2067 dpcm_be_dai_hw_free(fe
, stream
);
2069 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2072 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2073 mutex_unlock(&fe
->card
->mutex
);
2077 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
2080 struct snd_soc_dpcm
*dpcm
;
2083 for_each_dpcm_be(fe
, stream
, dpcm
) {
2085 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2086 struct snd_pcm_substream
*be_substream
=
2087 snd_soc_dpcm_get_substream(be
, stream
);
2089 /* is this op for this BE ? */
2090 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2093 dev_dbg(be
->dev
, "ASoC: trigger BE %s cmd %d\n",
2094 be
->dai_link
->name
, cmd
);
2097 case SNDRV_PCM_TRIGGER_START
:
2098 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
2099 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2100 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2103 ret
= soc_pcm_trigger(be_substream
, cmd
);
2107 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2109 case SNDRV_PCM_TRIGGER_RESUME
:
2110 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
2113 ret
= soc_pcm_trigger(be_substream
, cmd
);
2117 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2119 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2120 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2123 ret
= soc_pcm_trigger(be_substream
, cmd
);
2127 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2129 case SNDRV_PCM_TRIGGER_STOP
:
2130 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
) &&
2131 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2134 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2137 ret
= soc_pcm_trigger(be_substream
, cmd
);
2141 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2143 case SNDRV_PCM_TRIGGER_SUSPEND
:
2144 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2147 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2150 ret
= soc_pcm_trigger(be_substream
, cmd
);
2154 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
2156 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2157 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2160 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2163 ret
= soc_pcm_trigger(be_substream
, cmd
);
2167 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2174 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger
);
2176 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream
*substream
,
2177 int cmd
, bool fe_first
)
2179 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
2182 /* call trigger on the frontend before the backend. */
2184 dev_dbg(fe
->dev
, "ASoC: pre trigger FE %s cmd %d\n",
2185 fe
->dai_link
->name
, cmd
);
2187 ret
= soc_pcm_trigger(substream
, cmd
);
2191 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2195 /* call trigger on the frontend after the backend. */
2196 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2200 dev_dbg(fe
->dev
, "ASoC: post trigger FE %s cmd %d\n",
2201 fe
->dai_link
->name
, cmd
);
2203 ret
= soc_pcm_trigger(substream
, cmd
);
2208 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2210 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
2211 int stream
= substream
->stream
;
2213 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2215 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
2218 case SND_SOC_DPCM_TRIGGER_PRE
:
2220 case SNDRV_PCM_TRIGGER_START
:
2221 case SNDRV_PCM_TRIGGER_RESUME
:
2222 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2223 case SNDRV_PCM_TRIGGER_DRAIN
:
2224 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2226 case SNDRV_PCM_TRIGGER_STOP
:
2227 case SNDRV_PCM_TRIGGER_SUSPEND
:
2228 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2229 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2236 case SND_SOC_DPCM_TRIGGER_POST
:
2238 case SNDRV_PCM_TRIGGER_START
:
2239 case SNDRV_PCM_TRIGGER_RESUME
:
2240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2241 case SNDRV_PCM_TRIGGER_DRAIN
:
2242 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2244 case SNDRV_PCM_TRIGGER_STOP
:
2245 case SNDRV_PCM_TRIGGER_SUSPEND
:
2246 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2247 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2254 case SND_SOC_DPCM_TRIGGER_BESPOKE
:
2255 /* bespoke trigger() - handles both FE and BEs */
2257 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd %d\n",
2258 fe
->dai_link
->name
, cmd
);
2260 ret
= snd_soc_pcm_dai_bespoke_trigger(substream
, cmd
);
2263 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
2264 fe
->dai_link
->name
);
2270 dev_err(fe
->dev
, "ASoC: trigger FE cmd: %d failed: %d\n",
2276 case SNDRV_PCM_TRIGGER_START
:
2277 case SNDRV_PCM_TRIGGER_RESUME
:
2278 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2279 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2281 case SNDRV_PCM_TRIGGER_STOP
:
2282 case SNDRV_PCM_TRIGGER_SUSPEND
:
2283 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2285 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2286 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2291 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
2295 static int dpcm_fe_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2297 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
2298 int stream
= substream
->stream
;
2300 /* if FE's runtime_update is already set, we're in race;
2301 * process this trigger later at exit
2303 if (fe
->dpcm
[stream
].runtime_update
!= SND_SOC_DPCM_UPDATE_NO
) {
2304 fe
->dpcm
[stream
].trigger_pending
= cmd
+ 1;
2305 return 0; /* delayed, assuming it's successful */
2308 /* we're alone, let's trigger */
2309 return dpcm_fe_dai_do_trigger(substream
, cmd
);
2312 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
2314 struct snd_soc_dpcm
*dpcm
;
2317 for_each_dpcm_be(fe
, stream
, dpcm
) {
2319 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2320 struct snd_pcm_substream
*be_substream
=
2321 snd_soc_dpcm_get_substream(be
, stream
);
2323 /* is this op for this BE ? */
2324 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2327 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2328 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2329 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
) &&
2330 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2333 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
2334 be
->dai_link
->name
);
2336 ret
= soc_pcm_prepare(be_substream
);
2338 dev_err(be
->dev
, "ASoC: backend prepare failed %d\n",
2343 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2348 static int dpcm_fe_dai_prepare(struct snd_pcm_substream
*substream
)
2350 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(substream
);
2351 int stream
= substream
->stream
, ret
= 0;
2353 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2355 dev_dbg(fe
->dev
, "ASoC: prepare FE %s\n", fe
->dai_link
->name
);
2357 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2359 /* there is no point preparing this FE if there are no BEs */
2360 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2361 dev_err(fe
->dev
, "ASoC: no backend DAIs enabled for %s\n",
2362 fe
->dai_link
->name
);
2367 ret
= dpcm_be_dai_prepare(fe
, stream
);
2371 /* call prepare on the frontend */
2372 ret
= soc_pcm_prepare(substream
);
2374 dev_err(fe
->dev
,"ASoC: prepare FE %s failed\n",
2375 fe
->dai_link
->name
);
2379 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2382 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2383 mutex_unlock(&fe
->card
->mutex
);
2388 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
2390 struct snd_pcm_substream
*substream
=
2391 snd_soc_dpcm_get_substream(fe
, stream
);
2392 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2395 dev_dbg(fe
->dev
, "ASoC: runtime %s close on FE %s\n",
2396 stream
? "capture" : "playback", fe
->dai_link
->name
);
2398 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
2399 /* call bespoke trigger - FE takes care of all BE triggers */
2400 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd stop\n",
2401 fe
->dai_link
->name
);
2403 err
= snd_soc_pcm_dai_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
2405 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
2407 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd stop\n",
2408 fe
->dai_link
->name
);
2410 err
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_STOP
);
2412 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
2415 err
= dpcm_be_dai_hw_free(fe
, stream
);
2417 dev_err(fe
->dev
,"ASoC: hw_free FE failed %d\n", err
);
2419 err
= dpcm_be_dai_shutdown(fe
, stream
);
2421 dev_err(fe
->dev
,"ASoC: shutdown FE failed %d\n", err
);
2423 /* run the stream event for each BE */
2424 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2429 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
2431 struct snd_pcm_substream
*substream
=
2432 snd_soc_dpcm_get_substream(fe
, stream
);
2433 struct snd_soc_dpcm
*dpcm
;
2434 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2436 unsigned long flags
;
2438 dev_dbg(fe
->dev
, "ASoC: runtime %s open on FE %s\n",
2439 stream
? "capture" : "playback", fe
->dai_link
->name
);
2441 /* Only start the BE if the FE is ready */
2442 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_FREE
||
2443 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
)
2446 /* startup must always be called for new BEs */
2447 ret
= dpcm_be_dai_startup(fe
, stream
);
2451 /* keep going if FE state is > open */
2452 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
2455 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2459 /* keep going if FE state is > hw_params */
2460 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
2464 ret
= dpcm_be_dai_prepare(fe
, stream
);
2468 /* run the stream event for each BE */
2469 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2471 /* keep going if FE state is > prepare */
2472 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PREPARE
||
2473 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_STOP
)
2476 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
2477 /* call trigger on the frontend - FE takes care of all BE triggers */
2478 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd start\n",
2479 fe
->dai_link
->name
);
2481 ret
= snd_soc_pcm_dai_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_START
);
2483 dev_err(fe
->dev
,"ASoC: bespoke trigger FE failed %d\n", ret
);
2487 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd start\n",
2488 fe
->dai_link
->name
);
2490 ret
= dpcm_be_dai_trigger(fe
, stream
,
2491 SNDRV_PCM_TRIGGER_START
);
2493 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
2501 dpcm_be_dai_hw_free(fe
, stream
);
2503 dpcm_be_dai_shutdown(fe
, stream
);
2505 /* disconnect any closed BEs */
2506 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
2507 for_each_dpcm_be(fe
, stream
, dpcm
) {
2508 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2509 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
)
2510 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2512 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
2517 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime
*fe
, int new)
2519 struct snd_soc_dapm_widget_list
*list
;
2524 if (!fe
->dai_link
->dynamic
)
2527 if (fe
->num_cpus
> 1) {
2529 "%s doesn't support Multi CPU yet\n", __func__
);
2533 /* only check active links */
2534 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe
, 0)))
2537 /* DAPM sync will call this to update DSP paths */
2538 dev_dbg(fe
->dev
, "ASoC: DPCM %s runtime update for FE %s\n",
2539 new ? "new" : "old", fe
->dai_link
->name
);
2541 for_each_pcm_streams(stream
) {
2543 /* skip if FE doesn't have playback/capture capability */
2544 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe
, 0), stream
) ||
2545 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe
, 0), stream
))
2548 /* skip if FE isn't currently playing/capturing */
2549 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe
, 0), stream
) ||
2550 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe
, 0), stream
))
2553 paths
= dpcm_path_get(fe
, stream
, &list
);
2555 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
2557 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
2558 "playback" : "capture");
2562 /* update any playback/capture paths */
2563 count
= dpcm_process_paths(fe
, stream
, &list
, new);
2565 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_BE
);
2567 ret
= dpcm_run_update_startup(fe
, stream
);
2569 ret
= dpcm_run_update_shutdown(fe
, stream
);
2571 dev_err(fe
->dev
, "ASoC: failed to shutdown some BEs\n");
2572 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2574 dpcm_clear_pending_state(fe
, stream
);
2575 dpcm_be_disconnect(fe
, stream
);
2578 dpcm_path_put(&list
);
2584 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2587 int snd_soc_dpcm_runtime_update(struct snd_soc_card
*card
)
2589 struct snd_soc_pcm_runtime
*fe
;
2592 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2593 /* shutdown all old paths first */
2594 for_each_card_rtds(card
, fe
) {
2595 ret
= soc_dpcm_fe_runtime_update(fe
, 0);
2600 /* bring new paths up */
2601 for_each_card_rtds(card
, fe
) {
2602 ret
= soc_dpcm_fe_runtime_update(fe
, 1);
2608 mutex_unlock(&card
->mutex
);
2611 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update
);
2613 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream
*fe_substream
)
2615 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(fe_substream
);
2616 struct snd_soc_dpcm
*dpcm
;
2617 int stream
= fe_substream
->stream
;
2619 /* mark FE's links ready to prune */
2620 for_each_dpcm_be(fe
, stream
, dpcm
)
2621 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2623 dpcm_be_disconnect(fe
, stream
);
2625 fe
->dpcm
[stream
].runtime
= NULL
;
2628 static int dpcm_fe_dai_close(struct snd_pcm_substream
*fe_substream
)
2630 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(fe_substream
);
2633 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2634 ret
= dpcm_fe_dai_shutdown(fe_substream
);
2636 dpcm_fe_dai_cleanup(fe_substream
);
2638 mutex_unlock(&fe
->card
->mutex
);
2642 static int dpcm_fe_dai_open(struct snd_pcm_substream
*fe_substream
)
2644 struct snd_soc_pcm_runtime
*fe
= asoc_substream_to_rtd(fe_substream
);
2645 struct snd_soc_dapm_widget_list
*list
;
2647 int stream
= fe_substream
->stream
;
2649 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2650 fe
->dpcm
[stream
].runtime
= fe_substream
->runtime
;
2652 ret
= dpcm_path_get(fe
, stream
, &list
);
2655 } else if (ret
== 0) {
2656 dev_dbg(fe
->dev
, "ASoC: %s no valid %s route\n",
2657 fe
->dai_link
->name
, stream
? "capture" : "playback");
2660 /* calculate valid and active FE <-> BE dpcms */
2661 dpcm_process_paths(fe
, stream
, &list
, 1);
2663 ret
= dpcm_fe_dai_startup(fe_substream
);
2665 dpcm_fe_dai_cleanup(fe_substream
);
2667 dpcm_clear_pending_state(fe
, stream
);
2668 dpcm_path_put(&list
);
2670 mutex_unlock(&fe
->card
->mutex
);
2674 /* create a new pcm */
2675 int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
2677 struct snd_soc_dai
*codec_dai
;
2678 struct snd_soc_dai
*cpu_dai
;
2679 struct snd_soc_component
*component
;
2680 struct snd_pcm
*pcm
;
2682 int ret
= 0, playback
= 0, capture
= 0;
2686 if (rtd
->dai_link
->dynamic
&& rtd
->num_cpus
> 1) {
2688 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2692 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
) {
2693 if (rtd
->dai_link
->dpcm_playback
) {
2694 stream
= SNDRV_PCM_STREAM_PLAYBACK
;
2696 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
2697 if (snd_soc_dai_stream_valid(cpu_dai
, stream
)) {
2704 dev_err(rtd
->card
->dev
,
2705 "No CPU DAIs support playback for stream %s\n",
2706 rtd
->dai_link
->stream_name
);
2710 if (rtd
->dai_link
->dpcm_capture
) {
2711 stream
= SNDRV_PCM_STREAM_CAPTURE
;
2713 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
2714 if (snd_soc_dai_stream_valid(cpu_dai
, stream
)) {
2721 dev_err(rtd
->card
->dev
,
2722 "No CPU DAIs support capture for stream %s\n",
2723 rtd
->dai_link
->stream_name
);
2728 /* Adapt stream for codec2codec links */
2729 int cpu_capture
= rtd
->dai_link
->params
?
2730 SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
2731 int cpu_playback
= rtd
->dai_link
->params
?
2732 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
2734 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
2735 if (rtd
->num_cpus
== 1) {
2736 cpu_dai
= asoc_rtd_to_cpu(rtd
, 0);
2737 } else if (rtd
->num_cpus
== rtd
->num_codecs
) {
2738 cpu_dai
= asoc_rtd_to_cpu(rtd
, i
);
2740 dev_err(rtd
->card
->dev
,
2741 "N cpus to M codecs link is not supported yet\n");
2745 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_PLAYBACK
) &&
2746 snd_soc_dai_stream_valid(cpu_dai
, cpu_playback
))
2748 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_CAPTURE
) &&
2749 snd_soc_dai_stream_valid(cpu_dai
, cpu_capture
))
2754 if (rtd
->dai_link
->playback_only
) {
2759 if (rtd
->dai_link
->capture_only
) {
2764 /* create the PCM */
2765 if (rtd
->dai_link
->params
) {
2766 snprintf(new_name
, sizeof(new_name
), "codec2codec(%s)",
2767 rtd
->dai_link
->stream_name
);
2769 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2770 playback
, capture
, &pcm
);
2771 } else if (rtd
->dai_link
->no_pcm
) {
2772 snprintf(new_name
, sizeof(new_name
), "(%s)",
2773 rtd
->dai_link
->stream_name
);
2775 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2776 playback
, capture
, &pcm
);
2778 if (rtd
->dai_link
->dynamic
)
2779 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2780 rtd
->dai_link
->stream_name
);
2782 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2783 rtd
->dai_link
->stream_name
,
2784 (rtd
->num_codecs
> 1) ?
2785 "multicodec" : asoc_rtd_to_codec(rtd
, 0)->name
, num
);
2787 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
, num
, playback
,
2791 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm %s for dailink %s: %d\n",
2792 new_name
, rtd
->dai_link
->name
, ret
);
2795 dev_dbg(rtd
->card
->dev
, "ASoC: registered pcm #%d %s\n",num
, new_name
);
2797 /* DAPM dai link stream work */
2798 if (rtd
->dai_link
->params
)
2799 rtd
->close_delayed_work_func
= codec2codec_close_delayed_work
;
2801 rtd
->close_delayed_work_func
= snd_soc_close_delayed_work
;
2803 pcm
->nonatomic
= rtd
->dai_link
->nonatomic
;
2805 pcm
->private_data
= rtd
;
2807 if (rtd
->dai_link
->no_pcm
|| rtd
->dai_link
->params
) {
2809 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
2811 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
2815 /* ASoC PCM operations */
2816 if (rtd
->dai_link
->dynamic
) {
2817 rtd
->ops
.open
= dpcm_fe_dai_open
;
2818 rtd
->ops
.hw_params
= dpcm_fe_dai_hw_params
;
2819 rtd
->ops
.prepare
= dpcm_fe_dai_prepare
;
2820 rtd
->ops
.trigger
= dpcm_fe_dai_trigger
;
2821 rtd
->ops
.hw_free
= dpcm_fe_dai_hw_free
;
2822 rtd
->ops
.close
= dpcm_fe_dai_close
;
2823 rtd
->ops
.pointer
= soc_pcm_pointer
;
2825 rtd
->ops
.open
= soc_pcm_open
;
2826 rtd
->ops
.hw_params
= soc_pcm_hw_params
;
2827 rtd
->ops
.prepare
= soc_pcm_prepare
;
2828 rtd
->ops
.trigger
= soc_pcm_trigger
;
2829 rtd
->ops
.hw_free
= soc_pcm_hw_free
;
2830 rtd
->ops
.close
= soc_pcm_close
;
2831 rtd
->ops
.pointer
= soc_pcm_pointer
;
2834 for_each_rtd_components(rtd
, i
, component
) {
2835 const struct snd_soc_component_driver
*drv
= component
->driver
;
2838 rtd
->ops
.ioctl
= snd_soc_pcm_component_ioctl
;
2840 rtd
->ops
.sync_stop
= snd_soc_pcm_component_sync_stop
;
2842 rtd
->ops
.copy_user
= snd_soc_pcm_component_copy_user
;
2844 rtd
->ops
.page
= snd_soc_pcm_component_page
;
2846 rtd
->ops
.mmap
= snd_soc_pcm_component_mmap
;
2850 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
2853 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &rtd
->ops
);
2855 ret
= snd_soc_pcm_component_new(rtd
);
2857 dev_err(rtd
->dev
, "ASoC: pcm %s constructor failed for dailink %s: %d\n",
2858 new_name
, rtd
->dai_link
->name
, ret
);
2862 pcm
->no_device_suspend
= true;
2864 dev_dbg(rtd
->card
->dev
, "%s <-> %s mapping ok\n",
2865 (rtd
->num_codecs
> 1) ? "multicodec" : asoc_rtd_to_codec(rtd
, 0)->name
,
2866 (rtd
->num_cpus
> 1) ? "multicpu" : asoc_rtd_to_cpu(rtd
, 0)->name
);
2870 /* is the current PCM operation for this FE ? */
2871 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
2873 if (fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
)
2877 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update
);
2879 /* is the current PCM operation for this BE ? */
2880 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime
*fe
,
2881 struct snd_soc_pcm_runtime
*be
, int stream
)
2883 if ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
) ||
2884 ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_BE
) &&
2885 be
->dpcm
[stream
].runtime_update
))
2889 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update
);
2891 /* get the substream for this BE */
2892 struct snd_pcm_substream
*
2893 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime
*be
, int stream
)
2895 return be
->pcm
->streams
[stream
].substream
;
2897 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream
);
2899 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime
*fe
,
2900 struct snd_soc_pcm_runtime
*be
,
2902 const enum snd_soc_dpcm_state
*states
,
2905 struct snd_soc_dpcm
*dpcm
;
2908 unsigned long flags
;
2911 spin_lock_irqsave(&fe
->card
->dpcm_lock
, flags
);
2912 for_each_dpcm_fe(be
, stream
, dpcm
) {
2917 state
= dpcm
->fe
->dpcm
[stream
].state
;
2918 for (i
= 0; i
< num_states
; i
++) {
2919 if (state
== states
[i
]) {
2925 spin_unlock_irqrestore(&fe
->card
->dpcm_lock
, flags
);
2927 /* it's safe to do this BE DAI */
2932 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2933 * are not running, paused or suspended for the specified stream direction.
2935 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime
*fe
,
2936 struct snd_soc_pcm_runtime
*be
, int stream
)
2938 const enum snd_soc_dpcm_state state
[] = {
2939 SND_SOC_DPCM_STATE_START
,
2940 SND_SOC_DPCM_STATE_PAUSED
,
2941 SND_SOC_DPCM_STATE_SUSPEND
,
2944 return snd_soc_dpcm_check_state(fe
, be
, stream
, state
, ARRAY_SIZE(state
));
2946 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop
);
2949 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2950 * running, paused or suspended for the specified stream direction.
2952 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime
*fe
,
2953 struct snd_soc_pcm_runtime
*be
, int stream
)
2955 const enum snd_soc_dpcm_state state
[] = {
2956 SND_SOC_DPCM_STATE_START
,
2957 SND_SOC_DPCM_STATE_PAUSED
,
2958 SND_SOC_DPCM_STATE_SUSPEND
,
2959 SND_SOC_DPCM_STATE_PREPARE
,
2962 return snd_soc_dpcm_check_state(fe
, be
, stream
, state
, ARRAY_SIZE(state
));
2964 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params
);