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/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/export.h>
20 #include <linux/debugfs.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dpcm.h>
26 #include <sound/soc-link.h>
27 #include <sound/initval.h>
29 #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
30 static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime
*rtd
,
31 const char *func
, int ret
)
33 /* Positive, Zero values are not errors */
37 /* Negative values might be errors */
45 "ASoC: error at %s on %s: %d\n",
46 func
, rtd
->dai_link
->name
, ret
);
52 /* is the current PCM operation for this FE ? */
54 static int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
56 if (fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
)
62 /* is the current PCM operation for this BE ? */
63 static int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime
*fe
,
64 struct snd_soc_pcm_runtime
*be
, int stream
)
66 if ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
) ||
67 ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_BE
) &&
68 be
->dpcm
[stream
].runtime_update
))
73 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime
*fe
,
74 struct snd_soc_pcm_runtime
*be
,
76 const enum snd_soc_dpcm_state
*states
,
79 struct snd_soc_dpcm
*dpcm
;
84 for_each_dpcm_fe(be
, stream
, dpcm
) {
89 state
= dpcm
->fe
->dpcm
[stream
].state
;
90 for (i
= 0; i
< num_states
; i
++) {
91 if (state
== states
[i
]) {
98 /* it's safe to do this BE DAI */
103 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
104 * are not running, paused or suspended for the specified stream direction.
106 static int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime
*fe
,
107 struct snd_soc_pcm_runtime
*be
, int stream
)
109 const enum snd_soc_dpcm_state state
[] = {
110 SND_SOC_DPCM_STATE_START
,
111 SND_SOC_DPCM_STATE_PAUSED
,
112 SND_SOC_DPCM_STATE_SUSPEND
,
115 return snd_soc_dpcm_check_state(fe
, be
, stream
, state
, ARRAY_SIZE(state
));
119 * We can only change hw params a BE DAI if any of it's FE are not prepared,
120 * running, paused or suspended for the specified stream direction.
122 static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime
*fe
,
123 struct snd_soc_pcm_runtime
*be
, int stream
)
125 const enum snd_soc_dpcm_state state
[] = {
126 SND_SOC_DPCM_STATE_START
,
127 SND_SOC_DPCM_STATE_PAUSED
,
128 SND_SOC_DPCM_STATE_SUSPEND
,
129 SND_SOC_DPCM_STATE_PREPARE
,
132 return snd_soc_dpcm_check_state(fe
, be
, stream
, state
, ARRAY_SIZE(state
));
136 * We can only prepare a BE DAI if any of it's FE are not prepared,
137 * running or paused for the specified stream direction.
139 static int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime
*fe
,
140 struct snd_soc_pcm_runtime
*be
, int stream
)
142 const enum snd_soc_dpcm_state state
[] = {
143 SND_SOC_DPCM_STATE_START
,
144 SND_SOC_DPCM_STATE_PAUSED
,
145 SND_SOC_DPCM_STATE_PREPARE
,
148 return snd_soc_dpcm_check_state(fe
, be
, stream
, state
, ARRAY_SIZE(state
));
151 #define DPCM_MAX_BE_USERS 8
153 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime
*rtd
)
155 return (rtd
)->dai_link
->num_cpus
== 1 ? snd_soc_rtd_to_cpu(rtd
, 0)->name
: "multicpu";
157 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime
*rtd
)
159 return (rtd
)->dai_link
->num_codecs
== 1 ? snd_soc_rtd_to_codec(rtd
, 0)->name
: "multicodec";
162 #ifdef CONFIG_DEBUG_FS
163 static const char *dpcm_state_string(enum snd_soc_dpcm_state state
)
166 case SND_SOC_DPCM_STATE_NEW
:
168 case SND_SOC_DPCM_STATE_OPEN
:
170 case SND_SOC_DPCM_STATE_HW_PARAMS
:
172 case SND_SOC_DPCM_STATE_PREPARE
:
174 case SND_SOC_DPCM_STATE_START
:
176 case SND_SOC_DPCM_STATE_STOP
:
178 case SND_SOC_DPCM_STATE_SUSPEND
:
180 case SND_SOC_DPCM_STATE_PAUSED
:
182 case SND_SOC_DPCM_STATE_HW_FREE
:
184 case SND_SOC_DPCM_STATE_CLOSE
:
191 static ssize_t
dpcm_show_state(struct snd_soc_pcm_runtime
*fe
,
192 int stream
, char *buf
, size_t size
)
194 struct snd_pcm_hw_params
*params
= &fe
->dpcm
[stream
].hw_params
;
195 struct snd_soc_dpcm
*dpcm
;
199 offset
+= scnprintf(buf
+ offset
, size
- offset
,
200 "[%s - %s]\n", fe
->dai_link
->name
,
201 stream
? "Capture" : "Playback");
203 offset
+= scnprintf(buf
+ offset
, size
- offset
, "State: %s\n",
204 dpcm_state_string(fe
->dpcm
[stream
].state
));
206 if ((fe
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
207 (fe
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
208 offset
+= scnprintf(buf
+ offset
, size
- offset
,
210 "Format = %s, Channels = %d, Rate = %d\n",
211 snd_pcm_format_name(params_format(params
)),
212 params_channels(params
),
213 params_rate(params
));
216 offset
+= scnprintf(buf
+ offset
, size
- offset
, "Backends:\n");
218 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
219 offset
+= scnprintf(buf
+ offset
, size
- offset
,
220 " No active DSP links\n");
224 for_each_dpcm_be(fe
, stream
, dpcm
) {
225 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
226 params
= &be
->dpcm
[stream
].hw_params
;
228 offset
+= scnprintf(buf
+ offset
, size
- offset
,
229 "- %s\n", be
->dai_link
->name
);
231 offset
+= scnprintf(buf
+ offset
, size
- offset
,
233 dpcm_state_string(be
->dpcm
[stream
].state
));
235 if ((be
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
236 (be
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
237 offset
+= scnprintf(buf
+ offset
, size
- offset
,
239 "Format = %s, Channels = %d, Rate = %d\n",
240 snd_pcm_format_name(params_format(params
)),
241 params_channels(params
),
242 params_rate(params
));
248 static ssize_t
dpcm_state_read_file(struct file
*file
, char __user
*user_buf
,
249 size_t count
, loff_t
*ppos
)
251 struct snd_soc_pcm_runtime
*fe
= file
->private_data
;
252 ssize_t out_count
= PAGE_SIZE
, offset
= 0, ret
= 0;
256 if (fe
->dai_link
->num_cpus
> 1) {
258 "%s doesn't support Multi CPU yet\n", __func__
);
262 buf
= kmalloc(out_count
, GFP_KERNEL
);
266 snd_soc_dpcm_mutex_lock(fe
);
267 for_each_pcm_streams(stream
)
268 if (snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe
, 0), stream
))
269 offset
+= dpcm_show_state(fe
, stream
,
272 snd_soc_dpcm_mutex_unlock(fe
);
274 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, offset
);
280 static const struct file_operations dpcm_state_fops
= {
282 .read
= dpcm_state_read_file
,
283 .llseek
= default_llseek
,
286 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime
*rtd
)
288 if (!rtd
->dai_link
->dynamic
)
291 if (!rtd
->card
->debugfs_card_root
)
294 rtd
->debugfs_dpcm_root
= debugfs_create_dir(rtd
->dai_link
->name
,
295 rtd
->card
->debugfs_card_root
);
297 debugfs_create_file("state", 0444, rtd
->debugfs_dpcm_root
,
298 rtd
, &dpcm_state_fops
);
301 static void dpcm_create_debugfs_state(struct snd_soc_dpcm
*dpcm
, int stream
)
305 name
= kasprintf(GFP_KERNEL
, "%s:%s", dpcm
->be
->dai_link
->name
,
306 snd_pcm_direction_name(stream
));
308 dpcm
->debugfs_state
= debugfs_create_dir(
309 name
, dpcm
->fe
->debugfs_dpcm_root
);
310 debugfs_create_u32("state", 0644, dpcm
->debugfs_state
,
316 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm
*dpcm
)
318 debugfs_remove_recursive(dpcm
->debugfs_state
);
322 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm
*dpcm
,
327 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm
*dpcm
)
332 /* Set FE's runtime_update state; the state is protected via PCM stream lock
333 * for avoiding the race with trigger callback.
334 * If the state is unset and a trigger is pending while the previous operation,
335 * process the pending trigger action here.
337 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
);
338 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime
*fe
,
339 int stream
, enum snd_soc_dpcm_update state
)
341 struct snd_pcm_substream
*substream
=
342 snd_soc_dpcm_get_substream(fe
, stream
);
344 snd_pcm_stream_lock_irq(substream
);
345 if (state
== SND_SOC_DPCM_UPDATE_NO
&& fe
->dpcm
[stream
].trigger_pending
) {
346 dpcm_fe_dai_do_trigger(substream
,
347 fe
->dpcm
[stream
].trigger_pending
- 1);
348 fe
->dpcm
[stream
].trigger_pending
= 0;
350 fe
->dpcm
[stream
].runtime_update
= state
;
351 snd_pcm_stream_unlock_irq(substream
);
354 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime
*be
,
355 int stream
, enum snd_soc_dpcm_update state
)
357 be
->dpcm
[stream
].runtime_update
= state
;
361 * snd_soc_runtime_action() - Increment/Decrement active count for
362 * PCM runtime components
363 * @rtd: ASoC PCM runtime that is activated
364 * @stream: Direction of the PCM stream
365 * @action: Activate stream if 1. Deactivate if -1.
367 * Increments/Decrements the active count for all the DAIs and components
368 * attached to a PCM runtime.
369 * Should typically be called when a stream is opened.
371 * Must be called with the rtd->card->pcm_mutex being held
373 void snd_soc_runtime_action(struct snd_soc_pcm_runtime
*rtd
,
374 int stream
, int action
)
376 struct snd_soc_component
*component
;
377 struct snd_soc_dai
*dai
;
380 snd_soc_dpcm_mutex_assert_held(rtd
);
382 for_each_rtd_dais(rtd
, i
, dai
)
383 snd_soc_dai_action(dai
, stream
, action
);
385 /* Increments/Decrements the active count for components without DAIs */
386 for_each_rtd_components(rtd
, i
, component
) {
387 if (component
->num_dai
)
389 component
->active
+= action
;
392 EXPORT_SYMBOL_GPL(snd_soc_runtime_action
);
395 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
396 * @rtd: The ASoC PCM runtime that should be checked.
398 * This function checks whether the power down delay should be ignored for a
399 * specific PCM runtime. Returns true if the delay is 0, if the DAI link has
400 * been configured to ignore the delay, or if none of the components benefits
401 * from having the delay.
403 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime
*rtd
)
405 struct snd_soc_component
*component
;
408 if (!rtd
->pmdown_time
|| rtd
->dai_link
->ignore_pmdown_time
)
411 for_each_rtd_components(rtd
, i
, component
)
412 if (component
->driver
->use_pmdown_time
)
413 /* No need to go through all components */
419 /* DPCM stream event, send event to FE and all active BEs. */
420 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime
*fe
, int dir
,
423 struct snd_soc_dpcm
*dpcm
;
425 snd_soc_dpcm_mutex_assert_held(fe
);
427 for_each_dpcm_be(fe
, dir
, dpcm
) {
429 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
431 dev_dbg(be
->dev
, "ASoC: BE %s event %d dir %d\n",
432 be
->dai_link
->name
, event
, dir
);
434 if ((event
== SND_SOC_DAPM_STREAM_STOP
) &&
435 (be
->dpcm
[dir
].users
>= 1))
438 snd_soc_dapm_stream_event(be
, dir
, event
);
441 snd_soc_dapm_stream_event(fe
, dir
, event
);
446 static void soc_pcm_set_dai_params(struct snd_soc_dai
*dai
,
447 struct snd_pcm_hw_params
*params
)
450 dai
->symmetric_rate
= params_rate(params
);
451 dai
->symmetric_channels
= params_channels(params
);
452 dai
->symmetric_sample_bits
= snd_pcm_format_physical_width(params_format(params
));
454 dai
->symmetric_rate
= 0;
455 dai
->symmetric_channels
= 0;
456 dai
->symmetric_sample_bits
= 0;
460 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
,
461 struct snd_soc_dai
*soc_dai
)
463 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
466 if (!snd_soc_dai_active(soc_dai
))
469 #define __soc_pcm_apply_symmetry(name, NAME) \
470 if (soc_dai->symmetric_##name && \
471 (soc_dai->driver->symmetric_##name || rtd->dai_link->symmetric_##name)) { \
472 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
473 #name, soc_dai->symmetric_##name); \
475 ret = snd_pcm_hw_constraint_single(substream->runtime, \
476 SNDRV_PCM_HW_PARAM_##NAME,\
477 soc_dai->symmetric_##name); \
479 dev_err(soc_dai->dev, \
480 "ASoC: Unable to apply %s constraint: %d\n",\
486 __soc_pcm_apply_symmetry(rate
, RATE
);
487 __soc_pcm_apply_symmetry(channels
, CHANNELS
);
488 __soc_pcm_apply_symmetry(sample_bits
, SAMPLE_BITS
);
493 static int soc_pcm_params_symmetry(struct snd_pcm_substream
*substream
,
494 struct snd_pcm_hw_params
*params
)
496 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
497 struct snd_soc_dai d
;
498 struct snd_soc_dai
*dai
;
499 struct snd_soc_dai
*cpu_dai
;
500 unsigned int symmetry
, i
;
503 soc_pcm_set_dai_params(&d
, params
);
505 #define __soc_pcm_params_symmetry(xxx) \
506 symmetry = rtd->dai_link->symmetric_##xxx; \
507 for_each_rtd_dais(rtd, i, dai) \
508 symmetry |= dai->driver->symmetric_##xxx; \
511 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
512 if (!snd_soc_dai_is_dummy(cpu_dai) && \
513 cpu_dai->symmetric_##xxx && \
514 cpu_dai->symmetric_##xxx != d.symmetric_##xxx) { \
515 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
516 #xxx, cpu_dai->name, cpu_dai->symmetric_##xxx, \
517 d.name, d.symmetric_##xxx); \
521 /* reject unmatched parameters when applying symmetry */
522 __soc_pcm_params_symmetry(rate
);
523 __soc_pcm_params_symmetry(channels
);
524 __soc_pcm_params_symmetry(sample_bits
);
529 static void soc_pcm_update_symmetry(struct snd_pcm_substream
*substream
)
531 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
532 struct snd_soc_dai_link
*link
= rtd
->dai_link
;
533 struct snd_soc_dai
*dai
;
534 unsigned int symmetry
, i
;
536 symmetry
= link
->symmetric_rate
||
537 link
->symmetric_channels
||
538 link
->symmetric_sample_bits
;
540 for_each_rtd_dais(rtd
, i
, dai
)
541 symmetry
= symmetry
||
542 dai
->driver
->symmetric_rate
||
543 dai
->driver
->symmetric_channels
||
544 dai
->driver
->symmetric_sample_bits
;
547 substream
->runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
550 static void soc_pcm_set_msb(struct snd_pcm_substream
*substream
, int bits
)
552 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
558 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0, 0, bits
);
560 dev_warn(rtd
->dev
, "ASoC: Failed to set MSB %d: %d\n",
564 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
)
566 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
567 struct snd_soc_dai
*cpu_dai
;
568 struct snd_soc_dai
*codec_dai
;
569 int stream
= substream
->stream
;
571 unsigned int bits
= 0, cpu_bits
= 0;
573 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
574 const struct snd_soc_pcm_stream
*pcm_codec
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
576 if (pcm_codec
->sig_bits
== 0) {
580 bits
= max(pcm_codec
->sig_bits
, bits
);
583 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
584 const struct snd_soc_pcm_stream
*pcm_cpu
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
586 if (pcm_cpu
->sig_bits
== 0) {
590 cpu_bits
= max(pcm_cpu
->sig_bits
, cpu_bits
);
593 soc_pcm_set_msb(substream
, bits
);
594 soc_pcm_set_msb(substream
, cpu_bits
);
597 static void soc_pcm_hw_init(struct snd_pcm_hardware
*hw
)
599 hw
->rates
= UINT_MAX
;
601 hw
->rate_max
= UINT_MAX
;
602 hw
->channels_min
= 0;
603 hw
->channels_max
= UINT_MAX
;
604 hw
->formats
= ULLONG_MAX
;
607 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware
*hw
,
608 const struct snd_soc_pcm_stream
*p
)
610 hw
->rates
= snd_pcm_rate_mask_intersect(hw
->rates
, p
->rates
);
612 /* setup hw->rate_min/max via hw->rates first */
613 snd_pcm_hw_limit_rates(hw
);
615 /* update hw->rate_min/max by snd_soc_pcm_stream */
616 hw
->rate_min
= max(hw
->rate_min
, p
->rate_min
);
617 hw
->rate_max
= min_not_zero(hw
->rate_max
, p
->rate_max
);
620 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware
*hw
,
621 const struct snd_soc_pcm_stream
*p
)
623 hw
->channels_min
= max(hw
->channels_min
, p
->channels_min
);
624 hw
->channels_max
= min(hw
->channels_max
, p
->channels_max
);
627 static void soc_pcm_hw_update_format(struct snd_pcm_hardware
*hw
,
628 const struct snd_soc_pcm_stream
*p
)
630 hw
->formats
&= p
->formats
;
633 static void soc_pcm_hw_update_subformat(struct snd_pcm_hardware
*hw
,
634 const struct snd_soc_pcm_stream
*p
)
636 hw
->subformats
&= p
->subformats
;
640 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
641 * @rtd: ASoC PCM runtime
642 * @hw: PCM hardware parameters (output)
643 * @stream: Direction of the PCM stream
645 * Calculates the subset of stream parameters supported by all DAIs
646 * associated with the PCM stream.
648 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime
*rtd
,
649 struct snd_pcm_hardware
*hw
, int stream
)
651 struct snd_soc_dai
*codec_dai
;
652 struct snd_soc_dai
*cpu_dai
;
653 const struct snd_soc_pcm_stream
*codec_stream
;
654 const struct snd_soc_pcm_stream
*cpu_stream
;
655 unsigned int cpu_chan_min
= 0, cpu_chan_max
= UINT_MAX
;
660 /* first calculate min/max only for CPUs in the DAI link */
661 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
664 * Skip CPUs which don't support the current stream type.
665 * Otherwise, since the rate, channel, and format values will
666 * zero in that case, we would have no usable settings left,
667 * causing the resulting setup to fail.
669 if (!snd_soc_dai_stream_valid(cpu_dai
, stream
))
672 cpu_stream
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
674 soc_pcm_hw_update_chan(hw
, cpu_stream
);
675 soc_pcm_hw_update_rate(hw
, cpu_stream
);
676 soc_pcm_hw_update_format(hw
, cpu_stream
);
677 soc_pcm_hw_update_subformat(hw
, cpu_stream
);
679 cpu_chan_min
= hw
->channels_min
;
680 cpu_chan_max
= hw
->channels_max
;
682 /* second calculate min/max only for CODECs in the DAI link */
683 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
686 * Skip CODECs which don't support the current stream type.
687 * Otherwise, since the rate, channel, and format values will
688 * zero in that case, we would have no usable settings left,
689 * causing the resulting setup to fail.
691 if (!snd_soc_dai_stream_valid(codec_dai
, stream
))
694 codec_stream
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
696 soc_pcm_hw_update_chan(hw
, codec_stream
);
697 soc_pcm_hw_update_rate(hw
, codec_stream
);
698 soc_pcm_hw_update_format(hw
, codec_stream
);
699 soc_pcm_hw_update_subformat(hw
, codec_stream
);
702 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
703 if (!hw
->channels_min
)
707 * chan min/max cannot be enforced if there are multiple CODEC DAIs
708 * connected to CPU DAI(s), use CPU DAI's directly and let
709 * channel allocation be fixed up later
711 if (rtd
->dai_link
->num_codecs
> 1) {
712 hw
->channels_min
= cpu_chan_min
;
713 hw
->channels_max
= cpu_chan_max
;
718 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw
);
720 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream
*substream
)
722 struct snd_pcm_hardware
*hw
= &substream
->runtime
->hw
;
723 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
724 u64 formats
= hw
->formats
;
727 * At least one CPU and one CODEC should match. Otherwise, we should
728 * have bailed out on a higher level, since there would be no CPU or
729 * CODEC to support the transfer direction in that case.
731 snd_soc_runtime_calc_hw(rtd
, hw
, substream
->stream
);
734 hw
->formats
&= formats
;
737 static int soc_pcm_components_open(struct snd_pcm_substream
*substream
)
739 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
740 struct snd_soc_component
*component
;
743 for_each_rtd_components(rtd
, i
, component
) {
744 ret
= snd_soc_component_module_get_when_open(component
, substream
);
748 ret
= snd_soc_component_open(component
, substream
);
756 static int soc_pcm_components_close(struct snd_pcm_substream
*substream
,
759 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
760 struct snd_soc_component
*component
;
763 for_each_rtd_components(rtd
, i
, component
) {
764 int r
= snd_soc_component_close(component
, substream
, rollback
);
766 ret
= r
; /* use last ret */
768 snd_soc_component_module_put_when_close(component
, substream
, rollback
);
774 static int soc_pcm_clean(struct snd_soc_pcm_runtime
*rtd
,
775 struct snd_pcm_substream
*substream
, int rollback
)
777 struct snd_soc_component
*component
;
778 struct snd_soc_dai
*dai
;
781 snd_soc_dpcm_mutex_assert_held(rtd
);
784 snd_soc_runtime_deactivate(rtd
, substream
->stream
);
786 /* Make sure DAI parameters cleared if the DAI becomes inactive */
787 for_each_rtd_dais(rtd
, i
, dai
) {
788 if (snd_soc_dai_active(dai
) == 0)
789 soc_pcm_set_dai_params(dai
, NULL
);
793 for_each_rtd_dais_reverse(rtd
, i
, dai
)
794 snd_soc_dai_shutdown(dai
, substream
, rollback
);
796 snd_soc_link_shutdown(substream
, rollback
);
798 soc_pcm_components_close(substream
, rollback
);
800 snd_soc_pcm_component_pm_runtime_put(rtd
, substream
, rollback
);
802 for_each_rtd_components(rtd
, i
, component
)
803 if (!snd_soc_component_active(component
))
804 pinctrl_pm_select_sleep_state(component
->dev
);
810 * Called by ALSA when a PCM substream is closed. Private data can be
811 * freed here. The cpu DAI, codec DAI, machine and components are also
814 static int __soc_pcm_close(struct snd_soc_pcm_runtime
*rtd
,
815 struct snd_pcm_substream
*substream
)
817 return soc_pcm_clean(rtd
, substream
, 0);
820 /* PCM close ops for non-DPCM streams */
821 static int soc_pcm_close(struct snd_pcm_substream
*substream
)
823 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
825 snd_soc_dpcm_mutex_lock(rtd
);
826 __soc_pcm_close(rtd
, substream
);
827 snd_soc_dpcm_mutex_unlock(rtd
);
831 static int soc_hw_sanity_check(struct snd_pcm_substream
*substream
)
833 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
834 struct snd_pcm_hardware
*hw
= &substream
->runtime
->hw
;
835 const char *name_cpu
= soc_cpu_dai_name(rtd
);
836 const char *name_codec
= soc_codec_dai_name(rtd
);
838 struct device
*dev
= rtd
->dev
;
848 err_msg
= "channels";
849 if (!hw
->channels_min
|| !hw
->channels_max
||
850 hw
->channels_min
> hw
->channels_max
)
853 dev_dbg(dev
, "ASoC: %s <-> %s info:\n", name_codec
,
855 dev_dbg(dev
, "ASoC: rate mask 0x%x\n", hw
->rates
);
856 dev_dbg(dev
, "ASoC: ch min %d max %d\n", hw
->channels_min
,
858 dev_dbg(dev
, "ASoC: rate min %d max %d\n", hw
->rate_min
,
864 dev_err(dev
, "ASoC: %s <-> %s No matching %s\n",
865 name_codec
, name_cpu
, err_msg
);
870 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
871 * then initialized and any private data can be allocated. This also calls
872 * startup for the cpu DAI, component, machine and codec DAI.
874 static int __soc_pcm_open(struct snd_soc_pcm_runtime
*rtd
,
875 struct snd_pcm_substream
*substream
)
877 struct snd_soc_component
*component
;
878 struct snd_soc_dai
*dai
;
881 snd_soc_dpcm_mutex_assert_held(rtd
);
883 for_each_rtd_components(rtd
, i
, component
)
884 pinctrl_pm_select_default_state(component
->dev
);
886 ret
= snd_soc_pcm_component_pm_runtime_get(rtd
, substream
);
890 ret
= soc_pcm_components_open(substream
);
894 ret
= snd_soc_link_startup(substream
);
898 /* startup the audio subsystem */
899 for_each_rtd_dais(rtd
, i
, dai
) {
900 ret
= snd_soc_dai_startup(dai
, substream
);
905 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
906 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
909 /* Check that the codec and cpu DAIs are compatible */
910 soc_pcm_init_runtime_hw(substream
);
912 soc_pcm_update_symmetry(substream
);
914 ret
= soc_hw_sanity_check(substream
);
918 soc_pcm_apply_msb(substream
);
920 /* Symmetry only applies if we've already got an active stream. */
921 for_each_rtd_dais(rtd
, i
, dai
) {
922 ret
= soc_pcm_apply_symmetry(substream
, dai
);
927 snd_soc_runtime_activate(rtd
, substream
->stream
);
931 soc_pcm_clean(rtd
, substream
, 1);
933 return soc_pcm_ret(rtd
, ret
);
936 /* PCM open ops for non-DPCM streams */
937 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
939 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
942 snd_soc_dpcm_mutex_lock(rtd
);
943 ret
= __soc_pcm_open(rtd
, substream
);
944 snd_soc_dpcm_mutex_unlock(rtd
);
949 * Called by ALSA when the PCM substream is prepared, can set format, sample
950 * rate, etc. This function is non atomic and can be called multiple times,
951 * it can refer to the runtime info.
953 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime
*rtd
,
954 struct snd_pcm_substream
*substream
)
956 struct snd_soc_dai
*dai
;
959 snd_soc_dpcm_mutex_assert_held(rtd
);
961 ret
= snd_soc_link_prepare(substream
);
965 ret
= snd_soc_pcm_component_prepare(substream
);
969 ret
= snd_soc_pcm_dai_prepare(substream
);
973 /* cancel any delayed stream shutdown that is pending */
974 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
977 cancel_delayed_work(&rtd
->delayed_work
);
980 snd_soc_dapm_stream_event(rtd
, substream
->stream
,
981 SND_SOC_DAPM_STREAM_START
);
983 for_each_rtd_dais(rtd
, i
, dai
) {
984 if (dai
->driver
->ops
&& !dai
->driver
->ops
->mute_unmute_on_trigger
)
985 snd_soc_dai_digital_mute(dai
, 0, substream
->stream
);
989 return soc_pcm_ret(rtd
, ret
);
992 /* PCM prepare ops for non-DPCM streams */
993 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
995 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
998 snd_soc_dpcm_mutex_lock(rtd
);
999 ret
= __soc_pcm_prepare(rtd
, substream
);
1000 snd_soc_dpcm_mutex_unlock(rtd
);
1004 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params
*params
,
1007 struct snd_interval
*interval
;
1008 int channels
= hweight_long(mask
);
1010 interval
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
1011 interval
->min
= channels
;
1012 interval
->max
= channels
;
1015 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime
*rtd
,
1016 struct snd_pcm_substream
*substream
, int rollback
)
1018 struct snd_soc_dai
*dai
;
1021 snd_soc_dpcm_mutex_assert_held(rtd
);
1023 /* clear the corresponding DAIs parameters when going to be inactive */
1024 for_each_rtd_dais(rtd
, i
, dai
) {
1025 if (snd_soc_dai_active(dai
) == 1)
1026 soc_pcm_set_dai_params(dai
, NULL
);
1028 if (snd_soc_dai_stream_active(dai
, substream
->stream
) == 1) {
1029 if (dai
->driver
->ops
&& !dai
->driver
->ops
->mute_unmute_on_trigger
)
1030 snd_soc_dai_digital_mute(dai
, 1, substream
->stream
);
1034 /* run the stream event */
1035 snd_soc_dapm_stream_stop(rtd
, substream
->stream
);
1037 /* free any machine hw params */
1038 snd_soc_link_hw_free(substream
, rollback
);
1040 /* free any component resources */
1041 snd_soc_pcm_component_hw_free(substream
, rollback
);
1043 /* now free hw params for the DAIs */
1044 for_each_rtd_dais(rtd
, i
, dai
)
1045 if (snd_soc_dai_stream_valid(dai
, substream
->stream
))
1046 snd_soc_dai_hw_free(dai
, substream
, rollback
);
1052 * Frees resources allocated by hw_params, can be called multiple times
1054 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime
*rtd
,
1055 struct snd_pcm_substream
*substream
)
1057 return soc_pcm_hw_clean(rtd
, substream
, 0);
1060 /* hw_free PCM ops for non-DPCM streams */
1061 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
1063 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1066 snd_soc_dpcm_mutex_lock(rtd
);
1067 ret
= __soc_pcm_hw_free(rtd
, substream
);
1068 snd_soc_dpcm_mutex_unlock(rtd
);
1073 * Called by ALSA when the hardware params are set by application. This
1074 * function can also be called multiple times and can allocate buffers
1075 * (using snd_pcm_lib_* ). It's non-atomic.
1077 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime
*rtd
,
1078 struct snd_pcm_substream
*substream
,
1079 struct snd_pcm_hw_params
*params
)
1081 struct snd_soc_dai
*cpu_dai
;
1082 struct snd_soc_dai
*codec_dai
;
1083 struct snd_pcm_hw_params tmp_params
;
1086 snd_soc_dpcm_mutex_assert_held(rtd
);
1088 ret
= soc_pcm_params_symmetry(substream
, params
);
1092 ret
= snd_soc_link_hw_params(substream
, params
);
1096 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
1097 unsigned int tdm_mask
= snd_soc_dai_tdm_mask_get(codec_dai
, substream
->stream
);
1100 * Skip CODECs which don't support the current stream type,
1101 * the idea being that if a CODEC is not used for the currently
1102 * set up transfer direction, it should not need to be
1103 * configured, especially since the configuration used might
1104 * not even be supported by that CODEC. There may be cases
1105 * however where a CODEC needs to be set up although it is
1106 * actually not being used for the transfer, e.g. if a
1107 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1108 * for the DAI link including a playback-only CODEC.
1109 * If this becomes necessary, we will have to augment the
1110 * machine driver setup with information on how to act, so
1111 * we can do the right thing here.
1113 if (!snd_soc_dai_stream_valid(codec_dai
, substream
->stream
))
1116 /* copy params for each codec */
1117 tmp_params
= *params
;
1119 /* fixup params based on TDM slot masks */
1121 soc_pcm_codec_params_fixup(&tmp_params
, tdm_mask
);
1123 ret
= snd_soc_dai_hw_params(codec_dai
, substream
,
1128 soc_pcm_set_dai_params(codec_dai
, &tmp_params
);
1129 snd_soc_dapm_update_dai(substream
, &tmp_params
, codec_dai
);
1132 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
1133 struct snd_soc_dai_link_ch_map
*ch_maps
;
1134 unsigned int ch_mask
= 0;
1138 * Skip CPUs which don't support the current stream
1139 * type. See soc_pcm_init_runtime_hw() for more details
1141 if (!snd_soc_dai_stream_valid(cpu_dai
, substream
->stream
))
1144 /* copy params for each cpu */
1145 tmp_params
= *params
;
1148 * construct cpu channel mask by combining ch_mask of each
1149 * codec which maps to the cpu.
1151 * soc.h :: [dai_link->ch_maps Image sample]
1153 for_each_rtd_ch_maps(rtd
, j
, ch_maps
)
1154 if (ch_maps
->cpu
== i
)
1155 ch_mask
|= ch_maps
->ch_mask
;
1157 /* fixup cpu channel number */
1159 soc_pcm_codec_params_fixup(&tmp_params
, ch_mask
);
1161 ret
= snd_soc_dai_hw_params(cpu_dai
, substream
, &tmp_params
);
1165 /* store the parameters for each DAI */
1166 soc_pcm_set_dai_params(cpu_dai
, &tmp_params
);
1167 snd_soc_dapm_update_dai(substream
, &tmp_params
, cpu_dai
);
1170 ret
= snd_soc_pcm_component_hw_params(substream
, params
);
1173 soc_pcm_hw_clean(rtd
, substream
, 1);
1175 return soc_pcm_ret(rtd
, ret
);
1178 /* hw_params PCM ops for non-DPCM streams */
1179 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
1180 struct snd_pcm_hw_params
*params
)
1182 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1185 snd_soc_dpcm_mutex_lock(rtd
);
1186 ret
= __soc_pcm_hw_params(rtd
, substream
, params
);
1187 snd_soc_dpcm_mutex_unlock(rtd
);
1191 #define TRIGGER_MAX 3
1192 static int (* const trigger
[][TRIGGER_MAX
])(struct snd_pcm_substream
*substream
, int cmd
, int rollback
) = {
1193 [SND_SOC_TRIGGER_ORDER_DEFAULT
] = {
1194 snd_soc_link_trigger
,
1195 snd_soc_pcm_component_trigger
,
1196 snd_soc_pcm_dai_trigger
,
1198 [SND_SOC_TRIGGER_ORDER_LDC
] = {
1199 snd_soc_link_trigger
,
1200 snd_soc_pcm_dai_trigger
,
1201 snd_soc_pcm_component_trigger
,
1205 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
1207 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1208 struct snd_soc_component
*component
;
1209 int ret
= 0, r
= 0, i
;
1211 int start
= 0, stop
= 0;
1214 * select START/STOP sequence
1216 for_each_rtd_components(rtd
, i
, component
) {
1217 if (component
->driver
->trigger_start
)
1218 start
= component
->driver
->trigger_start
;
1219 if (component
->driver
->trigger_stop
)
1220 stop
= component
->driver
->trigger_stop
;
1222 if (rtd
->dai_link
->trigger_start
)
1223 start
= rtd
->dai_link
->trigger_start
;
1224 if (rtd
->dai_link
->trigger_stop
)
1225 stop
= rtd
->dai_link
->trigger_stop
;
1227 if (start
< 0 || start
>= SND_SOC_TRIGGER_ORDER_MAX
||
1228 stop
< 0 || stop
>= SND_SOC_TRIGGER_ORDER_MAX
)
1235 case SNDRV_PCM_TRIGGER_START
:
1236 case SNDRV_PCM_TRIGGER_RESUME
:
1237 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1238 for (i
= 0; i
< TRIGGER_MAX
; i
++) {
1239 r
= trigger
[start
][i
](substream
, cmd
, 0);
1246 * Rollback if START failed
1247 * find correspond STOP command
1253 case SNDRV_PCM_TRIGGER_START
:
1254 cmd
= SNDRV_PCM_TRIGGER_STOP
;
1256 case SNDRV_PCM_TRIGGER_RESUME
:
1257 cmd
= SNDRV_PCM_TRIGGER_SUSPEND
;
1259 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1260 cmd
= SNDRV_PCM_TRIGGER_PAUSE_PUSH
;
1269 case SNDRV_PCM_TRIGGER_STOP
:
1270 case SNDRV_PCM_TRIGGER_SUSPEND
:
1271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1272 for (i
= TRIGGER_MAX
; i
> 0; i
--) {
1273 r
= trigger
[stop
][i
- 1](substream
, cmd
, rollback
);
1283 * soc level wrapper for pointer callback
1284 * If cpu_dai, codec_dai, component driver has the delay callback, then
1285 * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1287 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
1289 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1290 snd_pcm_uframes_t offset
= 0;
1291 snd_pcm_sframes_t codec_delay
= 0;
1292 snd_pcm_sframes_t cpu_delay
= 0;
1294 offset
= snd_soc_pcm_component_pointer(substream
);
1296 /* should be called *after* snd_soc_pcm_component_pointer() */
1297 snd_soc_pcm_dai_delay(substream
, &cpu_delay
, &codec_delay
);
1298 snd_soc_pcm_component_delay(substream
, &cpu_delay
, &codec_delay
);
1300 runtime
->delay
= cpu_delay
+ codec_delay
;
1305 /* connect a FE and BE */
1306 static int dpcm_be_connect(struct snd_soc_pcm_runtime
*fe
,
1307 struct snd_soc_pcm_runtime
*be
, int stream
)
1309 struct snd_pcm_substream
*fe_substream
;
1310 struct snd_pcm_substream
*be_substream
;
1311 struct snd_soc_dpcm
*dpcm
;
1313 snd_soc_dpcm_mutex_assert_held(fe
);
1315 /* only add new dpcms */
1316 for_each_dpcm_be(fe
, stream
, dpcm
) {
1317 if (dpcm
->be
== be
&& dpcm
->fe
== fe
)
1321 fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1322 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1324 if (!fe_substream
->pcm
->nonatomic
&& be_substream
->pcm
->nonatomic
) {
1325 dev_err(be
->dev
, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1329 if (fe_substream
->pcm
->nonatomic
&& !be_substream
->pcm
->nonatomic
) {
1330 dev_dbg(be
->dev
, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1331 be_substream
->pcm
->nonatomic
= 1;
1334 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
1340 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_NEW
;
1341 snd_pcm_stream_lock_irq(fe_substream
);
1342 list_add(&dpcm
->list_be
, &fe
->dpcm
[stream
].be_clients
);
1343 list_add(&dpcm
->list_fe
, &be
->dpcm
[stream
].fe_clients
);
1344 snd_pcm_stream_unlock_irq(fe_substream
);
1346 dev_dbg(fe
->dev
, "connected new DPCM %s path %s %s %s\n",
1347 snd_pcm_direction_name(stream
), fe
->dai_link
->name
,
1348 stream
? "<-" : "->", be
->dai_link
->name
);
1350 dpcm_create_debugfs_state(dpcm
, stream
);
1355 /* reparent a BE onto another FE */
1356 static void dpcm_be_reparent(struct snd_soc_pcm_runtime
*fe
,
1357 struct snd_soc_pcm_runtime
*be
, int stream
)
1359 struct snd_soc_dpcm
*dpcm
;
1360 struct snd_pcm_substream
*fe_substream
, *be_substream
;
1362 /* reparent if BE is connected to other FEs */
1363 if (!be
->dpcm
[stream
].users
)
1366 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1370 for_each_dpcm_fe(be
, stream
, dpcm
) {
1374 dev_dbg(fe
->dev
, "reparent %s path %s %s %s\n",
1375 snd_pcm_direction_name(stream
),
1376 dpcm
->fe
->dai_link
->name
,
1377 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1379 fe_substream
= snd_soc_dpcm_get_substream(dpcm
->fe
, stream
);
1380 be_substream
->runtime
= fe_substream
->runtime
;
1385 /* disconnect a BE and FE */
1386 void dpcm_be_disconnect(struct snd_soc_pcm_runtime
*fe
, int stream
)
1388 struct snd_soc_dpcm
*dpcm
, *d
;
1389 struct snd_pcm_substream
*substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1390 LIST_HEAD(deleted_dpcms
);
1392 snd_soc_dpcm_mutex_assert_held(fe
);
1394 snd_pcm_stream_lock_irq(substream
);
1395 for_each_dpcm_be_safe(fe
, stream
, dpcm
, d
) {
1396 dev_dbg(fe
->dev
, "ASoC: BE %s disconnect check for %s\n",
1397 snd_pcm_direction_name(stream
),
1398 dpcm
->be
->dai_link
->name
);
1400 if (dpcm
->state
!= SND_SOC_DPCM_LINK_STATE_FREE
)
1403 dev_dbg(fe
->dev
, "freed DSP %s path %s %s %s\n",
1404 snd_pcm_direction_name(stream
), fe
->dai_link
->name
,
1405 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1407 /* BEs still alive need new FE */
1408 dpcm_be_reparent(fe
, dpcm
->be
, stream
);
1410 list_del(&dpcm
->list_be
);
1411 list_move(&dpcm
->list_fe
, &deleted_dpcms
);
1413 snd_pcm_stream_unlock_irq(substream
);
1415 while (!list_empty(&deleted_dpcms
)) {
1416 dpcm
= list_first_entry(&deleted_dpcms
, struct snd_soc_dpcm
,
1418 list_del(&dpcm
->list_fe
);
1419 dpcm_remove_debugfs_state(dpcm
);
1424 /* get BE for DAI widget and stream */
1425 static struct snd_soc_pcm_runtime
*dpcm_get_be(struct snd_soc_card
*card
,
1426 struct snd_soc_dapm_widget
*widget
, int stream
)
1428 struct snd_soc_pcm_runtime
*be
;
1429 struct snd_soc_dapm_widget
*w
;
1430 struct snd_soc_dai
*dai
;
1433 dev_dbg(card
->dev
, "ASoC: find BE for widget %s\n", widget
->name
);
1435 for_each_card_rtds(card
, be
) {
1437 if (!be
->dai_link
->no_pcm
)
1440 if (!snd_soc_dpcm_get_substream(be
, stream
))
1443 for_each_rtd_dais(be
, i
, dai
) {
1444 w
= snd_soc_dai_get_widget(dai
, stream
);
1446 dev_dbg(card
->dev
, "ASoC: try BE : %s\n",
1447 w
? w
->name
: "(not set)");
1454 /* Widget provided is not a BE */
1458 int widget_in_list(struct snd_soc_dapm_widget_list
*list
,
1459 struct snd_soc_dapm_widget
*widget
)
1461 struct snd_soc_dapm_widget
*w
;
1464 for_each_dapm_widgets(list
, i
, w
)
1470 EXPORT_SYMBOL_GPL(widget_in_list
);
1472 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget
*widget
, enum snd_soc_dapm_direction dir
)
1474 struct snd_soc_card
*card
= widget
->dapm
->card
;
1475 struct snd_soc_pcm_runtime
*rtd
;
1478 /* adjust dir to stream */
1479 if (dir
== SND_SOC_DAPM_DIR_OUT
)
1480 stream
= SNDRV_PCM_STREAM_PLAYBACK
;
1482 stream
= SNDRV_PCM_STREAM_CAPTURE
;
1484 rtd
= dpcm_get_be(card
, widget
, stream
);
1490 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be
);
1492 int dpcm_path_get(struct snd_soc_pcm_runtime
*fe
,
1493 int stream
, struct snd_soc_dapm_widget_list
**list
)
1495 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(fe
, 0);
1498 if (fe
->dai_link
->num_cpus
> 1) {
1500 "%s doesn't support Multi CPU yet\n", __func__
);
1504 /* get number of valid DAI paths and their widgets */
1505 paths
= snd_soc_dapm_dai_get_connected_widgets(cpu_dai
, stream
, list
,
1506 fe
->card
->component_chaining
?
1507 NULL
: dpcm_end_walk_at_be
);
1510 dev_dbg(fe
->dev
, "ASoC: found %d audio %s paths\n", paths
,
1511 snd_pcm_direction_name(stream
));
1512 else if (paths
== 0)
1513 dev_dbg(fe
->dev
, "ASoC: %s no valid %s path\n", fe
->dai_link
->name
,
1514 snd_pcm_direction_name(stream
));
1519 void dpcm_path_put(struct snd_soc_dapm_widget_list
**list
)
1521 snd_soc_dapm_dai_free_widgets(list
);
1524 static bool dpcm_be_is_active(struct snd_soc_dpcm
*dpcm
, int stream
,
1525 struct snd_soc_dapm_widget_list
*list
)
1527 struct snd_soc_dai
*dai
;
1530 /* is there a valid DAI widget for this BE */
1531 for_each_rtd_dais(dpcm
->be
, i
, dai
) {
1532 struct snd_soc_dapm_widget
*widget
= snd_soc_dai_get_widget(dai
, stream
);
1535 * The BE is pruned only if none of the dai
1536 * widgets are in the active list.
1538 if (widget
&& widget_in_list(list
, widget
))
1545 static int dpcm_prune_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1546 struct snd_soc_dapm_widget_list
**list_
)
1548 struct snd_soc_dpcm
*dpcm
;
1551 /* Destroy any old FE <--> BE connections */
1552 for_each_dpcm_be(fe
, stream
, dpcm
) {
1553 if (dpcm_be_is_active(dpcm
, stream
, *list_
))
1556 dev_dbg(fe
->dev
, "ASoC: pruning %s BE %s for %s\n",
1557 snd_pcm_direction_name(stream
),
1558 dpcm
->be
->dai_link
->name
, fe
->dai_link
->name
);
1559 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1560 dpcm_set_be_update_state(dpcm
->be
, stream
, SND_SOC_DPCM_UPDATE_BE
);
1564 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", prune
);
1568 static int dpcm_add_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1569 struct snd_soc_dapm_widget_list
**list_
)
1571 struct snd_soc_card
*card
= fe
->card
;
1572 struct snd_soc_dapm_widget_list
*list
= *list_
;
1573 struct snd_soc_pcm_runtime
*be
;
1574 struct snd_soc_dapm_widget
*widget
;
1575 struct snd_pcm_substream
*fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1576 int i
, new = 0, err
;
1578 /* don't connect if FE is not running */
1579 if (!fe_substream
->runtime
&& !fe
->fe_compr
)
1582 /* Create any new FE <--> BE connections */
1583 for_each_dapm_widgets(list
, i
, widget
) {
1585 switch (widget
->id
) {
1586 case snd_soc_dapm_dai_in
:
1587 if (stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1590 case snd_soc_dapm_dai_out
:
1591 if (stream
!= SNDRV_PCM_STREAM_CAPTURE
)
1598 /* is there a valid BE rtd for this widget */
1599 be
= dpcm_get_be(card
, widget
, stream
);
1601 dev_dbg(fe
->dev
, "ASoC: no BE found for %s\n",
1607 * Filter for systems with 'component_chaining' enabled.
1608 * This helps to avoid unnecessary re-configuration of an
1609 * already active BE on such systems.
1611 if (fe
->card
->component_chaining
&&
1612 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1613 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1616 /* newly connected FE and BE */
1617 err
= dpcm_be_connect(fe
, be
, stream
);
1619 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
1622 } else if (err
== 0) /* already connected */
1626 dpcm_set_be_update_state(be
, stream
, SND_SOC_DPCM_UPDATE_BE
);
1630 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
1635 * Find the corresponding BE DAIs that source or sink audio to this
1638 int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
1639 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
1642 return dpcm_add_paths(fe
, stream
, list
);
1644 return dpcm_prune_paths(fe
, stream
, list
);
1647 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime
*fe
, int stream
)
1649 struct snd_soc_dpcm
*dpcm
;
1651 for_each_dpcm_be(fe
, stream
, dpcm
)
1652 dpcm_set_be_update_state(dpcm
->be
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1655 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime
*fe
, int stream
,
1656 int do_hw_free
, struct snd_soc_dpcm
*last
)
1658 struct snd_soc_dpcm
*dpcm
;
1660 /* disable any enabled and non active backends */
1661 for_each_dpcm_be(fe
, stream
, dpcm
) {
1662 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1663 struct snd_pcm_substream
*be_substream
=
1664 snd_soc_dpcm_get_substream(be
, stream
);
1669 /* is this op for this BE ? */
1670 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1673 if (be
->dpcm
[stream
].users
== 0) {
1674 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1675 snd_pcm_direction_name(stream
),
1676 be
->dpcm
[stream
].state
);
1680 if (--be
->dpcm
[stream
].users
!= 0)
1683 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) {
1687 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) {
1688 __soc_pcm_hw_free(be
, be_substream
);
1689 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1693 __soc_pcm_close(be
, be_substream
);
1694 be_substream
->runtime
= NULL
;
1695 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1699 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1701 struct snd_pcm_substream
*fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1702 struct snd_soc_pcm_runtime
*be
;
1703 struct snd_soc_dpcm
*dpcm
;
1706 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1707 for_each_dpcm_be(fe
, stream
, dpcm
) {
1708 struct snd_pcm_substream
*be_substream
;
1711 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1713 if (!be_substream
) {
1714 dev_err(be
->dev
, "ASoC: no backend %s stream\n",
1715 snd_pcm_direction_name(stream
));
1719 /* is this op for this BE ? */
1720 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1723 /* first time the dpcm is open ? */
1724 if (be
->dpcm
[stream
].users
== DPCM_MAX_BE_USERS
) {
1725 dev_err(be
->dev
, "ASoC: too many users %s at open %d\n",
1726 snd_pcm_direction_name(stream
),
1727 be
->dpcm
[stream
].state
);
1731 if (be
->dpcm
[stream
].users
++ != 0)
1734 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1735 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1738 dev_dbg(be
->dev
, "ASoC: open %s BE %s\n",
1739 snd_pcm_direction_name(stream
), be
->dai_link
->name
);
1741 be_substream
->runtime
= fe_substream
->runtime
;
1742 err
= __soc_pcm_open(be
, be_substream
);
1744 be
->dpcm
[stream
].users
--;
1745 if (be
->dpcm
[stream
].users
< 0)
1746 dev_err(be
->dev
, "ASoC: no users %s at unwind %d\n",
1747 snd_pcm_direction_name(stream
),
1748 be
->dpcm
[stream
].state
);
1750 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1753 be
->dpcm
[stream
].be_start
= 0;
1754 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1761 dpcm_be_dai_startup_rollback(fe
, stream
, dpcm
);
1763 return soc_pcm_ret(fe
, err
);
1766 static void dpcm_runtime_setup_fe(struct snd_pcm_substream
*substream
)
1768 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1769 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1770 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1771 struct snd_soc_dai
*dai
;
1772 int stream
= substream
->stream
;
1773 u64 formats
= hw
->formats
;
1776 soc_pcm_hw_init(hw
);
1779 hw
->formats
&= formats
;
1781 for_each_rtd_cpu_dais(fe
, i
, dai
) {
1782 const struct snd_soc_pcm_stream
*cpu_stream
;
1785 * Skip CPUs which don't support the current stream
1786 * type. See soc_pcm_init_runtime_hw() for more details
1788 if (!snd_soc_dai_stream_valid(dai
, stream
))
1791 cpu_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1793 soc_pcm_hw_update_rate(hw
, cpu_stream
);
1794 soc_pcm_hw_update_chan(hw
, cpu_stream
);
1795 soc_pcm_hw_update_format(hw
, cpu_stream
);
1796 soc_pcm_hw_update_subformat(hw
, cpu_stream
);
1801 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream
*substream
)
1803 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1804 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1805 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1806 struct snd_soc_dpcm
*dpcm
;
1807 struct snd_soc_dai
*dai
;
1808 int stream
= substream
->stream
;
1810 if (!fe
->dai_link
->dpcm_merged_format
)
1814 * It returns merged BE codec format
1815 * if FE want to use it (= dpcm_merged_format)
1818 for_each_dpcm_be(fe
, stream
, dpcm
) {
1819 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1820 const struct snd_soc_pcm_stream
*codec_stream
;
1823 for_each_rtd_codec_dais(be
, i
, dai
) {
1825 * Skip CODECs which don't support the current stream
1826 * type. See soc_pcm_init_runtime_hw() for more details
1828 if (!snd_soc_dai_stream_valid(dai
, stream
))
1831 codec_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1833 soc_pcm_hw_update_format(hw
, codec_stream
);
1834 soc_pcm_hw_update_subformat(hw
, codec_stream
);
1839 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream
*substream
)
1841 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1842 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1843 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1844 struct snd_soc_dpcm
*dpcm
;
1845 int stream
= substream
->stream
;
1847 if (!fe
->dai_link
->dpcm_merged_chan
)
1851 * It returns merged BE codec channel;
1852 * if FE want to use it (= dpcm_merged_chan)
1855 for_each_dpcm_be(fe
, stream
, dpcm
) {
1856 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1857 const struct snd_soc_pcm_stream
*cpu_stream
;
1858 struct snd_soc_dai
*dai
;
1861 for_each_rtd_cpu_dais(be
, i
, dai
) {
1863 * Skip CPUs which don't support the current stream
1864 * type. See soc_pcm_init_runtime_hw() for more details
1866 if (!snd_soc_dai_stream_valid(dai
, stream
))
1869 cpu_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1871 soc_pcm_hw_update_chan(hw
, cpu_stream
);
1875 * chan min/max cannot be enforced if there are multiple CODEC
1876 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1878 if (be
->dai_link
->num_codecs
== 1) {
1879 const struct snd_soc_pcm_stream
*codec_stream
= snd_soc_dai_get_pcm_stream(
1880 snd_soc_rtd_to_codec(be
, 0), stream
);
1882 soc_pcm_hw_update_chan(hw
, codec_stream
);
1887 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream
*substream
)
1889 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1890 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1891 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1892 struct snd_soc_dpcm
*dpcm
;
1893 int stream
= substream
->stream
;
1895 if (!fe
->dai_link
->dpcm_merged_rate
)
1899 * It returns merged BE codec channel;
1900 * if FE want to use it (= dpcm_merged_chan)
1903 for_each_dpcm_be(fe
, stream
, dpcm
) {
1904 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1905 const struct snd_soc_pcm_stream
*pcm
;
1906 struct snd_soc_dai
*dai
;
1909 for_each_rtd_dais(be
, i
, dai
) {
1911 * Skip DAIs which don't support the current stream
1912 * type. See soc_pcm_init_runtime_hw() for more details
1914 if (!snd_soc_dai_stream_valid(dai
, stream
))
1917 pcm
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1919 soc_pcm_hw_update_rate(hw
, pcm
);
1924 static int dpcm_apply_symmetry(struct snd_pcm_substream
*fe_substream
,
1927 struct snd_soc_dpcm
*dpcm
;
1928 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
1929 struct snd_soc_dai
*fe_cpu_dai
;
1933 /* apply symmetry for FE */
1934 soc_pcm_update_symmetry(fe_substream
);
1936 for_each_rtd_cpu_dais (fe
, i
, fe_cpu_dai
) {
1937 /* Symmetry only applies if we've got an active stream. */
1938 err
= soc_pcm_apply_symmetry(fe_substream
, fe_cpu_dai
);
1943 /* apply symmetry for BE */
1944 for_each_dpcm_be(fe
, stream
, dpcm
) {
1945 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1946 struct snd_pcm_substream
*be_substream
=
1947 snd_soc_dpcm_get_substream(be
, stream
);
1948 struct snd_soc_pcm_runtime
*rtd
;
1949 struct snd_soc_dai
*dai
;
1951 /* A backend may not have the requested substream */
1955 rtd
= snd_soc_substream_to_rtd(be_substream
);
1956 if (rtd
->dai_link
->be_hw_params_fixup
)
1959 soc_pcm_update_symmetry(be_substream
);
1961 /* Symmetry only applies if we've got an active stream. */
1962 for_each_rtd_dais(rtd
, i
, dai
) {
1963 err
= soc_pcm_apply_symmetry(fe_substream
, dai
);
1969 return soc_pcm_ret(fe
, err
);
1972 static int dpcm_fe_dai_startup(struct snd_pcm_substream
*fe_substream
)
1974 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
1975 int stream
= fe_substream
->stream
, ret
= 0;
1977 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1979 ret
= dpcm_be_dai_startup(fe
, stream
);
1983 dev_dbg(fe
->dev
, "ASoC: open FE %s\n", fe
->dai_link
->name
);
1985 /* start the DAI frontend */
1986 ret
= __soc_pcm_open(fe
, fe_substream
);
1990 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1992 dpcm_runtime_setup_fe(fe_substream
);
1994 dpcm_runtime_setup_be_format(fe_substream
);
1995 dpcm_runtime_setup_be_chan(fe_substream
);
1996 dpcm_runtime_setup_be_rate(fe_substream
);
1998 ret
= dpcm_apply_symmetry(fe_substream
, stream
);
2002 dpcm_be_dai_startup_unwind(fe
, stream
);
2004 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2006 return soc_pcm_ret(fe
, ret
);
2009 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream
*substream
)
2011 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2012 int stream
= substream
->stream
;
2014 snd_soc_dpcm_mutex_assert_held(fe
);
2016 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2018 /* shutdown the BEs */
2019 dpcm_be_dai_shutdown(fe
, stream
);
2021 dev_dbg(fe
->dev
, "ASoC: close FE %s\n", fe
->dai_link
->name
);
2023 /* now shutdown the frontend */
2024 __soc_pcm_close(fe
, substream
);
2026 /* run the stream stop event */
2027 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_STOP
);
2029 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
2030 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2034 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime
*fe
, int stream
)
2036 struct snd_soc_dpcm
*dpcm
;
2038 /* only hw_params backends that are either sinks or sources
2039 * to this frontend DAI */
2040 for_each_dpcm_be(fe
, stream
, dpcm
) {
2042 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2043 struct snd_pcm_substream
*be_substream
=
2044 snd_soc_dpcm_get_substream(be
, stream
);
2046 /* is this op for this BE ? */
2047 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2050 /* only free hw when no longer used - check all FEs */
2051 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2054 /* do not free hw if this BE is used by other FE */
2055 if (be
->dpcm
[stream
].users
> 1)
2058 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2059 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
2060 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
2061 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
) &&
2062 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2063 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
2066 dev_dbg(be
->dev
, "ASoC: hw_free BE %s\n",
2067 be
->dai_link
->name
);
2069 __soc_pcm_hw_free(be
, be_substream
);
2071 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
2075 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream
*substream
)
2077 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2078 int stream
= substream
->stream
;
2080 snd_soc_dpcm_mutex_lock(fe
);
2081 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2083 dev_dbg(fe
->dev
, "ASoC: hw_free FE %s\n", fe
->dai_link
->name
);
2085 /* call hw_free on the frontend */
2086 soc_pcm_hw_clean(fe
, substream
, 0);
2088 /* only hw_params backends that are either sinks or sources
2089 * to this frontend DAI */
2090 dpcm_be_dai_hw_free(fe
, stream
);
2092 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
2093 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2095 snd_soc_dpcm_mutex_unlock(fe
);
2099 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
2101 struct snd_soc_pcm_runtime
*be
;
2102 struct snd_pcm_substream
*be_substream
;
2103 struct snd_soc_dpcm
*dpcm
;
2106 for_each_dpcm_be(fe
, stream
, dpcm
) {
2107 struct snd_pcm_hw_params hw_params
;
2110 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2112 /* is this op for this BE ? */
2113 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2116 /* copy params for each dpcm */
2117 memcpy(&hw_params
, &fe
->dpcm
[stream
].hw_params
,
2118 sizeof(struct snd_pcm_hw_params
));
2120 /* perform any hw_params fixups */
2121 ret
= snd_soc_link_be_hw_params_fixup(be
, &hw_params
);
2125 /* copy the fixed-up hw params for BE dai */
2126 memcpy(&be
->dpcm
[stream
].hw_params
, &hw_params
,
2127 sizeof(struct snd_pcm_hw_params
));
2129 /* only allow hw_params() if no connected FEs are running */
2130 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
2133 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
2134 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2135 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
))
2138 dev_dbg(be
->dev
, "ASoC: hw_params BE %s\n",
2139 be
->dai_link
->name
);
2141 ret
= __soc_pcm_hw_params(be
, be_substream
, &hw_params
);
2145 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2150 dev_dbg(fe
->dev
, "ASoC: %s() failed at %s (%d)\n",
2151 __func__
, be
->dai_link
->name
, ret
);
2153 /* disable any enabled and non active backends */
2154 for_each_dpcm_be_rollback(fe
, stream
, dpcm
) {
2156 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2158 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2161 /* only allow hw_free() if no connected FEs are running */
2162 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2165 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
2166 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2167 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
2168 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
2171 __soc_pcm_hw_free(be
, be_substream
);
2177 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream
*substream
,
2178 struct snd_pcm_hw_params
*params
)
2180 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2181 int ret
, stream
= substream
->stream
;
2183 snd_soc_dpcm_mutex_lock(fe
);
2184 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2186 memcpy(&fe
->dpcm
[stream
].hw_params
, params
,
2187 sizeof(struct snd_pcm_hw_params
));
2188 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2192 dev_dbg(fe
->dev
, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2193 fe
->dai_link
->name
, params_rate(params
),
2194 params_channels(params
), params_format(params
));
2196 /* call hw_params on the frontend */
2197 ret
= __soc_pcm_hw_params(fe
, substream
, params
);
2199 dpcm_be_dai_hw_free(fe
, stream
);
2201 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2204 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2205 snd_soc_dpcm_mutex_unlock(fe
);
2207 return soc_pcm_ret(fe
, ret
);
2210 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
2213 struct snd_soc_pcm_runtime
*be
;
2214 bool pause_stop_transition
;
2215 struct snd_soc_dpcm
*dpcm
;
2216 unsigned long flags
;
2219 for_each_dpcm_be(fe
, stream
, dpcm
) {
2220 struct snd_pcm_substream
*be_substream
;
2223 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2225 snd_pcm_stream_lock_irqsave_nested(be_substream
, flags
);
2227 /* is this op for this BE ? */
2228 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2231 dev_dbg(be
->dev
, "ASoC: trigger BE %s cmd %d\n",
2232 be
->dai_link
->name
, cmd
);
2235 case SNDRV_PCM_TRIGGER_START
:
2236 if (!be
->dpcm
[stream
].be_start
&&
2237 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
2238 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2239 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2242 be
->dpcm
[stream
].be_start
++;
2243 if (be
->dpcm
[stream
].be_start
!= 1)
2246 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PAUSED
)
2247 ret
= soc_pcm_trigger(be_substream
,
2248 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
2250 ret
= soc_pcm_trigger(be_substream
,
2251 SNDRV_PCM_TRIGGER_START
);
2253 be
->dpcm
[stream
].be_start
--;
2257 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2259 case SNDRV_PCM_TRIGGER_RESUME
:
2260 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
2263 be
->dpcm
[stream
].be_start
++;
2264 if (be
->dpcm
[stream
].be_start
!= 1)
2267 ret
= soc_pcm_trigger(be_substream
, cmd
);
2269 be
->dpcm
[stream
].be_start
--;
2273 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2275 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2276 if (!be
->dpcm
[stream
].be_start
&&
2277 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
) &&
2278 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2281 fe
->dpcm
[stream
].fe_pause
= false;
2282 be
->dpcm
[stream
].be_pause
--;
2284 be
->dpcm
[stream
].be_start
++;
2285 if (be
->dpcm
[stream
].be_start
!= 1)
2288 ret
= soc_pcm_trigger(be_substream
, cmd
);
2290 be
->dpcm
[stream
].be_start
--;
2294 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2296 case SNDRV_PCM_TRIGGER_STOP
:
2297 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
) &&
2298 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2301 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_START
)
2302 be
->dpcm
[stream
].be_start
--;
2304 if (be
->dpcm
[stream
].be_start
!= 0)
2307 pause_stop_transition
= false;
2308 if (fe
->dpcm
[stream
].fe_pause
) {
2309 pause_stop_transition
= true;
2310 fe
->dpcm
[stream
].fe_pause
= false;
2311 be
->dpcm
[stream
].be_pause
--;
2314 if (be
->dpcm
[stream
].be_pause
!= 0)
2315 ret
= soc_pcm_trigger(be_substream
, SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
2317 ret
= soc_pcm_trigger(be_substream
, SNDRV_PCM_TRIGGER_STOP
);
2320 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_START
)
2321 be
->dpcm
[stream
].be_start
++;
2322 if (pause_stop_transition
) {
2323 fe
->dpcm
[stream
].fe_pause
= true;
2324 be
->dpcm
[stream
].be_pause
++;
2329 if (be
->dpcm
[stream
].be_pause
!= 0)
2330 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2332 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2335 case SNDRV_PCM_TRIGGER_SUSPEND
:
2336 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2339 be
->dpcm
[stream
].be_start
--;
2340 if (be
->dpcm
[stream
].be_start
!= 0)
2343 ret
= soc_pcm_trigger(be_substream
, cmd
);
2345 be
->dpcm
[stream
].be_start
++;
2349 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
2351 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2352 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2355 fe
->dpcm
[stream
].fe_pause
= true;
2356 be
->dpcm
[stream
].be_pause
++;
2358 be
->dpcm
[stream
].be_start
--;
2359 if (be
->dpcm
[stream
].be_start
!= 0)
2362 ret
= soc_pcm_trigger(be_substream
, cmd
);
2364 be
->dpcm
[stream
].be_start
++;
2368 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2372 snd_pcm_stream_unlock_irqrestore(be_substream
, flags
);
2376 return soc_pcm_ret(fe
, ret
);
2378 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger
);
2380 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream
*substream
,
2381 int cmd
, bool fe_first
)
2383 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2386 /* call trigger on the frontend before the backend. */
2388 dev_dbg(fe
->dev
, "ASoC: pre trigger FE %s cmd %d\n",
2389 fe
->dai_link
->name
, cmd
);
2391 ret
= soc_pcm_trigger(substream
, cmd
);
2395 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2399 /* call trigger on the frontend after the backend. */
2400 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2404 dev_dbg(fe
->dev
, "ASoC: post trigger FE %s cmd %d\n",
2405 fe
->dai_link
->name
, cmd
);
2407 ret
= soc_pcm_trigger(substream
, cmd
);
2412 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2414 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2415 int stream
= substream
->stream
;
2417 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2419 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
2422 case SND_SOC_DPCM_TRIGGER_PRE
:
2424 case SNDRV_PCM_TRIGGER_START
:
2425 case SNDRV_PCM_TRIGGER_RESUME
:
2426 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2427 case SNDRV_PCM_TRIGGER_DRAIN
:
2428 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2430 case SNDRV_PCM_TRIGGER_STOP
:
2431 case SNDRV_PCM_TRIGGER_SUSPEND
:
2432 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2433 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2440 case SND_SOC_DPCM_TRIGGER_POST
:
2442 case SNDRV_PCM_TRIGGER_START
:
2443 case SNDRV_PCM_TRIGGER_RESUME
:
2444 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2445 case SNDRV_PCM_TRIGGER_DRAIN
:
2446 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2448 case SNDRV_PCM_TRIGGER_STOP
:
2449 case SNDRV_PCM_TRIGGER_SUSPEND
:
2450 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2451 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2459 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
2460 fe
->dai_link
->name
);
2466 dev_err(fe
->dev
, "ASoC: trigger FE cmd: %d failed: %d\n",
2472 case SNDRV_PCM_TRIGGER_START
:
2473 case SNDRV_PCM_TRIGGER_RESUME
:
2474 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2475 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2477 case SNDRV_PCM_TRIGGER_STOP
:
2478 case SNDRV_PCM_TRIGGER_SUSPEND
:
2479 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2481 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2482 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2487 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
2491 static int dpcm_fe_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2493 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2494 int stream
= substream
->stream
;
2496 /* if FE's runtime_update is already set, we're in race;
2497 * process this trigger later at exit
2499 if (fe
->dpcm
[stream
].runtime_update
!= SND_SOC_DPCM_UPDATE_NO
) {
2500 fe
->dpcm
[stream
].trigger_pending
= cmd
+ 1;
2501 return 0; /* delayed, assuming it's successful */
2504 /* we're alone, let's trigger */
2505 return dpcm_fe_dai_do_trigger(substream
, cmd
);
2508 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
2510 struct snd_soc_dpcm
*dpcm
;
2513 for_each_dpcm_be(fe
, stream
, dpcm
) {
2515 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2516 struct snd_pcm_substream
*be_substream
=
2517 snd_soc_dpcm_get_substream(be
, stream
);
2519 /* is this op for this BE ? */
2520 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2523 if (!snd_soc_dpcm_can_be_prepared(fe
, be
, stream
))
2526 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2527 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2528 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
) &&
2529 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2532 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
2533 be
->dai_link
->name
);
2535 ret
= __soc_pcm_prepare(be
, be_substream
);
2539 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2542 return soc_pcm_ret(fe
, ret
);
2545 static int dpcm_fe_dai_prepare(struct snd_pcm_substream
*substream
)
2547 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2548 int stream
= substream
->stream
, ret
= 0;
2550 snd_soc_dpcm_mutex_lock(fe
);
2552 dev_dbg(fe
->dev
, "ASoC: prepare FE %s\n", fe
->dai_link
->name
);
2554 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2556 /* there is no point preparing this FE if there are no BEs */
2557 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2558 /* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles */
2559 dev_err_once(fe
->dev
, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
2560 fe
->dai_link
->name
);
2561 dev_dbg(fe
->dev
, "ASoC: no backend DAIs enabled for %s\n",
2562 fe
->dai_link
->name
);
2567 ret
= dpcm_be_dai_prepare(fe
, stream
);
2571 /* call prepare on the frontend */
2572 ret
= __soc_pcm_prepare(fe
, substream
);
2576 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2579 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2580 snd_soc_dpcm_mutex_unlock(fe
);
2582 return soc_pcm_ret(fe
, ret
);
2585 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
2589 dev_dbg(fe
->dev
, "ASoC: runtime %s close on FE %s\n",
2590 snd_pcm_direction_name(stream
), fe
->dai_link
->name
);
2592 err
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_STOP
);
2594 dpcm_be_dai_hw_free(fe
, stream
);
2596 dpcm_be_dai_shutdown(fe
, stream
);
2598 /* run the stream event for each BE */
2599 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2601 return soc_pcm_ret(fe
, err
);
2604 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
2606 struct snd_soc_dpcm
*dpcm
;
2609 dev_dbg(fe
->dev
, "ASoC: runtime %s open on FE %s\n",
2610 snd_pcm_direction_name(stream
), fe
->dai_link
->name
);
2612 /* Only start the BE if the FE is ready */
2613 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_FREE
||
2614 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
) {
2615 dev_err(fe
->dev
, "ASoC: FE %s is not ready %d\n",
2616 fe
->dai_link
->name
, fe
->dpcm
[stream
].state
);
2621 /* startup must always be called for new BEs */
2622 ret
= dpcm_be_dai_startup(fe
, stream
);
2626 /* keep going if FE state is > open */
2627 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
2630 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2634 /* keep going if FE state is > hw_params */
2635 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
2638 ret
= dpcm_be_dai_prepare(fe
, stream
);
2642 /* run the stream event for each BE */
2643 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2645 /* keep going if FE state is > prepare */
2646 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PREPARE
||
2647 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_STOP
)
2650 ret
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_START
);
2657 dpcm_be_dai_hw_free(fe
, stream
);
2659 dpcm_be_dai_shutdown(fe
, stream
);
2661 /* disconnect any pending BEs */
2662 for_each_dpcm_be(fe
, stream
, dpcm
) {
2663 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2665 /* is this op for this BE ? */
2666 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2669 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
||
2670 be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_NEW
)
2671 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2674 return soc_pcm_ret(fe
, ret
);
2677 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime
*fe
, int new)
2679 struct snd_soc_dapm_widget_list
*list
;
2683 if (!fe
->dai_link
->dynamic
)
2686 if (fe
->dai_link
->num_cpus
> 1) {
2688 "%s doesn't support Multi CPU yet\n", __func__
);
2692 /* only check active links */
2693 if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe
, 0)))
2696 /* DAPM sync will call this to update DSP paths */
2697 dev_dbg(fe
->dev
, "ASoC: DPCM %s runtime update for FE %s\n",
2698 new ? "new" : "old", fe
->dai_link
->name
);
2700 for_each_pcm_streams(stream
) {
2702 /* skip if FE doesn't have playback/capture capability */
2703 if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe
, 0), stream
) ||
2704 !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe
, 0), stream
))
2707 /* skip if FE isn't currently playing/capturing */
2708 if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe
, 0), stream
) ||
2709 !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe
, 0), stream
))
2712 paths
= dpcm_path_get(fe
, stream
, &list
);
2716 /* update any playback/capture paths */
2717 count
= dpcm_process_paths(fe
, stream
, &list
, new);
2719 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_BE
);
2721 dpcm_run_update_startup(fe
, stream
);
2723 dpcm_run_update_shutdown(fe
, stream
);
2724 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2726 dpcm_clear_pending_state(fe
, stream
);
2727 dpcm_be_disconnect(fe
, stream
);
2730 dpcm_path_put(&list
);
2736 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2739 int snd_soc_dpcm_runtime_update(struct snd_soc_card
*card
)
2741 struct snd_soc_pcm_runtime
*fe
;
2744 snd_soc_dpcm_mutex_lock(card
);
2745 /* shutdown all old paths first */
2746 for_each_card_rtds(card
, fe
) {
2747 ret
= soc_dpcm_fe_runtime_update(fe
, 0);
2752 /* bring new paths up */
2753 for_each_card_rtds(card
, fe
) {
2754 ret
= soc_dpcm_fe_runtime_update(fe
, 1);
2760 snd_soc_dpcm_mutex_unlock(card
);
2763 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update
);
2765 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream
*fe_substream
)
2767 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2768 struct snd_soc_dpcm
*dpcm
;
2769 int stream
= fe_substream
->stream
;
2771 snd_soc_dpcm_mutex_assert_held(fe
);
2773 /* mark FE's links ready to prune */
2774 for_each_dpcm_be(fe
, stream
, dpcm
)
2775 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2777 dpcm_be_disconnect(fe
, stream
);
2780 static int dpcm_fe_dai_close(struct snd_pcm_substream
*fe_substream
)
2782 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2785 snd_soc_dpcm_mutex_lock(fe
);
2786 ret
= dpcm_fe_dai_shutdown(fe_substream
);
2788 dpcm_fe_dai_cleanup(fe_substream
);
2790 snd_soc_dpcm_mutex_unlock(fe
);
2794 static int dpcm_fe_dai_open(struct snd_pcm_substream
*fe_substream
)
2796 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2797 struct snd_soc_dapm_widget_list
*list
;
2799 int stream
= fe_substream
->stream
;
2801 snd_soc_dpcm_mutex_lock(fe
);
2803 ret
= dpcm_path_get(fe
, stream
, &list
);
2807 /* calculate valid and active FE <-> BE dpcms */
2808 dpcm_process_paths(fe
, stream
, &list
, 1);
2810 ret
= dpcm_fe_dai_startup(fe_substream
);
2812 dpcm_fe_dai_cleanup(fe_substream
);
2814 dpcm_clear_pending_state(fe
, stream
);
2815 dpcm_path_put(&list
);
2817 snd_soc_dpcm_mutex_unlock(fe
);
2821 static int soc_get_playback_capture(struct snd_soc_pcm_runtime
*rtd
,
2822 int *playback
, int *capture
)
2824 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
2825 struct snd_soc_dai
*cpu_dai
;
2826 struct snd_soc_dai
*codec_dai
;
2827 struct snd_soc_dai_link_ch_map
*ch_maps
;
2828 struct snd_soc_dai
*dummy_dai
= snd_soc_find_dai(&snd_soc_dummy_dlc
);
2831 int has_playback
= 0;
2832 int has_capture
= 0;
2835 if (dai_link
->dynamic
&& dai_link
->num_cpus
> 1) {
2836 dev_err(rtd
->dev
, "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2840 /* Adapt stream for codec2codec links */
2841 cpu_capture
= snd_soc_get_stream_cpu(dai_link
, SNDRV_PCM_STREAM_CAPTURE
);
2842 cpu_playback
= snd_soc_get_stream_cpu(dai_link
, SNDRV_PCM_STREAM_PLAYBACK
);
2846 * soc.h :: [dai_link->ch_maps Image sample]
2848 for_each_rtd_ch_maps(rtd
, i
, ch_maps
) {
2849 cpu_dai
= snd_soc_rtd_to_cpu(rtd
, ch_maps
->cpu
);
2850 codec_dai
= snd_soc_rtd_to_codec(rtd
, ch_maps
->codec
);
2855 * DPCM Codec has been no checked before.
2856 * It should be checked, but it breaks compatibility.
2858 * For example there is a case that CPU have loopback capabilities which is used
2859 * for tests on boards where the Codec has no capture capabilities. In this case,
2860 * Codec capture validation check will be fail, but system should allow capture
2861 * capabilities. We have no solution for it today.
2863 if (dai_link
->dynamic
|| dai_link
->no_pcm
)
2864 codec_dai
= dummy_dai
;
2866 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_PLAYBACK
) &&
2867 snd_soc_dai_stream_valid(cpu_dai
, cpu_playback
))
2869 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_CAPTURE
) &&
2870 snd_soc_dai_stream_valid(cpu_dai
, cpu_capture
))
2874 if (dai_link
->playback_only
)
2877 if (dai_link
->capture_only
)
2880 if (!has_playback
&& !has_capture
) {
2881 dev_err(rtd
->dev
, "substream %s has no playback, no capture\n",
2882 dai_link
->stream_name
);
2887 *playback
= has_playback
;
2888 *capture
= has_capture
;
2893 static int soc_create_pcm(struct snd_pcm
**pcm
,
2894 struct snd_soc_pcm_runtime
*rtd
,
2895 int playback
, int capture
)
2900 /* create the PCM */
2901 if (rtd
->dai_link
->c2c_params
) {
2902 snprintf(new_name
, sizeof(new_name
), "codec2codec(%s)",
2903 rtd
->dai_link
->stream_name
);
2905 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, rtd
->id
,
2906 playback
, capture
, pcm
);
2907 } else if (rtd
->dai_link
->no_pcm
) {
2908 snprintf(new_name
, sizeof(new_name
), "(%s)",
2909 rtd
->dai_link
->stream_name
);
2911 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, rtd
->id
,
2912 playback
, capture
, pcm
);
2914 if (rtd
->dai_link
->dynamic
)
2915 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2916 rtd
->dai_link
->stream_name
);
2918 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2919 rtd
->dai_link
->stream_name
,
2920 soc_codec_dai_name(rtd
), rtd
->id
);
2922 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
, rtd
->id
, playback
,
2926 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm %s for dailink %s: %d\n",
2927 new_name
, rtd
->dai_link
->name
, ret
);
2930 dev_dbg(rtd
->card
->dev
, "ASoC: registered pcm #%d %s\n", rtd
->id
, new_name
);
2935 /* create a new pcm */
2936 int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
)
2938 struct snd_soc_component
*component
;
2939 struct snd_pcm
*pcm
;
2940 int ret
= 0, playback
= 0, capture
= 0;
2943 ret
= soc_get_playback_capture(rtd
, &playback
, &capture
);
2947 ret
= soc_create_pcm(&pcm
, rtd
, playback
, capture
);
2951 /* DAPM dai link stream work */
2953 * Currently nothing to do for c2c links
2954 * Since c2c links are internal nodes in the DAPM graph and
2955 * don't interface with the outside world or application layer
2956 * we don't have to do any special handling on close.
2958 if (!rtd
->dai_link
->c2c_params
)
2959 rtd
->close_delayed_work_func
= snd_soc_close_delayed_work
;
2962 pcm
->nonatomic
= rtd
->dai_link
->nonatomic
;
2963 pcm
->private_data
= rtd
;
2964 pcm
->no_device_suspend
= true;
2966 if (rtd
->dai_link
->no_pcm
|| rtd
->dai_link
->c2c_params
) {
2968 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
2970 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
2974 /* ASoC PCM operations */
2975 if (rtd
->dai_link
->dynamic
) {
2976 rtd
->ops
.open
= dpcm_fe_dai_open
;
2977 rtd
->ops
.hw_params
= dpcm_fe_dai_hw_params
;
2978 rtd
->ops
.prepare
= dpcm_fe_dai_prepare
;
2979 rtd
->ops
.trigger
= dpcm_fe_dai_trigger
;
2980 rtd
->ops
.hw_free
= dpcm_fe_dai_hw_free
;
2981 rtd
->ops
.close
= dpcm_fe_dai_close
;
2982 rtd
->ops
.pointer
= soc_pcm_pointer
;
2984 rtd
->ops
.open
= soc_pcm_open
;
2985 rtd
->ops
.hw_params
= soc_pcm_hw_params
;
2986 rtd
->ops
.prepare
= soc_pcm_prepare
;
2987 rtd
->ops
.trigger
= soc_pcm_trigger
;
2988 rtd
->ops
.hw_free
= soc_pcm_hw_free
;
2989 rtd
->ops
.close
= soc_pcm_close
;
2990 rtd
->ops
.pointer
= soc_pcm_pointer
;
2993 for_each_rtd_components(rtd
, i
, component
) {
2994 const struct snd_soc_component_driver
*drv
= component
->driver
;
2997 rtd
->ops
.ioctl
= snd_soc_pcm_component_ioctl
;
2999 rtd
->ops
.sync_stop
= snd_soc_pcm_component_sync_stop
;
3001 rtd
->ops
.copy
= snd_soc_pcm_component_copy
;
3003 rtd
->ops
.page
= snd_soc_pcm_component_page
;
3005 rtd
->ops
.mmap
= snd_soc_pcm_component_mmap
;
3007 rtd
->ops
.ack
= snd_soc_pcm_component_ack
;
3011 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
3014 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &rtd
->ops
);
3016 ret
= snd_soc_pcm_component_new(rtd
);
3020 dev_dbg(rtd
->card
->dev
, "%s <-> %s mapping ok\n",
3021 soc_codec_dai_name(rtd
), soc_cpu_dai_name(rtd
));
3025 /* get the substream for this BE */
3026 struct snd_pcm_substream
*
3027 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime
*be
, int stream
)
3029 return be
->pcm
->streams
[stream
].substream
;
3031 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream
);