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