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