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