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