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