2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai
*dai
, int stream
)
44 struct snd_soc_pcm_stream
*codec_stream
;
46 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
47 codec_stream
= &dai
->driver
->playback
;
49 codec_stream
= &dai
->driver
->capture
;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream
->rates
;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime
*rtd
, int stream
)
67 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
70 lockdep_assert_held(&rtd
->pcm_mutex
);
72 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
73 cpu_dai
->playback_active
++;
74 for (i
= 0; i
< rtd
->num_codecs
; i
++)
75 rtd
->codec_dais
[i
]->playback_active
++;
77 cpu_dai
->capture_active
++;
78 for (i
= 0; i
< rtd
->num_codecs
; i
++)
79 rtd
->codec_dais
[i
]->capture_active
++;
83 cpu_dai
->component
->active
++;
84 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
85 rtd
->codec_dais
[i
]->active
++;
86 rtd
->codec_dais
[i
]->component
->active
++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime
*rtd
, int stream
)
102 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
105 lockdep_assert_held(&rtd
->pcm_mutex
);
107 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
108 cpu_dai
->playback_active
--;
109 for (i
= 0; i
< rtd
->num_codecs
; i
++)
110 rtd
->codec_dais
[i
]->playback_active
--;
112 cpu_dai
->capture_active
--;
113 for (i
= 0; i
< rtd
->num_codecs
; i
++)
114 rtd
->codec_dais
[i
]->capture_active
--;
118 cpu_dai
->component
->active
--;
119 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
120 rtd
->codec_dais
[i
]->component
->active
--;
121 rtd
->codec_dais
[i
]->active
--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime
*rtd
)
139 if (!rtd
->pmdown_time
|| rtd
->dai_link
->ignore_pmdown_time
)
142 for (i
= 0; i
< rtd
->num_codecs
; i
++)
143 ignore
&= rtd
->codec_dais
[i
]->component
->ignore_pmdown_time
;
145 return rtd
->cpu_dai
->component
->ignore_pmdown_time
&& ignore
;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
156 const struct snd_pcm_hardware
*hw
)
158 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
159 runtime
->hw
.info
= hw
->info
;
160 runtime
->hw
.formats
= hw
->formats
;
161 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
162 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
163 runtime
->hw
.periods_min
= hw
->periods_min
;
164 runtime
->hw
.periods_max
= hw
->periods_max
;
165 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
166 runtime
->hw
.fifo_size
= hw
->fifo_size
;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime
*fe
, int dir
,
175 struct snd_soc_dpcm
*dpcm
;
177 list_for_each_entry(dpcm
, &fe
->dpcm
[dir
].be_clients
, list_be
) {
179 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
181 dev_dbg(be
->dev
, "ASoC: BE %s event %d dir %d\n",
182 be
->dai_link
->name
, event
, dir
);
184 if ((event
== SND_SOC_DAPM_STREAM_STOP
) &&
185 (be
->dpcm
[dir
].users
>= 1))
188 snd_soc_dapm_stream_event(be
, dir
, event
);
191 snd_soc_dapm_stream_event(fe
, dir
, event
);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
,
197 struct snd_soc_dai
*soc_dai
)
199 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
202 if (soc_dai
->rate
&& (soc_dai
->driver
->symmetric_rates
||
203 rtd
->dai_link
->symmetric_rates
)) {
204 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %dHz rate\n",
207 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
208 SNDRV_PCM_HW_PARAM_RATE
,
211 dev_err(soc_dai
->dev
,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai
->channels
&& (soc_dai
->driver
->symmetric_channels
||
219 rtd
->dai_link
->symmetric_channels
)) {
220 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d channel(s)\n",
223 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
224 SNDRV_PCM_HW_PARAM_CHANNELS
,
227 dev_err(soc_dai
->dev
,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai
->sample_bits
&& (soc_dai
->driver
->symmetric_samplebits
||
235 rtd
->dai_link
->symmetric_samplebits
)) {
236 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai
->sample_bits
);
239 ret
= snd_pcm_hw_constraint_single(substream
->runtime
,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
241 soc_dai
->sample_bits
);
243 dev_err(soc_dai
->dev
,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream
*substream
,
254 struct snd_pcm_hw_params
*params
)
256 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
257 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
258 unsigned int rate
, channels
, sample_bits
, symmetry
, i
;
260 rate
= params_rate(params
);
261 channels
= params_channels(params
);
262 sample_bits
= snd_pcm_format_physical_width(params_format(params
));
264 /* reject unmatched parameters when applying symmetry */
265 symmetry
= cpu_dai
->driver
->symmetric_rates
||
266 rtd
->dai_link
->symmetric_rates
;
268 for (i
= 0; i
< rtd
->num_codecs
; i
++)
269 symmetry
|= rtd
->codec_dais
[i
]->driver
->symmetric_rates
;
271 if (symmetry
&& cpu_dai
->rate
&& cpu_dai
->rate
!= rate
) {
272 dev_err(rtd
->dev
, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai
->rate
, rate
);
277 symmetry
= cpu_dai
->driver
->symmetric_channels
||
278 rtd
->dai_link
->symmetric_channels
;
280 for (i
= 0; i
< rtd
->num_codecs
; i
++)
281 symmetry
|= rtd
->codec_dais
[i
]->driver
->symmetric_channels
;
283 if (symmetry
&& cpu_dai
->channels
&& cpu_dai
->channels
!= channels
) {
284 dev_err(rtd
->dev
, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai
->channels
, channels
);
289 symmetry
= cpu_dai
->driver
->symmetric_samplebits
||
290 rtd
->dai_link
->symmetric_samplebits
;
292 for (i
= 0; i
< rtd
->num_codecs
; i
++)
293 symmetry
|= rtd
->codec_dais
[i
]->driver
->symmetric_samplebits
;
295 if (symmetry
&& cpu_dai
->sample_bits
&& cpu_dai
->sample_bits
!= sample_bits
) {
296 dev_err(rtd
->dev
, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai
->sample_bits
, sample_bits
);
304 static bool soc_pcm_has_symmetry(struct snd_pcm_substream
*substream
)
306 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
307 struct snd_soc_dai_driver
*cpu_driver
= rtd
->cpu_dai
->driver
;
308 struct snd_soc_dai_link
*link
= rtd
->dai_link
;
309 unsigned int symmetry
, i
;
311 symmetry
= cpu_driver
->symmetric_rates
|| link
->symmetric_rates
||
312 cpu_driver
->symmetric_channels
|| link
->symmetric_channels
||
313 cpu_driver
->symmetric_samplebits
|| link
->symmetric_samplebits
;
315 for (i
= 0; i
< rtd
->num_codecs
; i
++)
316 symmetry
= symmetry
||
317 rtd
->codec_dais
[i
]->driver
->symmetric_rates
||
318 rtd
->codec_dais
[i
]->driver
->symmetric_channels
||
319 rtd
->codec_dais
[i
]->driver
->symmetric_samplebits
;
324 static void soc_pcm_set_msb(struct snd_pcm_substream
*substream
, int bits
)
326 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
332 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0, 0, bits
);
334 dev_warn(rtd
->dev
, "ASoC: Failed to set MSB %d: %d\n",
338 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
)
340 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
341 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
342 struct snd_soc_dai
*codec_dai
;
344 unsigned int bits
= 0, cpu_bits
;
346 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
347 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
348 codec_dai
= rtd
->codec_dais
[i
];
349 if (codec_dai
->driver
->playback
.sig_bits
== 0) {
353 bits
= max(codec_dai
->driver
->playback
.sig_bits
, bits
);
355 cpu_bits
= cpu_dai
->driver
->playback
.sig_bits
;
357 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
358 codec_dai
= rtd
->codec_dais
[i
];
359 if (codec_dai
->driver
->capture
.sig_bits
== 0) {
363 bits
= max(codec_dai
->driver
->capture
.sig_bits
, bits
);
365 cpu_bits
= cpu_dai
->driver
->capture
.sig_bits
;
368 soc_pcm_set_msb(substream
, bits
);
369 soc_pcm_set_msb(substream
, cpu_bits
);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream
*substream
)
374 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
375 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
376 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
377 struct snd_soc_dai_driver
*cpu_dai_drv
= rtd
->cpu_dai
->driver
;
378 struct snd_soc_dai_driver
*codec_dai_drv
;
379 struct snd_soc_pcm_stream
*codec_stream
;
380 struct snd_soc_pcm_stream
*cpu_stream
;
381 unsigned int chan_min
= 0, chan_max
= UINT_MAX
;
382 unsigned int rate_min
= 0, rate_max
= UINT_MAX
;
383 unsigned int rates
= UINT_MAX
;
384 u64 formats
= ULLONG_MAX
;
387 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
388 cpu_stream
= &cpu_dai_drv
->playback
;
390 cpu_stream
= &cpu_dai_drv
->capture
;
392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
404 if (!snd_soc_dai_stream_valid(rtd
->codec_dais
[i
],
408 codec_dai_drv
= rtd
->codec_dais
[i
]->driver
;
409 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
410 codec_stream
= &codec_dai_drv
->playback
;
412 codec_stream
= &codec_dai_drv
->capture
;
413 chan_min
= max(chan_min
, codec_stream
->channels_min
);
414 chan_max
= min(chan_max
, codec_stream
->channels_max
);
415 rate_min
= max(rate_min
, codec_stream
->rate_min
);
416 rate_max
= min_not_zero(rate_max
, codec_stream
->rate_max
);
417 formats
&= codec_stream
->formats
;
418 rates
= snd_pcm_rate_mask_intersect(codec_stream
->rates
, rates
);
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
426 if (rtd
->num_codecs
> 1) {
427 chan_min
= cpu_stream
->channels_min
;
428 chan_max
= cpu_stream
->channels_max
;
431 hw
->channels_min
= max(chan_min
, cpu_stream
->channels_min
);
432 hw
->channels_max
= min(chan_max
, cpu_stream
->channels_max
);
434 hw
->formats
&= formats
& cpu_stream
->formats
;
436 hw
->formats
= formats
& cpu_stream
->formats
;
437 hw
->rates
= snd_pcm_rate_mask_intersect(rates
, cpu_stream
->rates
);
439 snd_pcm_limit_hw_rates(runtime
);
441 hw
->rate_min
= max(hw
->rate_min
, cpu_stream
->rate_min
);
442 hw
->rate_min
= max(hw
->rate_min
, rate_min
);
443 hw
->rate_max
= min_not_zero(hw
->rate_max
, cpu_stream
->rate_max
);
444 hw
->rate_max
= min_not_zero(hw
->rate_max
, rate_max
);
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
452 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
454 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
455 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
456 struct snd_soc_platform
*platform
= rtd
->platform
;
457 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
458 struct snd_soc_dai
*codec_dai
;
459 const char *codec_dai_name
= "multicodec";
462 pinctrl_pm_select_default_state(cpu_dai
->dev
);
463 for (i
= 0; i
< rtd
->num_codecs
; i
++)
464 pinctrl_pm_select_default_state(rtd
->codec_dais
[i
]->dev
);
465 pm_runtime_get_sync(cpu_dai
->dev
);
466 for (i
= 0; i
< rtd
->num_codecs
; i
++)
467 pm_runtime_get_sync(rtd
->codec_dais
[i
]->dev
);
468 pm_runtime_get_sync(platform
->dev
);
470 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
472 /* startup the audio subsystem */
473 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->startup
) {
474 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
476 dev_err(cpu_dai
->dev
, "ASoC: can't open interface"
477 " %s: %d\n", cpu_dai
->name
, ret
);
482 if (platform
->driver
->ops
&& platform
->driver
->ops
->open
) {
483 ret
= platform
->driver
->ops
->open(substream
);
485 dev_err(platform
->dev
, "ASoC: can't open platform"
486 " %s: %d\n", platform
->component
.name
, ret
);
491 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
492 codec_dai
= rtd
->codec_dais
[i
];
493 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->startup
) {
494 ret
= codec_dai
->driver
->ops
->startup(substream
,
497 dev_err(codec_dai
->dev
,
498 "ASoC: can't open codec %s: %d\n",
499 codec_dai
->name
, ret
);
504 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
505 codec_dai
->tx_mask
= 0;
507 codec_dai
->rx_mask
= 0;
510 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
511 ret
= rtd
->dai_link
->ops
->startup(substream
);
513 pr_err("ASoC: %s startup failed: %d\n",
514 rtd
->dai_link
->name
, ret
);
519 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
520 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
523 /* Check that the codec and cpu DAIs are compatible */
524 soc_pcm_init_runtime_hw(substream
);
526 if (rtd
->num_codecs
== 1)
527 codec_dai_name
= rtd
->codec_dai
->name
;
529 if (soc_pcm_has_symmetry(substream
))
530 runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
533 if (!runtime
->hw
.rates
) {
534 printk(KERN_ERR
"ASoC: %s <-> %s No matching rates\n",
535 codec_dai_name
, cpu_dai
->name
);
538 if (!runtime
->hw
.formats
) {
539 printk(KERN_ERR
"ASoC: %s <-> %s No matching formats\n",
540 codec_dai_name
, cpu_dai
->name
);
543 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
||
544 runtime
->hw
.channels_min
> runtime
->hw
.channels_max
) {
545 printk(KERN_ERR
"ASoC: %s <-> %s No matching channels\n",
546 codec_dai_name
, cpu_dai
->name
);
550 soc_pcm_apply_msb(substream
);
552 /* Symmetry only applies if we've already got an active stream. */
553 if (cpu_dai
->active
) {
554 ret
= soc_pcm_apply_symmetry(substream
, cpu_dai
);
559 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
560 if (rtd
->codec_dais
[i
]->active
) {
561 ret
= soc_pcm_apply_symmetry(substream
,
568 pr_debug("ASoC: %s <-> %s info:\n",
569 codec_dai_name
, cpu_dai
->name
);
570 pr_debug("ASoC: rate mask 0x%x\n", runtime
->hw
.rates
);
571 pr_debug("ASoC: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
572 runtime
->hw
.channels_max
);
573 pr_debug("ASoC: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
574 runtime
->hw
.rate_max
);
578 snd_soc_runtime_activate(rtd
, substream
->stream
);
580 mutex_unlock(&rtd
->pcm_mutex
);
584 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
585 rtd
->dai_link
->ops
->shutdown(substream
);
592 codec_dai
= rtd
->codec_dais
[i
];
593 if (codec_dai
->driver
->ops
->shutdown
)
594 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
597 if (platform
->driver
->ops
&& platform
->driver
->ops
->close
)
598 platform
->driver
->ops
->close(substream
);
601 if (cpu_dai
->driver
->ops
->shutdown
)
602 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
604 mutex_unlock(&rtd
->pcm_mutex
);
606 pm_runtime_put(platform
->dev
);
607 for (i
= 0; i
< rtd
->num_codecs
; i
++)
608 pm_runtime_put(rtd
->codec_dais
[i
]->dev
);
609 pm_runtime_put(cpu_dai
->dev
);
610 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
611 if (!rtd
->codec_dais
[i
]->active
)
612 pinctrl_pm_select_sleep_state(rtd
->codec_dais
[i
]->dev
);
614 if (!cpu_dai
->active
)
615 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
621 * Power down the audio subsystem pmdown_time msecs after close is called.
622 * This is to ensure there are no pops or clicks in between any music tracks
623 * due to DAPM power cycling.
625 static void close_delayed_work(struct work_struct
*work
)
627 struct snd_soc_pcm_runtime
*rtd
=
628 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
629 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[0];
631 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
633 dev_dbg(rtd
->dev
, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
634 codec_dai
->driver
->playback
.stream_name
,
635 codec_dai
->playback_active
? "active" : "inactive",
636 rtd
->pop_wait
? "yes" : "no");
638 /* are we waiting on this codec DAI stream */
639 if (rtd
->pop_wait
== 1) {
641 snd_soc_dapm_stream_event(rtd
, SNDRV_PCM_STREAM_PLAYBACK
,
642 SND_SOC_DAPM_STREAM_STOP
);
645 mutex_unlock(&rtd
->pcm_mutex
);
649 * Called by ALSA when a PCM substream is closed. Private data can be
650 * freed here. The cpu DAI, codec DAI, machine and platform are also
653 static int soc_pcm_close(struct snd_pcm_substream
*substream
)
655 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
656 struct snd_soc_platform
*platform
= rtd
->platform
;
657 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
658 struct snd_soc_dai
*codec_dai
;
661 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
663 snd_soc_runtime_deactivate(rtd
, substream
->stream
);
665 /* clear the corresponding DAIs rate when inactive */
666 if (!cpu_dai
->active
)
669 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
670 codec_dai
= rtd
->codec_dais
[i
];
671 if (!codec_dai
->active
)
675 snd_soc_dai_digital_mute(cpu_dai
, 1, substream
->stream
);
677 if (cpu_dai
->driver
->ops
->shutdown
)
678 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
680 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
681 codec_dai
= rtd
->codec_dais
[i
];
682 if (codec_dai
->driver
->ops
->shutdown
)
683 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
686 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
687 rtd
->dai_link
->ops
->shutdown(substream
);
689 if (platform
->driver
->ops
&& platform
->driver
->ops
->close
)
690 platform
->driver
->ops
->close(substream
);
692 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
693 if (snd_soc_runtime_ignore_pmdown_time(rtd
)) {
694 /* powered down playback stream now */
695 snd_soc_dapm_stream_event(rtd
,
696 SNDRV_PCM_STREAM_PLAYBACK
,
697 SND_SOC_DAPM_STREAM_STOP
);
699 /* start delayed pop wq here for playback streams */
701 queue_delayed_work(system_power_efficient_wq
,
703 msecs_to_jiffies(rtd
->pmdown_time
));
706 /* capture streams can be powered down now */
707 snd_soc_dapm_stream_event(rtd
, SNDRV_PCM_STREAM_CAPTURE
,
708 SND_SOC_DAPM_STREAM_STOP
);
711 mutex_unlock(&rtd
->pcm_mutex
);
713 pm_runtime_put(platform
->dev
);
714 for (i
= 0; i
< rtd
->num_codecs
; i
++)
715 pm_runtime_put(rtd
->codec_dais
[i
]->dev
);
716 pm_runtime_put(cpu_dai
->dev
);
717 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
718 if (!rtd
->codec_dais
[i
]->active
)
719 pinctrl_pm_select_sleep_state(rtd
->codec_dais
[i
]->dev
);
721 if (!cpu_dai
->active
)
722 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
728 * Called by ALSA when the PCM substream is prepared, can set format, sample
729 * rate, etc. This function is non atomic and can be called multiple times,
730 * it can refer to the runtime info.
732 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
734 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
735 struct snd_soc_platform
*platform
= rtd
->platform
;
736 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
737 struct snd_soc_dai
*codec_dai
;
740 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
742 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
743 ret
= rtd
->dai_link
->ops
->prepare(substream
);
745 dev_err(rtd
->card
->dev
, "ASoC: machine prepare error:"
751 if (platform
->driver
->ops
&& platform
->driver
->ops
->prepare
) {
752 ret
= platform
->driver
->ops
->prepare(substream
);
754 dev_err(platform
->dev
, "ASoC: platform prepare error:"
760 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
761 codec_dai
= rtd
->codec_dais
[i
];
762 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->prepare
) {
763 ret
= codec_dai
->driver
->ops
->prepare(substream
,
766 dev_err(codec_dai
->dev
,
767 "ASoC: codec DAI prepare error: %d\n",
774 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->prepare
) {
775 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
777 dev_err(cpu_dai
->dev
,
778 "ASoC: cpu DAI prepare error: %d\n", ret
);
783 /* cancel any delayed stream shutdown that is pending */
784 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
787 cancel_delayed_work(&rtd
->delayed_work
);
790 snd_soc_dapm_stream_event(rtd
, substream
->stream
,
791 SND_SOC_DAPM_STREAM_START
);
793 for (i
= 0; i
< rtd
->num_codecs
; i
++)
794 snd_soc_dai_digital_mute(rtd
->codec_dais
[i
], 0,
796 snd_soc_dai_digital_mute(cpu_dai
, 0, substream
->stream
);
799 mutex_unlock(&rtd
->pcm_mutex
);
803 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params
*params
,
806 struct snd_interval
*interval
;
807 int channels
= hweight_long(mask
);
809 interval
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
810 interval
->min
= channels
;
811 interval
->max
= channels
;
814 int soc_dai_hw_params(struct snd_pcm_substream
*substream
,
815 struct snd_pcm_hw_params
*params
,
816 struct snd_soc_dai
*dai
)
820 if (dai
->driver
->ops
&& dai
->driver
->ops
->hw_params
) {
821 ret
= dai
->driver
->ops
->hw_params(substream
, params
, dai
);
823 dev_err(dai
->dev
, "ASoC: can't set %s hw params: %d\n",
833 * Called by ALSA when the hardware params are set by application. This
834 * function can also be called multiple times and can allocate buffers
835 * (using snd_pcm_lib_* ). It's non-atomic.
837 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
838 struct snd_pcm_hw_params
*params
)
840 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
841 struct snd_soc_platform
*platform
= rtd
->platform
;
842 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
845 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
847 ret
= soc_pcm_params_symmetry(substream
, params
);
851 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
852 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
854 dev_err(rtd
->card
->dev
, "ASoC: machine hw_params"
855 " failed: %d\n", ret
);
860 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
861 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
862 struct snd_pcm_hw_params codec_params
;
865 * Skip CODECs which don't support the current stream type,
866 * the idea being that if a CODEC is not used for the currently
867 * set up transfer direction, it should not need to be
868 * configured, especially since the configuration used might
869 * not even be supported by that CODEC. There may be cases
870 * however where a CODEC needs to be set up although it is
871 * actually not being used for the transfer, e.g. if a
872 * capture-only CODEC is acting as an LRCLK and/or BCLK master
873 * for the DAI link including a playback-only CODEC.
874 * If this becomes necessary, we will have to augment the
875 * machine driver setup with information on how to act, so
876 * we can do the right thing here.
878 if (!snd_soc_dai_stream_valid(codec_dai
, substream
->stream
))
881 /* copy params for each codec */
882 codec_params
= *params
;
884 /* fixup params based on TDM slot masks */
885 if (codec_dai
->tx_mask
)
886 soc_pcm_codec_params_fixup(&codec_params
,
888 if (codec_dai
->rx_mask
)
889 soc_pcm_codec_params_fixup(&codec_params
,
892 ret
= soc_dai_hw_params(substream
, &codec_params
, codec_dai
);
896 codec_dai
->rate
= params_rate(&codec_params
);
897 codec_dai
->channels
= params_channels(&codec_params
);
898 codec_dai
->sample_bits
= snd_pcm_format_physical_width(
899 params_format(&codec_params
));
902 ret
= soc_dai_hw_params(substream
, params
, cpu_dai
);
906 if (platform
->driver
->ops
&& platform
->driver
->ops
->hw_params
) {
907 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
909 dev_err(platform
->dev
, "ASoC: %s hw params failed: %d\n",
910 platform
->component
.name
, ret
);
915 /* store the parameters for each DAIs */
916 cpu_dai
->rate
= params_rate(params
);
917 cpu_dai
->channels
= params_channels(params
);
918 cpu_dai
->sample_bits
=
919 snd_pcm_format_physical_width(params_format(params
));
922 mutex_unlock(&rtd
->pcm_mutex
);
926 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_free
)
927 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
934 struct snd_soc_dai
*codec_dai
= rtd
->codec_dais
[i
];
935 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_free
)
936 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
940 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
941 rtd
->dai_link
->ops
->hw_free(substream
);
943 mutex_unlock(&rtd
->pcm_mutex
);
948 * Frees resources allocated by hw_params, can be called multiple times
950 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
952 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
953 struct snd_soc_platform
*platform
= rtd
->platform
;
954 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
955 struct snd_soc_dai
*codec_dai
;
956 bool playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
959 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
961 /* clear the corresponding DAIs parameters when going to be inactive */
962 if (cpu_dai
->active
== 1) {
964 cpu_dai
->channels
= 0;
965 cpu_dai
->sample_bits
= 0;
968 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
969 codec_dai
= rtd
->codec_dais
[i
];
970 if (codec_dai
->active
== 1) {
972 codec_dai
->channels
= 0;
973 codec_dai
->sample_bits
= 0;
977 /* apply codec digital mute */
978 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
979 if ((playback
&& rtd
->codec_dais
[i
]->playback_active
== 1) ||
980 (!playback
&& rtd
->codec_dais
[i
]->capture_active
== 1))
981 snd_soc_dai_digital_mute(rtd
->codec_dais
[i
], 1,
985 /* free any machine hw params */
986 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
987 rtd
->dai_link
->ops
->hw_free(substream
);
989 /* free any DMA resources */
990 if (platform
->driver
->ops
&& platform
->driver
->ops
->hw_free
)
991 platform
->driver
->ops
->hw_free(substream
);
993 /* now free hw params for the DAIs */
994 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
995 codec_dai
= rtd
->codec_dais
[i
];
996 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_free
)
997 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
1000 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_free
)
1001 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
1003 mutex_unlock(&rtd
->pcm_mutex
);
1007 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
1009 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1010 struct snd_soc_platform
*platform
= rtd
->platform
;
1011 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1012 struct snd_soc_dai
*codec_dai
;
1015 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1016 codec_dai
= rtd
->codec_dais
[i
];
1017 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->trigger
) {
1018 ret
= codec_dai
->driver
->ops
->trigger(substream
,
1025 if (platform
->driver
->ops
&& platform
->driver
->ops
->trigger
) {
1026 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
1031 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->trigger
) {
1032 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
1037 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->trigger
) {
1038 ret
= rtd
->dai_link
->ops
->trigger(substream
, cmd
);
1046 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream
*substream
,
1049 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1050 struct snd_soc_platform
*platform
= rtd
->platform
;
1051 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1052 struct snd_soc_dai
*codec_dai
;
1055 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1056 codec_dai
= rtd
->codec_dais
[i
];
1057 if (codec_dai
->driver
->ops
&&
1058 codec_dai
->driver
->ops
->bespoke_trigger
) {
1059 ret
= codec_dai
->driver
->ops
->bespoke_trigger(substream
,
1066 if (platform
->driver
->bespoke_trigger
) {
1067 ret
= platform
->driver
->bespoke_trigger(substream
, cmd
);
1072 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->bespoke_trigger
) {
1073 ret
= cpu_dai
->driver
->ops
->bespoke_trigger(substream
, cmd
, cpu_dai
);
1080 * soc level wrapper for pointer callback
1081 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1082 * the runtime->delay will be updated accordingly.
1084 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
1086 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1087 struct snd_soc_platform
*platform
= rtd
->platform
;
1088 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1089 struct snd_soc_dai
*codec_dai
;
1090 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1091 snd_pcm_uframes_t offset
= 0;
1092 snd_pcm_sframes_t delay
= 0;
1093 snd_pcm_sframes_t codec_delay
= 0;
1096 if (platform
->driver
->ops
&& platform
->driver
->ops
->pointer
)
1097 offset
= platform
->driver
->ops
->pointer(substream
);
1099 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->delay
)
1100 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
1102 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
1103 codec_dai
= rtd
->codec_dais
[i
];
1104 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->delay
)
1105 codec_delay
= max(codec_delay
,
1106 codec_dai
->driver
->ops
->delay(substream
,
1109 delay
+= codec_delay
;
1112 * None of the existing platform drivers implement delay(), so
1113 * for now the codec_dai of first multicodec entry is used
1115 if (platform
->driver
->delay
)
1116 delay
+= platform
->driver
->delay(substream
, rtd
->codec_dais
[0]);
1118 runtime
->delay
= delay
;
1123 /* connect a FE and BE */
1124 static int dpcm_be_connect(struct snd_soc_pcm_runtime
*fe
,
1125 struct snd_soc_pcm_runtime
*be
, int stream
)
1127 struct snd_soc_dpcm
*dpcm
;
1129 /* only add new dpcms */
1130 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1131 if (dpcm
->be
== be
&& dpcm
->fe
== fe
)
1135 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
1141 be
->dpcm
[stream
].runtime
= fe
->dpcm
[stream
].runtime
;
1142 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_NEW
;
1143 list_add(&dpcm
->list_be
, &fe
->dpcm
[stream
].be_clients
);
1144 list_add(&dpcm
->list_fe
, &be
->dpcm
[stream
].fe_clients
);
1146 dev_dbg(fe
->dev
, "connected new DPCM %s path %s %s %s\n",
1147 stream
? "capture" : "playback", fe
->dai_link
->name
,
1148 stream
? "<-" : "->", be
->dai_link
->name
);
1150 #ifdef CONFIG_DEBUG_FS
1151 if (fe
->debugfs_dpcm_root
)
1152 dpcm
->debugfs_state
= debugfs_create_u32(be
->dai_link
->name
, 0644,
1153 fe
->debugfs_dpcm_root
, &dpcm
->state
);
1158 /* reparent a BE onto another FE */
1159 static void dpcm_be_reparent(struct snd_soc_pcm_runtime
*fe
,
1160 struct snd_soc_pcm_runtime
*be
, int stream
)
1162 struct snd_soc_dpcm
*dpcm
;
1163 struct snd_pcm_substream
*fe_substream
, *be_substream
;
1165 /* reparent if BE is connected to other FEs */
1166 if (!be
->dpcm
[stream
].users
)
1169 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
1171 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
1175 dev_dbg(fe
->dev
, "reparent %s path %s %s %s\n",
1176 stream
? "capture" : "playback",
1177 dpcm
->fe
->dai_link
->name
,
1178 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1180 fe_substream
= snd_soc_dpcm_get_substream(dpcm
->fe
, stream
);
1181 be_substream
->runtime
= fe_substream
->runtime
;
1186 /* disconnect a BE and FE */
1187 void dpcm_be_disconnect(struct snd_soc_pcm_runtime
*fe
, int stream
)
1189 struct snd_soc_dpcm
*dpcm
, *d
;
1191 list_for_each_entry_safe(dpcm
, d
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1192 dev_dbg(fe
->dev
, "ASoC: BE %s disconnect check for %s\n",
1193 stream
? "capture" : "playback",
1194 dpcm
->be
->dai_link
->name
);
1196 if (dpcm
->state
!= SND_SOC_DPCM_LINK_STATE_FREE
)
1199 dev_dbg(fe
->dev
, "freed DSP %s path %s %s %s\n",
1200 stream
? "capture" : "playback", fe
->dai_link
->name
,
1201 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
1203 /* BEs still alive need new FE */
1204 dpcm_be_reparent(fe
, dpcm
->be
, stream
);
1206 #ifdef CONFIG_DEBUG_FS
1207 debugfs_remove(dpcm
->debugfs_state
);
1209 list_del(&dpcm
->list_be
);
1210 list_del(&dpcm
->list_fe
);
1215 /* get BE for DAI widget and stream */
1216 static struct snd_soc_pcm_runtime
*dpcm_get_be(struct snd_soc_card
*card
,
1217 struct snd_soc_dapm_widget
*widget
, int stream
)
1219 struct snd_soc_pcm_runtime
*be
;
1222 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1223 for (i
= 0; i
< card
->num_links
; i
++) {
1226 if (!be
->dai_link
->no_pcm
)
1229 if (be
->cpu_dai
->playback_widget
== widget
)
1232 for (j
= 0; j
< be
->num_codecs
; j
++) {
1233 struct snd_soc_dai
*dai
= be
->codec_dais
[j
];
1234 if (dai
->playback_widget
== widget
)
1240 for (i
= 0; i
< card
->num_links
; i
++) {
1243 if (!be
->dai_link
->no_pcm
)
1246 if (be
->cpu_dai
->capture_widget
== widget
)
1249 for (j
= 0; j
< be
->num_codecs
; j
++) {
1250 struct snd_soc_dai
*dai
= be
->codec_dais
[j
];
1251 if (dai
->capture_widget
== widget
)
1257 dev_err(card
->dev
, "ASoC: can't get %s BE for %s\n",
1258 stream
? "capture" : "playback", widget
->name
);
1262 static inline struct snd_soc_dapm_widget
*
1263 dai_get_widget(struct snd_soc_dai
*dai
, int stream
)
1265 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1266 return dai
->playback_widget
;
1268 return dai
->capture_widget
;
1271 static int widget_in_list(struct snd_soc_dapm_widget_list
*list
,
1272 struct snd_soc_dapm_widget
*widget
)
1276 for (i
= 0; i
< list
->num_widgets
; i
++) {
1277 if (widget
== list
->widgets
[i
])
1284 int dpcm_path_get(struct snd_soc_pcm_runtime
*fe
,
1285 int stream
, struct snd_soc_dapm_widget_list
**list
)
1287 struct snd_soc_dai
*cpu_dai
= fe
->cpu_dai
;
1290 /* get number of valid DAI paths and their widgets */
1291 paths
= snd_soc_dapm_dai_get_connected_widgets(cpu_dai
, stream
, list
);
1293 dev_dbg(fe
->dev
, "ASoC: found %d audio %s paths\n", paths
,
1294 stream
? "capture" : "playback");
1299 static int dpcm_prune_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1300 struct snd_soc_dapm_widget_list
**list_
)
1302 struct snd_soc_dpcm
*dpcm
;
1303 struct snd_soc_dapm_widget_list
*list
= *list_
;
1304 struct snd_soc_dapm_widget
*widget
;
1307 /* Destroy any old FE <--> BE connections */
1308 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1311 /* is there a valid CPU DAI widget for this BE */
1312 widget
= dai_get_widget(dpcm
->be
->cpu_dai
, stream
);
1314 /* prune the BE if it's no longer in our active list */
1315 if (widget
&& widget_in_list(list
, widget
))
1318 /* is there a valid CODEC DAI widget for this BE */
1319 for (i
= 0; i
< dpcm
->be
->num_codecs
; i
++) {
1320 struct snd_soc_dai
*dai
= dpcm
->be
->codec_dais
[i
];
1321 widget
= dai_get_widget(dai
, stream
);
1323 /* prune the BE if it's no longer in our active list */
1324 if (widget
&& widget_in_list(list
, widget
))
1328 dev_dbg(fe
->dev
, "ASoC: pruning %s BE %s for %s\n",
1329 stream
? "capture" : "playback",
1330 dpcm
->be
->dai_link
->name
, fe
->dai_link
->name
);
1331 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1332 dpcm
->be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1336 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", prune
);
1340 static int dpcm_add_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1341 struct snd_soc_dapm_widget_list
**list_
)
1343 struct snd_soc_card
*card
= fe
->card
;
1344 struct snd_soc_dapm_widget_list
*list
= *list_
;
1345 struct snd_soc_pcm_runtime
*be
;
1346 int i
, new = 0, err
;
1348 /* Create any new FE <--> BE connections */
1349 for (i
= 0; i
< list
->num_widgets
; i
++) {
1351 switch (list
->widgets
[i
]->id
) {
1352 case snd_soc_dapm_dai_in
:
1353 if (stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1356 case snd_soc_dapm_dai_out
:
1357 if (stream
!= SNDRV_PCM_STREAM_CAPTURE
)
1364 /* is there a valid BE rtd for this widget */
1365 be
= dpcm_get_be(card
, list
->widgets
[i
], stream
);
1367 dev_err(fe
->dev
, "ASoC: no BE found for %s\n",
1368 list
->widgets
[i
]->name
);
1372 /* make sure BE is a real BE */
1373 if (!be
->dai_link
->no_pcm
)
1376 /* don't connect if FE is not running */
1377 if (!fe
->dpcm
[stream
].runtime
&& !fe
->fe_compr
)
1380 /* newly connected FE and BE */
1381 err
= dpcm_be_connect(fe
, be
, stream
);
1383 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
1384 list
->widgets
[i
]->name
);
1386 } else if (err
== 0) /* already connected */
1390 be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1394 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
1399 * Find the corresponding BE DAIs that source or sink audio to this
1402 int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
1403 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
1406 return dpcm_add_paths(fe
, stream
, list
);
1408 return dpcm_prune_paths(fe
, stream
, list
);
1411 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime
*fe
, int stream
)
1413 struct snd_soc_dpcm
*dpcm
;
1415 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
1416 dpcm
->be
->dpcm
[stream
].runtime_update
=
1417 SND_SOC_DPCM_UPDATE_NO
;
1420 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime
*fe
,
1423 struct snd_soc_dpcm
*dpcm
;
1425 /* disable any enabled and non active backends */
1426 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1428 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1429 struct snd_pcm_substream
*be_substream
=
1430 snd_soc_dpcm_get_substream(be
, stream
);
1432 if (be
->dpcm
[stream
].users
== 0)
1433 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1434 stream
? "capture" : "playback",
1435 be
->dpcm
[stream
].state
);
1437 if (--be
->dpcm
[stream
].users
!= 0)
1440 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1443 soc_pcm_close(be_substream
);
1444 be_substream
->runtime
= NULL
;
1445 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1449 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1451 struct snd_soc_dpcm
*dpcm
;
1454 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1455 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1457 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1458 struct snd_pcm_substream
*be_substream
=
1459 snd_soc_dpcm_get_substream(be
, stream
);
1461 if (!be_substream
) {
1462 dev_err(be
->dev
, "ASoC: no backend %s stream\n",
1463 stream
? "capture" : "playback");
1467 /* is this op for this BE ? */
1468 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1471 /* first time the dpcm is open ? */
1472 if (be
->dpcm
[stream
].users
== DPCM_MAX_BE_USERS
)
1473 dev_err(be
->dev
, "ASoC: too many users %s at open %d\n",
1474 stream
? "capture" : "playback",
1475 be
->dpcm
[stream
].state
);
1477 if (be
->dpcm
[stream
].users
++ != 0)
1480 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1481 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1484 dev_dbg(be
->dev
, "ASoC: open %s BE %s\n",
1485 stream
? "capture" : "playback", be
->dai_link
->name
);
1487 be_substream
->runtime
= be
->dpcm
[stream
].runtime
;
1488 err
= soc_pcm_open(be_substream
);
1490 dev_err(be
->dev
, "ASoC: BE open failed %d\n", err
);
1491 be
->dpcm
[stream
].users
--;
1492 if (be
->dpcm
[stream
].users
< 0)
1493 dev_err(be
->dev
, "ASoC: no users %s at unwind %d\n",
1494 stream
? "capture" : "playback",
1495 be
->dpcm
[stream
].state
);
1497 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1501 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1508 /* disable any enabled and non active backends */
1509 list_for_each_entry_continue_reverse(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1510 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1511 struct snd_pcm_substream
*be_substream
=
1512 snd_soc_dpcm_get_substream(be
, stream
);
1514 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1517 if (be
->dpcm
[stream
].users
== 0)
1518 dev_err(be
->dev
, "ASoC: no users %s at close %d\n",
1519 stream
? "capture" : "playback",
1520 be
->dpcm
[stream
].state
);
1522 if (--be
->dpcm
[stream
].users
!= 0)
1525 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1528 soc_pcm_close(be_substream
);
1529 be_substream
->runtime
= NULL
;
1530 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1536 static void dpcm_init_runtime_hw(struct snd_pcm_runtime
*runtime
,
1537 struct snd_soc_pcm_stream
*stream
,
1540 runtime
->hw
.rate_min
= stream
->rate_min
;
1541 runtime
->hw
.rate_max
= stream
->rate_max
;
1542 runtime
->hw
.channels_min
= stream
->channels_min
;
1543 runtime
->hw
.channels_max
= stream
->channels_max
;
1544 if (runtime
->hw
.formats
)
1545 runtime
->hw
.formats
&= formats
& stream
->formats
;
1547 runtime
->hw
.formats
= formats
& stream
->formats
;
1548 runtime
->hw
.rates
= stream
->rates
;
1551 static u64
dpcm_runtime_base_format(struct snd_pcm_substream
*substream
)
1553 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1554 struct snd_soc_dpcm
*dpcm
;
1555 u64 formats
= ULLONG_MAX
;
1556 int stream
= substream
->stream
;
1558 if (!fe
->dai_link
->dpcm_merged_format
)
1562 * It returns merged BE codec format
1563 * if FE want to use it (= dpcm_merged_format)
1566 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1567 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1568 struct snd_soc_dai_driver
*codec_dai_drv
;
1569 struct snd_soc_pcm_stream
*codec_stream
;
1572 for (i
= 0; i
< be
->num_codecs
; i
++) {
1574 * Skip CODECs which don't support the current stream
1575 * type. See soc_pcm_init_runtime_hw() for more details
1577 if (!snd_soc_dai_stream_valid(be
->codec_dais
[i
],
1581 codec_dai_drv
= be
->codec_dais
[i
]->driver
;
1582 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1583 codec_stream
= &codec_dai_drv
->playback
;
1585 codec_stream
= &codec_dai_drv
->capture
;
1587 formats
&= codec_stream
->formats
;
1594 static void dpcm_set_fe_runtime(struct snd_pcm_substream
*substream
)
1596 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1597 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1598 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1599 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
1600 u64 format
= dpcm_runtime_base_format(substream
);
1602 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1603 dpcm_init_runtime_hw(runtime
, &cpu_dai_drv
->playback
, format
);
1605 dpcm_init_runtime_hw(runtime
, &cpu_dai_drv
->capture
, format
);
1608 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
);
1610 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1611 * for avoiding the race with trigger callback.
1612 * If the state is unset and a trigger is pending while the previous operation,
1613 * process the pending trigger action here.
1615 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime
*fe
,
1616 int stream
, enum snd_soc_dpcm_update state
)
1618 struct snd_pcm_substream
*substream
=
1619 snd_soc_dpcm_get_substream(fe
, stream
);
1621 snd_pcm_stream_lock_irq(substream
);
1622 if (state
== SND_SOC_DPCM_UPDATE_NO
&& fe
->dpcm
[stream
].trigger_pending
) {
1623 dpcm_fe_dai_do_trigger(substream
,
1624 fe
->dpcm
[stream
].trigger_pending
- 1);
1625 fe
->dpcm
[stream
].trigger_pending
= 0;
1627 fe
->dpcm
[stream
].runtime_update
= state
;
1628 snd_pcm_stream_unlock_irq(substream
);
1631 static int dpcm_fe_dai_startup(struct snd_pcm_substream
*fe_substream
)
1633 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
1634 struct snd_pcm_runtime
*runtime
= fe_substream
->runtime
;
1635 int stream
= fe_substream
->stream
, ret
= 0;
1637 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1639 ret
= dpcm_be_dai_startup(fe
, fe_substream
->stream
);
1641 dev_err(fe
->dev
,"ASoC: failed to start some BEs %d\n", ret
);
1645 dev_dbg(fe
->dev
, "ASoC: open FE %s\n", fe
->dai_link
->name
);
1647 /* start the DAI frontend */
1648 ret
= soc_pcm_open(fe_substream
);
1650 dev_err(fe
->dev
,"ASoC: failed to start FE %d\n", ret
);
1654 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1656 dpcm_set_fe_runtime(fe_substream
);
1657 snd_pcm_limit_hw_rates(runtime
);
1659 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1663 dpcm_be_dai_startup_unwind(fe
, fe_substream
->stream
);
1665 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1669 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
1671 struct snd_soc_dpcm
*dpcm
;
1673 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1674 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1676 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1677 struct snd_pcm_substream
*be_substream
=
1678 snd_soc_dpcm_get_substream(be
, stream
);
1680 /* is this op for this BE ? */
1681 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1684 if (be
->dpcm
[stream
].users
== 0)
1685 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1686 stream
? "capture" : "playback",
1687 be
->dpcm
[stream
].state
);
1689 if (--be
->dpcm
[stream
].users
!= 0)
1692 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1693 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)) {
1694 soc_pcm_hw_free(be_substream
);
1695 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1698 dev_dbg(be
->dev
, "ASoC: close BE %s\n",
1699 dpcm
->fe
->dai_link
->name
);
1701 soc_pcm_close(be_substream
);
1702 be_substream
->runtime
= NULL
;
1704 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1709 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream
*substream
)
1711 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1712 int stream
= substream
->stream
;
1714 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1716 /* shutdown the BEs */
1717 dpcm_be_dai_shutdown(fe
, substream
->stream
);
1719 dev_dbg(fe
->dev
, "ASoC: close FE %s\n", fe
->dai_link
->name
);
1721 /* now shutdown the frontend */
1722 soc_pcm_close(substream
);
1724 /* run the stream event for each BE */
1725 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_STOP
);
1727 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1728 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1732 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime
*fe
, int stream
)
1734 struct snd_soc_dpcm
*dpcm
;
1736 /* only hw_params backends that are either sinks or sources
1737 * to this frontend DAI */
1738 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1740 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1741 struct snd_pcm_substream
*be_substream
=
1742 snd_soc_dpcm_get_substream(be
, stream
);
1744 /* is this op for this BE ? */
1745 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1748 /* only free hw when no longer used - check all FEs */
1749 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1752 /* do not free hw if this BE is used by other FE */
1753 if (be
->dpcm
[stream
].users
> 1)
1756 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1757 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
1758 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1759 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
) &&
1760 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
) &&
1761 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
1764 dev_dbg(be
->dev
, "ASoC: hw_free BE %s\n",
1765 dpcm
->fe
->dai_link
->name
);
1767 soc_pcm_hw_free(be_substream
);
1769 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1775 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream
*substream
)
1777 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1778 int err
, stream
= substream
->stream
;
1780 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1781 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1783 dev_dbg(fe
->dev
, "ASoC: hw_free FE %s\n", fe
->dai_link
->name
);
1785 /* call hw_free on the frontend */
1786 err
= soc_pcm_hw_free(substream
);
1788 dev_err(fe
->dev
,"ASoC: hw_free FE %s failed\n",
1789 fe
->dai_link
->name
);
1791 /* only hw_params backends that are either sinks or sources
1792 * to this frontend DAI */
1793 err
= dpcm_be_dai_hw_free(fe
, stream
);
1795 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1796 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1798 mutex_unlock(&fe
->card
->mutex
);
1802 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
1804 struct snd_soc_dpcm
*dpcm
;
1807 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1809 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1810 struct snd_pcm_substream
*be_substream
=
1811 snd_soc_dpcm_get_substream(be
, stream
);
1813 /* is this op for this BE ? */
1814 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1817 /* only allow hw_params() if no connected FEs are running */
1818 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
1821 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
1822 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1823 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
))
1826 dev_dbg(be
->dev
, "ASoC: hw_params BE %s\n",
1827 dpcm
->fe
->dai_link
->name
);
1829 /* copy params for each dpcm */
1830 memcpy(&dpcm
->hw_params
, &fe
->dpcm
[stream
].hw_params
,
1831 sizeof(struct snd_pcm_hw_params
));
1833 /* perform any hw_params fixups */
1834 if (be
->dai_link
->be_hw_params_fixup
) {
1835 ret
= be
->dai_link
->be_hw_params_fixup(be
,
1839 "ASoC: hw_params BE fixup failed %d\n",
1845 ret
= soc_pcm_hw_params(be_substream
, &dpcm
->hw_params
);
1847 dev_err(dpcm
->be
->dev
,
1848 "ASoC: hw_params BE failed %d\n", ret
);
1852 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
1857 /* disable any enabled and non active backends */
1858 list_for_each_entry_continue_reverse(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1859 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1860 struct snd_pcm_substream
*be_substream
=
1861 snd_soc_dpcm_get_substream(be
, stream
);
1863 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1866 /* only allow hw_free() if no connected FEs are running */
1867 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1870 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
1871 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1872 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1873 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1876 soc_pcm_hw_free(be_substream
);
1882 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream
*substream
,
1883 struct snd_pcm_hw_params
*params
)
1885 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1886 int ret
, stream
= substream
->stream
;
1888 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1889 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
1891 memcpy(&fe
->dpcm
[substream
->stream
].hw_params
, params
,
1892 sizeof(struct snd_pcm_hw_params
));
1893 ret
= dpcm_be_dai_hw_params(fe
, substream
->stream
);
1895 dev_err(fe
->dev
,"ASoC: hw_params BE failed %d\n", ret
);
1899 dev_dbg(fe
->dev
, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1900 fe
->dai_link
->name
, params_rate(params
),
1901 params_channels(params
), params_format(params
));
1903 /* call hw_params on the frontend */
1904 ret
= soc_pcm_hw_params(substream
, params
);
1906 dev_err(fe
->dev
,"ASoC: hw_params FE failed %d\n", ret
);
1907 dpcm_be_dai_hw_free(fe
, stream
);
1909 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
1912 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
1913 mutex_unlock(&fe
->card
->mutex
);
1917 static int dpcm_do_trigger(struct snd_soc_dpcm
*dpcm
,
1918 struct snd_pcm_substream
*substream
, int cmd
)
1922 dev_dbg(dpcm
->be
->dev
, "ASoC: trigger BE %s cmd %d\n",
1923 dpcm
->fe
->dai_link
->name
, cmd
);
1925 ret
= soc_pcm_trigger(substream
, cmd
);
1927 dev_err(dpcm
->be
->dev
,"ASoC: trigger BE failed %d\n", ret
);
1932 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
1935 struct snd_soc_dpcm
*dpcm
;
1938 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1940 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1941 struct snd_pcm_substream
*be_substream
=
1942 snd_soc_dpcm_get_substream(be
, stream
);
1944 /* is this op for this BE ? */
1945 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1949 case SNDRV_PCM_TRIGGER_START
:
1950 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
1951 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1954 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1958 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1960 case SNDRV_PCM_TRIGGER_RESUME
:
1961 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
1964 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1968 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1970 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1971 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
1974 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1978 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1980 case SNDRV_PCM_TRIGGER_STOP
:
1981 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1984 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1987 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1991 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
1993 case SNDRV_PCM_TRIGGER_SUSPEND
:
1994 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1997 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2000 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
2004 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
2006 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2007 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2010 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
2013 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
2017 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2024 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger
);
2026 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2028 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
2029 int stream
= substream
->stream
, ret
;
2030 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2032 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
2035 case SND_SOC_DPCM_TRIGGER_PRE
:
2036 /* call trigger on the frontend before the backend. */
2038 dev_dbg(fe
->dev
, "ASoC: pre trigger FE %s cmd %d\n",
2039 fe
->dai_link
->name
, cmd
);
2041 ret
= soc_pcm_trigger(substream
, cmd
);
2043 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
2047 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2049 case SND_SOC_DPCM_TRIGGER_POST
:
2050 /* call trigger on the frontend after the backend. */
2052 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
2054 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
2058 dev_dbg(fe
->dev
, "ASoC: post trigger FE %s cmd %d\n",
2059 fe
->dai_link
->name
, cmd
);
2061 ret
= soc_pcm_trigger(substream
, cmd
);
2063 case SND_SOC_DPCM_TRIGGER_BESPOKE
:
2064 /* bespoke trigger() - handles both FE and BEs */
2066 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd %d\n",
2067 fe
->dai_link
->name
, cmd
);
2069 ret
= soc_pcm_bespoke_trigger(substream
, cmd
);
2071 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
2076 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
2077 fe
->dai_link
->name
);
2083 case SNDRV_PCM_TRIGGER_START
:
2084 case SNDRV_PCM_TRIGGER_RESUME
:
2085 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
2086 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
2088 case SNDRV_PCM_TRIGGER_STOP
:
2089 case SNDRV_PCM_TRIGGER_SUSPEND
:
2090 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
2092 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
2093 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
2098 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
2102 static int dpcm_fe_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
)
2104 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
2105 int stream
= substream
->stream
;
2107 /* if FE's runtime_update is already set, we're in race;
2108 * process this trigger later at exit
2110 if (fe
->dpcm
[stream
].runtime_update
!= SND_SOC_DPCM_UPDATE_NO
) {
2111 fe
->dpcm
[stream
].trigger_pending
= cmd
+ 1;
2112 return 0; /* delayed, assuming it's successful */
2115 /* we're alone, let's trigger */
2116 return dpcm_fe_dai_do_trigger(substream
, cmd
);
2119 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
2121 struct snd_soc_dpcm
*dpcm
;
2124 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
2126 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2127 struct snd_pcm_substream
*be_substream
=
2128 snd_soc_dpcm_get_substream(be
, stream
);
2130 /* is this op for this BE ? */
2131 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
2134 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2135 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
2138 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
2139 dpcm
->fe
->dai_link
->name
);
2141 ret
= soc_pcm_prepare(be_substream
);
2143 dev_err(be
->dev
, "ASoC: backend prepare failed %d\n",
2148 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2153 static int dpcm_fe_dai_prepare(struct snd_pcm_substream
*substream
)
2155 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
2156 int stream
= substream
->stream
, ret
= 0;
2158 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2160 dev_dbg(fe
->dev
, "ASoC: prepare FE %s\n", fe
->dai_link
->name
);
2162 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_FE
);
2164 /* there is no point preparing this FE if there are no BEs */
2165 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2166 dev_err(fe
->dev
, "ASoC: no backend DAIs enabled for %s\n",
2167 fe
->dai_link
->name
);
2172 ret
= dpcm_be_dai_prepare(fe
, substream
->stream
);
2176 /* call prepare on the frontend */
2177 ret
= soc_pcm_prepare(substream
);
2179 dev_err(fe
->dev
,"ASoC: prepare FE %s failed\n",
2180 fe
->dai_link
->name
);
2184 /* run the stream event for each BE */
2185 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_START
);
2186 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
2189 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2190 mutex_unlock(&fe
->card
->mutex
);
2195 static int soc_pcm_ioctl(struct snd_pcm_substream
*substream
,
2196 unsigned int cmd
, void *arg
)
2198 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
2199 struct snd_soc_platform
*platform
= rtd
->platform
;
2201 if (platform
->driver
->ops
&& platform
->driver
->ops
->ioctl
)
2202 return platform
->driver
->ops
->ioctl(substream
, cmd
, arg
);
2203 return snd_pcm_lib_ioctl(substream
, cmd
, arg
);
2206 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
2208 struct snd_pcm_substream
*substream
=
2209 snd_soc_dpcm_get_substream(fe
, stream
);
2210 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2213 dev_dbg(fe
->dev
, "ASoC: runtime %s close on FE %s\n",
2214 stream
? "capture" : "playback", fe
->dai_link
->name
);
2216 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
2217 /* call bespoke trigger - FE takes care of all BE triggers */
2218 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd stop\n",
2219 fe
->dai_link
->name
);
2221 err
= soc_pcm_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
2223 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
2225 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd stop\n",
2226 fe
->dai_link
->name
);
2228 err
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_STOP
);
2230 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
2233 err
= dpcm_be_dai_hw_free(fe
, stream
);
2235 dev_err(fe
->dev
,"ASoC: hw_free FE failed %d\n", err
);
2237 err
= dpcm_be_dai_shutdown(fe
, stream
);
2239 dev_err(fe
->dev
,"ASoC: shutdown FE failed %d\n", err
);
2241 /* run the stream event for each BE */
2242 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2247 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
2249 struct snd_pcm_substream
*substream
=
2250 snd_soc_dpcm_get_substream(fe
, stream
);
2251 struct snd_soc_dpcm
*dpcm
;
2252 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
2255 dev_dbg(fe
->dev
, "ASoC: runtime %s open on FE %s\n",
2256 stream
? "capture" : "playback", fe
->dai_link
->name
);
2258 /* Only start the BE if the FE is ready */
2259 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_FREE
||
2260 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
)
2263 /* startup must always be called for new BEs */
2264 ret
= dpcm_be_dai_startup(fe
, stream
);
2268 /* keep going if FE state is > open */
2269 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
2272 ret
= dpcm_be_dai_hw_params(fe
, stream
);
2276 /* keep going if FE state is > hw_params */
2277 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
2281 ret
= dpcm_be_dai_prepare(fe
, stream
);
2285 /* run the stream event for each BE */
2286 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
2288 /* keep going if FE state is > prepare */
2289 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PREPARE
||
2290 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_STOP
)
2293 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
2294 /* call trigger on the frontend - FE takes care of all BE triggers */
2295 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd start\n",
2296 fe
->dai_link
->name
);
2298 ret
= soc_pcm_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_START
);
2300 dev_err(fe
->dev
,"ASoC: bespoke trigger FE failed %d\n", ret
);
2304 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd start\n",
2305 fe
->dai_link
->name
);
2307 ret
= dpcm_be_dai_trigger(fe
, stream
,
2308 SNDRV_PCM_TRIGGER_START
);
2310 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
2318 dpcm_be_dai_hw_free(fe
, stream
);
2320 dpcm_be_dai_shutdown(fe
, stream
);
2322 /* disconnect any non started BEs */
2323 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
2324 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2325 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
2326 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2332 static int dpcm_run_new_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
2336 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_BE
);
2337 ret
= dpcm_run_update_startup(fe
, stream
);
2339 dev_err(fe
->dev
, "ASoC: failed to startup some BEs\n");
2340 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2345 static int dpcm_run_old_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
2349 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_BE
);
2350 ret
= dpcm_run_update_shutdown(fe
, stream
);
2352 dev_err(fe
->dev
, "ASoC: failed to shutdown some BEs\n");
2353 dpcm_set_fe_update_state(fe
, stream
, SND_SOC_DPCM_UPDATE_NO
);
2358 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2361 int soc_dpcm_runtime_update(struct snd_soc_card
*card
)
2363 int i
, old
, new, paths
;
2365 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2366 for (i
= 0; i
< card
->num_rtd
; i
++) {
2367 struct snd_soc_dapm_widget_list
*list
;
2368 struct snd_soc_pcm_runtime
*fe
= &card
->rtd
[i
];
2370 /* make sure link is FE */
2371 if (!fe
->dai_link
->dynamic
)
2374 /* only check active links */
2375 if (!fe
->cpu_dai
->active
)
2378 /* DAPM sync will call this to update DSP paths */
2379 dev_dbg(fe
->dev
, "ASoC: DPCM runtime update for FE %s\n",
2380 fe
->dai_link
->name
);
2382 /* skip if FE doesn't have playback capability */
2383 if (!fe
->cpu_dai
->driver
->playback
.channels_min
2384 || !fe
->codec_dai
->driver
->playback
.channels_min
)
2387 /* skip if FE isn't currently playing */
2388 if (!fe
->cpu_dai
->playback_active
2389 || !fe
->codec_dai
->playback_active
)
2392 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
);
2394 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
2395 fe
->dai_link
->name
, "playback");
2396 mutex_unlock(&card
->mutex
);
2400 /* update any new playback paths */
2401 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
, 1);
2403 dpcm_run_new_update(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2404 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2405 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2408 /* update any old playback paths */
2409 old
= dpcm_process_paths(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
, 0);
2411 dpcm_run_old_update(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2412 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2413 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2416 dpcm_path_put(&list
);
2418 /* skip if FE doesn't have capture capability */
2419 if (!fe
->cpu_dai
->driver
->capture
.channels_min
2420 || !fe
->codec_dai
->driver
->capture
.channels_min
)
2423 /* skip if FE isn't currently capturing */
2424 if (!fe
->cpu_dai
->capture_active
2425 || !fe
->codec_dai
->capture_active
)
2428 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
);
2430 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
2431 fe
->dai_link
->name
, "capture");
2432 mutex_unlock(&card
->mutex
);
2436 /* update any new capture paths */
2437 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
, 1);
2439 dpcm_run_new_update(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2440 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2441 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2444 /* update any old capture paths */
2445 old
= dpcm_process_paths(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
, 0);
2447 dpcm_run_old_update(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2448 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2449 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2452 dpcm_path_put(&list
);
2455 mutex_unlock(&card
->mutex
);
2458 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime
*fe
, int mute
)
2460 struct snd_soc_dpcm
*dpcm
;
2461 struct list_head
*clients
=
2462 &fe
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
;
2464 list_for_each_entry(dpcm
, clients
, list_be
) {
2466 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2469 if (be
->dai_link
->ignore_suspend
)
2472 for (i
= 0; i
< be
->num_codecs
; i
++) {
2473 struct snd_soc_dai
*dai
= be
->codec_dais
[i
];
2474 struct snd_soc_dai_driver
*drv
= dai
->driver
;
2476 dev_dbg(be
->dev
, "ASoC: BE digital mute %s\n",
2477 be
->dai_link
->name
);
2479 if (drv
->ops
&& drv
->ops
->digital_mute
&&
2480 dai
->playback_active
)
2481 drv
->ops
->digital_mute(dai
, mute
);
2488 static int dpcm_fe_dai_open(struct snd_pcm_substream
*fe_substream
)
2490 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
2491 struct snd_soc_dpcm
*dpcm
;
2492 struct snd_soc_dapm_widget_list
*list
;
2494 int stream
= fe_substream
->stream
;
2496 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2497 fe
->dpcm
[stream
].runtime
= fe_substream
->runtime
;
2499 ret
= dpcm_path_get(fe
, stream
, &list
);
2501 mutex_unlock(&fe
->card
->mutex
);
2503 } else if (ret
== 0) {
2504 dev_dbg(fe
->dev
, "ASoC: %s no valid %s route\n",
2505 fe
->dai_link
->name
, stream
? "capture" : "playback");
2508 /* calculate valid and active FE <-> BE dpcms */
2509 dpcm_process_paths(fe
, stream
, &list
, 1);
2511 ret
= dpcm_fe_dai_startup(fe_substream
);
2513 /* clean up all links */
2514 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
2515 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2517 dpcm_be_disconnect(fe
, stream
);
2518 fe
->dpcm
[stream
].runtime
= NULL
;
2521 dpcm_clear_pending_state(fe
, stream
);
2522 dpcm_path_put(&list
);
2523 mutex_unlock(&fe
->card
->mutex
);
2527 static int dpcm_fe_dai_close(struct snd_pcm_substream
*fe_substream
)
2529 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
2530 struct snd_soc_dpcm
*dpcm
;
2531 int stream
= fe_substream
->stream
, ret
;
2533 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2534 ret
= dpcm_fe_dai_shutdown(fe_substream
);
2536 /* mark FE's links ready to prune */
2537 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
2538 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2540 dpcm_be_disconnect(fe
, stream
);
2542 fe
->dpcm
[stream
].runtime
= NULL
;
2543 mutex_unlock(&fe
->card
->mutex
);
2547 /* create a new pcm */
2548 int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
2550 struct snd_soc_platform
*platform
= rtd
->platform
;
2551 struct snd_soc_dai
*codec_dai
;
2552 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2553 struct snd_pcm
*pcm
;
2555 int ret
= 0, playback
= 0, capture
= 0;
2558 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
) {
2559 playback
= rtd
->dai_link
->dpcm_playback
;
2560 capture
= rtd
->dai_link
->dpcm_capture
;
2562 for (i
= 0; i
< rtd
->num_codecs
; i
++) {
2563 codec_dai
= rtd
->codec_dais
[i
];
2564 if (codec_dai
->driver
->playback
.channels_min
)
2566 if (codec_dai
->driver
->capture
.channels_min
)
2570 capture
= capture
&& cpu_dai
->driver
->capture
.channels_min
;
2571 playback
= playback
&& cpu_dai
->driver
->playback
.channels_min
;
2574 if (rtd
->dai_link
->playback_only
) {
2579 if (rtd
->dai_link
->capture_only
) {
2584 /* create the PCM */
2585 if (rtd
->dai_link
->no_pcm
) {
2586 snprintf(new_name
, sizeof(new_name
), "(%s)",
2587 rtd
->dai_link
->stream_name
);
2589 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2590 playback
, capture
, &pcm
);
2592 if (rtd
->dai_link
->dynamic
)
2593 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2594 rtd
->dai_link
->stream_name
);
2596 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2597 rtd
->dai_link
->stream_name
,
2598 (rtd
->num_codecs
> 1) ?
2599 "multicodec" : rtd
->codec_dai
->name
, num
);
2601 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
, num
, playback
,
2605 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm for %s\n",
2606 rtd
->dai_link
->name
);
2609 dev_dbg(rtd
->card
->dev
, "ASoC: registered pcm #%d %s\n",num
, new_name
);
2611 /* DAPM dai link stream work */
2612 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
2614 pcm
->nonatomic
= rtd
->dai_link
->nonatomic
;
2616 pcm
->private_data
= rtd
;
2618 if (rtd
->dai_link
->no_pcm
) {
2620 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
2622 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
2626 /* ASoC PCM operations */
2627 if (rtd
->dai_link
->dynamic
) {
2628 rtd
->ops
.open
= dpcm_fe_dai_open
;
2629 rtd
->ops
.hw_params
= dpcm_fe_dai_hw_params
;
2630 rtd
->ops
.prepare
= dpcm_fe_dai_prepare
;
2631 rtd
->ops
.trigger
= dpcm_fe_dai_trigger
;
2632 rtd
->ops
.hw_free
= dpcm_fe_dai_hw_free
;
2633 rtd
->ops
.close
= dpcm_fe_dai_close
;
2634 rtd
->ops
.pointer
= soc_pcm_pointer
;
2635 rtd
->ops
.ioctl
= soc_pcm_ioctl
;
2637 rtd
->ops
.open
= soc_pcm_open
;
2638 rtd
->ops
.hw_params
= soc_pcm_hw_params
;
2639 rtd
->ops
.prepare
= soc_pcm_prepare
;
2640 rtd
->ops
.trigger
= soc_pcm_trigger
;
2641 rtd
->ops
.hw_free
= soc_pcm_hw_free
;
2642 rtd
->ops
.close
= soc_pcm_close
;
2643 rtd
->ops
.pointer
= soc_pcm_pointer
;
2644 rtd
->ops
.ioctl
= soc_pcm_ioctl
;
2647 if (platform
->driver
->ops
) {
2648 rtd
->ops
.ack
= platform
->driver
->ops
->ack
;
2649 rtd
->ops
.copy
= platform
->driver
->ops
->copy
;
2650 rtd
->ops
.silence
= platform
->driver
->ops
->silence
;
2651 rtd
->ops
.page
= platform
->driver
->ops
->page
;
2652 rtd
->ops
.mmap
= platform
->driver
->ops
->mmap
;
2656 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
2659 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &rtd
->ops
);
2661 if (platform
->driver
->pcm_new
) {
2662 ret
= platform
->driver
->pcm_new(rtd
);
2664 dev_err(platform
->dev
,
2665 "ASoC: pcm constructor failed: %d\n",
2671 pcm
->private_free
= platform
->driver
->pcm_free
;
2673 dev_info(rtd
->card
->dev
, "%s <-> %s mapping ok\n",
2674 (rtd
->num_codecs
> 1) ? "multicodec" : rtd
->codec_dai
->name
,
2679 /* is the current PCM operation for this FE ? */
2680 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
2682 if (fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
)
2686 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update
);
2688 /* is the current PCM operation for this BE ? */
2689 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime
*fe
,
2690 struct snd_soc_pcm_runtime
*be
, int stream
)
2692 if ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
) ||
2693 ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_BE
) &&
2694 be
->dpcm
[stream
].runtime_update
))
2698 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update
);
2700 /* get the substream for this BE */
2701 struct snd_pcm_substream
*
2702 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime
*be
, int stream
)
2704 return be
->pcm
->streams
[stream
].substream
;
2706 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream
);
2708 /* get the BE runtime state */
2709 enum snd_soc_dpcm_state
2710 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime
*be
, int stream
)
2712 return be
->dpcm
[stream
].state
;
2714 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state
);
2716 /* set the BE runtime state */
2717 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime
*be
,
2718 int stream
, enum snd_soc_dpcm_state state
)
2720 be
->dpcm
[stream
].state
= state
;
2722 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state
);
2725 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2726 * are not running, paused or suspended for the specified stream direction.
2728 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime
*fe
,
2729 struct snd_soc_pcm_runtime
*be
, int stream
)
2731 struct snd_soc_dpcm
*dpcm
;
2734 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
2739 state
= dpcm
->fe
->dpcm
[stream
].state
;
2740 if (state
== SND_SOC_DPCM_STATE_START
||
2741 state
== SND_SOC_DPCM_STATE_PAUSED
||
2742 state
== SND_SOC_DPCM_STATE_SUSPEND
)
2746 /* it's safe to free/stop this BE DAI */
2749 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop
);
2752 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2753 * running, paused or suspended for the specified stream direction.
2755 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime
*fe
,
2756 struct snd_soc_pcm_runtime
*be
, int stream
)
2758 struct snd_soc_dpcm
*dpcm
;
2761 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
2766 state
= dpcm
->fe
->dpcm
[stream
].state
;
2767 if (state
== SND_SOC_DPCM_STATE_START
||
2768 state
== SND_SOC_DPCM_STATE_PAUSED
||
2769 state
== SND_SOC_DPCM_STATE_SUSPEND
||
2770 state
== SND_SOC_DPCM_STATE_PREPARE
)
2774 /* it's safe to change hw_params */
2777 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params
);
2779 int snd_soc_platform_trigger(struct snd_pcm_substream
*substream
,
2780 int cmd
, struct snd_soc_platform
*platform
)
2782 if (platform
->driver
->ops
&& platform
->driver
->ops
->trigger
)
2783 return platform
->driver
->ops
->trigger(substream
, cmd
);
2786 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger
);
2788 #ifdef CONFIG_DEBUG_FS
2789 static char *dpcm_state_string(enum snd_soc_dpcm_state state
)
2792 case SND_SOC_DPCM_STATE_NEW
:
2794 case SND_SOC_DPCM_STATE_OPEN
:
2796 case SND_SOC_DPCM_STATE_HW_PARAMS
:
2798 case SND_SOC_DPCM_STATE_PREPARE
:
2800 case SND_SOC_DPCM_STATE_START
:
2802 case SND_SOC_DPCM_STATE_STOP
:
2804 case SND_SOC_DPCM_STATE_SUSPEND
:
2806 case SND_SOC_DPCM_STATE_PAUSED
:
2808 case SND_SOC_DPCM_STATE_HW_FREE
:
2810 case SND_SOC_DPCM_STATE_CLOSE
:
2817 static ssize_t
dpcm_show_state(struct snd_soc_pcm_runtime
*fe
,
2818 int stream
, char *buf
, size_t size
)
2820 struct snd_pcm_hw_params
*params
= &fe
->dpcm
[stream
].hw_params
;
2821 struct snd_soc_dpcm
*dpcm
;
2825 offset
+= snprintf(buf
+ offset
, size
- offset
,
2826 "[%s - %s]\n", fe
->dai_link
->name
,
2827 stream
? "Capture" : "Playback");
2829 offset
+= snprintf(buf
+ offset
, size
- offset
, "State: %s\n",
2830 dpcm_state_string(fe
->dpcm
[stream
].state
));
2832 if ((fe
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2833 (fe
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
2834 offset
+= snprintf(buf
+ offset
, size
- offset
,
2836 "Format = %s, Channels = %d, Rate = %d\n",
2837 snd_pcm_format_name(params_format(params
)),
2838 params_channels(params
),
2839 params_rate(params
));
2842 offset
+= snprintf(buf
+ offset
, size
- offset
, "Backends:\n");
2844 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2845 offset
+= snprintf(buf
+ offset
, size
- offset
,
2846 " No active DSP links\n");
2850 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
2851 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2852 params
= &dpcm
->hw_params
;
2854 offset
+= snprintf(buf
+ offset
, size
- offset
,
2855 "- %s\n", be
->dai_link
->name
);
2857 offset
+= snprintf(buf
+ offset
, size
- offset
,
2859 dpcm_state_string(be
->dpcm
[stream
].state
));
2861 if ((be
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2862 (be
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
2863 offset
+= snprintf(buf
+ offset
, size
- offset
,
2864 " Hardware Params: "
2865 "Format = %s, Channels = %d, Rate = %d\n",
2866 snd_pcm_format_name(params_format(params
)),
2867 params_channels(params
),
2868 params_rate(params
));
2875 static ssize_t
dpcm_state_read_file(struct file
*file
, char __user
*user_buf
,
2876 size_t count
, loff_t
*ppos
)
2878 struct snd_soc_pcm_runtime
*fe
= file
->private_data
;
2879 ssize_t out_count
= PAGE_SIZE
, offset
= 0, ret
= 0;
2882 buf
= kmalloc(out_count
, GFP_KERNEL
);
2886 if (fe
->cpu_dai
->driver
->playback
.channels_min
)
2887 offset
+= dpcm_show_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
,
2888 buf
+ offset
, out_count
- offset
);
2890 if (fe
->cpu_dai
->driver
->capture
.channels_min
)
2891 offset
+= dpcm_show_state(fe
, SNDRV_PCM_STREAM_CAPTURE
,
2892 buf
+ offset
, out_count
- offset
);
2894 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, offset
);
2900 static const struct file_operations dpcm_state_fops
= {
2901 .open
= simple_open
,
2902 .read
= dpcm_state_read_file
,
2903 .llseek
= default_llseek
,
2906 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime
*rtd
)
2911 if (!rtd
->card
->debugfs_card_root
)
2914 rtd
->debugfs_dpcm_root
= debugfs_create_dir(rtd
->dai_link
->name
,
2915 rtd
->card
->debugfs_card_root
);
2916 if (!rtd
->debugfs_dpcm_root
) {
2918 "ASoC: Failed to create dpcm debugfs directory %s\n",
2919 rtd
->dai_link
->name
);
2923 rtd
->debugfs_dpcm_state
= debugfs_create_file("state", 0444,
2924 rtd
->debugfs_dpcm_root
,
2925 rtd
, &dpcm_state_fops
);