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