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