Commit | Line | Data |
---|---|---|
ddee627c LG |
1 | /* |
2 | * soc-pcm.c -- ALSA SoC PCM | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
5 | * Copyright 2005 Openedhand Ltd. | |
6 | * Copyright (C) 2010 Slimlogic Ltd. | |
7 | * Copyright (C) 2010 Texas Instruments Inc. | |
8 | * | |
9 | * Authors: Liam Girdwood <lrg@ti.com> | |
10 | * Mark Brown <broonie@opensource.wolfsonmicro.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2 of the License, or (at your | |
15 | * option) any later version. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/delay.h> | |
988e8cc4 | 22 | #include <linux/pinctrl/consumer.h> |
d6652ef8 | 23 | #include <linux/pm_runtime.h> |
ddee627c LG |
24 | #include <linux/slab.h> |
25 | #include <linux/workqueue.h> | |
01d7584c | 26 | #include <linux/export.h> |
f86dcef8 | 27 | #include <linux/debugfs.h> |
ddee627c LG |
28 | #include <sound/core.h> |
29 | #include <sound/pcm.h> | |
30 | #include <sound/pcm_params.h> | |
31 | #include <sound/soc.h> | |
01d7584c | 32 | #include <sound/soc-dpcm.h> |
ddee627c LG |
33 | #include <sound/initval.h> |
34 | ||
01d7584c LG |
35 | #define DPCM_MAX_BE_USERS 8 |
36 | ||
208a1589 LPC |
37 | /** |
38 | * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay | |
39 | * @rtd: The ASoC PCM runtime that should be checked. | |
40 | * | |
41 | * This function checks whether the power down delay should be ignored for a | |
42 | * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has | |
43 | * been configured to ignore the delay, or if none of the components benefits | |
44 | * from having the delay. | |
45 | */ | |
46 | bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) | |
47 | { | |
48 | bool ignore = true; | |
49 | ||
50 | if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) | |
51 | return true; | |
52 | ||
53 | if (rtd->cpu_dai->codec) | |
54 | ignore &= rtd->cpu_dai->codec->ignore_pmdown_time; | |
55 | ||
56 | ignore &= rtd->codec_dai->codec->ignore_pmdown_time; | |
57 | ||
58 | return ignore; | |
59 | } | |
60 | ||
90996f43 LPC |
61 | /** |
62 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters | |
63 | * @substream: the pcm substream | |
64 | * @hw: the hardware parameters | |
65 | * | |
66 | * Sets the substream runtime hardware parameters. | |
67 | */ | |
68 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, | |
69 | const struct snd_pcm_hardware *hw) | |
70 | { | |
71 | struct snd_pcm_runtime *runtime = substream->runtime; | |
72 | runtime->hw.info = hw->info; | |
73 | runtime->hw.formats = hw->formats; | |
74 | runtime->hw.period_bytes_min = hw->period_bytes_min; | |
75 | runtime->hw.period_bytes_max = hw->period_bytes_max; | |
76 | runtime->hw.periods_min = hw->periods_min; | |
77 | runtime->hw.periods_max = hw->periods_max; | |
78 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; | |
79 | runtime->hw.fifo_size = hw->fifo_size; | |
80 | return 0; | |
81 | } | |
82 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); | |
83 | ||
01d7584c | 84 | /* DPCM stream event, send event to FE and all active BEs. */ |
23607025 | 85 | int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, |
01d7584c LG |
86 | int event) |
87 | { | |
88 | struct snd_soc_dpcm *dpcm; | |
89 | ||
90 | list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) { | |
91 | ||
92 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
93 | ||
103d84a3 | 94 | dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", |
01d7584c LG |
95 | be->dai_link->name, event, dir); |
96 | ||
97 | snd_soc_dapm_stream_event(be, dir, event); | |
98 | } | |
99 | ||
100 | snd_soc_dapm_stream_event(fe, dir, event); | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
17841020 DA |
105 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, |
106 | struct snd_soc_dai *soc_dai) | |
ddee627c LG |
107 | { |
108 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
ddee627c LG |
109 | int ret; |
110 | ||
3635bf09 NC |
111 | if (soc_dai->rate && (soc_dai->driver->symmetric_rates || |
112 | rtd->dai_link->symmetric_rates)) { | |
113 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", | |
114 | soc_dai->rate); | |
115 | ||
116 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, | |
117 | SNDRV_PCM_HW_PARAM_RATE, | |
118 | soc_dai->rate, soc_dai->rate); | |
119 | if (ret < 0) { | |
120 | dev_err(soc_dai->dev, | |
121 | "ASoC: Unable to apply rate constraint: %d\n", | |
122 | ret); | |
123 | return ret; | |
124 | } | |
125 | } | |
ddee627c | 126 | |
3635bf09 NC |
127 | if (soc_dai->channels && (soc_dai->driver->symmetric_channels || |
128 | rtd->dai_link->symmetric_channels)) { | |
129 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", | |
130 | soc_dai->channels); | |
ddee627c | 131 | |
3635bf09 NC |
132 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, |
133 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
134 | soc_dai->channels, | |
135 | soc_dai->channels); | |
136 | if (ret < 0) { | |
137 | dev_err(soc_dai->dev, | |
138 | "ASoC: Unable to apply channel symmetry constraint: %d\n", | |
139 | ret); | |
140 | return ret; | |
141 | } | |
ddee627c LG |
142 | } |
143 | ||
3635bf09 NC |
144 | if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || |
145 | rtd->dai_link->symmetric_samplebits)) { | |
146 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", | |
147 | soc_dai->sample_bits); | |
ddee627c | 148 | |
3635bf09 NC |
149 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, |
150 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
151 | soc_dai->sample_bits, | |
152 | soc_dai->sample_bits); | |
153 | if (ret < 0) { | |
154 | dev_err(soc_dai->dev, | |
155 | "ASoC: Unable to apply sample bits symmetry constraint: %d\n", | |
156 | ret); | |
157 | return ret; | |
158 | } | |
ddee627c LG |
159 | } |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
3635bf09 NC |
164 | static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, |
165 | struct snd_pcm_hw_params *params) | |
166 | { | |
167 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
168 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
169 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
170 | unsigned int rate, channels, sample_bits, symmetry; | |
171 | ||
172 | rate = params_rate(params); | |
173 | channels = params_channels(params); | |
174 | sample_bits = snd_pcm_format_physical_width(params_format(params)); | |
175 | ||
176 | /* reject unmatched parameters when applying symmetry */ | |
177 | symmetry = cpu_dai->driver->symmetric_rates || | |
178 | codec_dai->driver->symmetric_rates || | |
179 | rtd->dai_link->symmetric_rates; | |
180 | if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { | |
181 | dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", | |
182 | cpu_dai->rate, rate); | |
183 | return -EINVAL; | |
ddee627c LG |
184 | } |
185 | ||
3635bf09 NC |
186 | symmetry = cpu_dai->driver->symmetric_channels || |
187 | codec_dai->driver->symmetric_channels || | |
188 | rtd->dai_link->symmetric_channels; | |
189 | if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { | |
190 | dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", | |
191 | cpu_dai->channels, channels); | |
192 | return -EINVAL; | |
193 | } | |
ddee627c | 194 | |
3635bf09 NC |
195 | symmetry = cpu_dai->driver->symmetric_samplebits || |
196 | codec_dai->driver->symmetric_samplebits || | |
197 | rtd->dai_link->symmetric_samplebits; | |
198 | if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { | |
199 | dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", | |
200 | cpu_dai->sample_bits, sample_bits); | |
201 | return -EINVAL; | |
ddee627c LG |
202 | } |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
62e5f676 LPC |
207 | static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) |
208 | { | |
209 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
210 | struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; | |
211 | struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver; | |
212 | struct snd_soc_dai_link *link = rtd->dai_link; | |
213 | ||
214 | return cpu_driver->symmetric_rates || codec_driver->symmetric_rates || | |
215 | link->symmetric_rates || cpu_driver->symmetric_channels || | |
216 | codec_driver->symmetric_channels || link->symmetric_channels || | |
217 | cpu_driver->symmetric_samplebits || | |
218 | codec_driver->symmetric_samplebits || | |
219 | link->symmetric_samplebits; | |
220 | } | |
221 | ||
58ba9b25 MB |
222 | /* |
223 | * List of sample sizes that might go over the bus for parameter | |
224 | * application. There ought to be a wildcard sample size for things | |
225 | * like the DAC/ADC resolution to use but there isn't right now. | |
226 | */ | |
227 | static int sample_sizes[] = { | |
88e33954 | 228 | 24, 32, |
58ba9b25 MB |
229 | }; |
230 | ||
231 | static void soc_pcm_apply_msb(struct snd_pcm_substream *substream, | |
232 | struct snd_soc_dai *dai) | |
233 | { | |
234 | int ret, i, bits; | |
235 | ||
236 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
237 | bits = dai->driver->playback.sig_bits; | |
238 | else | |
239 | bits = dai->driver->capture.sig_bits; | |
240 | ||
241 | if (!bits) | |
242 | return; | |
243 | ||
244 | for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) { | |
278047fd MB |
245 | if (bits >= sample_sizes[i]) |
246 | continue; | |
247 | ||
248 | ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, | |
249 | sample_sizes[i], bits); | |
58ba9b25 MB |
250 | if (ret != 0) |
251 | dev_warn(dai->dev, | |
103d84a3 | 252 | "ASoC: Failed to set MSB %d/%d: %d\n", |
58ba9b25 MB |
253 | bits, sample_sizes[i], ret); |
254 | } | |
255 | } | |
256 | ||
78e45c99 | 257 | static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
bd477c31 LPC |
258 | struct snd_soc_pcm_stream *codec_stream, |
259 | struct snd_soc_pcm_stream *cpu_stream) | |
260 | { | |
78e45c99 LPC |
261 | struct snd_pcm_hardware *hw = &runtime->hw; |
262 | ||
bd477c31 LPC |
263 | hw->channels_min = max(codec_stream->channels_min, |
264 | cpu_stream->channels_min); | |
265 | hw->channels_max = min(codec_stream->channels_max, | |
266 | cpu_stream->channels_max); | |
16d7ea91 LPC |
267 | if (hw->formats) |
268 | hw->formats &= codec_stream->formats & cpu_stream->formats; | |
269 | else | |
270 | hw->formats = codec_stream->formats & cpu_stream->formats; | |
55dcdb50 LPC |
271 | hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates, |
272 | cpu_stream->rates); | |
78e45c99 | 273 | |
817873f4 LPC |
274 | hw->rate_min = 0; |
275 | hw->rate_max = UINT_MAX; | |
78e45c99 LPC |
276 | |
277 | snd_pcm_limit_hw_rates(runtime); | |
278 | ||
279 | hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); | |
280 | hw->rate_min = max(hw->rate_min, codec_stream->rate_min); | |
281 | hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); | |
282 | hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max); | |
bd477c31 LPC |
283 | } |
284 | ||
ddee627c LG |
285 | /* |
286 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is | |
287 | * then initialized and any private data can be allocated. This also calls | |
288 | * startup for the cpu DAI, platform, machine and codec DAI. | |
289 | */ | |
290 | static int soc_pcm_open(struct snd_pcm_substream *substream) | |
291 | { | |
292 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
293 | struct snd_pcm_runtime *runtime = substream->runtime; | |
294 | struct snd_soc_platform *platform = rtd->platform; | |
295 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
296 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
297 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; | |
298 | struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; | |
299 | int ret = 0; | |
300 | ||
988e8cc4 NC |
301 | pinctrl_pm_select_default_state(cpu_dai->dev); |
302 | pinctrl_pm_select_default_state(codec_dai->dev); | |
d6652ef8 MB |
303 | pm_runtime_get_sync(cpu_dai->dev); |
304 | pm_runtime_get_sync(codec_dai->dev); | |
305 | pm_runtime_get_sync(platform->dev); | |
306 | ||
b8c0dab9 | 307 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c LG |
308 | |
309 | /* startup the audio subsystem */ | |
c5914b0a | 310 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) { |
ddee627c LG |
311 | ret = cpu_dai->driver->ops->startup(substream, cpu_dai); |
312 | if (ret < 0) { | |
103d84a3 LG |
313 | dev_err(cpu_dai->dev, "ASoC: can't open interface" |
314 | " %s: %d\n", cpu_dai->name, ret); | |
ddee627c LG |
315 | goto out; |
316 | } | |
317 | } | |
318 | ||
319 | if (platform->driver->ops && platform->driver->ops->open) { | |
320 | ret = platform->driver->ops->open(substream); | |
321 | if (ret < 0) { | |
103d84a3 LG |
322 | dev_err(platform->dev, "ASoC: can't open platform" |
323 | " %s: %d\n", platform->name, ret); | |
ddee627c LG |
324 | goto platform_err; |
325 | } | |
326 | } | |
327 | ||
c5914b0a | 328 | if (codec_dai->driver->ops && codec_dai->driver->ops->startup) { |
ddee627c LG |
329 | ret = codec_dai->driver->ops->startup(substream, codec_dai); |
330 | if (ret < 0) { | |
103d84a3 LG |
331 | dev_err(codec_dai->dev, "ASoC: can't open codec" |
332 | " %s: %d\n", codec_dai->name, ret); | |
ddee627c LG |
333 | goto codec_dai_err; |
334 | } | |
335 | } | |
336 | ||
337 | if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { | |
338 | ret = rtd->dai_link->ops->startup(substream); | |
339 | if (ret < 0) { | |
103d84a3 | 340 | pr_err("ASoC: %s startup failed: %d\n", |
25bfe662 | 341 | rtd->dai_link->name, ret); |
ddee627c LG |
342 | goto machine_err; |
343 | } | |
344 | } | |
345 | ||
01d7584c LG |
346 | /* Dynamic PCM DAI links compat checks use dynamic capabilities */ |
347 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) | |
348 | goto dynamic; | |
349 | ||
ddee627c LG |
350 | /* Check that the codec and cpu DAIs are compatible */ |
351 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
78e45c99 | 352 | soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback, |
bd477c31 | 353 | &cpu_dai_drv->playback); |
ddee627c | 354 | } else { |
78e45c99 | 355 | soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture, |
bd477c31 | 356 | &cpu_dai_drv->capture); |
ddee627c LG |
357 | } |
358 | ||
62e5f676 LPC |
359 | if (soc_pcm_has_symmetry(substream)) |
360 | runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; | |
361 | ||
ddee627c | 362 | ret = -EINVAL; |
ddee627c | 363 | if (!runtime->hw.rates) { |
103d84a3 | 364 | printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", |
ddee627c LG |
365 | codec_dai->name, cpu_dai->name); |
366 | goto config_err; | |
367 | } | |
368 | if (!runtime->hw.formats) { | |
103d84a3 | 369 | printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", |
ddee627c LG |
370 | codec_dai->name, cpu_dai->name); |
371 | goto config_err; | |
372 | } | |
373 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || | |
374 | runtime->hw.channels_min > runtime->hw.channels_max) { | |
103d84a3 | 375 | printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", |
ddee627c LG |
376 | codec_dai->name, cpu_dai->name); |
377 | goto config_err; | |
378 | } | |
379 | ||
58ba9b25 MB |
380 | soc_pcm_apply_msb(substream, codec_dai); |
381 | soc_pcm_apply_msb(substream, cpu_dai); | |
382 | ||
ddee627c | 383 | /* Symmetry only applies if we've already got an active stream. */ |
17841020 DA |
384 | if (cpu_dai->active) { |
385 | ret = soc_pcm_apply_symmetry(substream, cpu_dai); | |
386 | if (ret != 0) | |
387 | goto config_err; | |
388 | } | |
389 | ||
390 | if (codec_dai->active) { | |
391 | ret = soc_pcm_apply_symmetry(substream, codec_dai); | |
ddee627c LG |
392 | if (ret != 0) |
393 | goto config_err; | |
394 | } | |
395 | ||
103d84a3 | 396 | pr_debug("ASoC: %s <-> %s info:\n", |
ddee627c | 397 | codec_dai->name, cpu_dai->name); |
103d84a3 LG |
398 | pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); |
399 | pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, | |
ddee627c | 400 | runtime->hw.channels_max); |
103d84a3 | 401 | pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, |
ddee627c LG |
402 | runtime->hw.rate_max); |
403 | ||
01d7584c | 404 | dynamic: |
ddee627c LG |
405 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
406 | cpu_dai->playback_active++; | |
407 | codec_dai->playback_active++; | |
408 | } else { | |
409 | cpu_dai->capture_active++; | |
410 | codec_dai->capture_active++; | |
411 | } | |
412 | cpu_dai->active++; | |
413 | codec_dai->active++; | |
414 | rtd->codec->active++; | |
b8c0dab9 | 415 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
416 | return 0; |
417 | ||
418 | config_err: | |
419 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) | |
420 | rtd->dai_link->ops->shutdown(substream); | |
421 | ||
422 | machine_err: | |
423 | if (codec_dai->driver->ops->shutdown) | |
424 | codec_dai->driver->ops->shutdown(substream, codec_dai); | |
425 | ||
426 | codec_dai_err: | |
427 | if (platform->driver->ops && platform->driver->ops->close) | |
428 | platform->driver->ops->close(substream); | |
429 | ||
430 | platform_err: | |
431 | if (cpu_dai->driver->ops->shutdown) | |
432 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); | |
433 | out: | |
b8c0dab9 | 434 | mutex_unlock(&rtd->pcm_mutex); |
d6652ef8 MB |
435 | |
436 | pm_runtime_put(platform->dev); | |
437 | pm_runtime_put(codec_dai->dev); | |
438 | pm_runtime_put(cpu_dai->dev); | |
988e8cc4 NC |
439 | if (!codec_dai->active) |
440 | pinctrl_pm_select_sleep_state(codec_dai->dev); | |
441 | if (!cpu_dai->active) | |
442 | pinctrl_pm_select_sleep_state(cpu_dai->dev); | |
d6652ef8 | 443 | |
ddee627c LG |
444 | return ret; |
445 | } | |
446 | ||
447 | /* | |
448 | * Power down the audio subsystem pmdown_time msecs after close is called. | |
449 | * This is to ensure there are no pops or clicks in between any music tracks | |
450 | * due to DAPM power cycling. | |
451 | */ | |
452 | static void close_delayed_work(struct work_struct *work) | |
453 | { | |
454 | struct snd_soc_pcm_runtime *rtd = | |
455 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); | |
456 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
457 | ||
b8c0dab9 | 458 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c | 459 | |
103d84a3 | 460 | dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", |
ddee627c LG |
461 | codec_dai->driver->playback.stream_name, |
462 | codec_dai->playback_active ? "active" : "inactive", | |
9bffb1fb | 463 | rtd->pop_wait ? "yes" : "no"); |
ddee627c LG |
464 | |
465 | /* are we waiting on this codec DAI stream */ | |
9bffb1fb MLC |
466 | if (rtd->pop_wait == 1) { |
467 | rtd->pop_wait = 0; | |
7bd3a6f3 | 468 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
d9b0951b | 469 | SND_SOC_DAPM_STREAM_STOP); |
ddee627c LG |
470 | } |
471 | ||
b8c0dab9 | 472 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
473 | } |
474 | ||
475 | /* | |
476 | * Called by ALSA when a PCM substream is closed. Private data can be | |
477 | * freed here. The cpu DAI, codec DAI, machine and platform are also | |
478 | * shutdown. | |
479 | */ | |
91d5e6b4 | 480 | static int soc_pcm_close(struct snd_pcm_substream *substream) |
ddee627c LG |
481 | { |
482 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
483 | struct snd_soc_platform *platform = rtd->platform; | |
484 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
485 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
486 | struct snd_soc_codec *codec = rtd->codec; | |
487 | ||
b8c0dab9 | 488 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c LG |
489 | |
490 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
491 | cpu_dai->playback_active--; | |
492 | codec_dai->playback_active--; | |
493 | } else { | |
494 | cpu_dai->capture_active--; | |
495 | codec_dai->capture_active--; | |
496 | } | |
497 | ||
498 | cpu_dai->active--; | |
499 | codec_dai->active--; | |
500 | codec->active--; | |
501 | ||
17841020 DA |
502 | /* clear the corresponding DAIs rate when inactive */ |
503 | if (!cpu_dai->active) | |
504 | cpu_dai->rate = 0; | |
505 | ||
506 | if (!codec_dai->active) | |
507 | codec_dai->rate = 0; | |
25b76791 | 508 | |
ddee627c LG |
509 | if (cpu_dai->driver->ops->shutdown) |
510 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); | |
511 | ||
512 | if (codec_dai->driver->ops->shutdown) | |
513 | codec_dai->driver->ops->shutdown(substream, codec_dai); | |
514 | ||
515 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) | |
516 | rtd->dai_link->ops->shutdown(substream); | |
517 | ||
518 | if (platform->driver->ops && platform->driver->ops->close) | |
519 | platform->driver->ops->close(substream); | |
520 | cpu_dai->runtime = NULL; | |
521 | ||
522 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
208a1589 | 523 | if (snd_soc_runtime_ignore_pmdown_time(rtd)) { |
1d69c5c5 PU |
524 | /* powered down playback stream now */ |
525 | snd_soc_dapm_stream_event(rtd, | |
7bd3a6f3 | 526 | SNDRV_PCM_STREAM_PLAYBACK, |
7bd3a6f3 | 527 | SND_SOC_DAPM_STREAM_STOP); |
1d69c5c5 PU |
528 | } else { |
529 | /* start delayed pop wq here for playback streams */ | |
9bffb1fb | 530 | rtd->pop_wait = 1; |
d4e1a73a MB |
531 | queue_delayed_work(system_power_efficient_wq, |
532 | &rtd->delayed_work, | |
533 | msecs_to_jiffies(rtd->pmdown_time)); | |
1d69c5c5 | 534 | } |
ddee627c LG |
535 | } else { |
536 | /* capture streams can be powered down now */ | |
7bd3a6f3 | 537 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, |
d9b0951b | 538 | SND_SOC_DAPM_STREAM_STOP); |
ddee627c LG |
539 | } |
540 | ||
b8c0dab9 | 541 | mutex_unlock(&rtd->pcm_mutex); |
d6652ef8 MB |
542 | |
543 | pm_runtime_put(platform->dev); | |
544 | pm_runtime_put(codec_dai->dev); | |
545 | pm_runtime_put(cpu_dai->dev); | |
988e8cc4 NC |
546 | if (!codec_dai->active) |
547 | pinctrl_pm_select_sleep_state(codec_dai->dev); | |
548 | if (!cpu_dai->active) | |
549 | pinctrl_pm_select_sleep_state(cpu_dai->dev); | |
d6652ef8 | 550 | |
ddee627c LG |
551 | return 0; |
552 | } | |
553 | ||
554 | /* | |
555 | * Called by ALSA when the PCM substream is prepared, can set format, sample | |
556 | * rate, etc. This function is non atomic and can be called multiple times, | |
557 | * it can refer to the runtime info. | |
558 | */ | |
559 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) | |
560 | { | |
561 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
562 | struct snd_soc_platform *platform = rtd->platform; | |
563 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
564 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
565 | int ret = 0; | |
566 | ||
b8c0dab9 | 567 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c LG |
568 | |
569 | if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { | |
570 | ret = rtd->dai_link->ops->prepare(substream); | |
571 | if (ret < 0) { | |
103d84a3 LG |
572 | dev_err(rtd->card->dev, "ASoC: machine prepare error:" |
573 | " %d\n", ret); | |
ddee627c LG |
574 | goto out; |
575 | } | |
576 | } | |
577 | ||
578 | if (platform->driver->ops && platform->driver->ops->prepare) { | |
579 | ret = platform->driver->ops->prepare(substream); | |
580 | if (ret < 0) { | |
103d84a3 LG |
581 | dev_err(platform->dev, "ASoC: platform prepare error:" |
582 | " %d\n", ret); | |
ddee627c LG |
583 | goto out; |
584 | } | |
585 | } | |
586 | ||
c5914b0a | 587 | if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) { |
ddee627c LG |
588 | ret = codec_dai->driver->ops->prepare(substream, codec_dai); |
589 | if (ret < 0) { | |
103d84a3 | 590 | dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n", |
25bfe662 | 591 | ret); |
ddee627c LG |
592 | goto out; |
593 | } | |
594 | } | |
595 | ||
c5914b0a | 596 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) { |
ddee627c LG |
597 | ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); |
598 | if (ret < 0) { | |
103d84a3 | 599 | dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n", |
25bfe662 | 600 | ret); |
ddee627c LG |
601 | goto out; |
602 | } | |
603 | } | |
604 | ||
605 | /* cancel any delayed stream shutdown that is pending */ | |
606 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
9bffb1fb MLC |
607 | rtd->pop_wait) { |
608 | rtd->pop_wait = 0; | |
ddee627c LG |
609 | cancel_delayed_work(&rtd->delayed_work); |
610 | } | |
611 | ||
d9b0951b LG |
612 | snd_soc_dapm_stream_event(rtd, substream->stream, |
613 | SND_SOC_DAPM_STREAM_START); | |
ddee627c | 614 | |
da18396f | 615 | snd_soc_dai_digital_mute(codec_dai, 0, substream->stream); |
ddee627c LG |
616 | |
617 | out: | |
b8c0dab9 | 618 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
619 | return ret; |
620 | } | |
621 | ||
622 | /* | |
623 | * Called by ALSA when the hardware params are set by application. This | |
624 | * function can also be called multiple times and can allocate buffers | |
625 | * (using snd_pcm_lib_* ). It's non-atomic. | |
626 | */ | |
627 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, | |
628 | struct snd_pcm_hw_params *params) | |
629 | { | |
630 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
631 | struct snd_soc_platform *platform = rtd->platform; | |
632 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
633 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
634 | int ret = 0; | |
635 | ||
b8c0dab9 | 636 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c | 637 | |
3635bf09 NC |
638 | ret = soc_pcm_params_symmetry(substream, params); |
639 | if (ret) | |
640 | goto out; | |
641 | ||
ddee627c LG |
642 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { |
643 | ret = rtd->dai_link->ops->hw_params(substream, params); | |
644 | if (ret < 0) { | |
103d84a3 LG |
645 | dev_err(rtd->card->dev, "ASoC: machine hw_params" |
646 | " failed: %d\n", ret); | |
ddee627c LG |
647 | goto out; |
648 | } | |
649 | } | |
650 | ||
c5914b0a | 651 | if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) { |
ddee627c LG |
652 | ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); |
653 | if (ret < 0) { | |
103d84a3 LG |
654 | dev_err(codec_dai->dev, "ASoC: can't set %s hw params:" |
655 | " %d\n", codec_dai->name, ret); | |
ddee627c LG |
656 | goto codec_err; |
657 | } | |
658 | } | |
659 | ||
c5914b0a | 660 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) { |
ddee627c LG |
661 | ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); |
662 | if (ret < 0) { | |
103d84a3 | 663 | dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n", |
25bfe662 | 664 | cpu_dai->name, ret); |
ddee627c LG |
665 | goto interface_err; |
666 | } | |
667 | } | |
668 | ||
669 | if (platform->driver->ops && platform->driver->ops->hw_params) { | |
670 | ret = platform->driver->ops->hw_params(substream, params); | |
671 | if (ret < 0) { | |
103d84a3 | 672 | dev_err(platform->dev, "ASoC: %s hw params failed: %d\n", |
25bfe662 | 673 | platform->name, ret); |
ddee627c LG |
674 | goto platform_err; |
675 | } | |
676 | } | |
677 | ||
3635bf09 | 678 | /* store the parameters for each DAIs */ |
17841020 | 679 | cpu_dai->rate = params_rate(params); |
3635bf09 NC |
680 | cpu_dai->channels = params_channels(params); |
681 | cpu_dai->sample_bits = | |
682 | snd_pcm_format_physical_width(params_format(params)); | |
683 | ||
17841020 | 684 | codec_dai->rate = params_rate(params); |
3635bf09 NC |
685 | codec_dai->channels = params_channels(params); |
686 | codec_dai->sample_bits = | |
687 | snd_pcm_format_physical_width(params_format(params)); | |
ddee627c LG |
688 | |
689 | out: | |
b8c0dab9 | 690 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
691 | return ret; |
692 | ||
693 | platform_err: | |
c5914b0a | 694 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) |
ddee627c LG |
695 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
696 | ||
697 | interface_err: | |
c5914b0a | 698 | if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) |
ddee627c LG |
699 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
700 | ||
701 | codec_err: | |
702 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) | |
703 | rtd->dai_link->ops->hw_free(substream); | |
704 | ||
b8c0dab9 | 705 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
706 | return ret; |
707 | } | |
708 | ||
709 | /* | |
710 | * Frees resources allocated by hw_params, can be called multiple times | |
711 | */ | |
712 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) | |
713 | { | |
714 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
715 | struct snd_soc_platform *platform = rtd->platform; | |
716 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
717 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
7f62b6ee | 718 | bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
ddee627c | 719 | |
b8c0dab9 | 720 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
ddee627c | 721 | |
d3383420 NC |
722 | /* clear the corresponding DAIs parameters when going to be inactive */ |
723 | if (cpu_dai->active == 1) { | |
724 | cpu_dai->rate = 0; | |
725 | cpu_dai->channels = 0; | |
726 | cpu_dai->sample_bits = 0; | |
727 | } | |
728 | ||
729 | if (codec_dai->active == 1) { | |
730 | codec_dai->rate = 0; | |
731 | codec_dai->channels = 0; | |
732 | codec_dai->sample_bits = 0; | |
733 | } | |
734 | ||
ddee627c | 735 | /* apply codec digital mute */ |
7f62b6ee NC |
736 | if ((playback && codec_dai->playback_active == 1) || |
737 | (!playback && codec_dai->capture_active == 1)) | |
da18396f | 738 | snd_soc_dai_digital_mute(codec_dai, 1, substream->stream); |
ddee627c LG |
739 | |
740 | /* free any machine hw params */ | |
741 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) | |
742 | rtd->dai_link->ops->hw_free(substream); | |
743 | ||
744 | /* free any DMA resources */ | |
745 | if (platform->driver->ops && platform->driver->ops->hw_free) | |
746 | platform->driver->ops->hw_free(substream); | |
747 | ||
748 | /* now free hw params for the DAIs */ | |
c5914b0a | 749 | if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) |
ddee627c LG |
750 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
751 | ||
c5914b0a | 752 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) |
ddee627c LG |
753 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
754 | ||
b8c0dab9 | 755 | mutex_unlock(&rtd->pcm_mutex); |
ddee627c LG |
756 | return 0; |
757 | } | |
758 | ||
759 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
760 | { | |
761 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
762 | struct snd_soc_platform *platform = rtd->platform; | |
763 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
764 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
765 | int ret; | |
766 | ||
c5914b0a | 767 | if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) { |
ddee627c LG |
768 | ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); |
769 | if (ret < 0) | |
770 | return ret; | |
771 | } | |
772 | ||
773 | if (platform->driver->ops && platform->driver->ops->trigger) { | |
774 | ret = platform->driver->ops->trigger(substream, cmd); | |
775 | if (ret < 0) | |
776 | return ret; | |
777 | } | |
778 | ||
c5914b0a | 779 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) { |
ddee627c LG |
780 | ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); |
781 | if (ret < 0) | |
782 | return ret; | |
783 | } | |
784 | return 0; | |
785 | } | |
786 | ||
45c0a188 MB |
787 | static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, |
788 | int cmd) | |
07bf84aa LG |
789 | { |
790 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
791 | struct snd_soc_platform *platform = rtd->platform; | |
792 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
793 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
794 | int ret; | |
795 | ||
c5914b0a MB |
796 | if (codec_dai->driver->ops && |
797 | codec_dai->driver->ops->bespoke_trigger) { | |
07bf84aa LG |
798 | ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai); |
799 | if (ret < 0) | |
800 | return ret; | |
801 | } | |
802 | ||
dcf0fa27 | 803 | if (platform->driver->bespoke_trigger) { |
07bf84aa LG |
804 | ret = platform->driver->bespoke_trigger(substream, cmd); |
805 | if (ret < 0) | |
806 | return ret; | |
807 | } | |
808 | ||
c5914b0a | 809 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) { |
07bf84aa LG |
810 | ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai); |
811 | if (ret < 0) | |
812 | return ret; | |
813 | } | |
814 | return 0; | |
815 | } | |
ddee627c LG |
816 | /* |
817 | * soc level wrapper for pointer callback | |
818 | * If cpu_dai, codec_dai, platform driver has the delay callback, than | |
819 | * the runtime->delay will be updated accordingly. | |
820 | */ | |
821 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) | |
822 | { | |
823 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
824 | struct snd_soc_platform *platform = rtd->platform; | |
825 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
826 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
827 | struct snd_pcm_runtime *runtime = substream->runtime; | |
828 | snd_pcm_uframes_t offset = 0; | |
829 | snd_pcm_sframes_t delay = 0; | |
830 | ||
831 | if (platform->driver->ops && platform->driver->ops->pointer) | |
832 | offset = platform->driver->ops->pointer(substream); | |
833 | ||
c5914b0a | 834 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay) |
ddee627c LG |
835 | delay += cpu_dai->driver->ops->delay(substream, cpu_dai); |
836 | ||
c5914b0a | 837 | if (codec_dai->driver->ops && codec_dai->driver->ops->delay) |
ddee627c LG |
838 | delay += codec_dai->driver->ops->delay(substream, codec_dai); |
839 | ||
840 | if (platform->driver->delay) | |
841 | delay += platform->driver->delay(substream, codec_dai); | |
842 | ||
843 | runtime->delay = delay; | |
844 | ||
845 | return offset; | |
846 | } | |
847 | ||
01d7584c LG |
848 | /* connect a FE and BE */ |
849 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, | |
850 | struct snd_soc_pcm_runtime *be, int stream) | |
851 | { | |
852 | struct snd_soc_dpcm *dpcm; | |
853 | ||
854 | /* only add new dpcms */ | |
855 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
856 | if (dpcm->be == be && dpcm->fe == fe) | |
857 | return 0; | |
858 | } | |
859 | ||
860 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); | |
861 | if (!dpcm) | |
862 | return -ENOMEM; | |
863 | ||
864 | dpcm->be = be; | |
865 | dpcm->fe = fe; | |
866 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; | |
867 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; | |
868 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); | |
869 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); | |
870 | ||
7cc302d2 | 871 | dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", |
01d7584c LG |
872 | stream ? "capture" : "playback", fe->dai_link->name, |
873 | stream ? "<-" : "->", be->dai_link->name); | |
874 | ||
f86dcef8 LG |
875 | #ifdef CONFIG_DEBUG_FS |
876 | dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, | |
877 | fe->debugfs_dpcm_root, &dpcm->state); | |
878 | #endif | |
01d7584c LG |
879 | return 1; |
880 | } | |
881 | ||
882 | /* reparent a BE onto another FE */ | |
883 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, | |
884 | struct snd_soc_pcm_runtime *be, int stream) | |
885 | { | |
886 | struct snd_soc_dpcm *dpcm; | |
887 | struct snd_pcm_substream *fe_substream, *be_substream; | |
888 | ||
889 | /* reparent if BE is connected to other FEs */ | |
890 | if (!be->dpcm[stream].users) | |
891 | return; | |
892 | ||
893 | be_substream = snd_soc_dpcm_get_substream(be, stream); | |
894 | ||
895 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { | |
896 | if (dpcm->fe == fe) | |
897 | continue; | |
898 | ||
7cc302d2 | 899 | dev_dbg(fe->dev, "reparent %s path %s %s %s\n", |
01d7584c LG |
900 | stream ? "capture" : "playback", |
901 | dpcm->fe->dai_link->name, | |
902 | stream ? "<-" : "->", dpcm->be->dai_link->name); | |
903 | ||
904 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); | |
905 | be_substream->runtime = fe_substream->runtime; | |
906 | break; | |
907 | } | |
908 | } | |
909 | ||
910 | /* disconnect a BE and FE */ | |
23607025 | 911 | void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
912 | { |
913 | struct snd_soc_dpcm *dpcm, *d; | |
914 | ||
915 | list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) { | |
103d84a3 | 916 | dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", |
01d7584c LG |
917 | stream ? "capture" : "playback", |
918 | dpcm->be->dai_link->name); | |
919 | ||
920 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) | |
921 | continue; | |
922 | ||
7cc302d2 | 923 | dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", |
01d7584c LG |
924 | stream ? "capture" : "playback", fe->dai_link->name, |
925 | stream ? "<-" : "->", dpcm->be->dai_link->name); | |
926 | ||
927 | /* BEs still alive need new FE */ | |
928 | dpcm_be_reparent(fe, dpcm->be, stream); | |
929 | ||
f86dcef8 LG |
930 | #ifdef CONFIG_DEBUG_FS |
931 | debugfs_remove(dpcm->debugfs_state); | |
932 | #endif | |
01d7584c LG |
933 | list_del(&dpcm->list_be); |
934 | list_del(&dpcm->list_fe); | |
935 | kfree(dpcm); | |
936 | } | |
937 | } | |
938 | ||
939 | /* get BE for DAI widget and stream */ | |
940 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, | |
941 | struct snd_soc_dapm_widget *widget, int stream) | |
942 | { | |
943 | struct snd_soc_pcm_runtime *be; | |
944 | int i; | |
945 | ||
946 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
947 | for (i = 0; i < card->num_links; i++) { | |
948 | be = &card->rtd[i]; | |
949 | ||
35ea0655 LG |
950 | if (!be->dai_link->no_pcm) |
951 | continue; | |
952 | ||
01d7584c LG |
953 | if (be->cpu_dai->playback_widget == widget || |
954 | be->codec_dai->playback_widget == widget) | |
955 | return be; | |
956 | } | |
957 | } else { | |
958 | ||
959 | for (i = 0; i < card->num_links; i++) { | |
960 | be = &card->rtd[i]; | |
961 | ||
35ea0655 LG |
962 | if (!be->dai_link->no_pcm) |
963 | continue; | |
964 | ||
01d7584c LG |
965 | if (be->cpu_dai->capture_widget == widget || |
966 | be->codec_dai->capture_widget == widget) | |
967 | return be; | |
968 | } | |
969 | } | |
970 | ||
103d84a3 | 971 | dev_err(card->dev, "ASoC: can't get %s BE for %s\n", |
01d7584c LG |
972 | stream ? "capture" : "playback", widget->name); |
973 | return NULL; | |
974 | } | |
975 | ||
976 | static inline struct snd_soc_dapm_widget * | |
977 | rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream) | |
978 | { | |
979 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
980 | return rtd->cpu_dai->playback_widget; | |
981 | else | |
982 | return rtd->cpu_dai->capture_widget; | |
983 | } | |
984 | ||
985 | static inline struct snd_soc_dapm_widget * | |
986 | rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream) | |
987 | { | |
988 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
989 | return rtd->codec_dai->playback_widget; | |
990 | else | |
991 | return rtd->codec_dai->capture_widget; | |
992 | } | |
993 | ||
994 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, | |
995 | struct snd_soc_dapm_widget *widget) | |
996 | { | |
997 | int i; | |
998 | ||
999 | for (i = 0; i < list->num_widgets; i++) { | |
1000 | if (widget == list->widgets[i]) | |
1001 | return 1; | |
1002 | } | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
23607025 | 1007 | int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
01d7584c LG |
1008 | int stream, struct snd_soc_dapm_widget_list **list_) |
1009 | { | |
1010 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; | |
1011 | struct snd_soc_dapm_widget_list *list; | |
1012 | int paths; | |
1013 | ||
1014 | list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) + | |
1015 | sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL); | |
1016 | if (list == NULL) | |
1017 | return -ENOMEM; | |
1018 | ||
1019 | /* get number of valid DAI paths and their widgets */ | |
1020 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list); | |
1021 | ||
103d84a3 | 1022 | dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, |
01d7584c LG |
1023 | stream ? "capture" : "playback"); |
1024 | ||
1025 | *list_ = list; | |
1026 | return paths; | |
1027 | } | |
1028 | ||
01d7584c LG |
1029 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
1030 | struct snd_soc_dapm_widget_list **list_) | |
1031 | { | |
1032 | struct snd_soc_dpcm *dpcm; | |
1033 | struct snd_soc_dapm_widget_list *list = *list_; | |
1034 | struct snd_soc_dapm_widget *widget; | |
1035 | int prune = 0; | |
1036 | ||
1037 | /* Destroy any old FE <--> BE connections */ | |
1038 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1039 | ||
1040 | /* is there a valid CPU DAI widget for this BE */ | |
1041 | widget = rtd_get_cpu_widget(dpcm->be, stream); | |
1042 | ||
1043 | /* prune the BE if it's no longer in our active list */ | |
1044 | if (widget && widget_in_list(list, widget)) | |
1045 | continue; | |
1046 | ||
1047 | /* is there a valid CODEC DAI widget for this BE */ | |
1048 | widget = rtd_get_codec_widget(dpcm->be, stream); | |
1049 | ||
1050 | /* prune the BE if it's no longer in our active list */ | |
1051 | if (widget && widget_in_list(list, widget)) | |
1052 | continue; | |
1053 | ||
103d84a3 | 1054 | dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", |
01d7584c LG |
1055 | stream ? "capture" : "playback", |
1056 | dpcm->be->dai_link->name, fe->dai_link->name); | |
1057 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
1058 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1059 | prune++; | |
1060 | } | |
1061 | ||
103d84a3 | 1062 | dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); |
01d7584c LG |
1063 | return prune; |
1064 | } | |
1065 | ||
1066 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, | |
1067 | struct snd_soc_dapm_widget_list **list_) | |
1068 | { | |
1069 | struct snd_soc_card *card = fe->card; | |
1070 | struct snd_soc_dapm_widget_list *list = *list_; | |
1071 | struct snd_soc_pcm_runtime *be; | |
1072 | int i, new = 0, err; | |
1073 | ||
1074 | /* Create any new FE <--> BE connections */ | |
1075 | for (i = 0; i < list->num_widgets; i++) { | |
1076 | ||
4616274d MB |
1077 | switch (list->widgets[i]->id) { |
1078 | case snd_soc_dapm_dai_in: | |
1079 | case snd_soc_dapm_dai_out: | |
1080 | break; | |
1081 | default: | |
01d7584c | 1082 | continue; |
4616274d | 1083 | } |
01d7584c LG |
1084 | |
1085 | /* is there a valid BE rtd for this widget */ | |
1086 | be = dpcm_get_be(card, list->widgets[i], stream); | |
1087 | if (!be) { | |
103d84a3 | 1088 | dev_err(fe->dev, "ASoC: no BE found for %s\n", |
01d7584c LG |
1089 | list->widgets[i]->name); |
1090 | continue; | |
1091 | } | |
1092 | ||
1093 | /* make sure BE is a real BE */ | |
1094 | if (!be->dai_link->no_pcm) | |
1095 | continue; | |
1096 | ||
1097 | /* don't connect if FE is not running */ | |
23607025 | 1098 | if (!fe->dpcm[stream].runtime && !fe->fe_compr) |
01d7584c LG |
1099 | continue; |
1100 | ||
1101 | /* newly connected FE and BE */ | |
1102 | err = dpcm_be_connect(fe, be, stream); | |
1103 | if (err < 0) { | |
103d84a3 | 1104 | dev_err(fe->dev, "ASoC: can't connect %s\n", |
01d7584c LG |
1105 | list->widgets[i]->name); |
1106 | break; | |
1107 | } else if (err == 0) /* already connected */ | |
1108 | continue; | |
1109 | ||
1110 | /* new */ | |
1111 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1112 | new++; | |
1113 | } | |
1114 | ||
103d84a3 | 1115 | dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); |
01d7584c LG |
1116 | return new; |
1117 | } | |
1118 | ||
1119 | /* | |
1120 | * Find the corresponding BE DAIs that source or sink audio to this | |
1121 | * FE substream. | |
1122 | */ | |
23607025 | 1123 | int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
01d7584c LG |
1124 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
1125 | { | |
1126 | if (new) | |
1127 | return dpcm_add_paths(fe, stream, list); | |
1128 | else | |
1129 | return dpcm_prune_paths(fe, stream, list); | |
1130 | } | |
1131 | ||
23607025 | 1132 | void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1133 | { |
1134 | struct snd_soc_dpcm *dpcm; | |
1135 | ||
1136 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) | |
1137 | dpcm->be->dpcm[stream].runtime_update = | |
1138 | SND_SOC_DPCM_UPDATE_NO; | |
1139 | } | |
1140 | ||
1141 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, | |
1142 | int stream) | |
1143 | { | |
1144 | struct snd_soc_dpcm *dpcm; | |
1145 | ||
1146 | /* disable any enabled and non active backends */ | |
1147 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1148 | ||
1149 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1150 | struct snd_pcm_substream *be_substream = | |
1151 | snd_soc_dpcm_get_substream(be, stream); | |
1152 | ||
1153 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1154 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
01d7584c LG |
1155 | stream ? "capture" : "playback", |
1156 | be->dpcm[stream].state); | |
1157 | ||
1158 | if (--be->dpcm[stream].users != 0) | |
1159 | continue; | |
1160 | ||
1161 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) | |
1162 | continue; | |
1163 | ||
1164 | soc_pcm_close(be_substream); | |
1165 | be_substream->runtime = NULL; | |
1166 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1167 | } | |
1168 | } | |
1169 | ||
23607025 | 1170 | int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1171 | { |
1172 | struct snd_soc_dpcm *dpcm; | |
1173 | int err, count = 0; | |
1174 | ||
1175 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ | |
1176 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1177 | ||
1178 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1179 | struct snd_pcm_substream *be_substream = | |
1180 | snd_soc_dpcm_get_substream(be, stream); | |
1181 | ||
2062b4c5 RKAL |
1182 | if (!be_substream) { |
1183 | dev_err(be->dev, "ASoC: no backend %s stream\n", | |
1184 | stream ? "capture" : "playback"); | |
1185 | continue; | |
1186 | } | |
1187 | ||
01d7584c LG |
1188 | /* is this op for this BE ? */ |
1189 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1190 | continue; | |
1191 | ||
1192 | /* first time the dpcm is open ? */ | |
1193 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) | |
103d84a3 | 1194 | dev_err(be->dev, "ASoC: too many users %s at open %d\n", |
01d7584c LG |
1195 | stream ? "capture" : "playback", |
1196 | be->dpcm[stream].state); | |
1197 | ||
1198 | if (be->dpcm[stream].users++ != 0) | |
1199 | continue; | |
1200 | ||
1201 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && | |
1202 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) | |
1203 | continue; | |
1204 | ||
2062b4c5 RKAL |
1205 | dev_dbg(be->dev, "ASoC: open %s BE %s\n", |
1206 | stream ? "capture" : "playback", be->dai_link->name); | |
01d7584c LG |
1207 | |
1208 | be_substream->runtime = be->dpcm[stream].runtime; | |
1209 | err = soc_pcm_open(be_substream); | |
1210 | if (err < 0) { | |
103d84a3 | 1211 | dev_err(be->dev, "ASoC: BE open failed %d\n", err); |
01d7584c LG |
1212 | be->dpcm[stream].users--; |
1213 | if (be->dpcm[stream].users < 0) | |
103d84a3 | 1214 | dev_err(be->dev, "ASoC: no users %s at unwind %d\n", |
01d7584c LG |
1215 | stream ? "capture" : "playback", |
1216 | be->dpcm[stream].state); | |
1217 | ||
1218 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1219 | goto unwind; | |
1220 | } | |
1221 | ||
1222 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; | |
1223 | count++; | |
1224 | } | |
1225 | ||
1226 | return count; | |
1227 | ||
1228 | unwind: | |
1229 | /* disable any enabled and non active backends */ | |
1230 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1231 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1232 | struct snd_pcm_substream *be_substream = | |
1233 | snd_soc_dpcm_get_substream(be, stream); | |
1234 | ||
1235 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1236 | continue; | |
1237 | ||
1238 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1239 | dev_err(be->dev, "ASoC: no users %s at close %d\n", |
01d7584c LG |
1240 | stream ? "capture" : "playback", |
1241 | be->dpcm[stream].state); | |
1242 | ||
1243 | if (--be->dpcm[stream].users != 0) | |
1244 | continue; | |
1245 | ||
1246 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) | |
1247 | continue; | |
1248 | ||
1249 | soc_pcm_close(be_substream); | |
1250 | be_substream->runtime = NULL; | |
1251 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1252 | } | |
1253 | ||
1254 | return err; | |
1255 | } | |
1256 | ||
08ae9b45 LPC |
1257 | static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
1258 | struct snd_soc_pcm_stream *stream) | |
1259 | { | |
1260 | runtime->hw.rate_min = stream->rate_min; | |
1261 | runtime->hw.rate_max = stream->rate_max; | |
1262 | runtime->hw.channels_min = stream->channels_min; | |
1263 | runtime->hw.channels_max = stream->channels_max; | |
002220a9 LPC |
1264 | if (runtime->hw.formats) |
1265 | runtime->hw.formats &= stream->formats; | |
1266 | else | |
1267 | runtime->hw.formats = stream->formats; | |
08ae9b45 LPC |
1268 | runtime->hw.rates = stream->rates; |
1269 | } | |
1270 | ||
45c0a188 | 1271 | static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
01d7584c LG |
1272 | { |
1273 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1274 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
1275 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
1276 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; | |
1277 | ||
08ae9b45 LPC |
1278 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
1279 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); | |
1280 | else | |
1281 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); | |
01d7584c LG |
1282 | } |
1283 | ||
1284 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) | |
1285 | { | |
1286 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
1287 | struct snd_pcm_runtime *runtime = fe_substream->runtime; | |
1288 | int stream = fe_substream->stream, ret = 0; | |
1289 | ||
1290 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1291 | ||
1292 | ret = dpcm_be_dai_startup(fe, fe_substream->stream); | |
1293 | if (ret < 0) { | |
103d84a3 | 1294 | dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); |
01d7584c LG |
1295 | goto be_err; |
1296 | } | |
1297 | ||
103d84a3 | 1298 | dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); |
01d7584c LG |
1299 | |
1300 | /* start the DAI frontend */ | |
1301 | ret = soc_pcm_open(fe_substream); | |
1302 | if (ret < 0) { | |
103d84a3 | 1303 | dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); |
01d7584c LG |
1304 | goto unwind; |
1305 | } | |
1306 | ||
1307 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; | |
1308 | ||
1309 | dpcm_set_fe_runtime(fe_substream); | |
1310 | snd_pcm_limit_hw_rates(runtime); | |
1311 | ||
1312 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1313 | return 0; | |
1314 | ||
1315 | unwind: | |
1316 | dpcm_be_dai_startup_unwind(fe, fe_substream->stream); | |
1317 | be_err: | |
1318 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1319 | return ret; | |
1320 | } | |
1321 | ||
23607025 | 1322 | int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1323 | { |
1324 | struct snd_soc_dpcm *dpcm; | |
1325 | ||
1326 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ | |
1327 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1328 | ||
1329 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1330 | struct snd_pcm_substream *be_substream = | |
1331 | snd_soc_dpcm_get_substream(be, stream); | |
1332 | ||
1333 | /* is this op for this BE ? */ | |
1334 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1335 | continue; | |
1336 | ||
1337 | if (be->dpcm[stream].users == 0) | |
103d84a3 | 1338 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
01d7584c LG |
1339 | stream ? "capture" : "playback", |
1340 | be->dpcm[stream].state); | |
1341 | ||
1342 | if (--be->dpcm[stream].users != 0) | |
1343 | continue; | |
1344 | ||
1345 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
1346 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) | |
1347 | continue; | |
1348 | ||
103d84a3 | 1349 | dev_dbg(be->dev, "ASoC: close BE %s\n", |
01d7584c LG |
1350 | dpcm->fe->dai_link->name); |
1351 | ||
1352 | soc_pcm_close(be_substream); | |
1353 | be_substream->runtime = NULL; | |
1354 | ||
1355 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1356 | } | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) | |
1361 | { | |
1362 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1363 | int stream = substream->stream; | |
1364 | ||
1365 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1366 | ||
1367 | /* shutdown the BEs */ | |
1368 | dpcm_be_dai_shutdown(fe, substream->stream); | |
1369 | ||
103d84a3 | 1370 | dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); |
01d7584c LG |
1371 | |
1372 | /* now shutdown the frontend */ | |
1373 | soc_pcm_close(substream); | |
1374 | ||
1375 | /* run the stream event for each BE */ | |
1376 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); | |
1377 | ||
1378 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
1379 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1380 | return 0; | |
1381 | } | |
1382 | ||
23607025 | 1383 | int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1384 | { |
1385 | struct snd_soc_dpcm *dpcm; | |
1386 | ||
1387 | /* only hw_params backends that are either sinks or sources | |
1388 | * to this frontend DAI */ | |
1389 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1390 | ||
1391 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1392 | struct snd_pcm_substream *be_substream = | |
1393 | snd_soc_dpcm_get_substream(be, stream); | |
1394 | ||
1395 | /* is this op for this BE ? */ | |
1396 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1397 | continue; | |
1398 | ||
1399 | /* only free hw when no longer used - check all FEs */ | |
1400 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1401 | continue; | |
1402 | ||
1403 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
1404 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && | |
1405 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
08b27848 | 1406 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && |
01d7584c LG |
1407 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
1408 | continue; | |
1409 | ||
103d84a3 | 1410 | dev_dbg(be->dev, "ASoC: hw_free BE %s\n", |
01d7584c LG |
1411 | dpcm->fe->dai_link->name); |
1412 | ||
1413 | soc_pcm_hw_free(be_substream); | |
1414 | ||
1415 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; | |
1416 | } | |
1417 | ||
1418 | return 0; | |
1419 | } | |
1420 | ||
45c0a188 | 1421 | static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
01d7584c LG |
1422 | { |
1423 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1424 | int err, stream = substream->stream; | |
1425 | ||
1426 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
1427 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1428 | ||
103d84a3 | 1429 | dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); |
01d7584c LG |
1430 | |
1431 | /* call hw_free on the frontend */ | |
1432 | err = soc_pcm_hw_free(substream); | |
1433 | if (err < 0) | |
103d84a3 | 1434 | dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", |
01d7584c LG |
1435 | fe->dai_link->name); |
1436 | ||
1437 | /* only hw_params backends that are either sinks or sources | |
1438 | * to this frontend DAI */ | |
1439 | err = dpcm_be_dai_hw_free(fe, stream); | |
1440 | ||
1441 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; | |
1442 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1443 | ||
1444 | mutex_unlock(&fe->card->mutex); | |
1445 | return 0; | |
1446 | } | |
1447 | ||
23607025 | 1448 | int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1449 | { |
1450 | struct snd_soc_dpcm *dpcm; | |
1451 | int ret; | |
1452 | ||
1453 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1454 | ||
1455 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1456 | struct snd_pcm_substream *be_substream = | |
1457 | snd_soc_dpcm_get_substream(be, stream); | |
1458 | ||
1459 | /* is this op for this BE ? */ | |
1460 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1461 | continue; | |
1462 | ||
1463 | /* only allow hw_params() if no connected FEs are running */ | |
1464 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) | |
1465 | continue; | |
1466 | ||
1467 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && | |
1468 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
1469 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) | |
1470 | continue; | |
1471 | ||
103d84a3 | 1472 | dev_dbg(be->dev, "ASoC: hw_params BE %s\n", |
01d7584c LG |
1473 | dpcm->fe->dai_link->name); |
1474 | ||
1475 | /* copy params for each dpcm */ | |
1476 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, | |
1477 | sizeof(struct snd_pcm_hw_params)); | |
1478 | ||
1479 | /* perform any hw_params fixups */ | |
1480 | if (be->dai_link->be_hw_params_fixup) { | |
1481 | ret = be->dai_link->be_hw_params_fixup(be, | |
1482 | &dpcm->hw_params); | |
1483 | if (ret < 0) { | |
1484 | dev_err(be->dev, | |
103d84a3 | 1485 | "ASoC: hw_params BE fixup failed %d\n", |
01d7584c LG |
1486 | ret); |
1487 | goto unwind; | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); | |
1492 | if (ret < 0) { | |
1493 | dev_err(dpcm->be->dev, | |
103d84a3 | 1494 | "ASoC: hw_params BE failed %d\n", ret); |
01d7584c LG |
1495 | goto unwind; |
1496 | } | |
1497 | ||
1498 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; | |
1499 | } | |
1500 | return 0; | |
1501 | ||
1502 | unwind: | |
1503 | /* disable any enabled and non active backends */ | |
1504 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1505 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1506 | struct snd_pcm_substream *be_substream = | |
1507 | snd_soc_dpcm_get_substream(be, stream); | |
1508 | ||
1509 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1510 | continue; | |
1511 | ||
1512 | /* only allow hw_free() if no connected FEs are running */ | |
1513 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1514 | continue; | |
1515 | ||
1516 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && | |
1517 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
1518 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && | |
1519 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) | |
1520 | continue; | |
1521 | ||
1522 | soc_pcm_hw_free(be_substream); | |
1523 | } | |
1524 | ||
1525 | return ret; | |
1526 | } | |
1527 | ||
45c0a188 MB |
1528 | static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
1529 | struct snd_pcm_hw_params *params) | |
01d7584c LG |
1530 | { |
1531 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1532 | int ret, stream = substream->stream; | |
1533 | ||
1534 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
1535 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1536 | ||
1537 | memcpy(&fe->dpcm[substream->stream].hw_params, params, | |
1538 | sizeof(struct snd_pcm_hw_params)); | |
1539 | ret = dpcm_be_dai_hw_params(fe, substream->stream); | |
1540 | if (ret < 0) { | |
103d84a3 | 1541 | dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); |
01d7584c LG |
1542 | goto out; |
1543 | } | |
1544 | ||
103d84a3 | 1545 | dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", |
01d7584c LG |
1546 | fe->dai_link->name, params_rate(params), |
1547 | params_channels(params), params_format(params)); | |
1548 | ||
1549 | /* call hw_params on the frontend */ | |
1550 | ret = soc_pcm_hw_params(substream, params); | |
1551 | if (ret < 0) { | |
103d84a3 | 1552 | dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); |
01d7584c LG |
1553 | dpcm_be_dai_hw_free(fe, stream); |
1554 | } else | |
1555 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; | |
1556 | ||
1557 | out: | |
1558 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1559 | mutex_unlock(&fe->card->mutex); | |
1560 | return ret; | |
1561 | } | |
1562 | ||
1563 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, | |
1564 | struct snd_pcm_substream *substream, int cmd) | |
1565 | { | |
1566 | int ret; | |
1567 | ||
103d84a3 | 1568 | dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", |
01d7584c LG |
1569 | dpcm->fe->dai_link->name, cmd); |
1570 | ||
1571 | ret = soc_pcm_trigger(substream, cmd); | |
1572 | if (ret < 0) | |
103d84a3 | 1573 | dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); |
01d7584c LG |
1574 | |
1575 | return ret; | |
1576 | } | |
1577 | ||
23607025 | 1578 | int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, |
45c0a188 | 1579 | int cmd) |
01d7584c LG |
1580 | { |
1581 | struct snd_soc_dpcm *dpcm; | |
1582 | int ret = 0; | |
1583 | ||
1584 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1585 | ||
1586 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1587 | struct snd_pcm_substream *be_substream = | |
1588 | snd_soc_dpcm_get_substream(be, stream); | |
1589 | ||
1590 | /* is this op for this BE ? */ | |
1591 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1592 | continue; | |
1593 | ||
1594 | switch (cmd) { | |
1595 | case SNDRV_PCM_TRIGGER_START: | |
1596 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && | |
1597 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) | |
1598 | continue; | |
1599 | ||
1600 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1601 | if (ret) | |
1602 | return ret; | |
1603 | ||
1604 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
1605 | break; | |
1606 | case SNDRV_PCM_TRIGGER_RESUME: | |
1607 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) | |
1608 | continue; | |
1609 | ||
1610 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1611 | if (ret) | |
1612 | return ret; | |
1613 | ||
1614 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
1615 | break; | |
1616 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1617 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) | |
1618 | continue; | |
1619 | ||
1620 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1621 | if (ret) | |
1622 | return ret; | |
1623 | ||
1624 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
1625 | break; | |
1626 | case SNDRV_PCM_TRIGGER_STOP: | |
1627 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
1628 | continue; | |
1629 | ||
1630 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1631 | continue; | |
1632 | ||
1633 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1634 | if (ret) | |
1635 | return ret; | |
1636 | ||
1637 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; | |
1638 | break; | |
1639 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
1640 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) | |
1641 | continue; | |
1642 | ||
1643 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1644 | continue; | |
1645 | ||
1646 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1647 | if (ret) | |
1648 | return ret; | |
1649 | ||
1650 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; | |
1651 | break; | |
1652 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1653 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
1654 | continue; | |
1655 | ||
1656 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) | |
1657 | continue; | |
1658 | ||
1659 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); | |
1660 | if (ret) | |
1661 | return ret; | |
1662 | ||
1663 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; | |
1664 | break; | |
1665 | } | |
1666 | } | |
1667 | ||
1668 | return ret; | |
1669 | } | |
1670 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); | |
1671 | ||
45c0a188 | 1672 | static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
01d7584c LG |
1673 | { |
1674 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1675 | int stream = substream->stream, ret; | |
1676 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; | |
1677 | ||
1678 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1679 | ||
1680 | switch (trigger) { | |
1681 | case SND_SOC_DPCM_TRIGGER_PRE: | |
1682 | /* call trigger on the frontend before the backend. */ | |
1683 | ||
103d84a3 | 1684 | dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", |
01d7584c LG |
1685 | fe->dai_link->name, cmd); |
1686 | ||
1687 | ret = soc_pcm_trigger(substream, cmd); | |
1688 | if (ret < 0) { | |
103d84a3 | 1689 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
01d7584c LG |
1690 | goto out; |
1691 | } | |
1692 | ||
1693 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); | |
1694 | break; | |
1695 | case SND_SOC_DPCM_TRIGGER_POST: | |
1696 | /* call trigger on the frontend after the backend. */ | |
1697 | ||
1698 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); | |
1699 | if (ret < 0) { | |
103d84a3 | 1700 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
01d7584c LG |
1701 | goto out; |
1702 | } | |
1703 | ||
103d84a3 | 1704 | dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", |
01d7584c LG |
1705 | fe->dai_link->name, cmd); |
1706 | ||
1707 | ret = soc_pcm_trigger(substream, cmd); | |
1708 | break; | |
07bf84aa LG |
1709 | case SND_SOC_DPCM_TRIGGER_BESPOKE: |
1710 | /* bespoke trigger() - handles both FE and BEs */ | |
1711 | ||
103d84a3 | 1712 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", |
07bf84aa LG |
1713 | fe->dai_link->name, cmd); |
1714 | ||
1715 | ret = soc_pcm_bespoke_trigger(substream, cmd); | |
1716 | if (ret < 0) { | |
103d84a3 | 1717 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
07bf84aa LG |
1718 | goto out; |
1719 | } | |
1720 | break; | |
01d7584c | 1721 | default: |
103d84a3 | 1722 | dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, |
01d7584c LG |
1723 | fe->dai_link->name); |
1724 | ret = -EINVAL; | |
1725 | goto out; | |
1726 | } | |
1727 | ||
1728 | switch (cmd) { | |
1729 | case SNDRV_PCM_TRIGGER_START: | |
1730 | case SNDRV_PCM_TRIGGER_RESUME: | |
1731 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1732 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
1733 | break; | |
1734 | case SNDRV_PCM_TRIGGER_STOP: | |
1735 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
1736 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1737 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; | |
1738 | break; | |
1739 | } | |
1740 | ||
1741 | out: | |
1742 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1743 | return ret; | |
1744 | } | |
1745 | ||
23607025 | 1746 | int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) |
01d7584c LG |
1747 | { |
1748 | struct snd_soc_dpcm *dpcm; | |
1749 | int ret = 0; | |
1750 | ||
1751 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1752 | ||
1753 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1754 | struct snd_pcm_substream *be_substream = | |
1755 | snd_soc_dpcm_get_substream(be, stream); | |
1756 | ||
1757 | /* is this op for this BE ? */ | |
1758 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) | |
1759 | continue; | |
1760 | ||
1761 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && | |
1762 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) | |
1763 | continue; | |
1764 | ||
103d84a3 | 1765 | dev_dbg(be->dev, "ASoC: prepare BE %s\n", |
01d7584c LG |
1766 | dpcm->fe->dai_link->name); |
1767 | ||
1768 | ret = soc_pcm_prepare(be_substream); | |
1769 | if (ret < 0) { | |
103d84a3 | 1770 | dev_err(be->dev, "ASoC: backend prepare failed %d\n", |
01d7584c LG |
1771 | ret); |
1772 | break; | |
1773 | } | |
1774 | ||
1775 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; | |
1776 | } | |
1777 | return ret; | |
1778 | } | |
1779 | ||
45c0a188 | 1780 | static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
01d7584c LG |
1781 | { |
1782 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
1783 | int stream = substream->stream, ret = 0; | |
1784 | ||
1785 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
1786 | ||
103d84a3 | 1787 | dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); |
01d7584c LG |
1788 | |
1789 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
1790 | ||
1791 | /* there is no point preparing this FE if there are no BEs */ | |
1792 | if (list_empty(&fe->dpcm[stream].be_clients)) { | |
103d84a3 | 1793 | dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", |
01d7584c LG |
1794 | fe->dai_link->name); |
1795 | ret = -EINVAL; | |
1796 | goto out; | |
1797 | } | |
1798 | ||
1799 | ret = dpcm_be_dai_prepare(fe, substream->stream); | |
1800 | if (ret < 0) | |
1801 | goto out; | |
1802 | ||
1803 | /* call prepare on the frontend */ | |
1804 | ret = soc_pcm_prepare(substream); | |
1805 | if (ret < 0) { | |
103d84a3 | 1806 | dev_err(fe->dev,"ASoC: prepare FE %s failed\n", |
01d7584c LG |
1807 | fe->dai_link->name); |
1808 | goto out; | |
1809 | } | |
1810 | ||
1811 | /* run the stream event for each BE */ | |
1812 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); | |
1813 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; | |
1814 | ||
1815 | out: | |
1816 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
1817 | mutex_unlock(&fe->card->mutex); | |
1818 | ||
1819 | return ret; | |
1820 | } | |
1821 | ||
be3f3f2c LG |
1822 | static int soc_pcm_ioctl(struct snd_pcm_substream *substream, |
1823 | unsigned int cmd, void *arg) | |
1824 | { | |
1825 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
1826 | struct snd_soc_platform *platform = rtd->platform; | |
1827 | ||
c5914b0a | 1828 | if (platform->driver->ops && platform->driver->ops->ioctl) |
be3f3f2c LG |
1829 | return platform->driver->ops->ioctl(substream, cmd, arg); |
1830 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
1831 | } | |
1832 | ||
618dae11 LG |
1833 | static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
1834 | { | |
07bf84aa LG |
1835 | struct snd_pcm_substream *substream = |
1836 | snd_soc_dpcm_get_substream(fe, stream); | |
1837 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; | |
618dae11 LG |
1838 | int err; |
1839 | ||
103d84a3 | 1840 | dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", |
618dae11 LG |
1841 | stream ? "capture" : "playback", fe->dai_link->name); |
1842 | ||
07bf84aa LG |
1843 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
1844 | /* call bespoke trigger - FE takes care of all BE triggers */ | |
103d84a3 | 1845 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", |
07bf84aa LG |
1846 | fe->dai_link->name); |
1847 | ||
1848 | err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); | |
1849 | if (err < 0) | |
103d84a3 | 1850 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
07bf84aa | 1851 | } else { |
103d84a3 | 1852 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", |
07bf84aa LG |
1853 | fe->dai_link->name); |
1854 | ||
1855 | err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); | |
1856 | if (err < 0) | |
103d84a3 | 1857 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
07bf84aa | 1858 | } |
618dae11 LG |
1859 | |
1860 | err = dpcm_be_dai_hw_free(fe, stream); | |
1861 | if (err < 0) | |
103d84a3 | 1862 | dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); |
618dae11 LG |
1863 | |
1864 | err = dpcm_be_dai_shutdown(fe, stream); | |
1865 | if (err < 0) | |
103d84a3 | 1866 | dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); |
618dae11 LG |
1867 | |
1868 | /* run the stream event for each BE */ | |
1869 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); | |
1870 | ||
1871 | return 0; | |
1872 | } | |
1873 | ||
1874 | static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) | |
1875 | { | |
07bf84aa LG |
1876 | struct snd_pcm_substream *substream = |
1877 | snd_soc_dpcm_get_substream(fe, stream); | |
618dae11 | 1878 | struct snd_soc_dpcm *dpcm; |
07bf84aa | 1879 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
618dae11 LG |
1880 | int ret; |
1881 | ||
103d84a3 | 1882 | dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", |
618dae11 LG |
1883 | stream ? "capture" : "playback", fe->dai_link->name); |
1884 | ||
1885 | /* Only start the BE if the FE is ready */ | |
1886 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || | |
1887 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) | |
1888 | return -EINVAL; | |
1889 | ||
1890 | /* startup must always be called for new BEs */ | |
1891 | ret = dpcm_be_dai_startup(fe, stream); | |
fffc0ca2 | 1892 | if (ret < 0) |
618dae11 | 1893 | goto disconnect; |
618dae11 LG |
1894 | |
1895 | /* keep going if FE state is > open */ | |
1896 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) | |
1897 | return 0; | |
01d7584c | 1898 | |
618dae11 | 1899 | ret = dpcm_be_dai_hw_params(fe, stream); |
fffc0ca2 | 1900 | if (ret < 0) |
618dae11 | 1901 | goto close; |
618dae11 LG |
1902 | |
1903 | /* keep going if FE state is > hw_params */ | |
1904 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) | |
1905 | return 0; | |
1906 | ||
1907 | ||
1908 | ret = dpcm_be_dai_prepare(fe, stream); | |
fffc0ca2 | 1909 | if (ret < 0) |
618dae11 | 1910 | goto hw_free; |
618dae11 LG |
1911 | |
1912 | /* run the stream event for each BE */ | |
1913 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); | |
1914 | ||
1915 | /* keep going if FE state is > prepare */ | |
1916 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || | |
1917 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) | |
1918 | return 0; | |
1919 | ||
07bf84aa LG |
1920 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
1921 | /* call trigger on the frontend - FE takes care of all BE triggers */ | |
103d84a3 | 1922 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", |
07bf84aa | 1923 | fe->dai_link->name); |
618dae11 | 1924 | |
07bf84aa LG |
1925 | ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); |
1926 | if (ret < 0) { | |
103d84a3 | 1927 | dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); |
07bf84aa LG |
1928 | goto hw_free; |
1929 | } | |
1930 | } else { | |
103d84a3 | 1931 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", |
07bf84aa LG |
1932 | fe->dai_link->name); |
1933 | ||
1934 | ret = dpcm_be_dai_trigger(fe, stream, | |
1935 | SNDRV_PCM_TRIGGER_START); | |
1936 | if (ret < 0) { | |
103d84a3 | 1937 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
07bf84aa LG |
1938 | goto hw_free; |
1939 | } | |
618dae11 LG |
1940 | } |
1941 | ||
1942 | return 0; | |
1943 | ||
1944 | hw_free: | |
1945 | dpcm_be_dai_hw_free(fe, stream); | |
1946 | close: | |
1947 | dpcm_be_dai_shutdown(fe, stream); | |
1948 | disconnect: | |
1949 | /* disconnect any non started BEs */ | |
1950 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
1951 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
1952 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) | |
1953 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
1954 | } | |
1955 | ||
1956 | return ret; | |
1957 | } | |
1958 | ||
1959 | static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) | |
1960 | { | |
1961 | int ret; | |
1962 | ||
1963 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1964 | ret = dpcm_run_update_startup(fe, stream); | |
1965 | if (ret < 0) | |
103d84a3 | 1966 | dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); |
618dae11 LG |
1967 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
1968 | ||
1969 | return ret; | |
1970 | } | |
1971 | ||
1972 | static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) | |
1973 | { | |
1974 | int ret; | |
1975 | ||
1976 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; | |
1977 | ret = dpcm_run_update_shutdown(fe, stream); | |
1978 | if (ret < 0) | |
103d84a3 | 1979 | dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); |
618dae11 LG |
1980 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
1981 | ||
1982 | return ret; | |
1983 | } | |
1984 | ||
1985 | /* Called by DAPM mixer/mux changes to update audio routing between PCMs and | |
1986 | * any DAI links. | |
1987 | */ | |
c3f48ae6 | 1988 | int soc_dpcm_runtime_update(struct snd_soc_card *card) |
618dae11 | 1989 | { |
618dae11 LG |
1990 | int i, old, new, paths; |
1991 | ||
618dae11 LG |
1992 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
1993 | for (i = 0; i < card->num_rtd; i++) { | |
1994 | struct snd_soc_dapm_widget_list *list; | |
1995 | struct snd_soc_pcm_runtime *fe = &card->rtd[i]; | |
1996 | ||
1997 | /* make sure link is FE */ | |
1998 | if (!fe->dai_link->dynamic) | |
1999 | continue; | |
2000 | ||
2001 | /* only check active links */ | |
2002 | if (!fe->cpu_dai->active) | |
2003 | continue; | |
2004 | ||
2005 | /* DAPM sync will call this to update DSP paths */ | |
103d84a3 | 2006 | dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n", |
618dae11 LG |
2007 | fe->dai_link->name); |
2008 | ||
2009 | /* skip if FE doesn't have playback capability */ | |
2010 | if (!fe->cpu_dai->driver->playback.channels_min) | |
2011 | goto capture; | |
2012 | ||
2013 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list); | |
2014 | if (paths < 0) { | |
103d84a3 | 2015 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
618dae11 LG |
2016 | fe->dai_link->name, "playback"); |
2017 | mutex_unlock(&card->mutex); | |
2018 | return paths; | |
2019 | } | |
2020 | ||
2021 | /* update any new playback paths */ | |
2022 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1); | |
2023 | if (new) { | |
2024 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2025 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2026 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2027 | } | |
2028 | ||
2029 | /* update any old playback paths */ | |
2030 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0); | |
2031 | if (old) { | |
2032 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2033 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2034 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); | |
2035 | } | |
2036 | ||
2037 | capture: | |
2038 | /* skip if FE doesn't have capture capability */ | |
2039 | if (!fe->cpu_dai->driver->capture.channels_min) | |
2040 | continue; | |
2041 | ||
2042 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list); | |
2043 | if (paths < 0) { | |
103d84a3 | 2044 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
618dae11 LG |
2045 | fe->dai_link->name, "capture"); |
2046 | mutex_unlock(&card->mutex); | |
2047 | return paths; | |
2048 | } | |
2049 | ||
2050 | /* update any new capture paths */ | |
2051 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1); | |
2052 | if (new) { | |
2053 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2054 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2055 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2056 | } | |
2057 | ||
2058 | /* update any old capture paths */ | |
2059 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0); | |
2060 | if (old) { | |
2061 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2062 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2063 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); | |
2064 | } | |
2065 | ||
2066 | dpcm_path_put(&list); | |
2067 | } | |
2068 | ||
2069 | mutex_unlock(&card->mutex); | |
2070 | return 0; | |
2071 | } | |
01d7584c LG |
2072 | int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) |
2073 | { | |
2074 | struct snd_soc_dpcm *dpcm; | |
2075 | struct list_head *clients = | |
2076 | &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients; | |
2077 | ||
2078 | list_for_each_entry(dpcm, clients, list_be) { | |
2079 | ||
2080 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
2081 | struct snd_soc_dai *dai = be->codec_dai; | |
2082 | struct snd_soc_dai_driver *drv = dai->driver; | |
2083 | ||
2084 | if (be->dai_link->ignore_suspend) | |
2085 | continue; | |
2086 | ||
103d84a3 | 2087 | dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name); |
01d7584c | 2088 | |
c5914b0a MB |
2089 | if (drv->ops && drv->ops->digital_mute && dai->playback_active) |
2090 | drv->ops->digital_mute(dai, mute); | |
01d7584c LG |
2091 | } |
2092 | ||
2093 | return 0; | |
2094 | } | |
2095 | ||
45c0a188 | 2096 | static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
01d7584c LG |
2097 | { |
2098 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
2099 | struct snd_soc_dpcm *dpcm; | |
2100 | struct snd_soc_dapm_widget_list *list; | |
2101 | int ret; | |
2102 | int stream = fe_substream->stream; | |
2103 | ||
2104 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2105 | fe->dpcm[stream].runtime = fe_substream->runtime; | |
2106 | ||
2107 | if (dpcm_path_get(fe, stream, &list) <= 0) { | |
103d84a3 | 2108 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
01d7584c | 2109 | fe->dai_link->name, stream ? "capture" : "playback"); |
01d7584c LG |
2110 | } |
2111 | ||
2112 | /* calculate valid and active FE <-> BE dpcms */ | |
2113 | dpcm_process_paths(fe, stream, &list, 1); | |
2114 | ||
2115 | ret = dpcm_fe_dai_startup(fe_substream); | |
2116 | if (ret < 0) { | |
2117 | /* clean up all links */ | |
2118 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) | |
2119 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
2120 | ||
2121 | dpcm_be_disconnect(fe, stream); | |
2122 | fe->dpcm[stream].runtime = NULL; | |
2123 | } | |
2124 | ||
2125 | dpcm_clear_pending_state(fe, stream); | |
2126 | dpcm_path_put(&list); | |
2127 | mutex_unlock(&fe->card->mutex); | |
2128 | return ret; | |
2129 | } | |
2130 | ||
45c0a188 | 2131 | static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
01d7584c LG |
2132 | { |
2133 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; | |
2134 | struct snd_soc_dpcm *dpcm; | |
2135 | int stream = fe_substream->stream, ret; | |
2136 | ||
2137 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
2138 | ret = dpcm_fe_dai_shutdown(fe_substream); | |
2139 | ||
2140 | /* mark FE's links ready to prune */ | |
2141 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) | |
2142 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
2143 | ||
2144 | dpcm_be_disconnect(fe, stream); | |
2145 | ||
2146 | fe->dpcm[stream].runtime = NULL; | |
2147 | mutex_unlock(&fe->card->mutex); | |
2148 | return ret; | |
2149 | } | |
2150 | ||
ddee627c LG |
2151 | /* create a new pcm */ |
2152 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) | |
2153 | { | |
ddee627c LG |
2154 | struct snd_soc_platform *platform = rtd->platform; |
2155 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
2156 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
2157 | struct snd_pcm *pcm; | |
2158 | char new_name[64]; | |
2159 | int ret = 0, playback = 0, capture = 0; | |
2160 | ||
01d7584c | 2161 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
1e9de42f LG |
2162 | playback = rtd->dai_link->dpcm_playback; |
2163 | capture = rtd->dai_link->dpcm_capture; | |
01d7584c | 2164 | } else { |
05679092 MB |
2165 | if (codec_dai->driver->playback.channels_min && |
2166 | cpu_dai->driver->playback.channels_min) | |
01d7584c | 2167 | playback = 1; |
05679092 MB |
2168 | if (codec_dai->driver->capture.channels_min && |
2169 | cpu_dai->driver->capture.channels_min) | |
01d7584c LG |
2170 | capture = 1; |
2171 | } | |
2172 | ||
d6bead02 FE |
2173 | if (rtd->dai_link->playback_only) { |
2174 | playback = 1; | |
2175 | capture = 0; | |
2176 | } | |
2177 | ||
2178 | if (rtd->dai_link->capture_only) { | |
2179 | playback = 0; | |
2180 | capture = 1; | |
2181 | } | |
2182 | ||
01d7584c LG |
2183 | /* create the PCM */ |
2184 | if (rtd->dai_link->no_pcm) { | |
2185 | snprintf(new_name, sizeof(new_name), "(%s)", | |
2186 | rtd->dai_link->stream_name); | |
2187 | ||
2188 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, | |
2189 | playback, capture, &pcm); | |
2190 | } else { | |
2191 | if (rtd->dai_link->dynamic) | |
2192 | snprintf(new_name, sizeof(new_name), "%s (*)", | |
2193 | rtd->dai_link->stream_name); | |
2194 | else | |
2195 | snprintf(new_name, sizeof(new_name), "%s %s-%d", | |
2196 | rtd->dai_link->stream_name, codec_dai->name, num); | |
2197 | ||
2198 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, | |
2199 | capture, &pcm); | |
2200 | } | |
ddee627c | 2201 | if (ret < 0) { |
103d84a3 | 2202 | dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", |
5cb9b748 | 2203 | rtd->dai_link->name); |
ddee627c LG |
2204 | return ret; |
2205 | } | |
103d84a3 | 2206 | dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); |
ddee627c LG |
2207 | |
2208 | /* DAPM dai link stream work */ | |
2209 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); | |
2210 | ||
2211 | rtd->pcm = pcm; | |
2212 | pcm->private_data = rtd; | |
01d7584c LG |
2213 | |
2214 | if (rtd->dai_link->no_pcm) { | |
2215 | if (playback) | |
2216 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; | |
2217 | if (capture) | |
2218 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; | |
2219 | goto out; | |
2220 | } | |
2221 | ||
2222 | /* ASoC PCM operations */ | |
2223 | if (rtd->dai_link->dynamic) { | |
2224 | rtd->ops.open = dpcm_fe_dai_open; | |
2225 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; | |
2226 | rtd->ops.prepare = dpcm_fe_dai_prepare; | |
2227 | rtd->ops.trigger = dpcm_fe_dai_trigger; | |
2228 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; | |
2229 | rtd->ops.close = dpcm_fe_dai_close; | |
2230 | rtd->ops.pointer = soc_pcm_pointer; | |
be3f3f2c | 2231 | rtd->ops.ioctl = soc_pcm_ioctl; |
01d7584c LG |
2232 | } else { |
2233 | rtd->ops.open = soc_pcm_open; | |
2234 | rtd->ops.hw_params = soc_pcm_hw_params; | |
2235 | rtd->ops.prepare = soc_pcm_prepare; | |
2236 | rtd->ops.trigger = soc_pcm_trigger; | |
2237 | rtd->ops.hw_free = soc_pcm_hw_free; | |
2238 | rtd->ops.close = soc_pcm_close; | |
2239 | rtd->ops.pointer = soc_pcm_pointer; | |
be3f3f2c | 2240 | rtd->ops.ioctl = soc_pcm_ioctl; |
01d7584c LG |
2241 | } |
2242 | ||
ddee627c | 2243 | if (platform->driver->ops) { |
01d7584c LG |
2244 | rtd->ops.ack = platform->driver->ops->ack; |
2245 | rtd->ops.copy = platform->driver->ops->copy; | |
2246 | rtd->ops.silence = platform->driver->ops->silence; | |
2247 | rtd->ops.page = platform->driver->ops->page; | |
2248 | rtd->ops.mmap = platform->driver->ops->mmap; | |
ddee627c LG |
2249 | } |
2250 | ||
2251 | if (playback) | |
01d7584c | 2252 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
ddee627c LG |
2253 | |
2254 | if (capture) | |
01d7584c | 2255 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
ddee627c LG |
2256 | |
2257 | if (platform->driver->pcm_new) { | |
2258 | ret = platform->driver->pcm_new(rtd); | |
2259 | if (ret < 0) { | |
b1bc7b3c MB |
2260 | dev_err(platform->dev, |
2261 | "ASoC: pcm constructor failed: %d\n", | |
2262 | ret); | |
ddee627c LG |
2263 | return ret; |
2264 | } | |
2265 | } | |
2266 | ||
2267 | pcm->private_free = platform->driver->pcm_free; | |
01d7584c | 2268 | out: |
7cc302d2 | 2269 | dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name, |
ddee627c LG |
2270 | cpu_dai->name); |
2271 | return ret; | |
2272 | } | |
01d7584c LG |
2273 | |
2274 | /* is the current PCM operation for this FE ? */ | |
2275 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) | |
2276 | { | |
2277 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) | |
2278 | return 1; | |
2279 | return 0; | |
2280 | } | |
2281 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); | |
2282 | ||
2283 | /* is the current PCM operation for this BE ? */ | |
2284 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, | |
2285 | struct snd_soc_pcm_runtime *be, int stream) | |
2286 | { | |
2287 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || | |
2288 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && | |
2289 | be->dpcm[stream].runtime_update)) | |
2290 | return 1; | |
2291 | return 0; | |
2292 | } | |
2293 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); | |
2294 | ||
2295 | /* get the substream for this BE */ | |
2296 | struct snd_pcm_substream * | |
2297 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) | |
2298 | { | |
2299 | return be->pcm->streams[stream].substream; | |
2300 | } | |
2301 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); | |
2302 | ||
2303 | /* get the BE runtime state */ | |
2304 | enum snd_soc_dpcm_state | |
2305 | snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) | |
2306 | { | |
2307 | return be->dpcm[stream].state; | |
2308 | } | |
2309 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); | |
2310 | ||
2311 | /* set the BE runtime state */ | |
2312 | void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, | |
2313 | int stream, enum snd_soc_dpcm_state state) | |
2314 | { | |
2315 | be->dpcm[stream].state = state; | |
2316 | } | |
2317 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); | |
2318 | ||
2319 | /* | |
2320 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE | |
2321 | * are not running, paused or suspended for the specified stream direction. | |
2322 | */ | |
2323 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, | |
2324 | struct snd_soc_pcm_runtime *be, int stream) | |
2325 | { | |
2326 | struct snd_soc_dpcm *dpcm; | |
2327 | int state; | |
2328 | ||
2329 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { | |
2330 | ||
2331 | if (dpcm->fe == fe) | |
2332 | continue; | |
2333 | ||
2334 | state = dpcm->fe->dpcm[stream].state; | |
2335 | if (state == SND_SOC_DPCM_STATE_START || | |
2336 | state == SND_SOC_DPCM_STATE_PAUSED || | |
2337 | state == SND_SOC_DPCM_STATE_SUSPEND) | |
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | /* it's safe to free/stop this BE DAI */ | |
2342 | return 1; | |
2343 | } | |
2344 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); | |
2345 | ||
2346 | /* | |
2347 | * We can only change hw params a BE DAI if any of it's FE are not prepared, | |
2348 | * running, paused or suspended for the specified stream direction. | |
2349 | */ | |
2350 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, | |
2351 | struct snd_soc_pcm_runtime *be, int stream) | |
2352 | { | |
2353 | struct snd_soc_dpcm *dpcm; | |
2354 | int state; | |
2355 | ||
2356 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { | |
2357 | ||
2358 | if (dpcm->fe == fe) | |
2359 | continue; | |
2360 | ||
2361 | state = dpcm->fe->dpcm[stream].state; | |
2362 | if (state == SND_SOC_DPCM_STATE_START || | |
2363 | state == SND_SOC_DPCM_STATE_PAUSED || | |
2364 | state == SND_SOC_DPCM_STATE_SUSPEND || | |
2365 | state == SND_SOC_DPCM_STATE_PREPARE) | |
2366 | return 0; | |
2367 | } | |
2368 | ||
2369 | /* it's safe to change hw_params */ | |
2370 | return 1; | |
2371 | } | |
2372 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); | |
f86dcef8 | 2373 | |
07bf84aa LG |
2374 | int snd_soc_platform_trigger(struct snd_pcm_substream *substream, |
2375 | int cmd, struct snd_soc_platform *platform) | |
2376 | { | |
c5914b0a | 2377 | if (platform->driver->ops && platform->driver->ops->trigger) |
07bf84aa LG |
2378 | return platform->driver->ops->trigger(substream, cmd); |
2379 | return 0; | |
2380 | } | |
2381 | EXPORT_SYMBOL_GPL(snd_soc_platform_trigger); | |
2382 | ||
f86dcef8 LG |
2383 | #ifdef CONFIG_DEBUG_FS |
2384 | static char *dpcm_state_string(enum snd_soc_dpcm_state state) | |
2385 | { | |
2386 | switch (state) { | |
2387 | case SND_SOC_DPCM_STATE_NEW: | |
2388 | return "new"; | |
2389 | case SND_SOC_DPCM_STATE_OPEN: | |
2390 | return "open"; | |
2391 | case SND_SOC_DPCM_STATE_HW_PARAMS: | |
2392 | return "hw_params"; | |
2393 | case SND_SOC_DPCM_STATE_PREPARE: | |
2394 | return "prepare"; | |
2395 | case SND_SOC_DPCM_STATE_START: | |
2396 | return "start"; | |
2397 | case SND_SOC_DPCM_STATE_STOP: | |
2398 | return "stop"; | |
2399 | case SND_SOC_DPCM_STATE_SUSPEND: | |
2400 | return "suspend"; | |
2401 | case SND_SOC_DPCM_STATE_PAUSED: | |
2402 | return "paused"; | |
2403 | case SND_SOC_DPCM_STATE_HW_FREE: | |
2404 | return "hw_free"; | |
2405 | case SND_SOC_DPCM_STATE_CLOSE: | |
2406 | return "close"; | |
2407 | } | |
2408 | ||
2409 | return "unknown"; | |
2410 | } | |
2411 | ||
f86dcef8 LG |
2412 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
2413 | int stream, char *buf, size_t size) | |
2414 | { | |
2415 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; | |
2416 | struct snd_soc_dpcm *dpcm; | |
2417 | ssize_t offset = 0; | |
2418 | ||
2419 | /* FE state */ | |
2420 | offset += snprintf(buf + offset, size - offset, | |
2421 | "[%s - %s]\n", fe->dai_link->name, | |
2422 | stream ? "Capture" : "Playback"); | |
2423 | ||
2424 | offset += snprintf(buf + offset, size - offset, "State: %s\n", | |
2425 | dpcm_state_string(fe->dpcm[stream].state)); | |
2426 | ||
2427 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && | |
2428 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) | |
2429 | offset += snprintf(buf + offset, size - offset, | |
2430 | "Hardware Params: " | |
2431 | "Format = %s, Channels = %d, Rate = %d\n", | |
2432 | snd_pcm_format_name(params_format(params)), | |
2433 | params_channels(params), | |
2434 | params_rate(params)); | |
2435 | ||
2436 | /* BEs state */ | |
2437 | offset += snprintf(buf + offset, size - offset, "Backends:\n"); | |
2438 | ||
2439 | if (list_empty(&fe->dpcm[stream].be_clients)) { | |
2440 | offset += snprintf(buf + offset, size - offset, | |
2441 | " No active DSP links\n"); | |
2442 | goto out; | |
2443 | } | |
2444 | ||
2445 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
2446 | struct snd_soc_pcm_runtime *be = dpcm->be; | |
2447 | params = &dpcm->hw_params; | |
2448 | ||
2449 | offset += snprintf(buf + offset, size - offset, | |
2450 | "- %s\n", be->dai_link->name); | |
2451 | ||
2452 | offset += snprintf(buf + offset, size - offset, | |
2453 | " State: %s\n", | |
2454 | dpcm_state_string(be->dpcm[stream].state)); | |
2455 | ||
2456 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && | |
2457 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) | |
2458 | offset += snprintf(buf + offset, size - offset, | |
2459 | " Hardware Params: " | |
2460 | "Format = %s, Channels = %d, Rate = %d\n", | |
2461 | snd_pcm_format_name(params_format(params)), | |
2462 | params_channels(params), | |
2463 | params_rate(params)); | |
2464 | } | |
2465 | ||
2466 | out: | |
2467 | return offset; | |
2468 | } | |
2469 | ||
2470 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, | |
2471 | size_t count, loff_t *ppos) | |
2472 | { | |
2473 | struct snd_soc_pcm_runtime *fe = file->private_data; | |
2474 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; | |
2475 | char *buf; | |
2476 | ||
2477 | buf = kmalloc(out_count, GFP_KERNEL); | |
2478 | if (!buf) | |
2479 | return -ENOMEM; | |
2480 | ||
2481 | if (fe->cpu_dai->driver->playback.channels_min) | |
2482 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, | |
2483 | buf + offset, out_count - offset); | |
2484 | ||
2485 | if (fe->cpu_dai->driver->capture.channels_min) | |
2486 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, | |
2487 | buf + offset, out_count - offset); | |
2488 | ||
f57b8488 | 2489 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
f86dcef8 | 2490 | |
f57b8488 LG |
2491 | kfree(buf); |
2492 | return ret; | |
f86dcef8 LG |
2493 | } |
2494 | ||
2495 | static const struct file_operations dpcm_state_fops = { | |
f57b8488 | 2496 | .open = simple_open, |
f86dcef8 LG |
2497 | .read = dpcm_state_read_file, |
2498 | .llseek = default_llseek, | |
2499 | }; | |
2500 | ||
2501 | int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) | |
2502 | { | |
b3bba9a1 MB |
2503 | if (!rtd->dai_link) |
2504 | return 0; | |
2505 | ||
f86dcef8 LG |
2506 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
2507 | rtd->card->debugfs_card_root); | |
2508 | if (!rtd->debugfs_dpcm_root) { | |
2509 | dev_dbg(rtd->dev, | |
2510 | "ASoC: Failed to create dpcm debugfs directory %s\n", | |
2511 | rtd->dai_link->name); | |
2512 | return -EINVAL; | |
2513 | } | |
2514 | ||
f57b8488 | 2515 | rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444, |
f86dcef8 LG |
2516 | rtd->debugfs_dpcm_root, |
2517 | rtd, &dpcm_state_fops); | |
2518 | ||
2519 | return 0; | |
2520 | } | |
2521 | #endif |