Commit | Line | Data |
---|---|---|
ed517582 KM |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | // | |
3 | // soc-pcm.c -- ALSA SoC PCM | |
4 | // | |
5 | // Copyright 2005 Wolfson Microelectronics PLC. | |
6 | // Copyright 2005 Openedhand Ltd. | |
7 | // Copyright (C) 2010 Slimlogic Ltd. | |
8 | // Copyright (C) 2010 Texas Instruments Inc. | |
9 | // | |
10 | // Authors: Liam Girdwood <lrg@ti.com> | |
11 | // Mark Brown <broonie@opensource.wolfsonmicro.com> | |
ddee627c LG |
12 | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/delay.h> | |
988e8cc4 | 16 | #include <linux/pinctrl/consumer.h> |
d6652ef8 | 17 | #include <linux/pm_runtime.h> |
ddee627c LG |
18 | #include <linux/slab.h> |
19 | #include <linux/workqueue.h> | |
01d7584c | 20 | #include <linux/export.h> |
f86dcef8 | 21 | #include <linux/debugfs.h> |
ddee627c LG |
22 | #include <sound/core.h> |
23 | #include <sound/pcm.h> | |
24 | #include <sound/pcm_params.h> | |
25 | #include <sound/soc.h> | |
01d7584c | 26 | #include <sound/soc-dpcm.h> |
ddee627c LG |
27 | #include <sound/initval.h> |
28 | ||
01d7584c LG |
29 | #define DPCM_MAX_BE_USERS 8 |
30 | ||
f183f927 KM |
31 | static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd, |
32 | struct snd_pcm_substream *substream) | |
33 | { | |
34 | if (rtd->dai_link->ops && | |
35 | rtd->dai_link->ops->startup) | |
36 | return rtd->dai_link->ops->startup(substream); | |
37 | return 0; | |
38 | } | |
39 | ||
0be429f9 KM |
40 | static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd, |
41 | struct snd_pcm_substream *substream) | |
42 | { | |
43 | if (rtd->dai_link->ops && | |
44 | rtd->dai_link->ops->shutdown) | |
45 | rtd->dai_link->ops->shutdown(substream); | |
46 | } | |
47 | ||
44c1a75b KM |
48 | static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd, |
49 | struct snd_pcm_substream *substream) | |
50 | { | |
51 | if (rtd->dai_link->ops && | |
52 | rtd->dai_link->ops->prepare) | |
53 | return rtd->dai_link->ops->prepare(substream); | |
54 | return 0; | |
55 | } | |
56 | ||
de9ad990 KM |
57 | static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd, |
58 | struct snd_pcm_substream *substream, | |
59 | struct snd_pcm_hw_params *params) | |
60 | { | |
61 | if (rtd->dai_link->ops && | |
62 | rtd->dai_link->ops->hw_params) | |
63 | return rtd->dai_link->ops->hw_params(substream, params); | |
64 | return 0; | |
65 | } | |
66 | ||
49f020e5 KM |
67 | static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd, |
68 | struct snd_pcm_substream *substream) | |
69 | { | |
70 | if (rtd->dai_link->ops && | |
71 | rtd->dai_link->ops->hw_free) | |
72 | rtd->dai_link->ops->hw_free(substream); | |
73 | } | |
74 | ||
ad2bf9f2 KM |
75 | static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd, |
76 | struct snd_pcm_substream *substream, | |
77 | int cmd) | |
78 | { | |
79 | if (rtd->dai_link->ops && | |
80 | rtd->dai_link->ops->trigger) | |
81 | return rtd->dai_link->ops->trigger(substream, cmd); | |
82 | return 0; | |
83 | } | |
84 | ||
7a5aaba4 KM |
85 | static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, |
86 | int stream, int action) | |
24894b76 LPC |
87 | { |
88 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
0b7990e3 | 89 | struct snd_soc_dai *codec_dai; |
2e5894d7 | 90 | int i; |
24894b76 | 91 | |
72b745e3 | 92 | lockdep_assert_held(&rtd->card->pcm_mutex); |
24894b76 | 93 | |
0f6011fd KM |
94 | cpu_dai->stream_active[stream] += action; |
95 | for_each_rtd_codec_dai(rtd, i, codec_dai) | |
96 | codec_dai->stream_active[stream] += action; | |
24894b76 | 97 | |
7a5aaba4 KM |
98 | cpu_dai->active += action; |
99 | cpu_dai->component->active += action; | |
0b7990e3 | 100 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
7a5aaba4 KM |
101 | codec_dai->active += action; |
102 | codec_dai->component->active += action; | |
2e5894d7 | 103 | } |
24894b76 LPC |
104 | } |
105 | ||
7a5aaba4 KM |
106 | /** |
107 | * snd_soc_runtime_activate() - Increment active count for PCM runtime components | |
108 | * @rtd: ASoC PCM runtime that is activated | |
109 | * @stream: Direction of the PCM stream | |
110 | * | |
111 | * Increments the active count for all the DAIs and components attached to a PCM | |
112 | * runtime. Should typically be called when a stream is opened. | |
113 | * | |
114 | * Must be called with the rtd->card->pcm_mutex being held | |
115 | */ | |
116 | void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) | |
117 | { | |
118 | snd_soc_runtime_action(rtd, stream, 1); | |
119 | } | |
120 | ||
24894b76 LPC |
121 | /** |
122 | * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components | |
123 | * @rtd: ASoC PCM runtime that is deactivated | |
124 | * @stream: Direction of the PCM stream | |
125 | * | |
126 | * Decrements the active count for all the DAIs and components attached to a PCM | |
127 | * runtime. Should typically be called when a stream is closed. | |
128 | * | |
72b745e3 | 129 | * Must be called with the rtd->card->pcm_mutex being held |
24894b76 LPC |
130 | */ |
131 | void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) | |
132 | { | |
7a5aaba4 | 133 | snd_soc_runtime_action(rtd, stream, -1); |
24894b76 LPC |
134 | } |
135 | ||
208a1589 LPC |
136 | /** |
137 | * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay | |
138 | * @rtd: The ASoC PCM runtime that should be checked. | |
139 | * | |
140 | * This function checks whether the power down delay should be ignored for a | |
141 | * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has | |
142 | * been configured to ignore the delay, or if none of the components benefits | |
143 | * from having the delay. | |
144 | */ | |
145 | bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) | |
146 | { | |
fbb16563 | 147 | struct snd_soc_component *component; |
2e5894d7 | 148 | bool ignore = true; |
613fb500 | 149 | int i; |
2e5894d7 | 150 | |
208a1589 LPC |
151 | if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) |
152 | return true; | |
153 | ||
613fb500 | 154 | for_each_rtd_components(rtd, i, component) |
72c38184 | 155 | ignore &= !component->driver->use_pmdown_time; |
fbb16563 | 156 | |
fbb16563 | 157 | return ignore; |
208a1589 LPC |
158 | } |
159 | ||
90996f43 LPC |
160 | /** |
161 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters | |
162 | * @substream: the pcm substream | |
163 | * @hw: the hardware parameters | |
164 | * | |
165 | * Sets the substream runtime hardware parameters. | |
166 | */ | |
167 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, | |
168 | const struct snd_pcm_hardware *hw) | |
169 | { | |
170 | struct snd_pcm_runtime *runtime = substream->runtime; | |
171 | runtime->hw.info = hw->info; | |
172 | runtime->hw.formats = hw->formats; | |
173 | runtime->hw.period_bytes_min = hw->period_bytes_min; | |
174 | runtime->hw.period_bytes_max = hw->period_bytes_max; | |
175 | runtime->hw.periods_min = hw->periods_min; | |
176 | runtime->hw.periods_max = hw->periods_max; | |
177 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; | |
178 | runtime->hw.fifo_size = hw->fifo_size; | |
179 | return 0; | |
180 | } | |
181 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); | |
182 | ||
01d7584c | 183 | /* DPCM stream event, send event to FE and all active BEs. */ |
23607025 | 184 | int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, |
01d7584c LG |
185 | int event) |
186 | { | |
187 | struct snd_soc_dpcm *dpcm; | |
188 | ||
8d6258a4 | 189 | for_each_dpcm_be(fe, dir, dpcm) { |
01d7584c LG |
190 | |
191 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
192 | ||
103d84a3 | 193 | dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", |
01d7584c LG |
194 | be->dai_link->name, event, dir); |
195 | ||
b1cd2e34 BG |
196 | if ((event == SND_SOC_DAPM_STREAM_STOP) && |
197 | (be->dpcm[dir].users >= 1)) | |
198 | continue; | |
199 | ||
01d7584c LG |
200 | snd_soc_dapm_stream_event(be, dir, event); |
201 | } | |
202 | ||
203 | snd_soc_dapm_stream_event(fe, dir, event); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
17841020 DA |
208 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, |
209 | struct snd_soc_dai *soc_dai) | |
ddee627c LG |
210 | { |
211 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
ddee627c LG |
212 | int ret; |
213 | ||
3635bf09 NC |
214 | if (soc_dai->rate && (soc_dai->driver->symmetric_rates || |
215 | rtd->dai_link->symmetric_rates)) { | |
216 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", | |
217 | soc_dai->rate); | |
218 | ||
4dcdd43b | 219 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
3635bf09 | 220 | SNDRV_PCM_HW_PARAM_RATE, |
4dcdd43b | 221 | soc_dai->rate); |
3635bf09 NC |
222 | if (ret < 0) { |
223 | dev_err(soc_dai->dev, | |
224 | "ASoC: Unable to apply rate constraint: %d\n", | |
225 | ret); | |
226 | return ret; | |
227 | } | |
228 | } | |
ddee627c | 229 | |
3635bf09 NC |
230 | if (soc_dai->channels && (soc_dai->driver->symmetric_channels || |
231 | rtd->dai_link->symmetric_channels)) { | |
232 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", | |
233 | soc_dai->channels); | |
ddee627c | 234 | |
4dcdd43b | 235 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
3635bf09 | 236 | SNDRV_PCM_HW_PARAM_CHANNELS, |
3635bf09 NC |
237 | soc_dai->channels); |
238 | if (ret < 0) { | |
239 | dev_err(soc_dai->dev, | |
240 | "ASoC: Unable to apply channel symmetry constraint: %d\n", | |
241 | ret); | |
242 | return ret; | |
243 | } | |
ddee627c LG |
244 | } |
245 | ||
3635bf09 NC |
246 | if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || |
247 | rtd->dai_link->symmetric_samplebits)) { | |
248 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", | |
249 | soc_dai->sample_bits); | |
ddee627c | 250 | |
4dcdd43b | 251 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
3635bf09 | 252 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
3635bf09 NC |
253 | soc_dai->sample_bits); |
254 | if (ret < 0) { | |
255 | dev_err(soc_dai->dev, | |
256 | "ASoC: Unable to apply sample bits symmetry constraint: %d\n", | |
257 | ret); | |
258 | return ret; | |
259 | } | |
ddee627c LG |
260 | } |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
3635bf09 NC |
265 | static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, |
266 | struct snd_pcm_hw_params *params) | |
267 | { | |
268 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
269 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
0b7990e3 | 270 | struct snd_soc_dai *codec_dai; |
2e5894d7 | 271 | unsigned int rate, channels, sample_bits, symmetry, i; |
3635bf09 NC |
272 | |
273 | rate = params_rate(params); | |
274 | channels = params_channels(params); | |
275 | sample_bits = snd_pcm_format_physical_width(params_format(params)); | |
276 | ||
277 | /* reject unmatched parameters when applying symmetry */ | |
278 | symmetry = cpu_dai->driver->symmetric_rates || | |
3635bf09 | 279 | rtd->dai_link->symmetric_rates; |
2e5894d7 | 280 | |
0b7990e3 KM |
281 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
282 | symmetry |= codec_dai->driver->symmetric_rates; | |
2e5894d7 | 283 | |
3635bf09 NC |
284 | if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { |
285 | dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", | |
286 | cpu_dai->rate, rate); | |
287 | return -EINVAL; | |
ddee627c LG |
288 | } |
289 | ||
3635bf09 | 290 | symmetry = cpu_dai->driver->symmetric_channels || |
3635bf09 | 291 | rtd->dai_link->symmetric_channels; |
2e5894d7 | 292 | |
0b7990e3 KM |
293 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
294 | symmetry |= codec_dai->driver->symmetric_channels; | |
2e5894d7 | 295 | |
3635bf09 NC |
296 | if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { |
297 | dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", | |
298 | cpu_dai->channels, channels); | |
299 | return -EINVAL; | |
300 | } | |
ddee627c | 301 | |
3635bf09 | 302 | symmetry = cpu_dai->driver->symmetric_samplebits || |
3635bf09 | 303 | rtd->dai_link->symmetric_samplebits; |
2e5894d7 | 304 | |
0b7990e3 KM |
305 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
306 | symmetry |= codec_dai->driver->symmetric_samplebits; | |
2e5894d7 | 307 | |
3635bf09 NC |
308 | if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { |
309 | dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", | |
310 | cpu_dai->sample_bits, sample_bits); | |
311 | return -EINVAL; | |
ddee627c LG |
312 | } |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
62e5f676 LPC |
317 | static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) |
318 | { | |
319 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
320 | struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; | |
62e5f676 | 321 | struct snd_soc_dai_link *link = rtd->dai_link; |
0b7990e3 | 322 | struct snd_soc_dai *codec_dai; |
2e5894d7 | 323 | unsigned int symmetry, i; |
62e5f676 | 324 | |
2e5894d7 BC |
325 | symmetry = cpu_driver->symmetric_rates || link->symmetric_rates || |
326 | cpu_driver->symmetric_channels || link->symmetric_channels || | |
327 | cpu_driver->symmetric_samplebits || link->symmetric_samplebits; | |
62e5f676 | 328 | |
0b7990e3 | 329 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
2e5894d7 | 330 | symmetry = symmetry || |
0b7990e3 KM |
331 | codec_dai->driver->symmetric_rates || |
332 | codec_dai->driver->symmetric_channels || | |
333 | codec_dai->driver->symmetric_samplebits; | |
2e5894d7 BC |
334 | |
335 | return symmetry; | |
62e5f676 LPC |
336 | } |
337 | ||
2e5894d7 | 338 | static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) |
58ba9b25 | 339 | { |
2e5894d7 | 340 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
c6068d3a | 341 | int ret; |
58ba9b25 MB |
342 | |
343 | if (!bits) | |
344 | return; | |
345 | ||
0e2a3751 LPC |
346 | ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); |
347 | if (ret != 0) | |
348 | dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", | |
349 | bits, ret); | |
58ba9b25 MB |
350 | } |
351 | ||
c8dd1fec BC |
352 | static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) |
353 | { | |
354 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
355 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
2e5894d7 | 356 | struct snd_soc_dai *codec_dai; |
57be9206 KM |
357 | struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; |
358 | int stream = substream->stream; | |
2e5894d7 | 359 | int i; |
c8dd1fec BC |
360 | unsigned int bits = 0, cpu_bits; |
361 | ||
57be9206 KM |
362 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
363 | pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); | |
364 | ||
365 | if (pcm_codec->sig_bits == 0) { | |
366 | bits = 0; | |
367 | break; | |
2e5894d7 | 368 | } |
57be9206 | 369 | bits = max(pcm_codec->sig_bits, bits); |
c8dd1fec BC |
370 | } |
371 | ||
57be9206 KM |
372 | pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); |
373 | cpu_bits = pcm_cpu->sig_bits; | |
374 | ||
2e5894d7 BC |
375 | soc_pcm_set_msb(substream, bits); |
376 | soc_pcm_set_msb(substream, cpu_bits); | |
c8dd1fec BC |
377 | } |
378 | ||
2e5894d7 | 379 | static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) |
bd477c31 | 380 | { |
2e5894d7 | 381 | struct snd_pcm_runtime *runtime = substream->runtime; |
78e45c99 | 382 | struct snd_pcm_hardware *hw = &runtime->hw; |
2e5894d7 | 383 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
0b7990e3 | 384 | struct snd_soc_dai *codec_dai; |
2e5894d7 BC |
385 | struct snd_soc_pcm_stream *codec_stream; |
386 | struct snd_soc_pcm_stream *cpu_stream; | |
387 | unsigned int chan_min = 0, chan_max = UINT_MAX; | |
388 | unsigned int rate_min = 0, rate_max = UINT_MAX; | |
389 | unsigned int rates = UINT_MAX; | |
390 | u64 formats = ULLONG_MAX; | |
acf253c1 | 391 | int stream = substream->stream; |
2e5894d7 | 392 | int i; |
78e45c99 | 393 | |
acf253c1 | 394 | cpu_stream = snd_soc_dai_get_pcm_stream(rtd->cpu_dai, stream); |
78e45c99 | 395 | |
2e5894d7 | 396 | /* first calculate min/max only for CODECs in the DAI link */ |
0b7990e3 | 397 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
cde79035 RW |
398 | |
399 | /* | |
400 | * Skip CODECs which don't support the current stream type. | |
401 | * Otherwise, since the rate, channel, and format values will | |
402 | * zero in that case, we would have no usable settings left, | |
403 | * causing the resulting setup to fail. | |
404 | * At least one CODEC should match, otherwise we should have | |
405 | * bailed out on a higher level, since there would be no | |
406 | * CODEC to support the transfer direction in that case. | |
407 | */ | |
0b7990e3 | 408 | if (!snd_soc_dai_stream_valid(codec_dai, |
cde79035 RW |
409 | substream->stream)) |
410 | continue; | |
411 | ||
acf253c1 KM |
412 | codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); |
413 | ||
2e5894d7 BC |
414 | chan_min = max(chan_min, codec_stream->channels_min); |
415 | chan_max = min(chan_max, codec_stream->channels_max); | |
416 | rate_min = max(rate_min, codec_stream->rate_min); | |
417 | rate_max = min_not_zero(rate_max, codec_stream->rate_max); | |
418 | formats &= codec_stream->formats; | |
419 | rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); | |
420 | } | |
421 | ||
422 | /* | |
423 | * chan min/max cannot be enforced if there are multiple CODEC DAIs | |
424 | * connected to a single CPU DAI, use CPU DAI's directly and let | |
425 | * channel allocation be fixed up later | |
426 | */ | |
427 | if (rtd->num_codecs > 1) { | |
428 | chan_min = cpu_stream->channels_min; | |
429 | chan_max = cpu_stream->channels_max; | |
430 | } | |
431 | ||
432 | hw->channels_min = max(chan_min, cpu_stream->channels_min); | |
433 | hw->channels_max = min(chan_max, cpu_stream->channels_max); | |
434 | if (hw->formats) | |
435 | hw->formats &= formats & cpu_stream->formats; | |
436 | else | |
437 | hw->formats = formats & cpu_stream->formats; | |
438 | hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates); | |
78e45c99 LPC |
439 | |
440 | snd_pcm_limit_hw_rates(runtime); | |
441 | ||
442 | hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); | |
2e5894d7 | 443 | hw->rate_min = max(hw->rate_min, rate_min); |
78e45c99 | 444 | hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); |
2e5894d7 | 445 | hw->rate_max = min_not_zero(hw->rate_max, rate_max); |
bd477c31 LPC |
446 | } |
447 | ||
dd03907b | 448 | static int soc_pcm_components_open(struct snd_pcm_substream *substream) |
e7ecfdb7 KM |
449 | { |
450 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
e7ecfdb7 | 451 | struct snd_soc_component *component; |
613fb500 | 452 | int i, ret = 0; |
e7ecfdb7 | 453 | |
613fb500 | 454 | for_each_rtd_components(rtd, i, component) { |
4a81e8f3 KM |
455 | ret = snd_soc_component_module_get_when_open(component); |
456 | if (ret < 0) { | |
e7ecfdb7 KM |
457 | dev_err(component->dev, |
458 | "ASoC: can't get module %s\n", | |
459 | component->name); | |
4a81e8f3 | 460 | return ret; |
e7ecfdb7 KM |
461 | } |
462 | ||
ae2f4849 | 463 | ret = snd_soc_component_open(component, substream); |
e7ecfdb7 KM |
464 | if (ret < 0) { |
465 | dev_err(component->dev, | |
466 | "ASoC: can't open component %s: %d\n", | |
467 | component->name, ret); | |
468 | return ret; | |
469 | } | |
470 | } | |
dd03907b | 471 | |
e7ecfdb7 KM |
472 | return 0; |
473 | } | |
474 | ||
dd03907b | 475 | static int soc_pcm_components_close(struct snd_pcm_substream *substream) |
244e2936 CK |
476 | { |
477 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
244e2936 | 478 | struct snd_soc_component *component; |
e82ebffc | 479 | int i, r, ret = 0; |
244e2936 | 480 | |
613fb500 | 481 | for_each_rtd_components(rtd, i, component) { |
e82ebffc KM |
482 | r = snd_soc_component_close(component, substream); |
483 | if (r < 0) | |
484 | ret = r; /* use last ret */ | |
485 | ||
4a81e8f3 | 486 | snd_soc_component_module_put_when_close(component); |
244e2936 CK |
487 | } |
488 | ||
3672beb8 | 489 | return ret; |
244e2936 CK |
490 | } |
491 | ||
62c86d1d KM |
492 | /* |
493 | * Called by ALSA when a PCM substream is closed. Private data can be | |
494 | * freed here. The cpu DAI, codec DAI, machine and components are also | |
495 | * shutdown. | |
496 | */ | |
497 | static int soc_pcm_close(struct snd_pcm_substream *substream) | |
498 | { | |
499 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
500 | struct snd_soc_component *component; | |
501 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
502 | struct snd_soc_dai *codec_dai; | |
503 | int i; | |
504 | ||
505 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); | |
506 | ||
507 | snd_soc_runtime_deactivate(rtd, substream->stream); | |
508 | ||
509 | snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream); | |
510 | ||
511 | snd_soc_dai_shutdown(cpu_dai, substream); | |
512 | ||
513 | for_each_rtd_codec_dai(rtd, i, codec_dai) | |
514 | snd_soc_dai_shutdown(codec_dai, substream); | |
515 | ||
516 | soc_rtd_shutdown(rtd, substream); | |
517 | ||
518 | soc_pcm_components_close(substream); | |
519 | ||
520 | snd_soc_dapm_stream_stop(rtd, substream->stream); | |
521 | ||
522 | mutex_unlock(&rtd->card->pcm_mutex); | |
523 | ||
524 | for_each_rtd_components(rtd, i, component) { | |
525 | pm_runtime_mark_last_busy(component->dev); | |
526 | pm_runtime_put_autosuspend(component->dev); | |
527 | } | |
528 | ||
529 | for_each_rtd_components(rtd, i, component) | |
530 | if (!component->active) | |
531 | pinctrl_pm_select_sleep_state(component->dev); | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
ddee627c LG |
536 | /* |
537 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is | |
538 | * then initialized and any private data can be allocated. This also calls | |
ef050bec | 539 | * startup for the cpu DAI, component, machine and codec DAI. |
ddee627c LG |
540 | */ |
541 | static int soc_pcm_open(struct snd_pcm_substream *substream) | |
542 | { | |
543 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
544 | struct snd_pcm_runtime *runtime = substream->runtime; | |
90be711e | 545 | struct snd_soc_component *component; |
ddee627c | 546 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 BC |
547 | struct snd_soc_dai *codec_dai; |
548 | const char *codec_dai_name = "multicodec"; | |
244e2936 | 549 | int i, ret = 0; |
ddee627c | 550 | |
76c39e86 KM |
551 | for_each_rtd_components(rtd, i, component) |
552 | pinctrl_pm_select_default_state(component->dev); | |
90be711e | 553 | |
613fb500 | 554 | for_each_rtd_components(rtd, i, component) |
90be711e | 555 | pm_runtime_get_sync(component->dev); |
d6652ef8 | 556 | |
72b745e3 | 557 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
ddee627c | 558 | |
5d9fa03e KM |
559 | ret = soc_pcm_components_open(substream); |
560 | if (ret < 0) | |
561 | goto component_err; | |
562 | ||
563 | ret = soc_rtd_startup(rtd, substream); | |
564 | if (ret < 0) { | |
565 | pr_err("ASoC: %s startup failed: %d\n", | |
566 | rtd->dai_link->name, ret); | |
567 | goto component_err; | |
568 | } | |
569 | ||
ddee627c | 570 | /* startup the audio subsystem */ |
5a52a045 KM |
571 | ret = snd_soc_dai_startup(cpu_dai, substream); |
572 | if (ret < 0) { | |
573 | dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n", | |
574 | cpu_dai->name, ret); | |
5d9fa03e | 575 | goto cpu_dai_err; |
ddee627c LG |
576 | } |
577 | ||
0b7990e3 | 578 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
5a52a045 KM |
579 | ret = snd_soc_dai_startup(codec_dai, substream); |
580 | if (ret < 0) { | |
581 | dev_err(codec_dai->dev, | |
582 | "ASoC: can't open codec %s: %d\n", | |
583 | codec_dai->name, ret); | |
5d9fa03e | 584 | goto config_err; |
ddee627c | 585 | } |
2e5894d7 BC |
586 | |
587 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
588 | codec_dai->tx_mask = 0; | |
589 | else | |
590 | codec_dai->rx_mask = 0; | |
ddee627c LG |
591 | } |
592 | ||
01d7584c LG |
593 | /* Dynamic PCM DAI links compat checks use dynamic capabilities */ |
594 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) | |
595 | goto dynamic; | |
596 | ||
ddee627c | 597 | /* Check that the codec and cpu DAIs are compatible */ |
2e5894d7 BC |
598 | soc_pcm_init_runtime_hw(substream); |
599 | ||
600 | if (rtd->num_codecs == 1) | |
601 | codec_dai_name = rtd->codec_dai->name; | |
ddee627c | 602 | |
62e5f676 LPC |
603 | if (soc_pcm_has_symmetry(substream)) |
604 | runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; | |
605 | ||
ddee627c | 606 | ret = -EINVAL; |
ddee627c | 607 | if (!runtime->hw.rates) { |
103d84a3 | 608 | printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", |
2e5894d7 | 609 | codec_dai_name, cpu_dai->name); |
ddee627c LG |
610 | goto config_err; |
611 | } | |
612 | if (!runtime->hw.formats) { | |
103d84a3 | 613 | printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", |
2e5894d7 | 614 | codec_dai_name, cpu_dai->name); |
ddee627c LG |
615 | goto config_err; |
616 | } | |
617 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || | |
618 | runtime->hw.channels_min > runtime->hw.channels_max) { | |
103d84a3 | 619 | printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", |
2e5894d7 | 620 | codec_dai_name, cpu_dai->name); |
ddee627c LG |
621 | goto config_err; |
622 | } | |
623 | ||
c8dd1fec | 624 | soc_pcm_apply_msb(substream); |
58ba9b25 | 625 | |
ddee627c | 626 | /* Symmetry only applies if we've already got an active stream. */ |
17841020 DA |
627 | if (cpu_dai->active) { |
628 | ret = soc_pcm_apply_symmetry(substream, cpu_dai); | |
629 | if (ret != 0) | |
630 | goto config_err; | |
631 | } | |
632 | ||
0b7990e3 KM |
633 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
634 | if (codec_dai->active) { | |
635 | ret = soc_pcm_apply_symmetry(substream, codec_dai); | |
2e5894d7 BC |
636 | if (ret != 0) |
637 | goto config_err; | |
638 | } | |
ddee627c LG |
639 | } |
640 | ||
103d84a3 | 641 | pr_debug("ASoC: %s <-> %s info:\n", |
2e5894d7 | 642 | codec_dai_name, cpu_dai->name); |
103d84a3 LG |
643 | pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); |
644 | pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, | |
ddee627c | 645 | runtime->hw.channels_max); |
103d84a3 | 646 | pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, |
ddee627c LG |
647 | runtime->hw.rate_max); |
648 | ||
01d7584c | 649 | dynamic: |
24894b76 LPC |
650 | |
651 | snd_soc_runtime_activate(rtd, substream->stream); | |
652 | ||
72b745e3 | 653 | mutex_unlock(&rtd->card->pcm_mutex); |
ddee627c LG |
654 | return 0; |
655 | ||
656 | config_err: | |
b56be800 | 657 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
330fcb51 | 658 | snd_soc_dai_shutdown(codec_dai, substream); |
5d9fa03e KM |
659 | cpu_dai_err: |
660 | snd_soc_dai_shutdown(cpu_dai, substream); | |
2e5894d7 | 661 | |
5d9fa03e | 662 | soc_rtd_shutdown(rtd, substream); |
b8135864 | 663 | component_err: |
dd03907b | 664 | soc_pcm_components_close(substream); |
e7ecfdb7 | 665 | |
72b745e3 | 666 | mutex_unlock(&rtd->card->pcm_mutex); |
d6652ef8 | 667 | |
613fb500 | 668 | for_each_rtd_components(rtd, i, component) { |
90be711e KM |
669 | pm_runtime_mark_last_busy(component->dev); |
670 | pm_runtime_put_autosuspend(component->dev); | |
3f809783 SK |
671 | } |
672 | ||
76c39e86 KM |
673 | for_each_rtd_components(rtd, i, component) |
674 | if (!component->active) | |
675 | pinctrl_pm_select_sleep_state(component->dev); | |
d6652ef8 | 676 | |
ddee627c LG |
677 | return ret; |
678 | } | |
679 | ||
4bf2e385 | 680 | static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) |
a342031c JB |
681 | { |
682 | /* | |
683 | * Currently nothing to do for c2c links | |
684 | * Since c2c links are internal nodes in the DAPM graph and | |
685 | * don't interface with the outside world or application layer | |
686 | * we don't have to do any special handling on close. | |
687 | */ | |
688 | } | |
689 | ||
ddee627c LG |
690 | /* |
691 | * Called by ALSA when the PCM substream is prepared, can set format, sample | |
692 | * rate, etc. This function is non atomic and can be called multiple times, | |
693 | * it can refer to the runtime info. | |
694 | */ | |
695 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) | |
696 | { | |
697 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
b8135864 | 698 | struct snd_soc_component *component; |
ddee627c | 699 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 BC |
700 | struct snd_soc_dai *codec_dai; |
701 | int i, ret = 0; | |
ddee627c | 702 | |
72b745e3 | 703 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
ddee627c | 704 | |
44c1a75b KM |
705 | ret = soc_rtd_prepare(rtd, substream); |
706 | if (ret < 0) { | |
707 | dev_err(rtd->card->dev, | |
708 | "ASoC: machine prepare error: %d\n", ret); | |
709 | goto out; | |
ddee627c LG |
710 | } |
711 | ||
613fb500 | 712 | for_each_rtd_components(rtd, i, component) { |
6d537233 | 713 | ret = snd_soc_component_prepare(component, substream); |
b8135864 KM |
714 | if (ret < 0) { |
715 | dev_err(component->dev, | |
716 | "ASoC: platform prepare error: %d\n", ret); | |
717 | goto out; | |
718 | } | |
719 | } | |
720 | ||
0b7990e3 | 721 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
4beb8e10 | 722 | ret = snd_soc_dai_prepare(codec_dai, substream); |
ddee627c | 723 | if (ret < 0) { |
4beb8e10 KM |
724 | dev_err(codec_dai->dev, |
725 | "ASoC: codec DAI prepare error: %d\n", | |
726 | ret); | |
ddee627c LG |
727 | goto out; |
728 | } | |
729 | } | |
730 | ||
4beb8e10 KM |
731 | ret = snd_soc_dai_prepare(cpu_dai, substream); |
732 | if (ret < 0) { | |
733 | dev_err(cpu_dai->dev, | |
734 | "ASoC: cpu DAI prepare error: %d\n", ret); | |
735 | goto out; | |
736 | } | |
737 | ||
ddee627c LG |
738 | /* cancel any delayed stream shutdown that is pending */ |
739 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
9bffb1fb MLC |
740 | rtd->pop_wait) { |
741 | rtd->pop_wait = 0; | |
ddee627c LG |
742 | cancel_delayed_work(&rtd->delayed_work); |
743 | } | |
744 | ||
d9b0951b LG |
745 | snd_soc_dapm_stream_event(rtd, substream->stream, |
746 | SND_SOC_DAPM_STREAM_START); | |
ddee627c | 747 | |
0b7990e3 KM |
748 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
749 | snd_soc_dai_digital_mute(codec_dai, 0, | |
2e5894d7 | 750 | substream->stream); |
ae11601b | 751 | snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); |
ddee627c LG |
752 | |
753 | out: | |
72b745e3 | 754 | mutex_unlock(&rtd->card->pcm_mutex); |
ddee627c LG |
755 | return ret; |
756 | } | |
757 | ||
2e5894d7 BC |
758 | static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, |
759 | unsigned int mask) | |
760 | { | |
761 | struct snd_interval *interval; | |
762 | int channels = hweight_long(mask); | |
763 | ||
764 | interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
765 | interval->min = channels; | |
766 | interval->max = channels; | |
767 | } | |
768 | ||
244e2936 CK |
769 | static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, |
770 | struct snd_soc_component *last) | |
771 | { | |
772 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
244e2936 | 773 | struct snd_soc_component *component; |
e82ebffc | 774 | int i, r, ret = 0; |
244e2936 | 775 | |
613fb500 | 776 | for_each_rtd_components(rtd, i, component) { |
244e2936 CK |
777 | if (component == last) |
778 | break; | |
779 | ||
e82ebffc KM |
780 | r = snd_soc_component_hw_free(component, substream); |
781 | if (r < 0) | |
782 | ret = r; /* use last ret */ | |
244e2936 CK |
783 | } |
784 | ||
eae7136a | 785 | return ret; |
244e2936 CK |
786 | } |
787 | ||
ddee627c LG |
788 | /* |
789 | * Called by ALSA when the hardware params are set by application. This | |
790 | * function can also be called multiple times and can allocate buffers | |
791 | * (using snd_pcm_lib_* ). It's non-atomic. | |
792 | */ | |
793 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, | |
794 | struct snd_pcm_hw_params *params) | |
795 | { | |
796 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
b8135864 | 797 | struct snd_soc_component *component; |
ddee627c | 798 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
0b7990e3 | 799 | struct snd_soc_dai *codec_dai; |
244e2936 | 800 | int i, ret = 0; |
ddee627c | 801 | |
72b745e3 | 802 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
5cca5951 SW |
803 | |
804 | ret = soc_pcm_params_symmetry(substream, params); | |
805 | if (ret) | |
806 | goto out; | |
807 | ||
de9ad990 KM |
808 | ret = soc_rtd_hw_params(rtd, substream, params); |
809 | if (ret < 0) { | |
810 | dev_err(rtd->card->dev, | |
811 | "ASoC: machine hw_params failed: %d\n", ret); | |
812 | goto out; | |
ddee627c LG |
813 | } |
814 | ||
0b7990e3 | 815 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
2e5894d7 BC |
816 | struct snd_pcm_hw_params codec_params; |
817 | ||
cde79035 RW |
818 | /* |
819 | * Skip CODECs which don't support the current stream type, | |
820 | * the idea being that if a CODEC is not used for the currently | |
821 | * set up transfer direction, it should not need to be | |
822 | * configured, especially since the configuration used might | |
823 | * not even be supported by that CODEC. There may be cases | |
824 | * however where a CODEC needs to be set up although it is | |
825 | * actually not being used for the transfer, e.g. if a | |
826 | * capture-only CODEC is acting as an LRCLK and/or BCLK master | |
827 | * for the DAI link including a playback-only CODEC. | |
828 | * If this becomes necessary, we will have to augment the | |
829 | * machine driver setup with information on how to act, so | |
830 | * we can do the right thing here. | |
831 | */ | |
832 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) | |
833 | continue; | |
834 | ||
2e5894d7 BC |
835 | /* copy params for each codec */ |
836 | codec_params = *params; | |
837 | ||
838 | /* fixup params based on TDM slot masks */ | |
570f18b6 RW |
839 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
840 | codec_dai->tx_mask) | |
2e5894d7 BC |
841 | soc_pcm_codec_params_fixup(&codec_params, |
842 | codec_dai->tx_mask); | |
570f18b6 RW |
843 | |
844 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && | |
845 | codec_dai->rx_mask) | |
2e5894d7 BC |
846 | soc_pcm_codec_params_fixup(&codec_params, |
847 | codec_dai->rx_mask); | |
848 | ||
aa6166c2 KM |
849 | ret = snd_soc_dai_hw_params(codec_dai, substream, |
850 | &codec_params); | |
93e6958a | 851 | if(ret < 0) |
ddee627c | 852 | goto codec_err; |
ddee627c | 853 | |
2e5894d7 BC |
854 | codec_dai->rate = params_rate(&codec_params); |
855 | codec_dai->channels = params_channels(&codec_params); | |
856 | codec_dai->sample_bits = snd_pcm_format_physical_width( | |
857 | params_format(&codec_params)); | |
078a85f2 CK |
858 | |
859 | snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); | |
ddee627c LG |
860 | } |
861 | ||
aa6166c2 | 862 | ret = snd_soc_dai_hw_params(cpu_dai, substream, params); |
93e6958a BC |
863 | if (ret < 0) |
864 | goto interface_err; | |
ddee627c | 865 | |
ca58221d KM |
866 | /* store the parameters for each DAIs */ |
867 | cpu_dai->rate = params_rate(params); | |
868 | cpu_dai->channels = params_channels(params); | |
869 | cpu_dai->sample_bits = | |
870 | snd_pcm_format_physical_width(params_format(params)); | |
871 | ||
872 | snd_soc_dapm_update_dai(substream, params, cpu_dai); | |
873 | ||
613fb500 | 874 | for_each_rtd_components(rtd, i, component) { |
245c539a | 875 | ret = snd_soc_component_hw_params(component, substream, params); |
244e2936 | 876 | if (ret < 0) { |
b8135864 KM |
877 | dev_err(component->dev, |
878 | "ASoC: %s hw params failed: %d\n", | |
244e2936 CK |
879 | component->name, ret); |
880 | goto component_err; | |
b8135864 KM |
881 | } |
882 | } | |
244e2936 | 883 | component = NULL; |
b8135864 | 884 | |
ddee627c | 885 | out: |
72b745e3 | 886 | mutex_unlock(&rtd->card->pcm_mutex); |
ddee627c LG |
887 | return ret; |
888 | ||
b8135864 | 889 | component_err: |
244e2936 | 890 | soc_pcm_components_hw_free(substream, component); |
b8135864 | 891 | |
846faaed | 892 | snd_soc_dai_hw_free(cpu_dai, substream); |
2371abdc | 893 | cpu_dai->rate = 0; |
ddee627c LG |
894 | |
895 | interface_err: | |
2e5894d7 | 896 | i = rtd->num_codecs; |
ddee627c LG |
897 | |
898 | codec_err: | |
6d11b128 | 899 | for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) { |
f47b9ad9 JB |
900 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) |
901 | continue; | |
902 | ||
846faaed | 903 | snd_soc_dai_hw_free(codec_dai, substream); |
2e5894d7 BC |
904 | codec_dai->rate = 0; |
905 | } | |
906 | ||
49f020e5 | 907 | soc_rtd_hw_free(rtd, substream); |
ddee627c | 908 | |
72b745e3 | 909 | mutex_unlock(&rtd->card->pcm_mutex); |
ddee627c LG |
910 | return ret; |
911 | } | |
912 | ||
913 | /* | |
914 | * Frees resources allocated by hw_params, can be called multiple times | |
915 | */ | |
916 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) | |
917 | { | |
918 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
ddee627c | 919 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 | 920 | struct snd_soc_dai *codec_dai; |
7f62b6ee | 921 | bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
2e5894d7 | 922 | int i; |
ddee627c | 923 | |
72b745e3 | 924 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
ddee627c | 925 | |
d3383420 NC |
926 | /* clear the corresponding DAIs parameters when going to be inactive */ |
927 | if (cpu_dai->active == 1) { | |
928 | cpu_dai->rate = 0; | |
929 | cpu_dai->channels = 0; | |
930 | cpu_dai->sample_bits = 0; | |
931 | } | |
932 | ||
0b7990e3 | 933 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
2e5894d7 BC |
934 | if (codec_dai->active == 1) { |
935 | codec_dai->rate = 0; | |
936 | codec_dai->channels = 0; | |
937 | codec_dai->sample_bits = 0; | |
938 | } | |
d3383420 NC |
939 | } |
940 | ||
ddee627c | 941 | /* apply codec digital mute */ |
0b7990e3 | 942 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
0f6011fd KM |
943 | int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]; |
944 | int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE]; | |
945 | ||
946 | if ((playback && playback_active == 1) || | |
947 | (!playback && capture_active == 1)) | |
0b7990e3 | 948 | snd_soc_dai_digital_mute(codec_dai, 1, |
2e5894d7 BC |
949 | substream->stream); |
950 | } | |
ddee627c LG |
951 | |
952 | /* free any machine hw params */ | |
49f020e5 | 953 | soc_rtd_hw_free(rtd, substream); |
ddee627c | 954 | |
b8135864 | 955 | /* free any component resources */ |
244e2936 | 956 | soc_pcm_components_hw_free(substream, NULL); |
b8135864 | 957 | |
ddee627c | 958 | /* now free hw params for the DAIs */ |
0b7990e3 | 959 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
f47b9ad9 JB |
960 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) |
961 | continue; | |
962 | ||
846faaed | 963 | snd_soc_dai_hw_free(codec_dai, substream); |
2e5894d7 | 964 | } |
ddee627c | 965 | |
846faaed | 966 | snd_soc_dai_hw_free(cpu_dai, substream); |
ddee627c | 967 | |
72b745e3 | 968 | mutex_unlock(&rtd->card->pcm_mutex); |
ddee627c LG |
969 | return 0; |
970 | } | |
971 | ||
4378f1fb | 972 | static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd) |
ddee627c LG |
973 | { |
974 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
b8135864 | 975 | struct snd_soc_component *component; |
ddee627c | 976 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 BC |
977 | struct snd_soc_dai *codec_dai; |
978 | int i, ret; | |
979 | ||
ad2bf9f2 KM |
980 | ret = soc_rtd_trigger(rtd, substream, cmd); |
981 | if (ret < 0) | |
982 | return ret; | |
ddee627c | 983 | |
613fb500 | 984 | for_each_rtd_components(rtd, i, component) { |
5693d50c | 985 | ret = snd_soc_component_trigger(component, substream, cmd); |
b8135864 KM |
986 | if (ret < 0) |
987 | return ret; | |
988 | } | |
989 | ||
901e822b | 990 | ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); |
95aef355 KM |
991 | if (ret < 0) |
992 | return ret; | |
4792b0db | 993 | |
4378f1fb PU |
994 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
995 | ret = snd_soc_dai_trigger(codec_dai, substream, cmd); | |
996 | if (ret < 0) | |
997 | return ret; | |
998 | } | |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd) | |
1004 | { | |
1005 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
1006 | struct snd_soc_component *component; | |
4378f1fb PU |
1007 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
1008 | struct snd_soc_dai *codec_dai; | |
1009 | int i, ret; | |
1010 | ||
1011 | for_each_rtd_codec_dai(rtd, i, codec_dai) { | |
1012 | ret = snd_soc_dai_trigger(codec_dai, substream, cmd); | |
1013 | if (ret < 0) | |
1014 | return ret; | |
1015 | } | |
1016 | ||
1017 | ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); | |
1018 | if (ret < 0) | |
1019 | return ret; | |
1020 | ||
613fb500 | 1021 | for_each_rtd_components(rtd, i, component) { |
4378f1fb PU |
1022 | ret = snd_soc_component_trigger(component, substream, cmd); |
1023 | if (ret < 0) | |
1024 | return ret; | |
1025 | } | |
1026 | ||
ad2bf9f2 KM |
1027 | ret = soc_rtd_trigger(rtd, substream, cmd); |
1028 | if (ret < 0) | |
1029 | return ret; | |
4792b0db | 1030 | |
ddee627c LG |
1031 | return 0; |
1032 | } | |
1033 | ||
4378f1fb PU |
1034 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
1035 | { | |
1036 | int ret; | |
1037 | ||
1038 | switch (cmd) { | |
1039 | case SNDRV_PCM_TRIGGER_START: | |
1040 | case SNDRV_PCM_TRIGGER_RESUME: | |
1041 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1042 | ret = soc_pcm_trigger_start(substream, cmd); | |
1043 | break; | |
1044 | case SNDRV_PCM_TRIGGER_STOP: | |
1045 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
1046 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1047 | ret = soc_pcm_trigger_stop(substream, cmd); | |
1048 | break; | |
1049 | default: | |
1050 | return -EINVAL; | |
1051 | } | |
1052 | ||
1053 | return ret; | |
1054 | } | |
1055 | ||
45c0a188 MB |
1056 | static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, |
1057 | int cmd) | |
07bf84aa LG |
1058 | { |
1059 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
07bf84aa | 1060 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 BC |
1061 | struct snd_soc_dai *codec_dai; |
1062 | int i, ret; | |
1063 | ||
0b7990e3 | 1064 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
5c0769af | 1065 | ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd); |
07bf84aa LG |
1066 | if (ret < 0) |
1067 | return ret; | |
1068 | } | |
5c0769af | 1069 | |
901e822b | 1070 | ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd); |
5c0769af KM |
1071 | if (ret < 0) |
1072 | return ret; | |
1073 | ||
07bf84aa LG |
1074 | return 0; |
1075 | } | |
ddee627c LG |
1076 | /* |
1077 | * soc level wrapper for pointer callback | |
ef050bec | 1078 | * If cpu_dai, codec_dai, component driver has the delay callback, then |
ddee627c LG |
1079 | * the runtime->delay will be updated accordingly. |
1080 | */ | |
1081 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) | |
1082 | { | |
1083 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
ddee627c | 1084 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2e5894d7 | 1085 | struct snd_soc_dai *codec_dai; |
ddee627c LG |
1086 | struct snd_pcm_runtime *runtime = substream->runtime; |
1087 | snd_pcm_uframes_t offset = 0; | |
1088 | snd_pcm_sframes_t delay = 0; | |
2e5894d7 BC |
1089 | snd_pcm_sframes_t codec_delay = 0; |
1090 | int i; | |
ddee627c | 1091 | |
9fb4c2bf AA |
1092 | /* clearing the previous total delay */ |
1093 | runtime->delay = 0; | |
1094 | ||
0035e256 | 1095 | offset = snd_soc_pcm_component_pointer(substream); |
b8135864 | 1096 | |
9fb4c2bf AA |
1097 | /* base delay if assigned in pointer callback */ |
1098 | delay = runtime->delay; | |
b8135864 | 1099 | |
1dea80d4 | 1100 | delay += snd_soc_dai_delay(cpu_dai, substream); |
ddee627c | 1101 | |
0b7990e3 | 1102 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
1dea80d4 KM |
1103 | codec_delay = max(codec_delay, |
1104 | snd_soc_dai_delay(codec_dai, substream)); | |
2e5894d7 BC |
1105 | } |
1106 | delay += codec_delay; | |
ddee627c | 1107 | |
ddee627c LG |
1108 | runtime->delay = delay; |
1109 | ||
1110 | return offset; | |
1111 | } | |
1112 | ||
01d7584c LG |
1113 | /* connect a FE and BE */ |
1114 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, | |
1115 | struct snd_soc_pcm_runtime *be, int stream) | |
1116 | { | |
1117 | struct snd_soc_dpcm *dpcm; | |
a9764869 | 1118 | unsigned long flags; |
bd0b609e | 1119 | #ifdef CONFIG_DEBUG_FS |
0632fa04 | 1120 | char *name; |
bd0b609e | 1121 | #endif |
01d7584c LG |
1122 | |
1123 | /* only add new dpcms */ | |
8d6258a4 | 1124 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1125 | if (dpcm->be == be && dpcm->fe == fe) |
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); | |
1130 | if (!dpcm) | |
1131 | return -ENOMEM; | |
1132 | ||
1133 | dpcm->be = be; | |
1134 | dpcm->fe = fe; | |
1135 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; | |
1136 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; | |
a9764869 | 1137 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
01d7584c LG |
1138 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); |
1139 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); | |
a9764869 | 1140 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
01d7584c | 1141 | |
7cc302d2 | 1142 | dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", |
01d7584c LG |
1143 | stream ? "capture" : "playback", fe->dai_link->name, |
1144 | stream ? "<-" : "->", be->dai_link->name); | |
1145 | ||
f86dcef8 | 1146 | #ifdef CONFIG_DEBUG_FS |
0632fa04 HG |
1147 | name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name, |
1148 | stream ? "capture" : "playback"); | |
1149 | if (name) { | |
1150 | dpcm->debugfs_state = debugfs_create_dir(name, | |
1151 | fe->debugfs_dpcm_root); | |
1152 | debugfs_create_u32("state", 0644, dpcm->debugfs_state, | |
1153 | &dpcm->state); | |
1154 | kfree(name); | |
1155 | } | |
f86dcef8 | 1156 | #endif |
01d7584c LG |
1157 | return 1; |
1158 | } | |
1159 | ||
1160 | /* reparent a BE onto another FE */ | |
1161 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, | |
1162 | struct snd_soc_pcm_runtime *be, int stream) | |
1163 | { | |
1164 | struct snd_soc_dpcm *dpcm; | |
1165 | struct snd_pcm_substream *fe_substream, *be_substream; | |
1166 | ||
1167 | /* reparent if BE is connected to other FEs */ | |
1168 | if (!be->dpcm[stream].users) | |
1169 | return; | |
1170 | ||
1171 | be_substream = snd_soc_dpcm_get_substream(be, stream); | |
1172 | ||
d2e24d64 | 1173 | for_each_dpcm_fe(be, stream, dpcm) { |
01d7584c LG |
1174 | if (dpcm->fe == fe) |
1175 | continue; | |
1176 | ||
7cc302d2 | 1177 | dev_dbg(fe->dev, "reparent %s path %s %s %s\n", |
01d7584c LG |
1178 | stream ? "capture" : "playback", |
1179 | dpcm->fe->dai_link->name, | |
1180 | stream ? "<-" : "->", dpcm->be->dai_link->name); | |
1181 | ||
1182 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); | |
1183 | be_substream->runtime = fe_substream->runtime; | |
1184 | break; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | /* disconnect a BE and FE */ | |
23607025 | 1189 | void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1190 | { |
1191 | struct snd_soc_dpcm *dpcm, *d; | |
a9764869 | 1192 | unsigned long flags; |
01d7584c | 1193 | |
8d6258a4 | 1194 | for_each_dpcm_be_safe(fe, stream, dpcm, d) { |
103d84a3 | 1195 | dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", |
01d7584c LG |
1196 | stream ? "capture" : "playback", |
1197 | dpcm->be->dai_link->name); | |
1198 | ||
1199 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) | |
1200 | continue; | |
1201 | ||
7cc302d2 | 1202 | dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", |
01d7584c LG |
1203 | stream ? "capture" : "playback", fe->dai_link->name, |
1204 | stream ? "<-" : "->", dpcm->be->dai_link->name); | |
1205 | ||
1206 | /* BEs still alive need new FE */ | |
1207 | dpcm_be_reparent(fe, dpcm->be, stream); | |
1208 | ||
f86dcef8 | 1209 | #ifdef CONFIG_DEBUG_FS |
fee531d6 | 1210 | debugfs_remove_recursive(dpcm->debugfs_state); |
f86dcef8 | 1211 | #endif |
a9764869 | 1212 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
01d7584c LG |
1213 | list_del(&dpcm->list_be); |
1214 | list_del(&dpcm->list_fe); | |
a9764869 | 1215 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
01d7584c LG |
1216 | kfree(dpcm); |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* get BE for DAI widget and stream */ | |
1221 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, | |
1222 | struct snd_soc_dapm_widget *widget, int stream) | |
1223 | { | |
1224 | struct snd_soc_pcm_runtime *be; | |
93597fae | 1225 | struct snd_soc_dapm_widget *w; |
7afecb30 | 1226 | struct snd_soc_dai *dai; |
1a497983 | 1227 | int i; |
01d7584c | 1228 | |
3c146465 LG |
1229 | dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); |
1230 | ||
93597fae | 1231 | for_each_card_rtds(card, be) { |
01d7584c | 1232 | |
93597fae KM |
1233 | if (!be->dai_link->no_pcm) |
1234 | continue; | |
35ea0655 | 1235 | |
0c01f6ca | 1236 | w = snd_soc_dai_get_widget(be->cpu_dai, stream); |
3c146465 | 1237 | |
93597fae KM |
1238 | dev_dbg(card->dev, "ASoC: try BE : %s\n", |
1239 | w ? w->name : "(not set)"); | |
2e5894d7 | 1240 | |
93597fae KM |
1241 | if (w == widget) |
1242 | return be; | |
35ea0655 | 1243 | |
93597fae | 1244 | for_each_rtd_codec_dai(be, i, dai) { |
0c01f6ca | 1245 | w = snd_soc_dai_get_widget(dai, stream); |
3c146465 | 1246 | |
93597fae | 1247 | if (w == widget) |
01d7584c LG |
1248 | return be; |
1249 | } | |
1250 | } | |
1251 | ||
9d6ee365 | 1252 | /* Widget provided is not a BE */ |
01d7584c LG |
1253 | return NULL; |
1254 | } | |
1255 | ||
01d7584c LG |
1256 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, |
1257 | struct snd_soc_dapm_widget *widget) | |
1258 | { | |
09e88f8a | 1259 | struct snd_soc_dapm_widget *w; |
01d7584c LG |
1260 | int i; |
1261 | ||
09e88f8a KM |
1262 | for_each_dapm_widgets(list, i, w) |
1263 | if (widget == w) | |
01d7584c | 1264 | return 1; |
01d7584c LG |
1265 | |
1266 | return 0; | |
1267 | } | |
1268 | ||
5fdd022c PS |
1269 | static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, |
1270 | enum snd_soc_dapm_direction dir) | |
1271 | { | |
1272 | struct snd_soc_card *card = widget->dapm->card; | |
1273 | struct snd_soc_pcm_runtime *rtd; | |
c2cd8216 | 1274 | int stream; |
5fdd022c | 1275 | |
c2cd8216 KM |
1276 | /* adjust dir to stream */ |
1277 | if (dir == SND_SOC_DAPM_DIR_OUT) | |
1278 | stream = SNDRV_PCM_STREAM_PLAYBACK; | |
1279 | else | |
1280 | stream = SNDRV_PCM_STREAM_CAPTURE; | |
5fdd022c | 1281 | |
027a4838 KM |
1282 | rtd = dpcm_get_be(card, widget, stream); |
1283 | if (rtd) | |
1284 | return true; | |
5fdd022c PS |
1285 | |
1286 | return false; | |
1287 | } | |
1288 | ||
23607025 | 1289 | int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
1ce43acf | 1290 | int stream, struct snd_soc_dapm_widget_list **list) |
01d7584c LG |
1291 | { |
1292 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; | |
01d7584c LG |
1293 | int paths; |
1294 | ||
01d7584c | 1295 | /* get number of valid DAI paths and their widgets */ |
6742064a | 1296 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, |
5fdd022c | 1297 | dpcm_end_walk_at_be); |
01d7584c | 1298 | |
103d84a3 | 1299 | dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, |
01d7584c LG |
1300 | stream ? "capture" : "playback"); |
1301 | ||
01d7584c LG |
1302 | return paths; |
1303 | } | |
1304 | ||
01d7584c LG |
1305 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
1306 | struct snd_soc_dapm_widget_list **list_) | |
1307 | { | |
1308 | struct snd_soc_dpcm *dpcm; | |
1309 | struct snd_soc_dapm_widget_list *list = *list_; | |
1310 | struct snd_soc_dapm_widget *widget; | |
7afecb30 | 1311 | struct snd_soc_dai *dai; |
01d7584c | 1312 | int prune = 0; |
bed646dc | 1313 | int do_prune; |
01d7584c LG |
1314 | |
1315 | /* Destroy any old FE <--> BE connections */ | |
8d6258a4 | 1316 | for_each_dpcm_be(fe, stream, dpcm) { |
2e5894d7 | 1317 | unsigned int i; |
01d7584c LG |
1318 | |
1319 | /* is there a valid CPU DAI widget for this BE */ | |
0c01f6ca | 1320 | widget = snd_soc_dai_get_widget(dpcm->be->cpu_dai, stream); |
01d7584c LG |
1321 | |
1322 | /* prune the BE if it's no longer in our active list */ | |
1323 | if (widget && widget_in_list(list, widget)) | |
1324 | continue; | |
1325 | ||
1326 | /* is there a valid CODEC DAI widget for this BE */ | |
bed646dc | 1327 | do_prune = 1; |
7afecb30 | 1328 | for_each_rtd_codec_dai(dpcm->be, i, dai) { |
0c01f6ca | 1329 | widget = snd_soc_dai_get_widget(dai, stream); |
01d7584c | 1330 | |
2e5894d7 BC |
1331 | /* prune the BE if it's no longer in our active list */ |
1332 | if (widget && widget_in_list(list, widget)) | |
bed646dc | 1333 | do_prune = 0; |
2e5894d7 | 1334 | } |
bed646dc KM |
1335 | if (!do_prune) |
1336 | continue; | |
01d7584c | 1337 | |
103d84a3 | 1338 | dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", |
01d7584c LG |
1339 | stream ? "capture" : "playback", |
1340 | dpcm->be->dai_link->name, fe->dai_link->name); | |
1341 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
1342 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1343 | prune++; | |
1344 | } | |
1345 | ||
103d84a3 | 1346 | dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); |
01d7584c LG |
1347 | return prune; |
1348 | } | |
1349 | ||
1350 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, | |
1351 | struct snd_soc_dapm_widget_list **list_) | |
1352 | { | |
1353 | struct snd_soc_card *card = fe->card; | |
1354 | struct snd_soc_dapm_widget_list *list = *list_; | |
1355 | struct snd_soc_pcm_runtime *be; | |
09e88f8a | 1356 | struct snd_soc_dapm_widget *widget; |
01d7584c LG |
1357 | int i, new = 0, err; |
1358 | ||
1359 | /* Create any new FE <--> BE connections */ | |
09e88f8a | 1360 | for_each_dapm_widgets(list, i, widget) { |
01d7584c | 1361 | |
09e88f8a | 1362 | switch (widget->id) { |
4616274d | 1363 | case snd_soc_dapm_dai_in: |
c5b8540d KC |
1364 | if (stream != SNDRV_PCM_STREAM_PLAYBACK) |
1365 | continue; | |
1366 | break; | |
4616274d | 1367 | case snd_soc_dapm_dai_out: |
c5b8540d KC |
1368 | if (stream != SNDRV_PCM_STREAM_CAPTURE) |
1369 | continue; | |
4616274d MB |
1370 | break; |
1371 | default: | |
01d7584c | 1372 | continue; |
4616274d | 1373 | } |
01d7584c LG |
1374 | |
1375 | /* is there a valid BE rtd for this widget */ | |
09e88f8a | 1376 | be = dpcm_get_be(card, widget, stream); |
01d7584c | 1377 | if (!be) { |
103d84a3 | 1378 | dev_err(fe->dev, "ASoC: no BE found for %s\n", |
09e88f8a | 1379 | widget->name); |
01d7584c LG |
1380 | continue; |
1381 | } | |
1382 | ||
1383 | /* make sure BE is a real BE */ | |
1384 | if (!be->dai_link->no_pcm) | |
1385 | continue; | |
1386 | ||
1387 | /* don't connect if FE is not running */ | |
23607025 | 1388 | if (!fe->dpcm[stream].runtime && !fe->fe_compr) |
01d7584c LG |
1389 | continue; |
1390 | ||
1391 | /* newly connected FE and BE */ | |
1392 | err = dpcm_be_connect(fe, be, stream); | |
1393 | if (err < 0) { | |
103d84a3 | 1394 | dev_err(fe->dev, "ASoC: can't connect %s\n", |
09e88f8a | 1395 | widget->name); |
01d7584c LG |
1396 | break; |
1397 | } else if (err == 0) /* already connected */ | |
1398 | continue; | |
1399 | ||
1400 | /* new */ | |
1401 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1402 | new++; | |
1403 | } | |
1404 | ||
103d84a3 | 1405 | dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); |
01d7584c LG |
1406 | return new; |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * Find the corresponding BE DAIs that source or sink audio to this | |
1411 | * FE substream. | |
1412 | */ | |
23607025 | 1413 | int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
01d7584c LG |
1414 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
1415 | { | |
1416 | if (new) | |
1417 | return dpcm_add_paths(fe, stream, list); | |
1418 | else | |
1419 | return dpcm_prune_paths(fe, stream, list); | |
1420 | } | |
1421 | ||
23607025 | 1422 | void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1423 | { |
1424 | struct snd_soc_dpcm *dpcm; | |
a9764869 | 1425 | unsigned long flags; |
01d7584c | 1426 | |
a9764869 | 1427 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
8d6258a4 | 1428 | for_each_dpcm_be(fe, stream, dpcm) |
01d7584c LG |
1429 | dpcm->be->dpcm[stream].runtime_update = |
1430 | SND_SOC_DPCM_UPDATE_NO; | |
a9764869 | 1431 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
01d7584c LG |
1432 | } |
1433 | ||
1434 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, | |
1435 | int stream) | |
1436 | { | |
1437 | struct snd_soc_dpcm *dpcm; | |
1438 | ||
1439 | /* disable any enabled and non active backends */ | |
8d6258a4 | 1440 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1441 | |
1442 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1443 | struct snd_pcm_substream *be_substream = | |
1444 | snd_soc_dpcm_get_substream(be, stream); | |
1445 | ||
1446 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1447 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
01d7584c LG |
1448 | stream ? "capture" : "playback", |
1449 | be->dpcm[stream].state); | |
1450 | ||
1451 | if (--be->dpcm[stream].users != 0) | |
1452 | continue; | |
1453 | ||
1454 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) | |
1455 | continue; | |
1456 | ||
1457 | soc_pcm_close(be_substream); | |
1458 | be_substream->runtime = NULL; | |
1459 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1460 | } | |
1461 | } | |
1462 | ||
23607025 | 1463 | int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1464 | { |
1465 | struct snd_soc_dpcm *dpcm; | |
1466 | int err, count = 0; | |
1467 | ||
1468 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ | |
8d6258a4 | 1469 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1470 | |
1471 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1472 | struct snd_pcm_substream *be_substream = | |
1473 | snd_soc_dpcm_get_substream(be, stream); | |
1474 | ||
2062b4c5 RKAL |
1475 | if (!be_substream) { |
1476 | dev_err(be->dev, "ASoC: no backend %s stream\n", | |
1477 | stream ? "capture" : "playback"); | |
1478 | continue; | |
1479 | } | |
1480 | ||
01d7584c LG |
1481 | /* is this op for this BE ? */ |
1482 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1483 | continue; | |
1484 | ||
1485 | /* first time the dpcm is open ? */ | |
1486 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) | |
103d84a3 | 1487 | dev_err(be->dev, "ASoC: too many users %s at open %d\n", |
01d7584c LG |
1488 | stream ? "capture" : "playback", |
1489 | be->dpcm[stream].state); | |
1490 | ||
1491 | if (be->dpcm[stream].users++ != 0) | |
1492 | continue; | |
1493 | ||
1494 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && | |
1495 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) | |
1496 | continue; | |
1497 | ||
2062b4c5 RKAL |
1498 | dev_dbg(be->dev, "ASoC: open %s BE %s\n", |
1499 | stream ? "capture" : "playback", be->dai_link->name); | |
01d7584c LG |
1500 | |
1501 | be_substream->runtime = be->dpcm[stream].runtime; | |
1502 | err = soc_pcm_open(be_substream); | |
1503 | if (err < 0) { | |
103d84a3 | 1504 | dev_err(be->dev, "ASoC: BE open failed %d\n", err); |
01d7584c LG |
1505 | be->dpcm[stream].users--; |
1506 | if (be->dpcm[stream].users < 0) | |
103d84a3 | 1507 | dev_err(be->dev, "ASoC: no users %s at unwind %d\n", |
01d7584c LG |
1508 | stream ? "capture" : "playback", |
1509 | be->dpcm[stream].state); | |
1510 | ||
1511 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1512 | goto unwind; | |
1513 | } | |
1514 | ||
1515 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; | |
1516 | count++; | |
1517 | } | |
1518 | ||
1519 | return count; | |
1520 | ||
1521 | unwind: | |
1522 | /* disable any enabled and non active backends */ | |
8d6258a4 | 1523 | for_each_dpcm_be_rollback(fe, stream, dpcm) { |
01d7584c LG |
1524 | struct snd_soc_pcm_runtime *be = dpcm->be; |
1525 | struct snd_pcm_substream *be_substream = | |
1526 | snd_soc_dpcm_get_substream(be, stream); | |
1527 | ||
1528 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1529 | continue; | |
1530 | ||
1531 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1532 | dev_err(be->dev, "ASoC: no users %s at close %d\n", |
01d7584c LG |
1533 | stream ? "capture" : "playback", |
1534 | be->dpcm[stream].state); | |
1535 | ||
1536 | if (--be->dpcm[stream].users != 0) | |
1537 | continue; | |
1538 | ||
1539 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) | |
1540 | continue; | |
1541 | ||
1542 | soc_pcm_close(be_substream); | |
1543 | be_substream->runtime = NULL; | |
1544 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1545 | } | |
1546 | ||
1547 | return err; | |
1548 | } | |
1549 | ||
08ae9b45 | 1550 | static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
435ffb76 | 1551 | struct snd_soc_pcm_stream *stream) |
08ae9b45 LPC |
1552 | { |
1553 | runtime->hw.rate_min = stream->rate_min; | |
e33ffbd9 | 1554 | runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); |
08ae9b45 LPC |
1555 | runtime->hw.channels_min = stream->channels_min; |
1556 | runtime->hw.channels_max = stream->channels_max; | |
002220a9 | 1557 | if (runtime->hw.formats) |
435ffb76 | 1558 | runtime->hw.formats &= stream->formats; |
002220a9 | 1559 | else |
435ffb76 | 1560 | runtime->hw.formats = stream->formats; |
08ae9b45 LPC |
1561 | runtime->hw.rates = stream->rates; |
1562 | } | |
1563 | ||
435ffb76 JB |
1564 | static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, |
1565 | u64 *formats) | |
b073ed4e KM |
1566 | { |
1567 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1568 | struct snd_soc_dpcm *dpcm; | |
7afecb30 | 1569 | struct snd_soc_dai *dai; |
b073ed4e KM |
1570 | int stream = substream->stream; |
1571 | ||
1572 | if (!fe->dai_link->dpcm_merged_format) | |
435ffb76 | 1573 | return; |
b073ed4e KM |
1574 | |
1575 | /* | |
1576 | * It returns merged BE codec format | |
1577 | * if FE want to use it (= dpcm_merged_format) | |
1578 | */ | |
1579 | ||
8d6258a4 | 1580 | for_each_dpcm_be(fe, stream, dpcm) { |
b073ed4e | 1581 | struct snd_soc_pcm_runtime *be = dpcm->be; |
b073ed4e KM |
1582 | struct snd_soc_pcm_stream *codec_stream; |
1583 | int i; | |
1584 | ||
7afecb30 | 1585 | for_each_rtd_codec_dai(be, i, dai) { |
4febced1 JB |
1586 | /* |
1587 | * Skip CODECs which don't support the current stream | |
1588 | * type. See soc_pcm_init_runtime_hw() for more details | |
1589 | */ | |
7afecb30 | 1590 | if (!snd_soc_dai_stream_valid(dai, stream)) |
4febced1 JB |
1591 | continue; |
1592 | ||
acf253c1 | 1593 | codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); |
b073ed4e | 1594 | |
435ffb76 | 1595 | *formats &= codec_stream->formats; |
b073ed4e KM |
1596 | } |
1597 | } | |
b073ed4e KM |
1598 | } |
1599 | ||
435ffb76 JB |
1600 | static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, |
1601 | unsigned int *channels_min, | |
1602 | unsigned int *channels_max) | |
f4c277b8 JW |
1603 | { |
1604 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1605 | struct snd_soc_dpcm *dpcm; | |
1606 | int stream = substream->stream; | |
1607 | ||
1608 | if (!fe->dai_link->dpcm_merged_chan) | |
1609 | return; | |
1610 | ||
f4c277b8 JW |
1611 | /* |
1612 | * It returns merged BE codec channel; | |
1613 | * if FE want to use it (= dpcm_merged_chan) | |
1614 | */ | |
1615 | ||
8d6258a4 | 1616 | for_each_dpcm_be(fe, stream, dpcm) { |
f4c277b8 | 1617 | struct snd_soc_pcm_runtime *be = dpcm->be; |
f4c277b8 | 1618 | struct snd_soc_pcm_stream *codec_stream; |
4f2bd18b JB |
1619 | struct snd_soc_pcm_stream *cpu_stream; |
1620 | ||
acf253c1 | 1621 | cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); |
4f2bd18b JB |
1622 | |
1623 | *channels_min = max(*channels_min, cpu_stream->channels_min); | |
1624 | *channels_max = min(*channels_max, cpu_stream->channels_max); | |
1625 | ||
1626 | /* | |
1627 | * chan min/max cannot be enforced if there are multiple CODEC | |
1628 | * DAIs connected to a single CPU DAI, use CPU DAI's directly | |
1629 | */ | |
1630 | if (be->num_codecs == 1) { | |
acf253c1 | 1631 | codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream); |
f4c277b8 JW |
1632 | |
1633 | *channels_min = max(*channels_min, | |
1634 | codec_stream->channels_min); | |
1635 | *channels_max = min(*channels_max, | |
1636 | codec_stream->channels_max); | |
1637 | } | |
1638 | } | |
1639 | } | |
1640 | ||
baacd8d1 JB |
1641 | static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, |
1642 | unsigned int *rates, | |
1643 | unsigned int *rate_min, | |
1644 | unsigned int *rate_max) | |
1645 | { | |
1646 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1647 | struct snd_soc_dpcm *dpcm; | |
1648 | int stream = substream->stream; | |
1649 | ||
1650 | if (!fe->dai_link->dpcm_merged_rate) | |
1651 | return; | |
1652 | ||
1653 | /* | |
1654 | * It returns merged BE codec channel; | |
1655 | * if FE want to use it (= dpcm_merged_chan) | |
1656 | */ | |
1657 | ||
8d6258a4 | 1658 | for_each_dpcm_be(fe, stream, dpcm) { |
baacd8d1 | 1659 | struct snd_soc_pcm_runtime *be = dpcm->be; |
baacd8d1 JB |
1660 | struct snd_soc_pcm_stream *codec_stream; |
1661 | struct snd_soc_pcm_stream *cpu_stream; | |
7afecb30 | 1662 | struct snd_soc_dai *dai; |
baacd8d1 JB |
1663 | int i; |
1664 | ||
acf253c1 | 1665 | cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); |
baacd8d1 JB |
1666 | |
1667 | *rate_min = max(*rate_min, cpu_stream->rate_min); | |
1668 | *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max); | |
1669 | *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates); | |
1670 | ||
7afecb30 | 1671 | for_each_rtd_codec_dai(be, i, dai) { |
baacd8d1 JB |
1672 | /* |
1673 | * Skip CODECs which don't support the current stream | |
1674 | * type. See soc_pcm_init_runtime_hw() for more details | |
1675 | */ | |
7afecb30 | 1676 | if (!snd_soc_dai_stream_valid(dai, stream)) |
baacd8d1 JB |
1677 | continue; |
1678 | ||
acf253c1 | 1679 | codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); |
baacd8d1 JB |
1680 | |
1681 | *rate_min = max(*rate_min, codec_stream->rate_min); | |
1682 | *rate_max = min_not_zero(*rate_max, | |
1683 | codec_stream->rate_max); | |
1684 | *rates = snd_pcm_rate_mask_intersect(*rates, | |
1685 | codec_stream->rates); | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 | ||
45c0a188 | 1690 | static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
01d7584c LG |
1691 | { |
1692 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1693 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
1694 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
1695 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; | |
1696 | ||
08ae9b45 | 1697 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
435ffb76 | 1698 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); |
08ae9b45 | 1699 | else |
435ffb76 | 1700 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); |
f4c277b8 | 1701 | |
435ffb76 JB |
1702 | dpcm_runtime_merge_format(substream, &runtime->hw.formats); |
1703 | dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, | |
1704 | &runtime->hw.channels_max); | |
baacd8d1 JB |
1705 | dpcm_runtime_merge_rate(substream, &runtime->hw.rates, |
1706 | &runtime->hw.rate_min, &runtime->hw.rate_max); | |
01d7584c LG |
1707 | } |
1708 | ||
ea9d0d77 TI |
1709 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); |
1710 | ||
1711 | /* Set FE's runtime_update state; the state is protected via PCM stream lock | |
1712 | * for avoiding the race with trigger callback. | |
1713 | * If the state is unset and a trigger is pending while the previous operation, | |
1714 | * process the pending trigger action here. | |
1715 | */ | |
1716 | static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, | |
1717 | int stream, enum snd_soc_dpcm_update state) | |
1718 | { | |
1719 | struct snd_pcm_substream *substream = | |
1720 | snd_soc_dpcm_get_substream(fe, stream); | |
1721 | ||
1722 | snd_pcm_stream_lock_irq(substream); | |
1723 | if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { | |
1724 | dpcm_fe_dai_do_trigger(substream, | |
1725 | fe->dpcm[stream].trigger_pending - 1); | |
1726 | fe->dpcm[stream].trigger_pending = 0; | |
1727 | } | |
1728 | fe->dpcm[stream].runtime_update = state; | |
1729 | snd_pcm_stream_unlock_irq(substream); | |
1730 | } | |
1731 | ||
906c7d69 PL |
1732 | static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, |
1733 | int stream) | |
1734 | { | |
1735 | struct snd_soc_dpcm *dpcm; | |
1736 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
1737 | struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai; | |
1738 | int err; | |
1739 | ||
1740 | /* apply symmetry for FE */ | |
1741 | if (soc_pcm_has_symmetry(fe_substream)) | |
1742 | fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; | |
1743 | ||
1744 | /* Symmetry only applies if we've got an active stream. */ | |
1745 | if (fe_cpu_dai->active) { | |
1746 | err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); | |
1747 | if (err < 0) | |
1748 | return err; | |
1749 | } | |
1750 | ||
1751 | /* apply symmetry for BE */ | |
8d6258a4 | 1752 | for_each_dpcm_be(fe, stream, dpcm) { |
906c7d69 PL |
1753 | struct snd_soc_pcm_runtime *be = dpcm->be; |
1754 | struct snd_pcm_substream *be_substream = | |
1755 | snd_soc_dpcm_get_substream(be, stream); | |
6246f283 | 1756 | struct snd_soc_pcm_runtime *rtd; |
0b7990e3 | 1757 | struct snd_soc_dai *codec_dai; |
906c7d69 PL |
1758 | int i; |
1759 | ||
6246f283 JB |
1760 | /* A backend may not have the requested substream */ |
1761 | if (!be_substream) | |
1762 | continue; | |
1763 | ||
1764 | rtd = be_substream->private_data; | |
f1176614 JK |
1765 | if (rtd->dai_link->be_hw_params_fixup) |
1766 | continue; | |
1767 | ||
906c7d69 PL |
1768 | if (soc_pcm_has_symmetry(be_substream)) |
1769 | be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; | |
1770 | ||
1771 | /* Symmetry only applies if we've got an active stream. */ | |
1772 | if (rtd->cpu_dai->active) { | |
99bcedbd KCC |
1773 | err = soc_pcm_apply_symmetry(fe_substream, |
1774 | rtd->cpu_dai); | |
906c7d69 PL |
1775 | if (err < 0) |
1776 | return err; | |
1777 | } | |
1778 | ||
0b7990e3 KM |
1779 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
1780 | if (codec_dai->active) { | |
99bcedbd | 1781 | err = soc_pcm_apply_symmetry(fe_substream, |
0b7990e3 | 1782 | codec_dai); |
906c7d69 PL |
1783 | if (err < 0) |
1784 | return err; | |
1785 | } | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | return 0; | |
1790 | } | |
1791 | ||
01d7584c LG |
1792 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) |
1793 | { | |
1794 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
1795 | struct snd_pcm_runtime *runtime = fe_substream->runtime; | |
1796 | int stream = fe_substream->stream, ret = 0; | |
1797 | ||
ea9d0d77 | 1798 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
01d7584c LG |
1799 | |
1800 | ret = dpcm_be_dai_startup(fe, fe_substream->stream); | |
1801 | if (ret < 0) { | |
103d84a3 | 1802 | dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); |
01d7584c LG |
1803 | goto be_err; |
1804 | } | |
1805 | ||
103d84a3 | 1806 | dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); |
01d7584c LG |
1807 | |
1808 | /* start the DAI frontend */ | |
1809 | ret = soc_pcm_open(fe_substream); | |
1810 | if (ret < 0) { | |
103d84a3 | 1811 | dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); |
01d7584c LG |
1812 | goto unwind; |
1813 | } | |
1814 | ||
1815 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; | |
1816 | ||
1817 | dpcm_set_fe_runtime(fe_substream); | |
1818 | snd_pcm_limit_hw_rates(runtime); | |
1819 | ||
906c7d69 PL |
1820 | ret = dpcm_apply_symmetry(fe_substream, stream); |
1821 | if (ret < 0) { | |
1822 | dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", | |
1823 | ret); | |
1824 | goto unwind; | |
1825 | } | |
1826 | ||
ea9d0d77 | 1827 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
1828 | return 0; |
1829 | ||
1830 | unwind: | |
1831 | dpcm_be_dai_startup_unwind(fe, fe_substream->stream); | |
1832 | be_err: | |
ea9d0d77 | 1833 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
1834 | return ret; |
1835 | } | |
1836 | ||
23607025 | 1837 | int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1838 | { |
1839 | struct snd_soc_dpcm *dpcm; | |
1840 | ||
1841 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ | |
8d6258a4 | 1842 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1843 | |
1844 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1845 | struct snd_pcm_substream *be_substream = | |
1846 | snd_soc_dpcm_get_substream(be, stream); | |
1847 | ||
1848 | /* is this op for this BE ? */ | |
1849 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1850 | continue; | |
1851 | ||
1852 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1853 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
01d7584c LG |
1854 | stream ? "capture" : "playback", |
1855 | be->dpcm[stream].state); | |
1856 | ||
1857 | if (--be->dpcm[stream].users != 0) | |
1858 | continue; | |
1859 | ||
1860 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
9c0ac70a KCC |
1861 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { |
1862 | soc_pcm_hw_free(be_substream); | |
1863 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; | |
1864 | } | |
01d7584c | 1865 | |
103d84a3 | 1866 | dev_dbg(be->dev, "ASoC: close BE %s\n", |
94d215cc | 1867 | be->dai_link->name); |
01d7584c LG |
1868 | |
1869 | soc_pcm_close(be_substream); | |
1870 | be_substream->runtime = NULL; | |
1871 | ||
1872 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1873 | } | |
1874 | return 0; | |
1875 | } | |
1876 | ||
1877 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) | |
1878 | { | |
1879 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1880 | int stream = substream->stream; | |
1881 | ||
ea9d0d77 | 1882 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
01d7584c LG |
1883 | |
1884 | /* shutdown the BEs */ | |
1885 | dpcm_be_dai_shutdown(fe, substream->stream); | |
1886 | ||
103d84a3 | 1887 | dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); |
01d7584c LG |
1888 | |
1889 | /* now shutdown the frontend */ | |
1890 | soc_pcm_close(substream); | |
1891 | ||
1892 | /* run the stream event for each BE */ | |
b0edff42 | 1893 | snd_soc_dapm_stream_stop(fe, stream); |
01d7584c LG |
1894 | |
1895 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
ea9d0d77 | 1896 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
1897 | return 0; |
1898 | } | |
1899 | ||
23607025 | 1900 | int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1901 | { |
1902 | struct snd_soc_dpcm *dpcm; | |
1903 | ||
1904 | /* only hw_params backends that are either sinks or sources | |
1905 | * to this frontend DAI */ | |
8d6258a4 | 1906 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1907 | |
1908 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1909 | struct snd_pcm_substream *be_substream = | |
1910 | snd_soc_dpcm_get_substream(be, stream); | |
1911 | ||
1912 | /* is this op for this BE ? */ | |
1913 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1914 | continue; | |
1915 | ||
1916 | /* only free hw when no longer used - check all FEs */ | |
1917 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1918 | continue; | |
1919 | ||
36fba62c QZ |
1920 | /* do not free hw if this BE is used by other FE */ |
1921 | if (be->dpcm[stream].users > 1) | |
1922 | continue; | |
1923 | ||
01d7584c LG |
1924 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
1925 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && | |
1926 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
08b27848 | 1927 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && |
5e82d2be VK |
1928 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
1929 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) | |
01d7584c LG |
1930 | continue; |
1931 | ||
103d84a3 | 1932 | dev_dbg(be->dev, "ASoC: hw_free BE %s\n", |
94d215cc | 1933 | be->dai_link->name); |
01d7584c LG |
1934 | |
1935 | soc_pcm_hw_free(be_substream); | |
1936 | ||
1937 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; | |
1938 | } | |
1939 | ||
1940 | return 0; | |
1941 | } | |
1942 | ||
45c0a188 | 1943 | static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
01d7584c LG |
1944 | { |
1945 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1946 | int err, stream = substream->stream; | |
1947 | ||
1948 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
ea9d0d77 | 1949 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
01d7584c | 1950 | |
103d84a3 | 1951 | dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); |
01d7584c LG |
1952 | |
1953 | /* call hw_free on the frontend */ | |
1954 | err = soc_pcm_hw_free(substream); | |
1955 | if (err < 0) | |
103d84a3 | 1956 | dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", |
01d7584c LG |
1957 | fe->dai_link->name); |
1958 | ||
1959 | /* only hw_params backends that are either sinks or sources | |
1960 | * to this frontend DAI */ | |
1961 | err = dpcm_be_dai_hw_free(fe, stream); | |
1962 | ||
1963 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; | |
ea9d0d77 | 1964 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
1965 | |
1966 | mutex_unlock(&fe->card->mutex); | |
1967 | return 0; | |
1968 | } | |
1969 | ||
23607025 | 1970 | int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1971 | { |
1972 | struct snd_soc_dpcm *dpcm; | |
1973 | int ret; | |
1974 | ||
8d6258a4 | 1975 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
1976 | |
1977 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1978 | struct snd_pcm_substream *be_substream = | |
1979 | snd_soc_dpcm_get_substream(be, stream); | |
1980 | ||
1981 | /* is this op for this BE ? */ | |
1982 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1983 | continue; | |
1984 | ||
01d7584c LG |
1985 | /* copy params for each dpcm */ |
1986 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, | |
1987 | sizeof(struct snd_pcm_hw_params)); | |
1988 | ||
1989 | /* perform any hw_params fixups */ | |
1990 | if (be->dai_link->be_hw_params_fixup) { | |
1991 | ret = be->dai_link->be_hw_params_fixup(be, | |
1992 | &dpcm->hw_params); | |
1993 | if (ret < 0) { | |
1994 | dev_err(be->dev, | |
103d84a3 | 1995 | "ASoC: hw_params BE fixup failed %d\n", |
01d7584c LG |
1996 | ret); |
1997 | goto unwind; | |
1998 | } | |
1999 | } | |
2000 | ||
ae061d2a LY |
2001 | /* copy the fixed-up hw params for BE dai */ |
2002 | memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, | |
2003 | sizeof(struct snd_pcm_hw_params)); | |
2004 | ||
b0639bd2 KM |
2005 | /* only allow hw_params() if no connected FEs are running */ |
2006 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) | |
2007 | continue; | |
2008 | ||
2009 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && | |
2010 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
2011 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) | |
2012 | continue; | |
2013 | ||
2014 | dev_dbg(be->dev, "ASoC: hw_params BE %s\n", | |
94d215cc | 2015 | be->dai_link->name); |
b0639bd2 | 2016 | |
01d7584c LG |
2017 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); |
2018 | if (ret < 0) { | |
2019 | dev_err(dpcm->be->dev, | |
103d84a3 | 2020 | "ASoC: hw_params BE failed %d\n", ret); |
01d7584c LG |
2021 | goto unwind; |
2022 | } | |
2023 | ||
2024 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; | |
2025 | } | |
2026 | return 0; | |
2027 | ||
2028 | unwind: | |
2029 | /* disable any enabled and non active backends */ | |
8d6258a4 | 2030 | for_each_dpcm_be_rollback(fe, stream, dpcm) { |
01d7584c LG |
2031 | struct snd_soc_pcm_runtime *be = dpcm->be; |
2032 | struct snd_pcm_substream *be_substream = | |
2033 | snd_soc_dpcm_get_substream(be, stream); | |
2034 | ||
2035 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
2036 | continue; | |
2037 | ||
2038 | /* only allow hw_free() if no connected FEs are running */ | |
2039 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
2040 | continue; | |
2041 | ||
2042 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && | |
2043 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
2044 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
2045 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) | |
2046 | continue; | |
2047 | ||
2048 | soc_pcm_hw_free(be_substream); | |
2049 | } | |
2050 | ||
2051 | return ret; | |
2052 | } | |
2053 | ||
45c0a188 MB |
2054 | static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
2055 | struct snd_pcm_hw_params *params) | |
01d7584c LG |
2056 | { |
2057 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
2058 | int ret, stream = substream->stream; | |
2059 | ||
2060 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
ea9d0d77 | 2061 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
01d7584c LG |
2062 | |
2063 | memcpy(&fe->dpcm[substream->stream].hw_params, params, | |
2064 | sizeof(struct snd_pcm_hw_params)); | |
2065 | ret = dpcm_be_dai_hw_params(fe, substream->stream); | |
2066 | if (ret < 0) { | |
103d84a3 | 2067 | dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); |
01d7584c LG |
2068 | goto out; |
2069 | } | |
2070 | ||
103d84a3 | 2071 | dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", |
01d7584c LG |
2072 | fe->dai_link->name, params_rate(params), |
2073 | params_channels(params), params_format(params)); | |
2074 | ||
2075 | /* call hw_params on the frontend */ | |
2076 | ret = soc_pcm_hw_params(substream, params); | |
2077 | if (ret < 0) { | |
103d84a3 | 2078 | dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); |
01d7584c LG |
2079 | dpcm_be_dai_hw_free(fe, stream); |
2080 | } else | |
2081 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; | |
2082 | ||
2083 | out: | |
ea9d0d77 | 2084 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
2085 | mutex_unlock(&fe->card->mutex); |
2086 | return ret; | |
2087 | } | |
2088 | ||
2089 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, | |
2090 | struct snd_pcm_substream *substream, int cmd) | |
2091 | { | |
2092 | int ret; | |
2093 | ||
103d84a3 | 2094 | dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", |
94d215cc | 2095 | dpcm->be->dai_link->name, cmd); |
01d7584c LG |
2096 | |
2097 | ret = soc_pcm_trigger(substream, cmd); | |
2098 | if (ret < 0) | |
103d84a3 | 2099 | dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); |
01d7584c LG |
2100 | |
2101 | return ret; | |
2102 | } | |
2103 | ||
23607025 | 2104 | int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, |
45c0a188 | 2105 | int cmd) |
01d7584c LG |
2106 | { |
2107 | struct snd_soc_dpcm *dpcm; | |
2108 | int ret = 0; | |
2109 | ||
8d6258a4 | 2110 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
2111 | |
2112 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
2113 | struct snd_pcm_substream *be_substream = | |
2114 | snd_soc_dpcm_get_substream(be, stream); | |
2115 | ||
2116 | /* is this op for this BE ? */ | |
2117 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
2118 | continue; | |
2119 | ||
2120 | switch (cmd) { | |
2121 | case SNDRV_PCM_TRIGGER_START: | |
2122 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && | |
2123 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) | |
2124 | continue; | |
2125 | ||
2126 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2127 | if (ret) | |
2128 | return ret; | |
2129 | ||
2130 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
2131 | break; | |
2132 | case SNDRV_PCM_TRIGGER_RESUME: | |
2133 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) | |
2134 | continue; | |
2135 | ||
2136 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2137 | if (ret) | |
2138 | return ret; | |
2139 | ||
2140 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
2141 | break; | |
2142 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
2143 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) | |
2144 | continue; | |
2145 | ||
2146 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2147 | if (ret) | |
2148 | return ret; | |
2149 | ||
2150 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
2151 | break; | |
2152 | case SNDRV_PCM_TRIGGER_STOP: | |
2153 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
2154 | continue; | |
2155 | ||
2156 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
2157 | continue; | |
2158 | ||
2159 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2160 | if (ret) | |
2161 | return ret; | |
2162 | ||
2163 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; | |
2164 | break; | |
2165 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
868a6ca8 | 2166 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
01d7584c LG |
2167 | continue; |
2168 | ||
2169 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
2170 | continue; | |
2171 | ||
2172 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2173 | if (ret) | |
2174 | return ret; | |
2175 | ||
2176 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; | |
2177 | break; | |
2178 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
2179 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
2180 | continue; | |
2181 | ||
2182 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
2183 | continue; | |
2184 | ||
2185 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
2186 | if (ret) | |
2187 | return ret; | |
2188 | ||
2189 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; | |
2190 | break; | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | return ret; | |
2195 | } | |
2196 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); | |
2197 | ||
acbf2774 RS |
2198 | static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, |
2199 | int cmd, bool fe_first) | |
2200 | { | |
2201 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
2202 | int ret; | |
2203 | ||
2204 | /* call trigger on the frontend before the backend. */ | |
2205 | if (fe_first) { | |
2206 | dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", | |
2207 | fe->dai_link->name, cmd); | |
2208 | ||
2209 | ret = soc_pcm_trigger(substream, cmd); | |
2210 | if (ret < 0) | |
2211 | return ret; | |
2212 | ||
2213 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); | |
2214 | return ret; | |
2215 | } | |
2216 | ||
2217 | /* call trigger on the frontend after the backend. */ | |
2218 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); | |
2219 | if (ret < 0) | |
2220 | return ret; | |
2221 | ||
2222 | dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", | |
2223 | fe->dai_link->name, cmd); | |
2224 | ||
2225 | ret = soc_pcm_trigger(substream, cmd); | |
2226 | ||
2227 | return ret; | |
2228 | } | |
2229 | ||
ea9d0d77 | 2230 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) |
01d7584c LG |
2231 | { |
2232 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
acbf2774 RS |
2233 | int stream = substream->stream; |
2234 | int ret = 0; | |
01d7584c LG |
2235 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
2236 | ||
2237 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
2238 | ||
2239 | switch (trigger) { | |
2240 | case SND_SOC_DPCM_TRIGGER_PRE: | |
acbf2774 RS |
2241 | switch (cmd) { |
2242 | case SNDRV_PCM_TRIGGER_START: | |
2243 | case SNDRV_PCM_TRIGGER_RESUME: | |
2244 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
2245 | ret = dpcm_dai_trigger_fe_be(substream, cmd, true); | |
2246 | break; | |
2247 | case SNDRV_PCM_TRIGGER_STOP: | |
2248 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
2249 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
2250 | ret = dpcm_dai_trigger_fe_be(substream, cmd, false); | |
2251 | break; | |
2252 | default: | |
2253 | ret = -EINVAL; | |
2254 | break; | |
01d7584c | 2255 | } |
01d7584c LG |
2256 | break; |
2257 | case SND_SOC_DPCM_TRIGGER_POST: | |
acbf2774 RS |
2258 | switch (cmd) { |
2259 | case SNDRV_PCM_TRIGGER_START: | |
2260 | case SNDRV_PCM_TRIGGER_RESUME: | |
2261 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
2262 | ret = dpcm_dai_trigger_fe_be(substream, cmd, false); | |
2263 | break; | |
2264 | case SNDRV_PCM_TRIGGER_STOP: | |
2265 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
2266 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
2267 | ret = dpcm_dai_trigger_fe_be(substream, cmd, true); | |
2268 | break; | |
2269 | default: | |
2270 | ret = -EINVAL; | |
2271 | break; | |
01d7584c | 2272 | } |
01d7584c | 2273 | break; |
07bf84aa LG |
2274 | case SND_SOC_DPCM_TRIGGER_BESPOKE: |
2275 | /* bespoke trigger() - handles both FE and BEs */ | |
2276 | ||
103d84a3 | 2277 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", |
07bf84aa LG |
2278 | fe->dai_link->name, cmd); |
2279 | ||
2280 | ret = soc_pcm_bespoke_trigger(substream, cmd); | |
07bf84aa | 2281 | break; |
01d7584c | 2282 | default: |
103d84a3 | 2283 | dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, |
01d7584c LG |
2284 | fe->dai_link->name); |
2285 | ret = -EINVAL; | |
2286 | goto out; | |
2287 | } | |
2288 | ||
acbf2774 RS |
2289 | if (ret < 0) { |
2290 | dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", | |
2291 | cmd, ret); | |
2292 | goto out; | |
2293 | } | |
2294 | ||
01d7584c LG |
2295 | switch (cmd) { |
2296 | case SNDRV_PCM_TRIGGER_START: | |
2297 | case SNDRV_PCM_TRIGGER_RESUME: | |
2298 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
2299 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
2300 | break; | |
2301 | case SNDRV_PCM_TRIGGER_STOP: | |
2302 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
01d7584c LG |
2303 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
2304 | break; | |
9f169b9f PL |
2305 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
2306 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; | |
2307 | break; | |
01d7584c LG |
2308 | } |
2309 | ||
2310 | out: | |
2311 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
2312 | return ret; | |
2313 | } | |
2314 | ||
ea9d0d77 TI |
2315 | static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
2316 | { | |
2317 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
2318 | int stream = substream->stream; | |
2319 | ||
2320 | /* if FE's runtime_update is already set, we're in race; | |
2321 | * process this trigger later at exit | |
2322 | */ | |
2323 | if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { | |
2324 | fe->dpcm[stream].trigger_pending = cmd + 1; | |
2325 | return 0; /* delayed, assuming it's successful */ | |
2326 | } | |
2327 | ||
2328 | /* we're alone, let's trigger */ | |
2329 | return dpcm_fe_dai_do_trigger(substream, cmd); | |
2330 | } | |
2331 | ||
23607025 | 2332 | int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
2333 | { |
2334 | struct snd_soc_dpcm *dpcm; | |
2335 | int ret = 0; | |
2336 | ||
8d6258a4 | 2337 | for_each_dpcm_be(fe, stream, dpcm) { |
01d7584c LG |
2338 | |
2339 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
2340 | struct snd_pcm_substream *be_substream = | |
2341 | snd_soc_dpcm_get_substream(be, stream); | |
2342 | ||
2343 | /* is this op for this BE ? */ | |
2344 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
2345 | continue; | |
2346 | ||
2347 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
95f444dc | 2348 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
5087a8f1 LY |
2349 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && |
2350 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) | |
01d7584c LG |
2351 | continue; |
2352 | ||
103d84a3 | 2353 | dev_dbg(be->dev, "ASoC: prepare BE %s\n", |
94d215cc | 2354 | be->dai_link->name); |
01d7584c LG |
2355 | |
2356 | ret = soc_pcm_prepare(be_substream); | |
2357 | if (ret < 0) { | |
103d84a3 | 2358 | dev_err(be->dev, "ASoC: backend prepare failed %d\n", |
01d7584c LG |
2359 | ret); |
2360 | break; | |
2361 | } | |
2362 | ||
2363 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; | |
2364 | } | |
2365 | return ret; | |
2366 | } | |
2367 | ||
45c0a188 | 2368 | static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
01d7584c LG |
2369 | { |
2370 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
2371 | int stream = substream->stream, ret = 0; | |
2372 | ||
2373 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2374 | ||
103d84a3 | 2375 | dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); |
01d7584c | 2376 | |
ea9d0d77 | 2377 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
01d7584c LG |
2378 | |
2379 | /* there is no point preparing this FE if there are no BEs */ | |
2380 | if (list_empty(&fe->dpcm[stream].be_clients)) { | |
103d84a3 | 2381 | dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", |
01d7584c LG |
2382 | fe->dai_link->name); |
2383 | ret = -EINVAL; | |
2384 | goto out; | |
2385 | } | |
2386 | ||
2387 | ret = dpcm_be_dai_prepare(fe, substream->stream); | |
2388 | if (ret < 0) | |
2389 | goto out; | |
2390 | ||
2391 | /* call prepare on the frontend */ | |
2392 | ret = soc_pcm_prepare(substream); | |
2393 | if (ret < 0) { | |
103d84a3 | 2394 | dev_err(fe->dev,"ASoC: prepare FE %s failed\n", |
01d7584c LG |
2395 | fe->dai_link->name); |
2396 | goto out; | |
2397 | } | |
2398 | ||
2399 | /* run the stream event for each BE */ | |
2400 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); | |
2401 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; | |
2402 | ||
2403 | out: | |
ea9d0d77 | 2404 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
01d7584c LG |
2405 | mutex_unlock(&fe->card->mutex); |
2406 | ||
2407 | return ret; | |
2408 | } | |
2409 | ||
618dae11 LG |
2410 | static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
2411 | { | |
07bf84aa LG |
2412 | struct snd_pcm_substream *substream = |
2413 | snd_soc_dpcm_get_substream(fe, stream); | |
2414 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; | |
618dae11 LG |
2415 | int err; |
2416 | ||
103d84a3 | 2417 | dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", |
618dae11 LG |
2418 | stream ? "capture" : "playback", fe->dai_link->name); |
2419 | ||
07bf84aa LG |
2420 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
2421 | /* call bespoke trigger - FE takes care of all BE triggers */ | |
103d84a3 | 2422 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", |
07bf84aa LG |
2423 | fe->dai_link->name); |
2424 | ||
2425 | err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); | |
2426 | if (err < 0) | |
103d84a3 | 2427 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
07bf84aa | 2428 | } else { |
103d84a3 | 2429 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", |
07bf84aa LG |
2430 | fe->dai_link->name); |
2431 | ||
2432 | err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); | |
2433 | if (err < 0) | |
103d84a3 | 2434 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
07bf84aa | 2435 | } |
618dae11 LG |
2436 | |
2437 | err = dpcm_be_dai_hw_free(fe, stream); | |
2438 | if (err < 0) | |
103d84a3 | 2439 | dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); |
618dae11 LG |
2440 | |
2441 | err = dpcm_be_dai_shutdown(fe, stream); | |
2442 | if (err < 0) | |
103d84a3 | 2443 | dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); |
618dae11 LG |
2444 | |
2445 | /* run the stream event for each BE */ | |
2446 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); | |
2447 | ||
2448 | return 0; | |
2449 | } | |
2450 | ||
2451 | static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) | |
2452 | { | |
07bf84aa LG |
2453 | struct snd_pcm_substream *substream = |
2454 | snd_soc_dpcm_get_substream(fe, stream); | |
618dae11 | 2455 | struct snd_soc_dpcm *dpcm; |
07bf84aa | 2456 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
618dae11 | 2457 | int ret; |
a9764869 | 2458 | unsigned long flags; |
618dae11 | 2459 | |
103d84a3 | 2460 | dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", |
618dae11 LG |
2461 | stream ? "capture" : "playback", fe->dai_link->name); |
2462 | ||
2463 | /* Only start the BE if the FE is ready */ | |
2464 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || | |
2465 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) | |
2466 | return -EINVAL; | |
2467 | ||
2468 | /* startup must always be called for new BEs */ | |
2469 | ret = dpcm_be_dai_startup(fe, stream); | |
fffc0ca2 | 2470 | if (ret < 0) |
618dae11 | 2471 | goto disconnect; |
618dae11 LG |
2472 | |
2473 | /* keep going if FE state is > open */ | |
2474 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) | |
2475 | return 0; | |
01d7584c | 2476 | |
618dae11 | 2477 | ret = dpcm_be_dai_hw_params(fe, stream); |
fffc0ca2 | 2478 | if (ret < 0) |
618dae11 | 2479 | goto close; |
618dae11 LG |
2480 | |
2481 | /* keep going if FE state is > hw_params */ | |
2482 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) | |
2483 | return 0; | |
2484 | ||
2485 | ||
2486 | ret = dpcm_be_dai_prepare(fe, stream); | |
fffc0ca2 | 2487 | if (ret < 0) |
618dae11 | 2488 | goto hw_free; |
618dae11 LG |
2489 | |
2490 | /* run the stream event for each BE */ | |
2491 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); | |
2492 | ||
2493 | /* keep going if FE state is > prepare */ | |
2494 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || | |
2495 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) | |
2496 | return 0; | |
2497 | ||
07bf84aa LG |
2498 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
2499 | /* call trigger on the frontend - FE takes care of all BE triggers */ | |
103d84a3 | 2500 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", |
07bf84aa | 2501 | fe->dai_link->name); |
618dae11 | 2502 | |
07bf84aa LG |
2503 | ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); |
2504 | if (ret < 0) { | |
103d84a3 | 2505 | dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); |
07bf84aa LG |
2506 | goto hw_free; |
2507 | } | |
2508 | } else { | |
103d84a3 | 2509 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", |
07bf84aa LG |
2510 | fe->dai_link->name); |
2511 | ||
2512 | ret = dpcm_be_dai_trigger(fe, stream, | |
2513 | SNDRV_PCM_TRIGGER_START); | |
2514 | if (ret < 0) { | |
103d84a3 | 2515 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
07bf84aa LG |
2516 | goto hw_free; |
2517 | } | |
618dae11 LG |
2518 | } |
2519 | ||
2520 | return 0; | |
2521 | ||
2522 | hw_free: | |
2523 | dpcm_be_dai_hw_free(fe, stream); | |
2524 | close: | |
2525 | dpcm_be_dai_shutdown(fe, stream); | |
2526 | disconnect: | |
2527 | /* disconnect any non started BEs */ | |
a9764869 | 2528 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
8d6258a4 | 2529 | for_each_dpcm_be(fe, stream, dpcm) { |
618dae11 LG |
2530 | struct snd_soc_pcm_runtime *be = dpcm->be; |
2531 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
2532 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
2533 | } | |
a9764869 | 2534 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
618dae11 LG |
2535 | |
2536 | return ret; | |
2537 | } | |
2538 | ||
2539 | static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) | |
2540 | { | |
2541 | int ret; | |
2542 | ||
ea9d0d77 | 2543 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
618dae11 LG |
2544 | ret = dpcm_run_update_startup(fe, stream); |
2545 | if (ret < 0) | |
103d84a3 | 2546 | dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); |
ea9d0d77 | 2547 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
618dae11 LG |
2548 | |
2549 | return ret; | |
2550 | } | |
2551 | ||
2552 | static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) | |
2553 | { | |
2554 | int ret; | |
2555 | ||
ea9d0d77 | 2556 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
618dae11 LG |
2557 | ret = dpcm_run_update_shutdown(fe, stream); |
2558 | if (ret < 0) | |
103d84a3 | 2559 | dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); |
ea9d0d77 | 2560 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
618dae11 LG |
2561 | |
2562 | return ret; | |
2563 | } | |
2564 | ||
de15d7ff | 2565 | static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) |
618dae11 | 2566 | { |
de15d7ff | 2567 | struct snd_soc_dapm_widget_list *list; |
7083f877 | 2568 | int stream; |
de15d7ff | 2569 | int count, paths; |
618dae11 | 2570 | |
de15d7ff JB |
2571 | if (!fe->dai_link->dynamic) |
2572 | return 0; | |
618dae11 | 2573 | |
de15d7ff JB |
2574 | /* only check active links */ |
2575 | if (!fe->cpu_dai->active) | |
2576 | return 0; | |
618dae11 | 2577 | |
de15d7ff JB |
2578 | /* DAPM sync will call this to update DSP paths */ |
2579 | dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", | |
2580 | new ? "new" : "old", fe->dai_link->name); | |
618dae11 | 2581 | |
7083f877 | 2582 | for_each_pcm_streams(stream) { |
618dae11 | 2583 | |
7083f877 KM |
2584 | /* skip if FE doesn't have playback/capture capability */ |
2585 | if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) || | |
2586 | !snd_soc_dai_stream_valid(fe->codec_dai, stream)) | |
2587 | continue; | |
de15d7ff | 2588 | |
7083f877 KM |
2589 | /* skip if FE isn't currently playing/capturing */ |
2590 | if (!fe->cpu_dai->stream_active[stream] || | |
2591 | !fe->codec_dai->stream_active[stream]) | |
2592 | continue; | |
075207d2 | 2593 | |
7083f877 KM |
2594 | paths = dpcm_path_get(fe, stream, &list); |
2595 | if (paths < 0) { | |
2596 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", | |
2597 | fe->dai_link->name, | |
2598 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
2599 | "playback" : "capture"); | |
2600 | return paths; | |
2601 | } | |
618dae11 | 2602 | |
7083f877 KM |
2603 | /* update any playback/capture paths */ |
2604 | count = dpcm_process_paths(fe, stream, &list, new); | |
2605 | if (count) { | |
2606 | if (new) | |
2607 | dpcm_run_new_update(fe, stream); | |
2608 | else | |
2609 | dpcm_run_old_update(fe, stream); | |
618dae11 | 2610 | |
7083f877 KM |
2611 | dpcm_clear_pending_state(fe, stream); |
2612 | dpcm_be_disconnect(fe, stream); | |
2613 | } | |
618dae11 | 2614 | |
7083f877 | 2615 | dpcm_path_put(&list); |
618dae11 LG |
2616 | } |
2617 | ||
618dae11 LG |
2618 | return 0; |
2619 | } | |
de15d7ff JB |
2620 | |
2621 | /* Called by DAPM mixer/mux changes to update audio routing between PCMs and | |
2622 | * any DAI links. | |
2623 | */ | |
2624 | int soc_dpcm_runtime_update(struct snd_soc_card *card) | |
2625 | { | |
2626 | struct snd_soc_pcm_runtime *fe; | |
2627 | int ret = 0; | |
2628 | ||
2629 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2630 | /* shutdown all old paths first */ | |
bcb1fd1f | 2631 | for_each_card_rtds(card, fe) { |
de15d7ff JB |
2632 | ret = soc_dpcm_fe_runtime_update(fe, 0); |
2633 | if (ret) | |
2634 | goto out; | |
2635 | } | |
2636 | ||
2637 | /* bring new paths up */ | |
bcb1fd1f | 2638 | for_each_card_rtds(card, fe) { |
de15d7ff JB |
2639 | ret = soc_dpcm_fe_runtime_update(fe, 1); |
2640 | if (ret) | |
2641 | goto out; | |
2642 | } | |
2643 | ||
2644 | out: | |
2645 | mutex_unlock(&card->mutex); | |
2646 | return ret; | |
2647 | } | |
01d7584c | 2648 | |
45c0a188 | 2649 | static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
01d7584c LG |
2650 | { |
2651 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
2652 | struct snd_soc_dpcm *dpcm; | |
2653 | struct snd_soc_dapm_widget_list *list; | |
2654 | int ret; | |
2655 | int stream = fe_substream->stream; | |
2656 | ||
2657 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2658 | fe->dpcm[stream].runtime = fe_substream->runtime; | |
2659 | ||
8f70e515 QZ |
2660 | ret = dpcm_path_get(fe, stream, &list); |
2661 | if (ret < 0) { | |
cae06eb9 | 2662 | goto open_end; |
8f70e515 | 2663 | } else if (ret == 0) { |
103d84a3 | 2664 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
01d7584c | 2665 | fe->dai_link->name, stream ? "capture" : "playback"); |
01d7584c LG |
2666 | } |
2667 | ||
2668 | /* calculate valid and active FE <-> BE dpcms */ | |
2669 | dpcm_process_paths(fe, stream, &list, 1); | |
2670 | ||
2671 | ret = dpcm_fe_dai_startup(fe_substream); | |
2672 | if (ret < 0) { | |
2673 | /* clean up all links */ | |
8d6258a4 | 2674 | for_each_dpcm_be(fe, stream, dpcm) |
01d7584c LG |
2675 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
2676 | ||
2677 | dpcm_be_disconnect(fe, stream); | |
2678 | fe->dpcm[stream].runtime = NULL; | |
2679 | } | |
2680 | ||
2681 | dpcm_clear_pending_state(fe, stream); | |
2682 | dpcm_path_put(&list); | |
cae06eb9 | 2683 | open_end: |
01d7584c LG |
2684 | mutex_unlock(&fe->card->mutex); |
2685 | return ret; | |
2686 | } | |
2687 | ||
45c0a188 | 2688 | static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
01d7584c LG |
2689 | { |
2690 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
2691 | struct snd_soc_dpcm *dpcm; | |
2692 | int stream = fe_substream->stream, ret; | |
2693 | ||
2694 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2695 | ret = dpcm_fe_dai_shutdown(fe_substream); | |
2696 | ||
2697 | /* mark FE's links ready to prune */ | |
8d6258a4 | 2698 | for_each_dpcm_be(fe, stream, dpcm) |
01d7584c LG |
2699 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
2700 | ||
2701 | dpcm_be_disconnect(fe, stream); | |
2702 | ||
2703 | fe->dpcm[stream].runtime = NULL; | |
2704 | mutex_unlock(&fe->card->mutex); | |
2705 | return ret; | |
2706 | } | |
2707 | ||
ddee627c LG |
2708 | /* create a new pcm */ |
2709 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) | |
2710 | { | |
2e5894d7 | 2711 | struct snd_soc_dai *codec_dai; |
ddee627c | 2712 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2b544dd7 | 2713 | struct snd_soc_component *component; |
ddee627c LG |
2714 | struct snd_pcm *pcm; |
2715 | char new_name[64]; | |
2716 | int ret = 0, playback = 0, capture = 0; | |
2e5894d7 | 2717 | int i; |
ddee627c | 2718 | |
01d7584c | 2719 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
1e9de42f LG |
2720 | playback = rtd->dai_link->dpcm_playback; |
2721 | capture = rtd->dai_link->dpcm_capture; | |
01d7584c | 2722 | } else { |
a342031c | 2723 | /* Adapt stream for codec2codec links */ |
a4877a6f SG |
2724 | int cpu_capture = rtd->dai_link->params ? |
2725 | SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; | |
2726 | int cpu_playback = rtd->dai_link->params ? | |
2727 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
a342031c | 2728 | |
0b7990e3 | 2729 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
467fece8 | 2730 | if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && |
a4877a6f | 2731 | snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) |
2e5894d7 | 2732 | playback = 1; |
467fece8 | 2733 | if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && |
a4877a6f | 2734 | snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) |
2e5894d7 BC |
2735 | capture = 1; |
2736 | } | |
01d7584c LG |
2737 | } |
2738 | ||
d6bead02 FE |
2739 | if (rtd->dai_link->playback_only) { |
2740 | playback = 1; | |
2741 | capture = 0; | |
2742 | } | |
2743 | ||
2744 | if (rtd->dai_link->capture_only) { | |
2745 | playback = 0; | |
2746 | capture = 1; | |
2747 | } | |
2748 | ||
01d7584c | 2749 | /* create the PCM */ |
a342031c JB |
2750 | if (rtd->dai_link->params) { |
2751 | snprintf(new_name, sizeof(new_name), "codec2codec(%s)", | |
2752 | rtd->dai_link->stream_name); | |
2753 | ||
2754 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, | |
2755 | playback, capture, &pcm); | |
2756 | } else if (rtd->dai_link->no_pcm) { | |
01d7584c LG |
2757 | snprintf(new_name, sizeof(new_name), "(%s)", |
2758 | rtd->dai_link->stream_name); | |
2759 | ||
2760 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, | |
2761 | playback, capture, &pcm); | |
2762 | } else { | |
2763 | if (rtd->dai_link->dynamic) | |
2764 | snprintf(new_name, sizeof(new_name), "%s (*)", | |
2765 | rtd->dai_link->stream_name); | |
2766 | else | |
2767 | snprintf(new_name, sizeof(new_name), "%s %s-%d", | |
2e5894d7 BC |
2768 | rtd->dai_link->stream_name, |
2769 | (rtd->num_codecs > 1) ? | |
2770 | "multicodec" : rtd->codec_dai->name, num); | |
01d7584c LG |
2771 | |
2772 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, | |
2773 | capture, &pcm); | |
2774 | } | |
ddee627c | 2775 | if (ret < 0) { |
103d84a3 | 2776 | dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", |
5cb9b748 | 2777 | rtd->dai_link->name); |
ddee627c LG |
2778 | return ret; |
2779 | } | |
103d84a3 | 2780 | dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); |
ddee627c LG |
2781 | |
2782 | /* DAPM dai link stream work */ | |
a342031c | 2783 | if (rtd->dai_link->params) |
4bf2e385 | 2784 | rtd->close_delayed_work_func = codec2codec_close_delayed_work; |
a342031c | 2785 | else |
83f94a2e | 2786 | rtd->close_delayed_work_func = snd_soc_close_delayed_work; |
ddee627c | 2787 | |
48c7699f | 2788 | pcm->nonatomic = rtd->dai_link->nonatomic; |
ddee627c LG |
2789 | rtd->pcm = pcm; |
2790 | pcm->private_data = rtd; | |
01d7584c | 2791 | |
a342031c | 2792 | if (rtd->dai_link->no_pcm || rtd->dai_link->params) { |
01d7584c LG |
2793 | if (playback) |
2794 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; | |
2795 | if (capture) | |
2796 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; | |
2797 | goto out; | |
2798 | } | |
2799 | ||
2800 | /* ASoC PCM operations */ | |
2801 | if (rtd->dai_link->dynamic) { | |
2802 | rtd->ops.open = dpcm_fe_dai_open; | |
2803 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; | |
2804 | rtd->ops.prepare = dpcm_fe_dai_prepare; | |
2805 | rtd->ops.trigger = dpcm_fe_dai_trigger; | |
2806 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; | |
2807 | rtd->ops.close = dpcm_fe_dai_close; | |
2808 | rtd->ops.pointer = soc_pcm_pointer; | |
2809 | } else { | |
2810 | rtd->ops.open = soc_pcm_open; | |
2811 | rtd->ops.hw_params = soc_pcm_hw_params; | |
2812 | rtd->ops.prepare = soc_pcm_prepare; | |
2813 | rtd->ops.trigger = soc_pcm_trigger; | |
2814 | rtd->ops.hw_free = soc_pcm_hw_free; | |
2815 | rtd->ops.close = soc_pcm_close; | |
2816 | rtd->ops.pointer = soc_pcm_pointer; | |
2817 | } | |
2818 | ||
613fb500 | 2819 | for_each_rtd_components(rtd, i, component) { |
2b544dd7 | 2820 | const struct snd_soc_component_driver *drv = component->driver; |
b8135864 | 2821 | |
3b1c952c TI |
2822 | if (drv->ioctl) |
2823 | rtd->ops.ioctl = snd_soc_pcm_component_ioctl; | |
1e5ddb6b TI |
2824 | if (drv->sync_stop) |
2825 | rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; | |
e9067bb5 | 2826 | if (drv->copy_user) |
82d81f5c | 2827 | rtd->ops.copy_user = snd_soc_pcm_component_copy_user; |
e9067bb5 | 2828 | if (drv->page) |
9c712e4f | 2829 | rtd->ops.page = snd_soc_pcm_component_page; |
e9067bb5 | 2830 | if (drv->mmap) |
205875e1 | 2831 | rtd->ops.mmap = snd_soc_pcm_component_mmap; |
ddee627c LG |
2832 | } |
2833 | ||
2834 | if (playback) | |
01d7584c | 2835 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
ddee627c LG |
2836 | |
2837 | if (capture) | |
01d7584c | 2838 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
ddee627c | 2839 | |
b2b2afbb | 2840 | ret = snd_soc_pcm_component_new(rtd); |
7484291e KM |
2841 | if (ret < 0) { |
2842 | dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret); | |
2843 | return ret; | |
ddee627c | 2844 | } |
c641e5b2 | 2845 | |
3d21ef0b | 2846 | pcm->no_device_suspend = true; |
01d7584c | 2847 | out: |
2e5894d7 BC |
2848 | dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", |
2849 | (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, | |
2850 | cpu_dai->name); | |
ddee627c LG |
2851 | return ret; |
2852 | } | |
01d7584c LG |
2853 | |
2854 | /* is the current PCM operation for this FE ? */ | |
2855 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) | |
2856 | { | |
2857 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) | |
2858 | return 1; | |
2859 | return 0; | |
2860 | } | |
2861 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); | |
2862 | ||
2863 | /* is the current PCM operation for this BE ? */ | |
2864 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, | |
2865 | struct snd_soc_pcm_runtime *be, int stream) | |
2866 | { | |
2867 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || | |
2868 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && | |
2869 | be->dpcm[stream].runtime_update)) | |
2870 | return 1; | |
2871 | return 0; | |
2872 | } | |
2873 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); | |
2874 | ||
2875 | /* get the substream for this BE */ | |
2876 | struct snd_pcm_substream * | |
2877 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) | |
2878 | { | |
2879 | return be->pcm->streams[stream].substream; | |
2880 | } | |
2881 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); | |
2882 | ||
085d22be KM |
2883 | static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, |
2884 | struct snd_soc_pcm_runtime *be, | |
2885 | int stream, | |
2886 | const enum snd_soc_dpcm_state *states, | |
2887 | int num_states) | |
01d7584c LG |
2888 | { |
2889 | struct snd_soc_dpcm *dpcm; | |
2890 | int state; | |
a9764869 KC |
2891 | int ret = 1; |
2892 | unsigned long flags; | |
085d22be | 2893 | int i; |
01d7584c | 2894 | |
a9764869 | 2895 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
d2e24d64 | 2896 | for_each_dpcm_fe(be, stream, dpcm) { |
01d7584c LG |
2897 | |
2898 | if (dpcm->fe == fe) | |
2899 | continue; | |
2900 | ||
2901 | state = dpcm->fe->dpcm[stream].state; | |
085d22be KM |
2902 | for (i = 0; i < num_states; i++) { |
2903 | if (state == states[i]) { | |
2904 | ret = 0; | |
2905 | break; | |
2906 | } | |
a9764869 | 2907 | } |
01d7584c | 2908 | } |
a9764869 | 2909 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
01d7584c | 2910 | |
085d22be | 2911 | /* it's safe to do this BE DAI */ |
a9764869 | 2912 | return ret; |
01d7584c | 2913 | } |
085d22be KM |
2914 | |
2915 | /* | |
2916 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE | |
2917 | * are not running, paused or suspended for the specified stream direction. | |
2918 | */ | |
2919 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, | |
2920 | struct snd_soc_pcm_runtime *be, int stream) | |
2921 | { | |
2922 | const enum snd_soc_dpcm_state state[] = { | |
2923 | SND_SOC_DPCM_STATE_START, | |
2924 | SND_SOC_DPCM_STATE_PAUSED, | |
2925 | SND_SOC_DPCM_STATE_SUSPEND, | |
2926 | }; | |
2927 | ||
2928 | return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); | |
2929 | } | |
01d7584c LG |
2930 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); |
2931 | ||
2932 | /* | |
2933 | * We can only change hw params a BE DAI if any of it's FE are not prepared, | |
2934 | * running, paused or suspended for the specified stream direction. | |
2935 | */ | |
2936 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, | |
2937 | struct snd_soc_pcm_runtime *be, int stream) | |
2938 | { | |
085d22be KM |
2939 | const enum snd_soc_dpcm_state state[] = { |
2940 | SND_SOC_DPCM_STATE_START, | |
2941 | SND_SOC_DPCM_STATE_PAUSED, | |
2942 | SND_SOC_DPCM_STATE_SUSPEND, | |
2943 | SND_SOC_DPCM_STATE_PREPARE, | |
2944 | }; | |
01d7584c | 2945 | |
085d22be | 2946 | return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); |
01d7584c LG |
2947 | } |
2948 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); | |
f86dcef8 LG |
2949 | |
2950 | #ifdef CONFIG_DEBUG_FS | |
85280141 | 2951 | static const char *dpcm_state_string(enum snd_soc_dpcm_state state) |
f86dcef8 LG |
2952 | { |
2953 | switch (state) { | |
2954 | case SND_SOC_DPCM_STATE_NEW: | |
2955 | return "new"; | |
2956 | case SND_SOC_DPCM_STATE_OPEN: | |
2957 | return "open"; | |
2958 | case SND_SOC_DPCM_STATE_HW_PARAMS: | |
2959 | return "hw_params"; | |
2960 | case SND_SOC_DPCM_STATE_PREPARE: | |
2961 | return "prepare"; | |
2962 | case SND_SOC_DPCM_STATE_START: | |
2963 | return "start"; | |
2964 | case SND_SOC_DPCM_STATE_STOP: | |
2965 | return "stop"; | |
2966 | case SND_SOC_DPCM_STATE_SUSPEND: | |
2967 | return "suspend"; | |
2968 | case SND_SOC_DPCM_STATE_PAUSED: | |
2969 | return "paused"; | |
2970 | case SND_SOC_DPCM_STATE_HW_FREE: | |
2971 | return "hw_free"; | |
2972 | case SND_SOC_DPCM_STATE_CLOSE: | |
2973 | return "close"; | |
2974 | } | |
2975 | ||
2976 | return "unknown"; | |
2977 | } | |
2978 | ||
f86dcef8 LG |
2979 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
2980 | int stream, char *buf, size_t size) | |
2981 | { | |
2982 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; | |
2983 | struct snd_soc_dpcm *dpcm; | |
2984 | ssize_t offset = 0; | |
a9764869 | 2985 | unsigned long flags; |
f86dcef8 LG |
2986 | |
2987 | /* FE state */ | |
2988 | offset += snprintf(buf + offset, size - offset, | |
2989 | "[%s - %s]\n", fe->dai_link->name, | |
2990 | stream ? "Capture" : "Playback"); | |
2991 | ||
2992 | offset += snprintf(buf + offset, size - offset, "State: %s\n", | |
2993 | dpcm_state_string(fe->dpcm[stream].state)); | |
2994 | ||
2995 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && | |
2996 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) | |
2997 | offset += snprintf(buf + offset, size - offset, | |
2998 | "Hardware Params: " | |
2999 | "Format = %s, Channels = %d, Rate = %d\n", | |
3000 | snd_pcm_format_name(params_format(params)), | |
3001 | params_channels(params), | |
3002 | params_rate(params)); | |
3003 | ||
3004 | /* BEs state */ | |
3005 | offset += snprintf(buf + offset, size - offset, "Backends:\n"); | |
3006 | ||
3007 | if (list_empty(&fe->dpcm[stream].be_clients)) { | |
3008 | offset += snprintf(buf + offset, size - offset, | |
3009 | " No active DSP links\n"); | |
3010 | goto out; | |
3011 | } | |
3012 | ||
a9764869 | 3013 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
8d6258a4 | 3014 | for_each_dpcm_be(fe, stream, dpcm) { |
f86dcef8 LG |
3015 | struct snd_soc_pcm_runtime *be = dpcm->be; |
3016 | params = &dpcm->hw_params; | |
3017 | ||
3018 | offset += snprintf(buf + offset, size - offset, | |
3019 | "- %s\n", be->dai_link->name); | |
3020 | ||
3021 | offset += snprintf(buf + offset, size - offset, | |
3022 | " State: %s\n", | |
3023 | dpcm_state_string(be->dpcm[stream].state)); | |
3024 | ||
3025 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && | |
3026 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) | |
3027 | offset += snprintf(buf + offset, size - offset, | |
3028 | " Hardware Params: " | |
3029 | "Format = %s, Channels = %d, Rate = %d\n", | |
3030 | snd_pcm_format_name(params_format(params)), | |
3031 | params_channels(params), | |
3032 | params_rate(params)); | |
3033 | } | |
a9764869 | 3034 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
f86dcef8 LG |
3035 | out: |
3036 | return offset; | |
3037 | } | |
3038 | ||
3039 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, | |
3040 | size_t count, loff_t *ppos) | |
3041 | { | |
3042 | struct snd_soc_pcm_runtime *fe = file->private_data; | |
3043 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; | |
7083f877 | 3044 | int stream; |
f86dcef8 LG |
3045 | char *buf; |
3046 | ||
3047 | buf = kmalloc(out_count, GFP_KERNEL); | |
3048 | if (!buf) | |
3049 | return -ENOMEM; | |
3050 | ||
7083f877 KM |
3051 | for_each_pcm_streams(stream) |
3052 | if (snd_soc_dai_stream_valid(fe->cpu_dai, stream)) | |
3053 | offset += dpcm_show_state(fe, stream, | |
3054 | buf + offset, | |
3055 | out_count - offset); | |
f86dcef8 | 3056 | |
f57b8488 | 3057 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
f86dcef8 | 3058 | |
f57b8488 LG |
3059 | kfree(buf); |
3060 | return ret; | |
f86dcef8 LG |
3061 | } |
3062 | ||
3063 | static const struct file_operations dpcm_state_fops = { | |
f57b8488 | 3064 | .open = simple_open, |
f86dcef8 LG |
3065 | .read = dpcm_state_read_file, |
3066 | .llseek = default_llseek, | |
3067 | }; | |
3068 | ||
2e55b90a | 3069 | void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) |
f86dcef8 | 3070 | { |
b3bba9a1 | 3071 | if (!rtd->dai_link) |
2e55b90a | 3072 | return; |
b3bba9a1 | 3073 | |
596becd3 KM |
3074 | if (!rtd->dai_link->dynamic) |
3075 | return; | |
3076 | ||
6553bf06 LPC |
3077 | if (!rtd->card->debugfs_card_root) |
3078 | return; | |
b3bba9a1 | 3079 | |
f86dcef8 LG |
3080 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
3081 | rtd->card->debugfs_card_root); | |
f86dcef8 | 3082 | |
f1e3f409 FE |
3083 | debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, |
3084 | rtd, &dpcm_state_fops); | |
f86dcef8 LG |
3085 | } |
3086 | #endif |