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 */
420 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
421 * @substream: the pcm substream
422 * @hw: the hardware parameters
424 * Sets the substream runtime hardware parameters.
426 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
427 const struct snd_pcm_hardware
*hw
)
429 substream
->runtime
->hw
= *hw
;
433 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
435 /* DPCM stream event, send event to FE and all active BEs. */
436 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime
*fe
, int dir
,
439 struct snd_soc_dpcm
*dpcm
;
441 snd_soc_dpcm_mutex_assert_held(fe
);
443 for_each_dpcm_be(fe
, dir
, dpcm
) {
445 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
447 dev_dbg(be
->dev
, "ASoC: BE %s event %d dir %d\n",
448 be
->dai_link
->name
, event
, dir
);
450 if ((event
== SND_SOC_DAPM_STREAM_STOP
) &&
451 (be
->dpcm
[dir
].users
>= 1))
454 snd_soc_dapm_stream_event(be
, dir
, event
);
457 snd_soc_dapm_stream_event(fe
, dir
, event
);
462 static void soc_pcm_set_dai_params(struct snd_soc_dai
*dai
,
463 struct snd_pcm_hw_params
*params
)
466 dai
->rate
= params_rate(params
);
467 dai
->channels
= params_channels(params
);
468 dai
->sample_bits
= snd_pcm_format_physical_width(params_format(params
));
472 dai
->sample_bits
= 0;
476 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
,
477 struct snd_soc_dai
*soc_dai
)
479 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
482 if (!snd_soc_dai_active(soc_dai
))
485 #define __soc_pcm_apply_symmetry(name, NAME) \
486 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
487 rtd->dai_link->symmetric_##name)) { \
488 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
489 #name, soc_dai->name); \
491 ret = snd_pcm_hw_constraint_single(substream->runtime, \
492 SNDRV_PCM_HW_PARAM_##NAME,\
495 dev_err(soc_dai->dev, \
496 "ASoC: Unable to apply %s constraint: %d\n",\
502 __soc_pcm_apply_symmetry(rate
, RATE
);
503 __soc_pcm_apply_symmetry(channels
, CHANNELS
);
504 __soc_pcm_apply_symmetry(sample_bits
, SAMPLE_BITS
);
509 static int soc_pcm_params_symmetry(struct snd_pcm_substream
*substream
,
510 struct snd_pcm_hw_params
*params
)
512 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
513 struct snd_soc_dai d
;
514 struct snd_soc_dai
*dai
;
515 struct snd_soc_dai
*cpu_dai
;
516 unsigned int symmetry
, i
;
519 soc_pcm_set_dai_params(&d
, params
);
521 #define __soc_pcm_params_symmetry(xxx) \
522 symmetry = rtd->dai_link->symmetric_##xxx; \
523 for_each_rtd_dais(rtd, i, dai) \
524 symmetry |= dai->driver->symmetric_##xxx; \
527 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
528 if (!snd_soc_dai_is_dummy(cpu_dai) && \
529 cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
530 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
531 #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
535 /* reject unmatched parameters when applying symmetry */
536 __soc_pcm_params_symmetry(rate
);
537 __soc_pcm_params_symmetry(channels
);
538 __soc_pcm_params_symmetry(sample_bits
);
543 static void soc_pcm_update_symmetry(struct snd_pcm_substream
*substream
)
545 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
546 struct snd_soc_dai_link
*link
= rtd
->dai_link
;
547 struct snd_soc_dai
*dai
;
548 unsigned int symmetry
, i
;
550 symmetry
= link
->symmetric_rate
||
551 link
->symmetric_channels
||
552 link
->symmetric_sample_bits
;
554 for_each_rtd_dais(rtd
, i
, dai
)
555 symmetry
= symmetry
||
556 dai
->driver
->symmetric_rate
||
557 dai
->driver
->symmetric_channels
||
558 dai
->driver
->symmetric_sample_bits
;
561 substream
->runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
564 static void soc_pcm_set_msb(struct snd_pcm_substream
*substream
, int bits
)
566 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
572 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0, 0, bits
);
574 dev_warn(rtd
->dev
, "ASoC: Failed to set MSB %d: %d\n",
578 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
)
580 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
581 struct snd_soc_dai
*cpu_dai
;
582 struct snd_soc_dai
*codec_dai
;
583 int stream
= substream
->stream
;
585 unsigned int bits
= 0, cpu_bits
= 0;
587 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
588 const struct snd_soc_pcm_stream
*pcm_codec
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
590 if (pcm_codec
->sig_bits
== 0) {
594 bits
= max(pcm_codec
->sig_bits
, bits
);
597 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
598 const struct snd_soc_pcm_stream
*pcm_cpu
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
600 if (pcm_cpu
->sig_bits
== 0) {
604 cpu_bits
= max(pcm_cpu
->sig_bits
, cpu_bits
);
607 soc_pcm_set_msb(substream
, bits
);
608 soc_pcm_set_msb(substream
, cpu_bits
);
611 static void soc_pcm_hw_init(struct snd_pcm_hardware
*hw
)
613 hw
->rates
= UINT_MAX
;
615 hw
->rate_max
= UINT_MAX
;
616 hw
->channels_min
= 0;
617 hw
->channels_max
= UINT_MAX
;
618 hw
->formats
= ULLONG_MAX
;
621 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware
*hw
,
622 const struct snd_soc_pcm_stream
*p
)
624 hw
->rates
= snd_pcm_rate_mask_intersect(hw
->rates
, p
->rates
);
626 /* setup hw->rate_min/max via hw->rates first */
627 snd_pcm_hw_limit_rates(hw
);
629 /* update hw->rate_min/max by snd_soc_pcm_stream */
630 hw
->rate_min
= max(hw
->rate_min
, p
->rate_min
);
631 hw
->rate_max
= min_not_zero(hw
->rate_max
, p
->rate_max
);
634 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware
*hw
,
635 const struct snd_soc_pcm_stream
*p
)
637 hw
->channels_min
= max(hw
->channels_min
, p
->channels_min
);
638 hw
->channels_max
= min(hw
->channels_max
, p
->channels_max
);
641 static void soc_pcm_hw_update_format(struct snd_pcm_hardware
*hw
,
642 const struct snd_soc_pcm_stream
*p
)
644 hw
->formats
&= p
->formats
;
647 static void soc_pcm_hw_update_subformat(struct snd_pcm_hardware
*hw
,
648 const struct snd_soc_pcm_stream
*p
)
650 hw
->subformats
&= p
->subformats
;
654 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
655 * @rtd: ASoC PCM runtime
656 * @hw: PCM hardware parameters (output)
657 * @stream: Direction of the PCM stream
659 * Calculates the subset of stream parameters supported by all DAIs
660 * associated with the PCM stream.
662 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime
*rtd
,
663 struct snd_pcm_hardware
*hw
, int stream
)
665 struct snd_soc_dai
*codec_dai
;
666 struct snd_soc_dai
*cpu_dai
;
667 const struct snd_soc_pcm_stream
*codec_stream
;
668 const struct snd_soc_pcm_stream
*cpu_stream
;
669 unsigned int cpu_chan_min
= 0, cpu_chan_max
= UINT_MAX
;
674 /* first calculate min/max only for CPUs in the DAI link */
675 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
678 * Skip CPUs which don't support the current stream type.
679 * Otherwise, since the rate, channel, and format values will
680 * zero in that case, we would have no usable settings left,
681 * causing the resulting setup to fail.
683 if (!snd_soc_dai_stream_valid(cpu_dai
, stream
))
686 cpu_stream
= snd_soc_dai_get_pcm_stream(cpu_dai
, stream
);
688 soc_pcm_hw_update_chan(hw
, cpu_stream
);
689 soc_pcm_hw_update_rate(hw
, cpu_stream
);
690 soc_pcm_hw_update_format(hw
, cpu_stream
);
691 soc_pcm_hw_update_subformat(hw
, cpu_stream
);
693 cpu_chan_min
= hw
->channels_min
;
694 cpu_chan_max
= hw
->channels_max
;
696 /* second calculate min/max only for CODECs in the DAI link */
697 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
700 * Skip CODECs which don't support the current stream type.
701 * Otherwise, since the rate, channel, and format values will
702 * zero in that case, we would have no usable settings left,
703 * causing the resulting setup to fail.
705 if (!snd_soc_dai_stream_valid(codec_dai
, stream
))
708 codec_stream
= snd_soc_dai_get_pcm_stream(codec_dai
, stream
);
710 soc_pcm_hw_update_chan(hw
, codec_stream
);
711 soc_pcm_hw_update_rate(hw
, codec_stream
);
712 soc_pcm_hw_update_format(hw
, codec_stream
);
713 soc_pcm_hw_update_subformat(hw
, codec_stream
);
716 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
717 if (!hw
->channels_min
)
721 * chan min/max cannot be enforced if there are multiple CODEC DAIs
722 * connected to CPU DAI(s), use CPU DAI's directly and let
723 * channel allocation be fixed up later
725 if (rtd
->dai_link
->num_codecs
> 1) {
726 hw
->channels_min
= cpu_chan_min
;
727 hw
->channels_max
= cpu_chan_max
;
732 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw
);
734 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream
*substream
)
736 struct snd_pcm_hardware
*hw
= &substream
->runtime
->hw
;
737 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
738 u64 formats
= hw
->formats
;
741 * At least one CPU and one CODEC should match. Otherwise, we should
742 * have bailed out on a higher level, since there would be no CPU or
743 * CODEC to support the transfer direction in that case.
745 snd_soc_runtime_calc_hw(rtd
, hw
, substream
->stream
);
748 hw
->formats
&= formats
;
751 static int soc_pcm_components_open(struct snd_pcm_substream
*substream
)
753 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
754 struct snd_soc_component
*component
;
757 for_each_rtd_components(rtd
, i
, component
) {
758 ret
= snd_soc_component_module_get_when_open(component
, substream
);
762 ret
= snd_soc_component_open(component
, substream
);
770 static int soc_pcm_components_close(struct snd_pcm_substream
*substream
,
773 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
774 struct snd_soc_component
*component
;
777 for_each_rtd_components(rtd
, i
, component
) {
778 int r
= snd_soc_component_close(component
, substream
, rollback
);
780 ret
= r
; /* use last ret */
782 snd_soc_component_module_put_when_close(component
, substream
, rollback
);
788 static int soc_pcm_clean(struct snd_soc_pcm_runtime
*rtd
,
789 struct snd_pcm_substream
*substream
, int rollback
)
791 struct snd_soc_component
*component
;
792 struct snd_soc_dai
*dai
;
795 snd_soc_dpcm_mutex_assert_held(rtd
);
798 snd_soc_runtime_deactivate(rtd
, substream
->stream
);
800 /* Make sure DAI parameters cleared if the DAI becomes inactive */
801 for_each_rtd_dais(rtd
, i
, dai
) {
802 if (snd_soc_dai_active(dai
) == 0 &&
803 (dai
->rate
|| dai
->channels
|| dai
->sample_bits
))
804 soc_pcm_set_dai_params(dai
, NULL
);
808 for_each_rtd_dais_reverse(rtd
, i
, dai
)
809 snd_soc_dai_shutdown(dai
, substream
, rollback
);
811 snd_soc_link_shutdown(substream
, rollback
);
813 soc_pcm_components_close(substream
, rollback
);
815 snd_soc_pcm_component_pm_runtime_put(rtd
, substream
, rollback
);
817 for_each_rtd_components(rtd
, i
, component
)
818 if (!snd_soc_component_active(component
))
819 pinctrl_pm_select_sleep_state(component
->dev
);
825 * Called by ALSA when a PCM substream is closed. Private data can be
826 * freed here. The cpu DAI, codec DAI, machine and components are also
829 static int __soc_pcm_close(struct snd_soc_pcm_runtime
*rtd
,
830 struct snd_pcm_substream
*substream
)
832 return soc_pcm_clean(rtd
, substream
, 0);
835 /* PCM close ops for non-DPCM streams */
836 static int soc_pcm_close(struct snd_pcm_substream
*substream
)
838 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
840 snd_soc_dpcm_mutex_lock(rtd
);
841 __soc_pcm_close(rtd
, substream
);
842 snd_soc_dpcm_mutex_unlock(rtd
);
846 static int soc_hw_sanity_check(struct snd_pcm_substream
*substream
)
848 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
849 struct snd_pcm_hardware
*hw
= &substream
->runtime
->hw
;
850 const char *name_cpu
= soc_cpu_dai_name(rtd
);
851 const char *name_codec
= soc_codec_dai_name(rtd
);
853 struct device
*dev
= rtd
->dev
;
863 err_msg
= "channels";
864 if (!hw
->channels_min
|| !hw
->channels_max
||
865 hw
->channels_min
> hw
->channels_max
)
868 dev_dbg(dev
, "ASoC: %s <-> %s info:\n", name_codec
,
870 dev_dbg(dev
, "ASoC: rate mask 0x%x\n", hw
->rates
);
871 dev_dbg(dev
, "ASoC: ch min %d max %d\n", hw
->channels_min
,
873 dev_dbg(dev
, "ASoC: rate min %d max %d\n", hw
->rate_min
,
879 dev_err(dev
, "ASoC: %s <-> %s No matching %s\n",
880 name_codec
, name_cpu
, err_msg
);
885 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
886 * then initialized and any private data can be allocated. This also calls
887 * startup for the cpu DAI, component, machine and codec DAI.
889 static int __soc_pcm_open(struct snd_soc_pcm_runtime
*rtd
,
890 struct snd_pcm_substream
*substream
)
892 struct snd_soc_component
*component
;
893 struct snd_soc_dai
*dai
;
896 snd_soc_dpcm_mutex_assert_held(rtd
);
898 for_each_rtd_components(rtd
, i
, component
)
899 pinctrl_pm_select_default_state(component
->dev
);
901 ret
= snd_soc_pcm_component_pm_runtime_get(rtd
, substream
);
905 ret
= soc_pcm_components_open(substream
);
909 ret
= snd_soc_link_startup(substream
);
913 /* startup the audio subsystem */
914 for_each_rtd_dais(rtd
, i
, dai
) {
915 ret
= snd_soc_dai_startup(dai
, substream
);
920 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
921 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
924 /* Check that the codec and cpu DAIs are compatible */
925 soc_pcm_init_runtime_hw(substream
);
927 soc_pcm_update_symmetry(substream
);
929 ret
= soc_hw_sanity_check(substream
);
933 soc_pcm_apply_msb(substream
);
935 /* Symmetry only applies if we've already got an active stream. */
936 for_each_rtd_dais(rtd
, i
, dai
) {
937 ret
= soc_pcm_apply_symmetry(substream
, dai
);
942 snd_soc_runtime_activate(rtd
, substream
->stream
);
946 soc_pcm_clean(rtd
, substream
, 1);
948 return soc_pcm_ret(rtd
, ret
);
951 /* PCM open ops for non-DPCM streams */
952 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
954 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
957 snd_soc_dpcm_mutex_lock(rtd
);
958 ret
= __soc_pcm_open(rtd
, substream
);
959 snd_soc_dpcm_mutex_unlock(rtd
);
964 * Called by ALSA when the PCM substream is prepared, can set format, sample
965 * rate, etc. This function is non atomic and can be called multiple times,
966 * it can refer to the runtime info.
968 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime
*rtd
,
969 struct snd_pcm_substream
*substream
)
971 struct snd_soc_dai
*dai
;
974 snd_soc_dpcm_mutex_assert_held(rtd
);
976 ret
= snd_soc_link_prepare(substream
);
980 ret
= snd_soc_pcm_component_prepare(substream
);
984 ret
= snd_soc_pcm_dai_prepare(substream
);
988 /* cancel any delayed stream shutdown that is pending */
989 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
992 cancel_delayed_work(&rtd
->delayed_work
);
995 snd_soc_dapm_stream_event(rtd
, substream
->stream
,
996 SND_SOC_DAPM_STREAM_START
);
998 for_each_rtd_dais(rtd
, i
, dai
) {
999 if (dai
->driver
->ops
&& !dai
->driver
->ops
->mute_unmute_on_trigger
)
1000 snd_soc_dai_digital_mute(dai
, 0, substream
->stream
);
1004 return soc_pcm_ret(rtd
, ret
);
1007 /* PCM prepare ops for non-DPCM streams */
1008 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
1010 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1013 snd_soc_dpcm_mutex_lock(rtd
);
1014 ret
= __soc_pcm_prepare(rtd
, substream
);
1015 snd_soc_dpcm_mutex_unlock(rtd
);
1019 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params
*params
,
1022 struct snd_interval
*interval
;
1023 int channels
= hweight_long(mask
);
1025 interval
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
1026 interval
->min
= channels
;
1027 interval
->max
= channels
;
1030 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime
*rtd
,
1031 struct snd_pcm_substream
*substream
, int rollback
)
1033 struct snd_soc_dai
*dai
;
1036 snd_soc_dpcm_mutex_assert_held(rtd
);
1038 /* clear the corresponding DAIs parameters when going to be inactive */
1039 for_each_rtd_dais(rtd
, i
, dai
) {
1040 if (snd_soc_dai_active(dai
) == 1)
1041 soc_pcm_set_dai_params(dai
, NULL
);
1043 if (snd_soc_dai_stream_active(dai
, substream
->stream
) == 1) {
1044 if (dai
->driver
->ops
&& !dai
->driver
->ops
->mute_unmute_on_trigger
)
1045 snd_soc_dai_digital_mute(dai
, 1, substream
->stream
);
1049 /* run the stream event */
1050 snd_soc_dapm_stream_stop(rtd
, substream
->stream
);
1052 /* free any machine hw params */
1053 snd_soc_link_hw_free(substream
, rollback
);
1055 /* free any component resources */
1056 snd_soc_pcm_component_hw_free(substream
, rollback
);
1058 /* now free hw params for the DAIs */
1059 for_each_rtd_dais(rtd
, i
, dai
)
1060 if (snd_soc_dai_stream_valid(dai
, substream
->stream
))
1061 snd_soc_dai_hw_free(dai
, substream
, rollback
);
1067 * Frees resources allocated by hw_params, can be called multiple times
1069 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime
*rtd
,
1070 struct snd_pcm_substream
*substream
)
1072 return soc_pcm_hw_clean(rtd
, substream
, 0);
1075 /* hw_free PCM ops for non-DPCM streams */
1076 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
1078 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1081 snd_soc_dpcm_mutex_lock(rtd
);
1082 ret
= __soc_pcm_hw_free(rtd
, substream
);
1083 snd_soc_dpcm_mutex_unlock(rtd
);
1088 * Called by ALSA when the hardware params are set by application. This
1089 * function can also be called multiple times and can allocate buffers
1090 * (using snd_pcm_lib_* ). It's non-atomic.
1092 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime
*rtd
,
1093 struct snd_pcm_substream
*substream
,
1094 struct snd_pcm_hw_params
*params
)
1096 struct snd_soc_dai
*cpu_dai
;
1097 struct snd_soc_dai
*codec_dai
;
1098 struct snd_pcm_hw_params tmp_params
;
1101 snd_soc_dpcm_mutex_assert_held(rtd
);
1103 ret
= soc_pcm_params_symmetry(substream
, params
);
1107 ret
= snd_soc_link_hw_params(substream
, params
);
1111 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
1112 unsigned int tdm_mask
= snd_soc_dai_tdm_mask_get(codec_dai
, substream
->stream
);
1115 * Skip CODECs which don't support the current stream type,
1116 * the idea being that if a CODEC is not used for the currently
1117 * set up transfer direction, it should not need to be
1118 * configured, especially since the configuration used might
1119 * not even be supported by that CODEC. There may be cases
1120 * however where a CODEC needs to be set up although it is
1121 * actually not being used for the transfer, e.g. if a
1122 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1123 * for the DAI link including a playback-only CODEC.
1124 * If this becomes necessary, we will have to augment the
1125 * machine driver setup with information on how to act, so
1126 * we can do the right thing here.
1128 if (!snd_soc_dai_stream_valid(codec_dai
, substream
->stream
))
1131 /* copy params for each codec */
1132 tmp_params
= *params
;
1134 /* fixup params based on TDM slot masks */
1136 soc_pcm_codec_params_fixup(&tmp_params
, tdm_mask
);
1138 ret
= snd_soc_dai_hw_params(codec_dai
, substream
,
1143 soc_pcm_set_dai_params(codec_dai
, &tmp_params
);
1144 snd_soc_dapm_update_dai(substream
, &tmp_params
, codec_dai
);
1147 for_each_rtd_cpu_dais(rtd
, i
, cpu_dai
) {
1148 struct snd_soc_dai_link_ch_map
*ch_maps
;
1149 unsigned int ch_mask
= 0;
1153 * Skip CPUs which don't support the current stream
1154 * type. See soc_pcm_init_runtime_hw() for more details
1156 if (!snd_soc_dai_stream_valid(cpu_dai
, substream
->stream
))
1159 /* copy params for each cpu */
1160 tmp_params
= *params
;
1163 * construct cpu channel mask by combining ch_mask of each
1164 * codec which maps to the cpu.
1166 * soc.h :: [dai_link->ch_maps Image sample]
1168 for_each_rtd_ch_maps(rtd
, j
, ch_maps
)
1169 if (ch_maps
->cpu
== i
)
1170 ch_mask
|= ch_maps
->ch_mask
;
1172 /* fixup cpu channel number */
1174 soc_pcm_codec_params_fixup(&tmp_params
, ch_mask
);
1176 ret
= snd_soc_dai_hw_params(cpu_dai
, substream
, &tmp_params
);
1180 /* store the parameters for each DAI */
1181 soc_pcm_set_dai_params(cpu_dai
, &tmp_params
);
1182 snd_soc_dapm_update_dai(substream
, &tmp_params
, cpu_dai
);
1185 ret
= snd_soc_pcm_component_hw_params(substream
, params
);
1188 soc_pcm_hw_clean(rtd
, substream
, 1);
1190 return soc_pcm_ret(rtd
, ret
);
1193 /* hw_params PCM ops for non-DPCM streams */
1194 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
1195 struct snd_pcm_hw_params
*params
)
1197 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1200 snd_soc_dpcm_mutex_lock(rtd
);
1201 ret
= __soc_pcm_hw_params(rtd
, substream
, params
);
1202 snd_soc_dpcm_mutex_unlock(rtd
);
1206 #define TRIGGER_MAX 3
1207 static int (* const trigger
[][TRIGGER_MAX
])(struct snd_pcm_substream
*substream
, int cmd
, int rollback
) = {
1208 [SND_SOC_TRIGGER_ORDER_DEFAULT
] = {
1209 snd_soc_link_trigger
,
1210 snd_soc_pcm_component_trigger
,
1211 snd_soc_pcm_dai_trigger
,
1213 [SND_SOC_TRIGGER_ORDER_LDC
] = {
1214 snd_soc_link_trigger
,
1215 snd_soc_pcm_dai_trigger
,
1216 snd_soc_pcm_component_trigger
,
1220 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
1222 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1223 struct snd_soc_component
*component
;
1224 int ret
= 0, r
= 0, i
;
1226 int start
= 0, stop
= 0;
1229 * select START/STOP sequence
1231 for_each_rtd_components(rtd
, i
, component
) {
1232 if (component
->driver
->trigger_start
)
1233 start
= component
->driver
->trigger_start
;
1234 if (component
->driver
->trigger_stop
)
1235 stop
= component
->driver
->trigger_stop
;
1237 if (rtd
->dai_link
->trigger_start
)
1238 start
= rtd
->dai_link
->trigger_start
;
1239 if (rtd
->dai_link
->trigger_stop
)
1240 stop
= rtd
->dai_link
->trigger_stop
;
1242 if (start
< 0 || start
>= SND_SOC_TRIGGER_ORDER_MAX
||
1243 stop
< 0 || stop
>= SND_SOC_TRIGGER_ORDER_MAX
)
1250 case SNDRV_PCM_TRIGGER_START
:
1251 case SNDRV_PCM_TRIGGER_RESUME
:
1252 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1253 for (i
= 0; i
< TRIGGER_MAX
; i
++) {
1254 r
= trigger
[start
][i
](substream
, cmd
, 0);
1261 * Rollback if START failed
1262 * find correspond STOP command
1268 case SNDRV_PCM_TRIGGER_START
:
1269 cmd
= SNDRV_PCM_TRIGGER_STOP
;
1271 case SNDRV_PCM_TRIGGER_RESUME
:
1272 cmd
= SNDRV_PCM_TRIGGER_SUSPEND
;
1274 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1275 cmd
= SNDRV_PCM_TRIGGER_PAUSE_PUSH
;
1284 case SNDRV_PCM_TRIGGER_STOP
:
1285 case SNDRV_PCM_TRIGGER_SUSPEND
:
1286 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1287 for (i
= TRIGGER_MAX
; i
> 0; i
--) {
1288 r
= trigger
[stop
][i
- 1](substream
, cmd
, rollback
);
1298 * soc level wrapper for pointer callback
1299 * If cpu_dai, codec_dai, component driver has the delay callback, then
1300 * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1302 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
1304 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1305 snd_pcm_uframes_t offset
= 0;
1306 snd_pcm_sframes_t codec_delay
= 0;
1307 snd_pcm_sframes_t cpu_delay
= 0;
1309 offset
= snd_soc_pcm_component_pointer(substream
);
1311 /* should be called *after* snd_soc_pcm_component_pointer() */
1312 snd_soc_pcm_dai_delay(substream
, &cpu_delay
, &codec_delay
);
1313 snd_soc_pcm_component_delay(substream
, &cpu_delay
, &codec_delay
);
1315 runtime
->delay
= cpu_delay
+ codec_delay
;
1320 /* connect a FE and BE */
1321 static int dpcm_be_connect(struct snd_soc_pcm_runtime
*fe
,
1322 struct snd_soc_pcm_runtime
*be
, int stream
)
1324 struct snd_pcm_substream
*fe_substream
;
1325 struct snd_pcm_substream
*be_substream
;
1326 struct snd_soc_dpcm
*dpcm
;
1328 snd_soc_dpcm_mutex_assert_held(fe
);
1330 /* only add new dpcms */
1331 for_each_dpcm_be(fe
, stream
, dpcm
) {
1332 if (dpcm
->be
== be
&& dpcm
->fe
== fe
)
1336 fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1337 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1339 if (!fe_substream
->pcm
->nonatomic
&& be_substream
->pcm
->nonatomic
) {
1340 dev_err(be
->dev
, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1344 if (fe_substream
->pcm
->nonatomic
&& !be_substream
->pcm
->nonatomic
) {
1345 dev_dbg(be
->dev
, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1346 be_substream
->pcm
->nonatomic
= 1;
1349 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
1355 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_NEW
;
1356 snd_pcm_stream_lock_irq(fe_substream
);
1357 list_add(&dpcm
->list_be
, &fe
->dpcm
[stream
].be_clients
);
1358 list_add(&dpcm
->list_fe
, &be
->dpcm
[stream
].fe_clients
);
1359 snd_pcm_stream_unlock_irq(fe_substream
);
1361 dev_dbg(fe
->dev
, "connected new DPCM %s path %s %s %s\n",
1362 snd_pcm_direction_name(stream
), fe
->dai_link
->name
,
1363 stream
? "<-" : "->", be
->dai_link
->name
);
1365 dpcm_create_debugfs_state(dpcm
, stream
);
1370 /* reparent a BE onto another FE */
1371 static void dpcm_be_reparent(struct snd_soc_pcm_runtime
*fe
,
1372 struct snd_soc_pcm_runtime
*be
, int stream
)
1374 struct snd_soc_dpcm
*dpcm
;
1375 struct snd_pcm_substream
*fe_substream
, *be_substream
;
1377 /* reparent if BE is connected to other FEs */
1378 if (!be
->dpcm
[stream
].users
)
1381 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1385 for_each_dpcm_fe(be
, stream
, dpcm
) {
1389 dev_dbg(fe
->dev
, "reparent %s path %s %s %s\n",
1390 snd_pcm_direction_name(stream
),
1391 dpcm
->fe
->dai_link
->name
,
1392 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1394 fe_substream
= snd_soc_dpcm_get_substream(dpcm
->fe
, stream
);
1395 be_substream
->runtime
= fe_substream
->runtime
;
1400 /* disconnect a BE and FE */
1401 void dpcm_be_disconnect(struct snd_soc_pcm_runtime
*fe
, int stream
)
1403 struct snd_soc_dpcm
*dpcm
, *d
;
1404 struct snd_pcm_substream
*substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1405 LIST_HEAD(deleted_dpcms
);
1407 snd_soc_dpcm_mutex_assert_held(fe
);
1409 snd_pcm_stream_lock_irq(substream
);
1410 for_each_dpcm_be_safe(fe
, stream
, dpcm
, d
) {
1411 dev_dbg(fe
->dev
, "ASoC: BE %s disconnect check for %s\n",
1412 snd_pcm_direction_name(stream
),
1413 dpcm
->be
->dai_link
->name
);
1415 if (dpcm
->state
!= SND_SOC_DPCM_LINK_STATE_FREE
)
1418 dev_dbg(fe
->dev
, "freed DSP %s path %s %s %s\n",
1419 snd_pcm_direction_name(stream
), fe
->dai_link
->name
,
1420 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1422 /* BEs still alive need new FE */
1423 dpcm_be_reparent(fe
, dpcm
->be
, stream
);
1425 list_del(&dpcm
->list_be
);
1426 list_move(&dpcm
->list_fe
, &deleted_dpcms
);
1428 snd_pcm_stream_unlock_irq(substream
);
1430 while (!list_empty(&deleted_dpcms
)) {
1431 dpcm
= list_first_entry(&deleted_dpcms
, struct snd_soc_dpcm
,
1433 list_del(&dpcm
->list_fe
);
1434 dpcm_remove_debugfs_state(dpcm
);
1439 /* get BE for DAI widget and stream */
1440 static struct snd_soc_pcm_runtime
*dpcm_get_be(struct snd_soc_card
*card
,
1441 struct snd_soc_dapm_widget
*widget
, int stream
)
1443 struct snd_soc_pcm_runtime
*be
;
1444 struct snd_soc_dapm_widget
*w
;
1445 struct snd_soc_dai
*dai
;
1448 dev_dbg(card
->dev
, "ASoC: find BE for widget %s\n", widget
->name
);
1450 for_each_card_rtds(card
, be
) {
1452 if (!be
->dai_link
->no_pcm
)
1455 if (!snd_soc_dpcm_get_substream(be
, stream
))
1458 for_each_rtd_dais(be
, i
, dai
) {
1459 w
= snd_soc_dai_get_widget(dai
, stream
);
1461 dev_dbg(card
->dev
, "ASoC: try BE : %s\n",
1462 w
? w
->name
: "(not set)");
1469 /* Widget provided is not a BE */
1473 int widget_in_list(struct snd_soc_dapm_widget_list
*list
,
1474 struct snd_soc_dapm_widget
*widget
)
1476 struct snd_soc_dapm_widget
*w
;
1479 for_each_dapm_widgets(list
, i
, w
)
1485 EXPORT_SYMBOL_GPL(widget_in_list
);
1487 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget
*widget
, enum snd_soc_dapm_direction dir
)
1489 struct snd_soc_card
*card
= widget
->dapm
->card
;
1490 struct snd_soc_pcm_runtime
*rtd
;
1493 /* adjust dir to stream */
1494 if (dir
== SND_SOC_DAPM_DIR_OUT
)
1495 stream
= SNDRV_PCM_STREAM_PLAYBACK
;
1497 stream
= SNDRV_PCM_STREAM_CAPTURE
;
1499 rtd
= dpcm_get_be(card
, widget
, stream
);
1505 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be
);
1507 int dpcm_path_get(struct snd_soc_pcm_runtime
*fe
,
1508 int stream
, struct snd_soc_dapm_widget_list
**list
)
1510 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(fe
, 0);
1513 if (fe
->dai_link
->num_cpus
> 1) {
1515 "%s doesn't support Multi CPU yet\n", __func__
);
1519 /* get number of valid DAI paths and their widgets */
1520 paths
= snd_soc_dapm_dai_get_connected_widgets(cpu_dai
, stream
, list
,
1521 fe
->card
->component_chaining
?
1522 NULL
: dpcm_end_walk_at_be
);
1525 dev_dbg(fe
->dev
, "ASoC: found %d audio %s paths\n", paths
,
1526 snd_pcm_direction_name(stream
));
1527 else if (paths
== 0)
1528 dev_dbg(fe
->dev
, "ASoC: %s no valid %s path\n", fe
->dai_link
->name
,
1529 snd_pcm_direction_name(stream
));
1534 void dpcm_path_put(struct snd_soc_dapm_widget_list
**list
)
1536 snd_soc_dapm_dai_free_widgets(list
);
1539 static bool dpcm_be_is_active(struct snd_soc_dpcm
*dpcm
, int stream
,
1540 struct snd_soc_dapm_widget_list
*list
)
1542 struct snd_soc_dai
*dai
;
1545 /* is there a valid DAI widget for this BE */
1546 for_each_rtd_dais(dpcm
->be
, i
, dai
) {
1547 struct snd_soc_dapm_widget
*widget
= snd_soc_dai_get_widget(dai
, stream
);
1550 * The BE is pruned only if none of the dai
1551 * widgets are in the active list.
1553 if (widget
&& widget_in_list(list
, widget
))
1560 static int dpcm_prune_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1561 struct snd_soc_dapm_widget_list
**list_
)
1563 struct snd_soc_dpcm
*dpcm
;
1566 /* Destroy any old FE <--> BE connections */
1567 for_each_dpcm_be(fe
, stream
, dpcm
) {
1568 if (dpcm_be_is_active(dpcm
, stream
, *list_
))
1571 dev_dbg(fe
->dev
, "ASoC: pruning %s BE %s for %s\n",
1572 snd_pcm_direction_name(stream
),
1573 dpcm
->be
->dai_link
->name
, fe
->dai_link
->name
);
1574 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1575 dpcm_set_be_update_state(dpcm
->be
, stream
, SND_SOC_DPCM_UPDATE_BE
);
1579 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", prune
);
1583 static int dpcm_add_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1584 struct snd_soc_dapm_widget_list
**list_
)
1586 struct snd_soc_card
*card
= fe
->card
;
1587 struct snd_soc_dapm_widget_list
*list
= *list_
;
1588 struct snd_soc_pcm_runtime
*be
;
1589 struct snd_soc_dapm_widget
*widget
;
1590 struct snd_pcm_substream
*fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1591 int i
, new = 0, err
;
1593 /* don't connect if FE is not running */
1594 if (!fe_substream
->runtime
&& !fe
->fe_compr
)
1597 /* Create any new FE <--> BE connections */
1598 for_each_dapm_widgets(list
, i
, widget
) {
1600 switch (widget
->id
) {
1601 case snd_soc_dapm_dai_in
:
1602 if (stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1605 case snd_soc_dapm_dai_out
:
1606 if (stream
!= SNDRV_PCM_STREAM_CAPTURE
)
1613 /* is there a valid BE rtd for this widget */
1614 be
= dpcm_get_be(card
, widget
, stream
);
1616 dev_dbg(fe
->dev
, "ASoC: no BE found for %s\n",
1622 * Filter for systems with 'component_chaining' enabled.
1623 * This helps to avoid unnecessary re-configuration of an
1624 * already active BE on such systems.
1626 if (fe
->card
->component_chaining
&&
1627 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1628 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1631 /* newly connected FE and BE */
1632 err
= dpcm_be_connect(fe
, be
, stream
);
1634 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
1637 } else if (err
== 0) /* already connected */
1641 dpcm_set_be_update_state(be
, stream
, SND_SOC_DPCM_UPDATE_BE
);
1645 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
1650 * Find the corresponding BE DAIs that source or sink audio to this
1653 int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
1654 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
1657 return dpcm_add_paths(fe
, stream
, list
);
1659 return dpcm_prune_paths(fe
, stream
, list
);
1662 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime
*fe
, int stream
)
1664 struct snd_soc_dpcm
*dpcm
;
1666 for_each_dpcm_be(fe
, stream
, dpcm
)
1667 dpcm_set_be_update_state(dpcm
->be
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1670 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime
*fe
, int stream
,
1671 int do_hw_free
, struct snd_soc_dpcm
*last
)
1673 struct snd_soc_dpcm
*dpcm
;
1675 /* disable any enabled and non active backends */
1676 for_each_dpcm_be(fe
, stream
, dpcm
) {
1677 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1678 struct snd_pcm_substream
*be_substream
=
1679 snd_soc_dpcm_get_substream(be
, stream
);
1684 /* is this op for this BE ? */
1685 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1688 if (be
->dpcm
[stream
].users
== 0) {
1689 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1690 snd_pcm_direction_name(stream
),
1691 be
->dpcm
[stream
].state
);
1695 if (--be
->dpcm
[stream
].users
!= 0)
1698 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) {
1702 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) {
1703 __soc_pcm_hw_free(be
, be_substream
);
1704 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1708 __soc_pcm_close(be
, be_substream
);
1709 be_substream
->runtime
= NULL
;
1710 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1714 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1716 struct snd_pcm_substream
*fe_substream
= snd_soc_dpcm_get_substream(fe
, stream
);
1717 struct snd_soc_pcm_runtime
*be
;
1718 struct snd_soc_dpcm
*dpcm
;
1721 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1722 for_each_dpcm_be(fe
, stream
, dpcm
) {
1723 struct snd_pcm_substream
*be_substream
;
1726 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1728 if (!be_substream
) {
1729 dev_err(be
->dev
, "ASoC: no backend %s stream\n",
1730 snd_pcm_direction_name(stream
));
1734 /* is this op for this BE ? */
1735 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1738 /* first time the dpcm is open ? */
1739 if (be
->dpcm
[stream
].users
== DPCM_MAX_BE_USERS
) {
1740 dev_err(be
->dev
, "ASoC: too many users %s at open %d\n",
1741 snd_pcm_direction_name(stream
),
1742 be
->dpcm
[stream
].state
);
1746 if (be
->dpcm
[stream
].users
++ != 0)
1749 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1750 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1753 dev_dbg(be
->dev
, "ASoC: open %s BE %s\n",
1754 snd_pcm_direction_name(stream
), be
->dai_link
->name
);
1756 be_substream
->runtime
= fe_substream
->runtime
;
1757 err
= __soc_pcm_open(be
, be_substream
);
1759 be
->dpcm
[stream
].users
--;
1760 if (be
->dpcm
[stream
].users
< 0)
1761 dev_err(be
->dev
, "ASoC: no users %s at unwind %d\n",
1762 snd_pcm_direction_name(stream
),
1763 be
->dpcm
[stream
].state
);
1765 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1768 be
->dpcm
[stream
].be_start
= 0;
1769 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1776 dpcm_be_dai_startup_rollback(fe
, stream
, dpcm
);
1778 return soc_pcm_ret(fe
, err
);
1781 static void dpcm_runtime_setup_fe(struct snd_pcm_substream
*substream
)
1783 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1784 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1785 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1786 struct snd_soc_dai
*dai
;
1787 int stream
= substream
->stream
;
1788 u64 formats
= hw
->formats
;
1791 soc_pcm_hw_init(hw
);
1794 hw
->formats
&= formats
;
1796 for_each_rtd_cpu_dais(fe
, i
, dai
) {
1797 const struct snd_soc_pcm_stream
*cpu_stream
;
1800 * Skip CPUs which don't support the current stream
1801 * type. See soc_pcm_init_runtime_hw() for more details
1803 if (!snd_soc_dai_stream_valid(dai
, stream
))
1806 cpu_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1808 soc_pcm_hw_update_rate(hw
, cpu_stream
);
1809 soc_pcm_hw_update_chan(hw
, cpu_stream
);
1810 soc_pcm_hw_update_format(hw
, cpu_stream
);
1811 soc_pcm_hw_update_subformat(hw
, cpu_stream
);
1816 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream
*substream
)
1818 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1819 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1820 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1821 struct snd_soc_dpcm
*dpcm
;
1822 struct snd_soc_dai
*dai
;
1823 int stream
= substream
->stream
;
1825 if (!fe
->dai_link
->dpcm_merged_format
)
1829 * It returns merged BE codec format
1830 * if FE want to use it (= dpcm_merged_format)
1833 for_each_dpcm_be(fe
, stream
, dpcm
) {
1834 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1835 const struct snd_soc_pcm_stream
*codec_stream
;
1838 for_each_rtd_codec_dais(be
, i
, dai
) {
1840 * Skip CODECs which don't support the current stream
1841 * type. See soc_pcm_init_runtime_hw() for more details
1843 if (!snd_soc_dai_stream_valid(dai
, stream
))
1846 codec_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1848 soc_pcm_hw_update_format(hw
, codec_stream
);
1849 soc_pcm_hw_update_subformat(hw
, codec_stream
);
1854 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream
*substream
)
1856 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1858 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1859 struct snd_soc_dpcm
*dpcm
;
1860 int stream
= substream
->stream
;
1862 if (!fe
->dai_link
->dpcm_merged_chan
)
1866 * It returns merged BE codec channel;
1867 * if FE want to use it (= dpcm_merged_chan)
1870 for_each_dpcm_be(fe
, stream
, dpcm
) {
1871 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1872 const struct snd_soc_pcm_stream
*cpu_stream
;
1873 struct snd_soc_dai
*dai
;
1876 for_each_rtd_cpu_dais(be
, i
, dai
) {
1878 * Skip CPUs which don't support the current stream
1879 * type. See soc_pcm_init_runtime_hw() for more details
1881 if (!snd_soc_dai_stream_valid(dai
, stream
))
1884 cpu_stream
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1886 soc_pcm_hw_update_chan(hw
, cpu_stream
);
1890 * chan min/max cannot be enforced if there are multiple CODEC
1891 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1893 if (be
->dai_link
->num_codecs
== 1) {
1894 const struct snd_soc_pcm_stream
*codec_stream
= snd_soc_dai_get_pcm_stream(
1895 snd_soc_rtd_to_codec(be
, 0), stream
);
1897 soc_pcm_hw_update_chan(hw
, codec_stream
);
1902 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream
*substream
)
1904 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
1905 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1906 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1907 struct snd_soc_dpcm
*dpcm
;
1908 int stream
= substream
->stream
;
1910 if (!fe
->dai_link
->dpcm_merged_rate
)
1914 * It returns merged BE codec channel;
1915 * if FE want to use it (= dpcm_merged_chan)
1918 for_each_dpcm_be(fe
, stream
, dpcm
) {
1919 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1920 const struct snd_soc_pcm_stream
*pcm
;
1921 struct snd_soc_dai
*dai
;
1924 for_each_rtd_dais(be
, i
, dai
) {
1926 * Skip DAIs which don't support the current stream
1927 * type. See soc_pcm_init_runtime_hw() for more details
1929 if (!snd_soc_dai_stream_valid(dai
, stream
))
1932 pcm
= snd_soc_dai_get_pcm_stream(dai
, stream
);
1934 soc_pcm_hw_update_rate(hw
, pcm
);
1939 static int dpcm_apply_symmetry(struct snd_pcm_substream
*fe_substream
,
1942 struct snd_soc_dpcm
*dpcm
;
1943 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
1944 struct snd_soc_dai
*fe_cpu_dai
;
1948 /* apply symmetry for FE */
1949 soc_pcm_update_symmetry(fe_substream
);
1951 for_each_rtd_cpu_dais (fe
, i
, fe_cpu_dai
) {
1952 /* Symmetry only applies if we've got an active stream. */
1953 err
= soc_pcm_apply_symmetry(fe_substream
, fe_cpu_dai
);
1958 /* apply symmetry for BE */
1959 for_each_dpcm_be(fe
, stream
, dpcm
) {
1960 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1961 struct snd_pcm_substream
*be_substream
=
1962 snd_soc_dpcm_get_substream(be
, stream
);
1963 struct snd_soc_pcm_runtime
*rtd
;
1964 struct snd_soc_dai
*dai
;
1966 /* A backend may not have the requested substream */
1970 rtd
= snd_soc_substream_to_rtd(be_substream
);
1971 if (rtd
->dai_link
->be_hw_params_fixup
)
1974 soc_pcm_update_symmetry(be_substream
);
1976 /* Symmetry only applies if we've got an active stream. */
1977 for_each_rtd_dais(rtd
, i
, dai
) {
1978 err
= soc_pcm_apply_symmetry(fe_substream
, dai
);
1984 return soc_pcm_ret(fe
, err
);
1987 static int dpcm_fe_dai_startup(struct snd_pcm_substream
*fe_substream
)
1989 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
1990 int stream
= fe_substream
->stream
, ret
= 0;
1992 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1994 ret
= dpcm_be_dai_startup(fe
, stream
);
1998 dev_dbg(fe
->dev
, "ASoC: open FE %s\n", fe
->dai_link
->name
);
2000 /* start the DAI frontend */
2001 ret
= __soc_pcm_open(fe
, fe_substream
);
2005 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
2007 dpcm_runtime_setup_fe(fe_substream
);
2009 dpcm_runtime_setup_be_format(fe_substream
);
2010 dpcm_runtime_setup_be_chan(fe_substream
);
2011 dpcm_runtime_setup_be_rate(fe_substream
);
2013 ret
= dpcm_apply_symmetry(fe_substream
, stream
);
2017 dpcm_be_dai_startup_unwind(fe
, stream
);
2019 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2021 return soc_pcm_ret(fe
, ret
);
2024 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream
*substream
)
2026 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2027 int stream
= substream
->stream
;
2029 snd_soc_dpcm_mutex_assert_held(fe
);
2031 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2033 /* shutdown the BEs */
2034 dpcm_be_dai_shutdown(fe
, stream
);
2036 dev_dbg(fe
->dev
, "ASoC: close FE %s\n", fe
->dai_link
->name
);
2038 /* now shutdown the frontend */
2039 __soc_pcm_close(fe
, substream
);
2041 /* run the stream stop event */
2042 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_STOP
);
2044 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
2045 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2049 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime
*fe
, int stream
)
2051 struct snd_soc_dpcm
*dpcm
;
2053 /* only hw_params backends that are either sinks or sources
2054 * to this frontend DAI */
2055 for_each_dpcm_be(fe
, stream
, dpcm
) {
2057 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2058 struct snd_pcm_substream
*be_substream
=
2059 snd_soc_dpcm_get_substream(be
, stream
);
2061 /* is this op for this BE ? */
2062 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2065 /* only free hw when no longer used - check all FEs */
2066 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2069 /* do not free hw if this BE is used by other FE */
2070 if (be
->dpcm
[stream
].users
> 1)
2073 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2074 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
2075 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
2076 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
) &&
2077 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2078 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
2081 dev_dbg(be
->dev
, "ASoC: hw_free BE %s\n",
2082 be
->dai_link
->name
);
2084 __soc_pcm_hw_free(be
, be_substream
);
2086 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
2090 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream
*substream
)
2092 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2093 int stream
= substream
->stream
;
2095 snd_soc_dpcm_mutex_lock(fe
);
2096 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2098 dev_dbg(fe
->dev
, "ASoC: hw_free FE %s\n", fe
->dai_link
->name
);
2100 /* call hw_free on the frontend */
2101 soc_pcm_hw_clean(fe
, substream
, 0);
2103 /* only hw_params backends that are either sinks or sources
2104 * to this frontend DAI */
2105 dpcm_be_dai_hw_free(fe
, stream
);
2107 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
2108 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2110 snd_soc_dpcm_mutex_unlock(fe
);
2114 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
2116 struct snd_soc_pcm_runtime
*be
;
2117 struct snd_pcm_substream
*be_substream
;
2118 struct snd_soc_dpcm
*dpcm
;
2121 for_each_dpcm_be(fe
, stream
, dpcm
) {
2122 struct snd_pcm_hw_params hw_params
;
2125 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2127 /* is this op for this BE ? */
2128 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2131 /* copy params for each dpcm */
2132 memcpy(&hw_params
, &fe
->dpcm
[stream
].hw_params
,
2133 sizeof(struct snd_pcm_hw_params
));
2135 /* perform any hw_params fixups */
2136 ret
= snd_soc_link_be_hw_params_fixup(be
, &hw_params
);
2140 /* copy the fixed-up hw params for BE dai */
2141 memcpy(&be
->dpcm
[stream
].hw_params
, &hw_params
,
2142 sizeof(struct snd_pcm_hw_params
));
2144 /* only allow hw_params() if no connected FEs are running */
2145 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
2148 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
2149 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2150 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
))
2153 dev_dbg(be
->dev
, "ASoC: hw_params BE %s\n",
2154 be
->dai_link
->name
);
2156 ret
= __soc_pcm_hw_params(be
, be_substream
, &hw_params
);
2160 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2165 dev_dbg(fe
->dev
, "ASoC: %s() failed at %s (%d)\n",
2166 __func__
, be
->dai_link
->name
, ret
);
2168 /* disable any enabled and non active backends */
2169 for_each_dpcm_be_rollback(fe
, stream
, dpcm
) {
2171 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2173 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2176 /* only allow hw_free() if no connected FEs are running */
2177 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2180 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
2181 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2182 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
2183 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
2186 __soc_pcm_hw_free(be
, be_substream
);
2192 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream
*substream
,
2193 struct snd_pcm_hw_params
*params
)
2195 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2196 int ret
, stream
= substream
->stream
;
2198 snd_soc_dpcm_mutex_lock(fe
);
2199 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2201 memcpy(&fe
->dpcm
[stream
].hw_params
, params
,
2202 sizeof(struct snd_pcm_hw_params
));
2203 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2207 dev_dbg(fe
->dev
, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2208 fe
->dai_link
->name
, params_rate(params
),
2209 params_channels(params
), params_format(params
));
2211 /* call hw_params on the frontend */
2212 ret
= __soc_pcm_hw_params(fe
, substream
, params
);
2214 dpcm_be_dai_hw_free(fe
, stream
);
2216 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
2219 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2220 snd_soc_dpcm_mutex_unlock(fe
);
2222 return soc_pcm_ret(fe
, ret
);
2225 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
2228 struct snd_soc_pcm_runtime
*be
;
2229 bool pause_stop_transition
;
2230 struct snd_soc_dpcm
*dpcm
;
2231 unsigned long flags
;
2234 for_each_dpcm_be(fe
, stream
, dpcm
) {
2235 struct snd_pcm_substream
*be_substream
;
2238 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
2240 snd_pcm_stream_lock_irqsave_nested(be_substream
, flags
);
2242 /* is this op for this BE ? */
2243 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2246 dev_dbg(be
->dev
, "ASoC: trigger BE %s cmd %d\n",
2247 be
->dai_link
->name
, cmd
);
2250 case SNDRV_PCM_TRIGGER_START
:
2251 if (!be
->dpcm
[stream
].be_start
&&
2252 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
2253 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2254 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2257 be
->dpcm
[stream
].be_start
++;
2258 if (be
->dpcm
[stream
].be_start
!= 1)
2261 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PAUSED
)
2262 ret
= soc_pcm_trigger(be_substream
,
2263 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
2265 ret
= soc_pcm_trigger(be_substream
,
2266 SNDRV_PCM_TRIGGER_START
);
2268 be
->dpcm
[stream
].be_start
--;
2272 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2274 case SNDRV_PCM_TRIGGER_RESUME
:
2275 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
2278 be
->dpcm
[stream
].be_start
++;
2279 if (be
->dpcm
[stream
].be_start
!= 1)
2282 ret
= soc_pcm_trigger(be_substream
, cmd
);
2284 be
->dpcm
[stream
].be_start
--;
2288 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2290 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2291 if (!be
->dpcm
[stream
].be_start
&&
2292 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
) &&
2293 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2296 fe
->dpcm
[stream
].fe_pause
= false;
2297 be
->dpcm
[stream
].be_pause
--;
2299 be
->dpcm
[stream
].be_start
++;
2300 if (be
->dpcm
[stream
].be_start
!= 1)
2303 ret
= soc_pcm_trigger(be_substream
, cmd
);
2305 be
->dpcm
[stream
].be_start
--;
2309 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2311 case SNDRV_PCM_TRIGGER_STOP
:
2312 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
) &&
2313 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2316 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_START
)
2317 be
->dpcm
[stream
].be_start
--;
2319 if (be
->dpcm
[stream
].be_start
!= 0)
2322 pause_stop_transition
= false;
2323 if (fe
->dpcm
[stream
].fe_pause
) {
2324 pause_stop_transition
= true;
2325 fe
->dpcm
[stream
].fe_pause
= false;
2326 be
->dpcm
[stream
].be_pause
--;
2329 if (be
->dpcm
[stream
].be_pause
!= 0)
2330 ret
= soc_pcm_trigger(be_substream
, SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
2332 ret
= soc_pcm_trigger(be_substream
, SNDRV_PCM_TRIGGER_STOP
);
2335 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_START
)
2336 be
->dpcm
[stream
].be_start
++;
2337 if (pause_stop_transition
) {
2338 fe
->dpcm
[stream
].fe_pause
= true;
2339 be
->dpcm
[stream
].be_pause
++;
2344 if (be
->dpcm
[stream
].be_pause
!= 0)
2345 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2347 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2350 case SNDRV_PCM_TRIGGER_SUSPEND
:
2351 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2354 be
->dpcm
[stream
].be_start
--;
2355 if (be
->dpcm
[stream
].be_start
!= 0)
2358 ret
= soc_pcm_trigger(be_substream
, cmd
);
2360 be
->dpcm
[stream
].be_start
++;
2364 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
2366 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2367 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2370 fe
->dpcm
[stream
].fe_pause
= true;
2371 be
->dpcm
[stream
].be_pause
++;
2373 be
->dpcm
[stream
].be_start
--;
2374 if (be
->dpcm
[stream
].be_start
!= 0)
2377 ret
= soc_pcm_trigger(be_substream
, cmd
);
2379 be
->dpcm
[stream
].be_start
++;
2383 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2387 snd_pcm_stream_unlock_irqrestore(be_substream
, flags
);
2391 return soc_pcm_ret(fe
, ret
);
2393 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger
);
2395 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream
*substream
,
2396 int cmd
, bool fe_first
)
2398 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2401 /* call trigger on the frontend before the backend. */
2403 dev_dbg(fe
->dev
, "ASoC: pre trigger FE %s cmd %d\n",
2404 fe
->dai_link
->name
, cmd
);
2406 ret
= soc_pcm_trigger(substream
, cmd
);
2410 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2414 /* call trigger on the frontend after the backend. */
2415 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2419 dev_dbg(fe
->dev
, "ASoC: post trigger FE %s cmd %d\n",
2420 fe
->dai_link
->name
, cmd
);
2422 ret
= soc_pcm_trigger(substream
, cmd
);
2427 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2429 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2430 int stream
= substream
->stream
;
2432 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2434 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
2437 case SND_SOC_DPCM_TRIGGER_PRE
:
2439 case SNDRV_PCM_TRIGGER_START
:
2440 case SNDRV_PCM_TRIGGER_RESUME
:
2441 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2442 case SNDRV_PCM_TRIGGER_DRAIN
:
2443 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2445 case SNDRV_PCM_TRIGGER_STOP
:
2446 case SNDRV_PCM_TRIGGER_SUSPEND
:
2447 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2448 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2455 case SND_SOC_DPCM_TRIGGER_POST
:
2457 case SNDRV_PCM_TRIGGER_START
:
2458 case SNDRV_PCM_TRIGGER_RESUME
:
2459 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2460 case SNDRV_PCM_TRIGGER_DRAIN
:
2461 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, false);
2463 case SNDRV_PCM_TRIGGER_STOP
:
2464 case SNDRV_PCM_TRIGGER_SUSPEND
:
2465 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2466 ret
= dpcm_dai_trigger_fe_be(substream
, cmd
, true);
2474 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
2475 fe
->dai_link
->name
);
2481 dev_err(fe
->dev
, "ASoC: trigger FE cmd: %d failed: %d\n",
2487 case SNDRV_PCM_TRIGGER_START
:
2488 case SNDRV_PCM_TRIGGER_RESUME
:
2489 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2490 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2492 case SNDRV_PCM_TRIGGER_STOP
:
2493 case SNDRV_PCM_TRIGGER_SUSPEND
:
2494 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2496 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2497 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2502 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
2506 static int dpcm_fe_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2508 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2509 int stream
= substream
->stream
;
2511 /* if FE's runtime_update is already set, we're in race;
2512 * process this trigger later at exit
2514 if (fe
->dpcm
[stream
].runtime_update
!= SND_SOC_DPCM_UPDATE_NO
) {
2515 fe
->dpcm
[stream
].trigger_pending
= cmd
+ 1;
2516 return 0; /* delayed, assuming it's successful */
2519 /* we're alone, let's trigger */
2520 return dpcm_fe_dai_do_trigger(substream
, cmd
);
2523 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
2525 struct snd_soc_dpcm
*dpcm
;
2528 for_each_dpcm_be(fe
, stream
, dpcm
) {
2530 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2531 struct snd_pcm_substream
*be_substream
=
2532 snd_soc_dpcm_get_substream(be
, stream
);
2534 /* is this op for this BE ? */
2535 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2538 if (!snd_soc_dpcm_can_be_prepared(fe
, be
, stream
))
2541 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2542 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
2543 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
) &&
2544 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
2547 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
2548 be
->dai_link
->name
);
2550 ret
= __soc_pcm_prepare(be
, be_substream
);
2554 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2557 return soc_pcm_ret(fe
, ret
);
2560 static int dpcm_fe_dai_prepare(struct snd_pcm_substream
*substream
)
2562 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(substream
);
2563 int stream
= substream
->stream
, ret
= 0;
2565 snd_soc_dpcm_mutex_lock(fe
);
2567 dev_dbg(fe
->dev
, "ASoC: prepare FE %s\n", fe
->dai_link
->name
);
2569 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2571 /* there is no point preparing this FE if there are no BEs */
2572 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2573 /* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles */
2574 dev_err_once(fe
->dev
, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
2575 fe
->dai_link
->name
);
2576 dev_dbg(fe
->dev
, "ASoC: no backend DAIs enabled for %s\n",
2577 fe
->dai_link
->name
);
2582 ret
= dpcm_be_dai_prepare(fe
, stream
);
2586 /* call prepare on the frontend */
2587 ret
= __soc_pcm_prepare(fe
, substream
);
2591 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2594 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2595 snd_soc_dpcm_mutex_unlock(fe
);
2597 return soc_pcm_ret(fe
, ret
);
2600 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
2604 dev_dbg(fe
->dev
, "ASoC: runtime %s close on FE %s\n",
2605 snd_pcm_direction_name(stream
), fe
->dai_link
->name
);
2607 err
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_STOP
);
2609 dpcm_be_dai_hw_free(fe
, stream
);
2611 dpcm_be_dai_shutdown(fe
, stream
);
2613 /* run the stream event for each BE */
2614 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2616 return soc_pcm_ret(fe
, err
);
2619 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
2621 struct snd_soc_dpcm
*dpcm
;
2624 dev_dbg(fe
->dev
, "ASoC: runtime %s open on FE %s\n",
2625 snd_pcm_direction_name(stream
), fe
->dai_link
->name
);
2627 /* Only start the BE if the FE is ready */
2628 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_FREE
||
2629 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
) {
2630 dev_err(fe
->dev
, "ASoC: FE %s is not ready %d\n",
2631 fe
->dai_link
->name
, fe
->dpcm
[stream
].state
);
2636 /* startup must always be called for new BEs */
2637 ret
= dpcm_be_dai_startup(fe
, stream
);
2641 /* keep going if FE state is > open */
2642 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
2645 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2649 /* keep going if FE state is > hw_params */
2650 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
2653 ret
= dpcm_be_dai_prepare(fe
, stream
);
2657 /* run the stream event for each BE */
2658 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2660 /* keep going if FE state is > prepare */
2661 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PREPARE
||
2662 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_STOP
)
2665 ret
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_START
);
2672 dpcm_be_dai_hw_free(fe
, stream
);
2674 dpcm_be_dai_shutdown(fe
, stream
);
2676 /* disconnect any pending BEs */
2677 for_each_dpcm_be(fe
, stream
, dpcm
) {
2678 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2680 /* is this op for this BE ? */
2681 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2684 if (be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
||
2685 be
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_NEW
)
2686 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2689 return soc_pcm_ret(fe
, ret
);
2692 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime
*fe
, int new)
2694 struct snd_soc_dapm_widget_list
*list
;
2698 if (!fe
->dai_link
->dynamic
)
2701 if (fe
->dai_link
->num_cpus
> 1) {
2703 "%s doesn't support Multi CPU yet\n", __func__
);
2707 /* only check active links */
2708 if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe
, 0)))
2711 /* DAPM sync will call this to update DSP paths */
2712 dev_dbg(fe
->dev
, "ASoC: DPCM %s runtime update for FE %s\n",
2713 new ? "new" : "old", fe
->dai_link
->name
);
2715 for_each_pcm_streams(stream
) {
2717 /* skip if FE doesn't have playback/capture capability */
2718 if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe
, 0), stream
) ||
2719 !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe
, 0), stream
))
2722 /* skip if FE isn't currently playing/capturing */
2723 if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe
, 0), stream
) ||
2724 !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe
, 0), stream
))
2727 paths
= dpcm_path_get(fe
, stream
, &list
);
2731 /* update any playback/capture paths */
2732 count
= dpcm_process_paths(fe
, stream
, &list
, new);
2734 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_BE
);
2736 dpcm_run_update_startup(fe
, stream
);
2738 dpcm_run_update_shutdown(fe
, stream
);
2739 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2741 dpcm_clear_pending_state(fe
, stream
);
2742 dpcm_be_disconnect(fe
, stream
);
2745 dpcm_path_put(&list
);
2751 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2754 int snd_soc_dpcm_runtime_update(struct snd_soc_card
*card
)
2756 struct snd_soc_pcm_runtime
*fe
;
2759 snd_soc_dpcm_mutex_lock(card
);
2760 /* shutdown all old paths first */
2761 for_each_card_rtds(card
, fe
) {
2762 ret
= soc_dpcm_fe_runtime_update(fe
, 0);
2767 /* bring new paths up */
2768 for_each_card_rtds(card
, fe
) {
2769 ret
= soc_dpcm_fe_runtime_update(fe
, 1);
2775 snd_soc_dpcm_mutex_unlock(card
);
2778 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update
);
2780 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream
*fe_substream
)
2782 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2783 struct snd_soc_dpcm
*dpcm
;
2784 int stream
= fe_substream
->stream
;
2786 snd_soc_dpcm_mutex_assert_held(fe
);
2788 /* mark FE's links ready to prune */
2789 for_each_dpcm_be(fe
, stream
, dpcm
)
2790 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2792 dpcm_be_disconnect(fe
, stream
);
2795 static int dpcm_fe_dai_close(struct snd_pcm_substream
*fe_substream
)
2797 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2800 snd_soc_dpcm_mutex_lock(fe
);
2801 ret
= dpcm_fe_dai_shutdown(fe_substream
);
2803 dpcm_fe_dai_cleanup(fe_substream
);
2805 snd_soc_dpcm_mutex_unlock(fe
);
2809 static int dpcm_fe_dai_open(struct snd_pcm_substream
*fe_substream
)
2811 struct snd_soc_pcm_runtime
*fe
= snd_soc_substream_to_rtd(fe_substream
);
2812 struct snd_soc_dapm_widget_list
*list
;
2814 int stream
= fe_substream
->stream
;
2816 snd_soc_dpcm_mutex_lock(fe
);
2818 ret
= dpcm_path_get(fe
, stream
, &list
);
2822 /* calculate valid and active FE <-> BE dpcms */
2823 dpcm_process_paths(fe
, stream
, &list
, 1);
2825 ret
= dpcm_fe_dai_startup(fe_substream
);
2827 dpcm_fe_dai_cleanup(fe_substream
);
2829 dpcm_clear_pending_state(fe
, stream
);
2830 dpcm_path_put(&list
);
2832 snd_soc_dpcm_mutex_unlock(fe
);
2836 static int soc_get_playback_capture(struct snd_soc_pcm_runtime
*rtd
,
2837 int *playback
, int *capture
)
2839 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
2840 struct snd_soc_dai
*cpu_dai
;
2841 struct snd_soc_dai_link_ch_map
*ch_maps
;
2842 int has_playback
= 0;
2843 int has_capture
= 0;
2846 if (dai_link
->dynamic
&& dai_link
->num_cpus
> 1) {
2847 dev_err(rtd
->dev
, "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2851 if (dai_link
->dynamic
|| dai_link
->no_pcm
) {
2853 for_each_rtd_ch_maps(rtd
, i
, ch_maps
) {
2854 cpu_dai
= snd_soc_rtd_to_cpu(rtd
, ch_maps
->cpu
);
2856 if (snd_soc_dai_stream_valid(cpu_dai
, SNDRV_PCM_STREAM_PLAYBACK
))
2859 if (snd_soc_dai_stream_valid(cpu_dai
, SNDRV_PCM_STREAM_CAPTURE
))
2866 * dpcm_xxx flag will be removed soon, Indicates warning if dpcm_xxx flag was used
2867 * as availability limitation
2869 if (has_playback
&& has_capture
) {
2870 if ( dai_link
->dpcm_playback
&&
2871 !dai_link
->dpcm_capture
&&
2872 !dai_link
->playback_only
) {
2873 dev_warn(rtd
->card
->dev
,
2874 "both playback/capture are available,"
2875 " but not using playback_only flag (%s)\n",
2876 dai_link
->stream_name
);
2877 dev_warn(rtd
->card
->dev
,
2878 "dpcm_playback/capture are no longer needed,"
2879 " please use playback/capture_only instead\n");
2883 if (!dai_link
->dpcm_playback
&&
2884 dai_link
->dpcm_capture
&&
2885 !dai_link
->capture_only
) {
2886 dev_warn(rtd
->card
->dev
,
2887 "both playback/capture are available,"
2888 " but not using capture_only flag (%s)\n",
2889 dai_link
->stream_name
);
2890 dev_warn(rtd
->card
->dev
,
2891 "dpcm_playback/capture are no longer needed,"
2892 " please use playback/capture_only instead\n");
2897 struct snd_soc_dai
*codec_dai
;
2899 /* Adapt stream for codec2codec links */
2900 int cpu_capture
= snd_soc_get_stream_cpu(dai_link
, SNDRV_PCM_STREAM_CAPTURE
);
2901 int cpu_playback
= snd_soc_get_stream_cpu(dai_link
, SNDRV_PCM_STREAM_PLAYBACK
);
2905 * soc.h :: [dai_link->ch_maps Image sample]
2907 for_each_rtd_ch_maps(rtd
, i
, ch_maps
) {
2908 cpu_dai
= snd_soc_rtd_to_cpu(rtd
, ch_maps
->cpu
);
2909 codec_dai
= snd_soc_rtd_to_codec(rtd
, ch_maps
->codec
);
2911 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_PLAYBACK
) &&
2912 snd_soc_dai_stream_valid(cpu_dai
, cpu_playback
))
2914 if (snd_soc_dai_stream_valid(codec_dai
, SNDRV_PCM_STREAM_CAPTURE
) &&
2915 snd_soc_dai_stream_valid(cpu_dai
, cpu_capture
))
2920 if (dai_link
->playback_only
)
2923 if (dai_link
->capture_only
)
2926 if (!has_playback
&& !has_capture
) {
2927 dev_err(rtd
->dev
, "substream %s has no playback, no capture\n",
2928 dai_link
->stream_name
);
2933 *playback
= has_playback
;
2934 *capture
= has_capture
;
2939 static int soc_create_pcm(struct snd_pcm
**pcm
,
2940 struct snd_soc_pcm_runtime
*rtd
,
2941 int playback
, int capture
, int num
)
2946 /* create the PCM */
2947 if (rtd
->dai_link
->c2c_params
) {
2948 snprintf(new_name
, sizeof(new_name
), "codec2codec(%s)",
2949 rtd
->dai_link
->stream_name
);
2951 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2952 playback
, capture
, pcm
);
2953 } else if (rtd
->dai_link
->no_pcm
) {
2954 snprintf(new_name
, sizeof(new_name
), "(%s)",
2955 rtd
->dai_link
->stream_name
);
2957 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2958 playback
, capture
, pcm
);
2960 if (rtd
->dai_link
->dynamic
)
2961 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2962 rtd
->dai_link
->stream_name
);
2964 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2965 rtd
->dai_link
->stream_name
,
2966 soc_codec_dai_name(rtd
), num
);
2968 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
, num
, playback
,
2972 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm %s for dailink %s: %d\n",
2973 new_name
, rtd
->dai_link
->name
, ret
);
2976 dev_dbg(rtd
->card
->dev
, "ASoC: registered pcm #%d %s\n",num
, new_name
);
2981 /* create a new pcm */
2982 int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
2984 struct snd_soc_component
*component
;
2985 struct snd_pcm
*pcm
;
2986 int ret
= 0, playback
= 0, capture
= 0;
2989 ret
= soc_get_playback_capture(rtd
, &playback
, &capture
);
2993 ret
= soc_create_pcm(&pcm
, rtd
, playback
, capture
, num
);
2997 /* DAPM dai link stream work */
2999 * Currently nothing to do for c2c links
3000 * Since c2c links are internal nodes in the DAPM graph and
3001 * don't interface with the outside world or application layer
3002 * we don't have to do any special handling on close.
3004 if (!rtd
->dai_link
->c2c_params
)
3005 rtd
->close_delayed_work_func
= snd_soc_close_delayed_work
;
3008 pcm
->nonatomic
= rtd
->dai_link
->nonatomic
;
3009 pcm
->private_data
= rtd
;
3010 pcm
->no_device_suspend
= true;
3012 if (rtd
->dai_link
->no_pcm
|| rtd
->dai_link
->c2c_params
) {
3014 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
3016 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
3020 /* ASoC PCM operations */
3021 if (rtd
->dai_link
->dynamic
) {
3022 rtd
->ops
.open
= dpcm_fe_dai_open
;
3023 rtd
->ops
.hw_params
= dpcm_fe_dai_hw_params
;
3024 rtd
->ops
.prepare
= dpcm_fe_dai_prepare
;
3025 rtd
->ops
.trigger
= dpcm_fe_dai_trigger
;
3026 rtd
->ops
.hw_free
= dpcm_fe_dai_hw_free
;
3027 rtd
->ops
.close
= dpcm_fe_dai_close
;
3028 rtd
->ops
.pointer
= soc_pcm_pointer
;
3030 rtd
->ops
.open
= soc_pcm_open
;
3031 rtd
->ops
.hw_params
= soc_pcm_hw_params
;
3032 rtd
->ops
.prepare
= soc_pcm_prepare
;
3033 rtd
->ops
.trigger
= soc_pcm_trigger
;
3034 rtd
->ops
.hw_free
= soc_pcm_hw_free
;
3035 rtd
->ops
.close
= soc_pcm_close
;
3036 rtd
->ops
.pointer
= soc_pcm_pointer
;
3039 for_each_rtd_components(rtd
, i
, component
) {
3040 const struct snd_soc_component_driver
*drv
= component
->driver
;
3043 rtd
->ops
.ioctl
= snd_soc_pcm_component_ioctl
;
3045 rtd
->ops
.sync_stop
= snd_soc_pcm_component_sync_stop
;
3047 rtd
->ops
.copy
= snd_soc_pcm_component_copy
;
3049 rtd
->ops
.page
= snd_soc_pcm_component_page
;
3051 rtd
->ops
.mmap
= snd_soc_pcm_component_mmap
;
3053 rtd
->ops
.ack
= snd_soc_pcm_component_ack
;
3057 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
3060 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &rtd
->ops
);
3062 ret
= snd_soc_pcm_component_new(rtd
);
3066 dev_dbg(rtd
->card
->dev
, "%s <-> %s mapping ok\n",
3067 soc_codec_dai_name(rtd
), soc_cpu_dai_name(rtd
));
3071 /* get the substream for this BE */
3072 struct snd_pcm_substream
*
3073 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime
*be
, int stream
)
3075 return be
->pcm
->streams
[stream
].substream
;
3077 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream
);