blob: 0e2261ee07b67bf6871faa0511134e9f9ab68e81 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-pcm.c -- ALSA SoC PCM
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Authors: Liam Girdwood <lrg@ti.com>
11// Mark Brown <broonie@opensource.wolfsonmicro.com>
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/pm_runtime.h>
18#include <linux/slab.h>
19#include <linux/workqueue.h>
20#include <linux/export.h>
21#include <linux/debugfs.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/soc-dpcm.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020027#include <sound/soc-link.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028#include <sound/initval.h>
29
30#define DPCM_MAX_BE_USERS 8
31
Olivier Deprez157378f2022-04-04 15:47:50 +020032#ifdef CONFIG_DEBUG_FS
33static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
34{
35 switch (state) {
36 case SND_SOC_DPCM_STATE_NEW:
37 return "new";
38 case SND_SOC_DPCM_STATE_OPEN:
39 return "open";
40 case SND_SOC_DPCM_STATE_HW_PARAMS:
41 return "hw_params";
42 case SND_SOC_DPCM_STATE_PREPARE:
43 return "prepare";
44 case SND_SOC_DPCM_STATE_START:
45 return "start";
46 case SND_SOC_DPCM_STATE_STOP:
47 return "stop";
48 case SND_SOC_DPCM_STATE_SUSPEND:
49 return "suspend";
50 case SND_SOC_DPCM_STATE_PAUSED:
51 return "paused";
52 case SND_SOC_DPCM_STATE_HW_FREE:
53 return "hw_free";
54 case SND_SOC_DPCM_STATE_CLOSE:
55 return "close";
56 }
57
58 return "unknown";
59}
60
61static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
62 int stream, char *buf, size_t size)
63{
64 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
65 struct snd_soc_dpcm *dpcm;
66 ssize_t offset = 0;
67 unsigned long flags;
68
69 /* FE state */
70 offset += scnprintf(buf + offset, size - offset,
71 "[%s - %s]\n", fe->dai_link->name,
72 stream ? "Capture" : "Playback");
73
74 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
75 dpcm_state_string(fe->dpcm[stream].state));
76
77 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
78 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
79 offset += scnprintf(buf + offset, size - offset,
80 "Hardware Params: "
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params)),
83 params_channels(params),
84 params_rate(params));
85
86 /* BEs state */
87 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
88
89 if (list_empty(&fe->dpcm[stream].be_clients)) {
90 offset += scnprintf(buf + offset, size - offset,
91 " No active DSP links\n");
92 goto out;
93 }
94
95 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
96 for_each_dpcm_be(fe, stream, dpcm) {
97 struct snd_soc_pcm_runtime *be = dpcm->be;
98 params = &dpcm->hw_params;
99
100 offset += scnprintf(buf + offset, size - offset,
101 "- %s\n", be->dai_link->name);
102
103 offset += scnprintf(buf + offset, size - offset,
104 " State: %s\n",
105 dpcm_state_string(be->dpcm[stream].state));
106
107 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
108 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
109 offset += scnprintf(buf + offset, size - offset,
110 " Hardware Params: "
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params)),
113 params_channels(params),
114 params_rate(params));
115 }
116 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
117out:
118 return offset;
119}
120
121static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
122 size_t count, loff_t *ppos)
123{
124 struct snd_soc_pcm_runtime *fe = file->private_data;
125 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
126 int stream;
127 char *buf;
128
129 if (fe->num_cpus > 1) {
130 dev_err(fe->dev,
131 "%s doesn't support Multi CPU yet\n", __func__);
132 return -EINVAL;
133 }
134
135 buf = kmalloc(out_count, GFP_KERNEL);
136 if (!buf)
137 return -ENOMEM;
138
139 for_each_pcm_streams(stream)
140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
141 offset += dpcm_show_state(fe, stream,
142 buf + offset,
143 out_count - offset);
144
145 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
146
147 kfree(buf);
148 return ret;
149}
150
151static const struct file_operations dpcm_state_fops = {
152 .open = simple_open,
153 .read = dpcm_state_read_file,
154 .llseek = default_llseek,
155};
156
157void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
158{
159 if (!rtd->dai_link)
160 return;
161
162 if (!rtd->dai_link->dynamic)
163 return;
164
165 if (!rtd->card->debugfs_card_root)
166 return;
167
168 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
169 rtd->card->debugfs_card_root);
170
171 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
172 rtd, &dpcm_state_fops);
173}
174
175static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
176{
177 char *name;
178
179 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
180 stream ? "capture" : "playback");
181 if (name) {
182 dpcm->debugfs_state = debugfs_create_dir(
183 name, dpcm->fe->debugfs_dpcm_root);
184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
185 &dpcm->state);
186 kfree(name);
187 }
188}
189
190static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
191{
192 debugfs_remove_recursive(dpcm->debugfs_state);
193}
194
195#else
196static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
197 int stream)
198{
199}
200
201static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
202{
203}
204#endif
205
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200207 * snd_soc_runtime_action() - Increment/Decrement active count for
208 * PCM runtime components
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209 * @rtd: ASoC PCM runtime that is activated
210 * @stream: Direction of the PCM stream
Olivier Deprez157378f2022-04-04 15:47:50 +0200211 * @action: Activate stream if 1. Deactivate if -1.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200213 * Increments/Decrements the active count for all the DAIs and components
214 * attached to a PCM runtime.
215 * Should typically be called when a stream is opened.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 *
David Brazdil0f672f62019-12-10 10:32:29 +0000217 * Must be called with the rtd->card->pcm_mutex being held
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200219void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
220 int stream, int action)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221{
Olivier Deprez157378f2022-04-04 15:47:50 +0200222 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 int i;
224
David Brazdil0f672f62019-12-10 10:32:29 +0000225 lockdep_assert_held(&rtd->card->pcm_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226
Olivier Deprez157378f2022-04-04 15:47:50 +0200227 for_each_rtd_dais(rtd, i, dai)
228 snd_soc_dai_action(dai, stream, action);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229}
Olivier Deprez157378f2022-04-04 15:47:50 +0200230EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231
232/**
233 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
234 * @rtd: The ASoC PCM runtime that should be checked.
235 *
236 * This function checks whether the power down delay should be ignored for a
237 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
238 * been configured to ignore the delay, or if none of the components benefits
239 * from having the delay.
240 */
241bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
242{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243 struct snd_soc_component *component;
244 bool ignore = true;
Olivier Deprez157378f2022-04-04 15:47:50 +0200245 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246
247 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
248 return true;
249
Olivier Deprez157378f2022-04-04 15:47:50 +0200250 for_each_rtd_components(rtd, i, component)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251 ignore &= !component->driver->use_pmdown_time;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252
253 return ignore;
254}
255
256/**
257 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
258 * @substream: the pcm substream
259 * @hw: the hardware parameters
260 *
261 * Sets the substream runtime hardware parameters.
262 */
263int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
264 const struct snd_pcm_hardware *hw)
265{
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 runtime->hw.info = hw->info;
268 runtime->hw.formats = hw->formats;
269 runtime->hw.period_bytes_min = hw->period_bytes_min;
270 runtime->hw.period_bytes_max = hw->period_bytes_max;
271 runtime->hw.periods_min = hw->periods_min;
272 runtime->hw.periods_max = hw->periods_max;
273 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
274 runtime->hw.fifo_size = hw->fifo_size;
275 return 0;
276}
277EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
278
279/* DPCM stream event, send event to FE and all active BEs. */
280int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
281 int event)
282{
283 struct snd_soc_dpcm *dpcm;
284
David Brazdil0f672f62019-12-10 10:32:29 +0000285 for_each_dpcm_be(fe, dir, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286
287 struct snd_soc_pcm_runtime *be = dpcm->be;
288
289 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
290 be->dai_link->name, event, dir);
291
292 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
293 (be->dpcm[dir].users >= 1))
294 continue;
295
296 snd_soc_dapm_stream_event(be, dir, event);
297 }
298
299 snd_soc_dapm_stream_event(fe, dir, event);
300
301 return 0;
302}
303
304static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
305 struct snd_soc_dai *soc_dai)
306{
Olivier Deprez157378f2022-04-04 15:47:50 +0200307 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308 int ret;
309
310 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
311 rtd->dai_link->symmetric_rates)) {
312 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
313 soc_dai->rate);
314
315 ret = snd_pcm_hw_constraint_single(substream->runtime,
316 SNDRV_PCM_HW_PARAM_RATE,
317 soc_dai->rate);
318 if (ret < 0) {
319 dev_err(soc_dai->dev,
320 "ASoC: Unable to apply rate constraint: %d\n",
321 ret);
322 return ret;
323 }
324 }
325
326 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
327 rtd->dai_link->symmetric_channels)) {
328 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
329 soc_dai->channels);
330
331 ret = snd_pcm_hw_constraint_single(substream->runtime,
332 SNDRV_PCM_HW_PARAM_CHANNELS,
333 soc_dai->channels);
334 if (ret < 0) {
335 dev_err(soc_dai->dev,
336 "ASoC: Unable to apply channel symmetry constraint: %d\n",
337 ret);
338 return ret;
339 }
340 }
341
342 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
343 rtd->dai_link->symmetric_samplebits)) {
344 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
345 soc_dai->sample_bits);
346
347 ret = snd_pcm_hw_constraint_single(substream->runtime,
348 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
349 soc_dai->sample_bits);
350 if (ret < 0) {
351 dev_err(soc_dai->dev,
352 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
353 ret);
354 return ret;
355 }
356 }
357
358 return 0;
359}
360
361static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
362 struct snd_pcm_hw_params *params)
363{
Olivier Deprez157378f2022-04-04 15:47:50 +0200364 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
365 struct snd_soc_dai *dai;
366 struct snd_soc_dai *cpu_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367 unsigned int rate, channels, sample_bits, symmetry, i;
368
369 rate = params_rate(params);
370 channels = params_channels(params);
371 sample_bits = snd_pcm_format_physical_width(params_format(params));
372
373 /* reject unmatched parameters when applying symmetry */
Olivier Deprez157378f2022-04-04 15:47:50 +0200374 symmetry = rtd->dai_link->symmetric_rates;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375
Olivier Deprez157378f2022-04-04 15:47:50 +0200376 for_each_rtd_cpu_dais(rtd, i, dai)
377 symmetry |= dai->driver->symmetric_rates;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378
Olivier Deprez157378f2022-04-04 15:47:50 +0200379 if (symmetry) {
380 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
381 if (cpu_dai->rate && cpu_dai->rate != rate) {
382 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
383 cpu_dai->rate, rate);
384 return -EINVAL;
385 }
386 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000387 }
388
Olivier Deprez157378f2022-04-04 15:47:50 +0200389 symmetry = rtd->dai_link->symmetric_channels;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390
Olivier Deprez157378f2022-04-04 15:47:50 +0200391 for_each_rtd_dais(rtd, i, dai)
392 symmetry |= dai->driver->symmetric_channels;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393
Olivier Deprez157378f2022-04-04 15:47:50 +0200394 if (symmetry) {
395 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
396 if (cpu_dai->channels &&
397 cpu_dai->channels != channels) {
398 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
399 cpu_dai->channels, channels);
400 return -EINVAL;
401 }
402 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403 }
404
Olivier Deprez157378f2022-04-04 15:47:50 +0200405 symmetry = rtd->dai_link->symmetric_samplebits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406
Olivier Deprez157378f2022-04-04 15:47:50 +0200407 for_each_rtd_dais(rtd, i, dai)
408 symmetry |= dai->driver->symmetric_samplebits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409
Olivier Deprez157378f2022-04-04 15:47:50 +0200410 if (symmetry) {
411 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
412 if (cpu_dai->sample_bits &&
413 cpu_dai->sample_bits != sample_bits) {
414 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
415 cpu_dai->sample_bits, sample_bits);
416 return -EINVAL;
417 }
418 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419 }
420
421 return 0;
422}
423
424static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
425{
Olivier Deprez157378f2022-04-04 15:47:50 +0200426 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000427 struct snd_soc_dai_link *link = rtd->dai_link;
Olivier Deprez157378f2022-04-04 15:47:50 +0200428 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 unsigned int symmetry, i;
430
Olivier Deprez157378f2022-04-04 15:47:50 +0200431 symmetry = link->symmetric_rates ||
432 link->symmetric_channels ||
433 link->symmetric_samplebits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434
Olivier Deprez157378f2022-04-04 15:47:50 +0200435 for_each_rtd_dais(rtd, i, dai)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436 symmetry = symmetry ||
Olivier Deprez157378f2022-04-04 15:47:50 +0200437 dai->driver->symmetric_rates ||
438 dai->driver->symmetric_channels ||
439 dai->driver->symmetric_samplebits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440
441 return symmetry;
442}
443
444static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
445{
Olivier Deprez157378f2022-04-04 15:47:50 +0200446 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000447 int ret;
448
449 if (!bits)
450 return;
451
452 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
453 if (ret != 0)
454 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
455 bits, ret);
456}
457
458static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
459{
Olivier Deprez157378f2022-04-04 15:47:50 +0200460 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
461 struct snd_soc_dai *cpu_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000462 struct snd_soc_dai *codec_dai;
Olivier Deprez157378f2022-04-04 15:47:50 +0200463 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
464 int stream = substream->stream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465 int i;
Olivier Deprez157378f2022-04-04 15:47:50 +0200466 unsigned int bits = 0, cpu_bits = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467
Olivier Deprez157378f2022-04-04 15:47:50 +0200468 for_each_rtd_codec_dais(rtd, i, codec_dai) {
469 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
470
471 if (pcm_codec->sig_bits == 0) {
472 bits = 0;
473 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000474 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200475 bits = max(pcm_codec->sig_bits, bits);
476 }
477
478 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
479 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
480
481 if (pcm_cpu->sig_bits == 0) {
482 cpu_bits = 0;
483 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000484 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200485 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 }
487
488 soc_pcm_set_msb(substream, bits);
489 soc_pcm_set_msb(substream, cpu_bits);
490}
491
Olivier Deprez157378f2022-04-04 15:47:50 +0200492/**
493 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
494 * @rtd: ASoC PCM runtime
495 * @hw: PCM hardware parameters (output)
496 * @stream: Direction of the PCM stream
497 *
498 * Calculates the subset of stream parameters supported by all DAIs
499 * associated with the PCM stream.
500 */
501int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
502 struct snd_pcm_hardware *hw, int stream)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503{
David Brazdil0f672f62019-12-10 10:32:29 +0000504 struct snd_soc_dai *codec_dai;
Olivier Deprez157378f2022-04-04 15:47:50 +0200505 struct snd_soc_dai *cpu_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506 struct snd_soc_pcm_stream *codec_stream;
507 struct snd_soc_pcm_stream *cpu_stream;
508 unsigned int chan_min = 0, chan_max = UINT_MAX;
Olivier Deprez157378f2022-04-04 15:47:50 +0200509 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 unsigned int rate_min = 0, rate_max = UINT_MAX;
Olivier Deprez157378f2022-04-04 15:47:50 +0200511 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
512 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513 u64 formats = ULLONG_MAX;
514 int i;
515
Olivier Deprez157378f2022-04-04 15:47:50 +0200516 /* first calculate min/max only for CPUs in the DAI link */
517 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000518
Olivier Deprez157378f2022-04-04 15:47:50 +0200519 /*
520 * Skip CPUs which don't support the current stream type.
521 * Otherwise, since the rate, channel, and format values will
522 * zero in that case, we would have no usable settings left,
523 * causing the resulting setup to fail.
524 */
525 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
526 continue;
527
528 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
529
530 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
531 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
532 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
533 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
534 formats &= cpu_stream->formats;
535 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
536 cpu_rates);
537 }
538
539 /* second calculate min/max only for CODECs in the DAI link */
540 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541
542 /*
543 * Skip CODECs which don't support the current stream type.
544 * Otherwise, since the rate, channel, and format values will
545 * zero in that case, we would have no usable settings left,
546 * causing the resulting setup to fail.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200548 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549 continue;
550
Olivier Deprez157378f2022-04-04 15:47:50 +0200551 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
552
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553 chan_min = max(chan_min, codec_stream->channels_min);
554 chan_max = min(chan_max, codec_stream->channels_max);
555 rate_min = max(rate_min, codec_stream->rate_min);
556 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
557 formats &= codec_stream->formats;
558 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
559 }
560
Olivier Deprez157378f2022-04-04 15:47:50 +0200561 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
562 if (!chan_min || !cpu_chan_min)
563 return -EINVAL;
564
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565 /*
566 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Olivier Deprez157378f2022-04-04 15:47:50 +0200567 * connected to CPU DAI(s), use CPU DAI's directly and let
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568 * channel allocation be fixed up later
569 */
570 if (rtd->num_codecs > 1) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200571 chan_min = cpu_chan_min;
572 chan_max = cpu_chan_max;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000573 }
574
Olivier Deprez157378f2022-04-04 15:47:50 +0200575 /* finally find a intersection between CODECs and CPUs */
576 hw->channels_min = max(chan_min, cpu_chan_min);
577 hw->channels_max = min(chan_max, cpu_chan_max);
578 hw->formats = formats;
579 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580
Olivier Deprez157378f2022-04-04 15:47:50 +0200581 snd_pcm_hw_limit_rates(hw);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582
Olivier Deprez157378f2022-04-04 15:47:50 +0200583 hw->rate_min = max(hw->rate_min, cpu_rate_min);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 hw->rate_min = max(hw->rate_min, rate_min);
Olivier Deprez157378f2022-04-04 15:47:50 +0200585 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587
David Brazdil0f672f62019-12-10 10:32:29 +0000588 return 0;
589}
Olivier Deprez157378f2022-04-04 15:47:50 +0200590EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
David Brazdil0f672f62019-12-10 10:32:29 +0000591
Olivier Deprez157378f2022-04-04 15:47:50 +0200592static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593{
Olivier Deprez157378f2022-04-04 15:47:50 +0200594 struct snd_pcm_hardware *hw = &substream->runtime->hw;
595 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
596 u64 formats = hw->formats;
597
598 /*
599 * At least one CPU and one CODEC should match. Otherwise, we should
600 * have bailed out on a higher level, since there would be no CPU or
601 * CODEC to support the transfer direction in that case.
602 */
603 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
604
605 if (formats)
606 hw->formats &= formats;
607}
608
609static int soc_pcm_components_open(struct snd_pcm_substream *substream)
610{
611 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 struct snd_soc_component *component;
Olivier Deprez157378f2022-04-04 15:47:50 +0200613 int i, ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614
Olivier Deprez157378f2022-04-04 15:47:50 +0200615 for_each_rtd_components(rtd, i, component) {
616 ret = snd_soc_component_module_get_when_open(component, substream);
617 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 break;
619
Olivier Deprez157378f2022-04-04 15:47:50 +0200620 ret = snd_soc_component_open(component, substream);
621 if (ret < 0)
622 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000623 }
624
David Brazdil0f672f62019-12-10 10:32:29 +0000625 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000626}
627
Olivier Deprez157378f2022-04-04 15:47:50 +0200628static int soc_pcm_components_close(struct snd_pcm_substream *substream,
629 int rollback)
630{
631 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
632 struct snd_soc_component *component;
633 int i, r, ret = 0;
634
635 for_each_rtd_components(rtd, i, component) {
636 r = snd_soc_component_close(component, substream, rollback);
637 if (r < 0)
638 ret = r; /* use last ret */
639
640 snd_soc_component_module_put_when_close(component, substream, rollback);
641 }
642
643 return ret;
644}
645
646static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
647{
648 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
649 struct snd_soc_component *component;
650 struct snd_soc_dai *dai;
651 int i;
652
653 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
654
655 if (!rollback)
656 snd_soc_runtime_deactivate(rtd, substream->stream);
657
658 for_each_rtd_dais(rtd, i, dai)
659 snd_soc_dai_shutdown(dai, substream, rollback);
660
661 snd_soc_link_shutdown(substream, rollback);
662
663 soc_pcm_components_close(substream, rollback);
664
665 if (!rollback)
666 snd_soc_dapm_stream_stop(rtd, substream->stream);
667
668 mutex_unlock(&rtd->card->pcm_mutex);
669
670 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
671
672 for_each_rtd_components(rtd, i, component)
673 if (!snd_soc_component_active(component))
674 pinctrl_pm_select_sleep_state(component->dev);
675
676 return 0;
677}
678
679/*
680 * Called by ALSA when a PCM substream is closed. Private data can be
681 * freed here. The cpu DAI, codec DAI, machine and components are also
682 * shutdown.
683 */
684static int soc_pcm_close(struct snd_pcm_substream *substream)
685{
686 return soc_pcm_clean(substream, 0);
687}
688
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000689/*
690 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
691 * then initialized and any private data can be allocated. This also calls
692 * startup for the cpu DAI, component, machine and codec DAI.
693 */
694static int soc_pcm_open(struct snd_pcm_substream *substream)
695{
Olivier Deprez157378f2022-04-04 15:47:50 +0200696 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000697 struct snd_pcm_runtime *runtime = substream->runtime;
698 struct snd_soc_component *component;
Olivier Deprez157378f2022-04-04 15:47:50 +0200699 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700 const char *codec_dai_name = "multicodec";
Olivier Deprez157378f2022-04-04 15:47:50 +0200701 const char *cpu_dai_name = "multicpu";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702 int i, ret = 0;
703
Olivier Deprez157378f2022-04-04 15:47:50 +0200704 for_each_rtd_components(rtd, i, component)
705 pinctrl_pm_select_default_state(component->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706
Olivier Deprez157378f2022-04-04 15:47:50 +0200707 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
708 if (ret < 0)
709 goto pm_err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000710
David Brazdil0f672f62019-12-10 10:32:29 +0000711 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712
Olivier Deprez157378f2022-04-04 15:47:50 +0200713 ret = soc_pcm_components_open(substream);
David Brazdil0f672f62019-12-10 10:32:29 +0000714 if (ret < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +0200715 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716
Olivier Deprez157378f2022-04-04 15:47:50 +0200717 ret = snd_soc_link_startup(substream);
718 if (ret < 0)
719 goto err;
720
721 /* startup the audio subsystem */
722 for_each_rtd_dais(rtd, i, dai) {
723 ret = snd_soc_dai_startup(dai, substream);
724 if (ret < 0)
725 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726 }
727
728 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
729 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
730 goto dynamic;
731
732 /* Check that the codec and cpu DAIs are compatible */
733 soc_pcm_init_runtime_hw(substream);
734
735 if (rtd->num_codecs == 1)
Olivier Deprez157378f2022-04-04 15:47:50 +0200736 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
737
738 if (rtd->num_cpus == 1)
739 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000740
741 if (soc_pcm_has_symmetry(substream))
742 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
743
744 ret = -EINVAL;
745 if (!runtime->hw.rates) {
746 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Olivier Deprez157378f2022-04-04 15:47:50 +0200747 codec_dai_name, cpu_dai_name);
748 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749 }
750 if (!runtime->hw.formats) {
751 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Olivier Deprez157378f2022-04-04 15:47:50 +0200752 codec_dai_name, cpu_dai_name);
753 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000754 }
755 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
756 runtime->hw.channels_min > runtime->hw.channels_max) {
757 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Olivier Deprez157378f2022-04-04 15:47:50 +0200758 codec_dai_name, cpu_dai_name);
759 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760 }
761
762 soc_pcm_apply_msb(substream);
763
764 /* Symmetry only applies if we've already got an active stream. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200765 for_each_rtd_dais(rtd, i, dai) {
766 if (snd_soc_dai_active(dai)) {
767 ret = soc_pcm_apply_symmetry(substream, dai);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000768 if (ret != 0)
Olivier Deprez157378f2022-04-04 15:47:50 +0200769 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000770 }
771 }
772
773 pr_debug("ASoC: %s <-> %s info:\n",
Olivier Deprez157378f2022-04-04 15:47:50 +0200774 codec_dai_name, cpu_dai_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000775 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
776 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
777 runtime->hw.channels_max);
778 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
779 runtime->hw.rate_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000780dynamic:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781 snd_soc_runtime_activate(rtd, substream->stream);
Olivier Deprez157378f2022-04-04 15:47:50 +0200782 ret = 0;
783err:
David Brazdil0f672f62019-12-10 10:32:29 +0000784 mutex_unlock(&rtd->card->pcm_mutex);
Olivier Deprez157378f2022-04-04 15:47:50 +0200785pm_err:
786 if (ret < 0)
787 soc_pcm_clean(substream, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000788
789 return ret;
790}
791
Olivier Deprez157378f2022-04-04 15:47:50 +0200792static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
David Brazdil0f672f62019-12-10 10:32:29 +0000793{
794 /*
795 * Currently nothing to do for c2c links
796 * Since c2c links are internal nodes in the DAPM graph and
797 * don't interface with the outside world or application layer
798 * we don't have to do any special handling on close.
799 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800}
801
802/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 * Called by ALSA when the PCM substream is prepared, can set format, sample
804 * rate, etc. This function is non atomic and can be called multiple times,
805 * it can refer to the runtime info.
806 */
807static int soc_pcm_prepare(struct snd_pcm_substream *substream)
808{
Olivier Deprez157378f2022-04-04 15:47:50 +0200809 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
810 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000811 int i, ret = 0;
812
David Brazdil0f672f62019-12-10 10:32:29 +0000813 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000814
Olivier Deprez157378f2022-04-04 15:47:50 +0200815 ret = snd_soc_link_prepare(substream);
816 if (ret < 0)
817 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000818
Olivier Deprez157378f2022-04-04 15:47:50 +0200819 ret = snd_soc_pcm_component_prepare(substream);
820 if (ret < 0)
821 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000822
Olivier Deprez157378f2022-04-04 15:47:50 +0200823 ret = snd_soc_pcm_dai_prepare(substream);
David Brazdil0f672f62019-12-10 10:32:29 +0000824 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200825 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
David Brazdil0f672f62019-12-10 10:32:29 +0000826 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000827 }
828
829 /* cancel any delayed stream shutdown that is pending */
830 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 rtd->pop_wait) {
832 rtd->pop_wait = 0;
833 cancel_delayed_work(&rtd->delayed_work);
834 }
835
836 snd_soc_dapm_stream_event(rtd, substream->stream,
837 SND_SOC_DAPM_STREAM_START);
838
Olivier Deprez157378f2022-04-04 15:47:50 +0200839 for_each_rtd_dais(rtd, i, dai)
840 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000841
842out:
David Brazdil0f672f62019-12-10 10:32:29 +0000843 mutex_unlock(&rtd->card->pcm_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844 return ret;
845}
846
847static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
848 unsigned int mask)
849{
850 struct snd_interval *interval;
851 int channels = hweight_long(mask);
852
853 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
854 interval->min = channels;
855 interval->max = channels;
856}
857
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000858/*
859 * Called by ALSA when the hardware params are set by application. This
860 * function can also be called multiple times and can allocate buffers
861 * (using snd_pcm_lib_* ). It's non-atomic.
862 */
863static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
864 struct snd_pcm_hw_params *params)
865{
Olivier Deprez157378f2022-04-04 15:47:50 +0200866 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000867 struct snd_soc_component *component;
Olivier Deprez157378f2022-04-04 15:47:50 +0200868 struct snd_soc_dai *cpu_dai;
David Brazdil0f672f62019-12-10 10:32:29 +0000869 struct snd_soc_dai *codec_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000870 int i, ret = 0;
871
David Brazdil0f672f62019-12-10 10:32:29 +0000872 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Olivier Deprez0e641232021-09-23 10:07:05 +0200873
874 ret = soc_pcm_params_symmetry(substream, params);
875 if (ret)
876 goto out;
877
Olivier Deprez157378f2022-04-04 15:47:50 +0200878 ret = snd_soc_link_hw_params(substream, params);
879 if (ret < 0)
880 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000881
Olivier Deprez157378f2022-04-04 15:47:50 +0200882 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000883 struct snd_pcm_hw_params codec_params;
884
885 /*
886 * Skip CODECs which don't support the current stream type,
887 * the idea being that if a CODEC is not used for the currently
888 * set up transfer direction, it should not need to be
889 * configured, especially since the configuration used might
890 * not even be supported by that CODEC. There may be cases
891 * however where a CODEC needs to be set up although it is
892 * actually not being used for the transfer, e.g. if a
893 * capture-only CODEC is acting as an LRCLK and/or BCLK master
894 * for the DAI link including a playback-only CODEC.
895 * If this becomes necessary, we will have to augment the
896 * machine driver setup with information on how to act, so
897 * we can do the right thing here.
898 */
899 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
900 continue;
901
902 /* copy params for each codec */
903 codec_params = *params;
904
905 /* fixup params based on TDM slot masks */
David Brazdil0f672f62019-12-10 10:32:29 +0000906 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
907 codec_dai->tx_mask)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000908 soc_pcm_codec_params_fixup(&codec_params,
909 codec_dai->tx_mask);
David Brazdil0f672f62019-12-10 10:32:29 +0000910
911 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
912 codec_dai->rx_mask)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 soc_pcm_codec_params_fixup(&codec_params,
914 codec_dai->rx_mask);
915
David Brazdil0f672f62019-12-10 10:32:29 +0000916 ret = snd_soc_dai_hw_params(codec_dai, substream,
917 &codec_params);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 if(ret < 0)
919 goto codec_err;
920
921 codec_dai->rate = params_rate(&codec_params);
922 codec_dai->channels = params_channels(&codec_params);
923 codec_dai->sample_bits = snd_pcm_format_physical_width(
924 params_format(&codec_params));
David Brazdil0f672f62019-12-10 10:32:29 +0000925
926 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 }
928
Olivier Deprez157378f2022-04-04 15:47:50 +0200929 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
930 /*
931 * Skip CPUs which don't support the current stream
932 * type. See soc_pcm_init_runtime_hw() for more details
933 */
934 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
935 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936
Olivier Deprez157378f2022-04-04 15:47:50 +0200937 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
938 if (ret < 0)
939 goto interface_err;
David Brazdil0f672f62019-12-10 10:32:29 +0000940
Olivier Deprez157378f2022-04-04 15:47:50 +0200941 /* store the parameters for each DAI */
942 cpu_dai->rate = params_rate(params);
943 cpu_dai->channels = params_channels(params);
944 cpu_dai->sample_bits =
945 snd_pcm_format_physical_width(params_format(params));
David Brazdil0f672f62019-12-10 10:32:29 +0000946
Olivier Deprez157378f2022-04-04 15:47:50 +0200947 snd_soc_dapm_update_dai(substream, params, cpu_dai);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200949
950 ret = snd_soc_pcm_component_hw_params(substream, params, &component);
951 if (ret < 0)
952 goto component_err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000954out:
David Brazdil0f672f62019-12-10 10:32:29 +0000955 mutex_unlock(&rtd->card->pcm_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956 return ret;
957
958component_err:
Olivier Deprez157378f2022-04-04 15:47:50 +0200959 snd_soc_pcm_component_hw_free(substream, component);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960
Olivier Deprez157378f2022-04-04 15:47:50 +0200961 i = rtd->num_cpus;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962
963interface_err:
Olivier Deprez157378f2022-04-04 15:47:50 +0200964 for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
965 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
966 continue;
967
968 snd_soc_dai_hw_free(cpu_dai, substream);
969 cpu_dai->rate = 0;
970 }
971
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972 i = rtd->num_codecs;
973
974codec_err:
Olivier Deprez157378f2022-04-04 15:47:50 +0200975 for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
David Brazdil0f672f62019-12-10 10:32:29 +0000976 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
977 continue;
978
979 snd_soc_dai_hw_free(codec_dai, substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980 codec_dai->rate = 0;
981 }
982
Olivier Deprez157378f2022-04-04 15:47:50 +0200983 snd_soc_link_hw_free(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984
David Brazdil0f672f62019-12-10 10:32:29 +0000985 mutex_unlock(&rtd->card->pcm_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000986 return ret;
987}
988
989/*
990 * Frees resources allocated by hw_params, can be called multiple times
991 */
992static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
993{
Olivier Deprez157378f2022-04-04 15:47:50 +0200994 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
995 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000996 int i;
997
David Brazdil0f672f62019-12-10 10:32:29 +0000998 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000999
1000 /* clear the corresponding DAIs parameters when going to be inactive */
Olivier Deprez157378f2022-04-04 15:47:50 +02001001 for_each_rtd_dais(rtd, i, dai) {
1002 int active = snd_soc_dai_stream_active(dai, substream->stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003
Olivier Deprez157378f2022-04-04 15:47:50 +02001004 if (snd_soc_dai_active(dai) == 1) {
1005 dai->rate = 0;
1006 dai->channels = 0;
1007 dai->sample_bits = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001008 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009
Olivier Deprez157378f2022-04-04 15:47:50 +02001010 if (active == 1)
1011 snd_soc_dai_digital_mute(dai, 1, substream->stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001012 }
1013
1014 /* free any machine hw params */
Olivier Deprez157378f2022-04-04 15:47:50 +02001015 snd_soc_link_hw_free(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001016
1017 /* free any component resources */
Olivier Deprez157378f2022-04-04 15:47:50 +02001018 snd_soc_pcm_component_hw_free(substream, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019
1020 /* now free hw params for the DAIs */
Olivier Deprez157378f2022-04-04 15:47:50 +02001021 for_each_rtd_dais(rtd, i, dai) {
1022 if (!snd_soc_dai_stream_valid(dai, substream->stream))
David Brazdil0f672f62019-12-10 10:32:29 +00001023 continue;
1024
Olivier Deprez157378f2022-04-04 15:47:50 +02001025 snd_soc_dai_hw_free(dai, substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001026 }
1027
David Brazdil0f672f62019-12-10 10:32:29 +00001028 mutex_unlock(&rtd->card->pcm_mutex);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029 return 0;
1030}
1031
1032static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1033{
Olivier Deprez157378f2022-04-04 15:47:50 +02001034 int ret = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035
Olivier Deprez157378f2022-04-04 15:47:50 +02001036 switch (cmd) {
1037 case SNDRV_PCM_TRIGGER_START:
1038 case SNDRV_PCM_TRIGGER_RESUME:
1039 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1040 ret = snd_soc_link_trigger(substream, cmd);
David Brazdil0f672f62019-12-10 10:32:29 +00001041 if (ret < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +02001042 break;
1043
1044 ret = snd_soc_pcm_component_trigger(substream, cmd);
1045 if (ret < 0)
1046 break;
1047
1048 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1049 break;
1050 case SNDRV_PCM_TRIGGER_STOP:
1051 case SNDRV_PCM_TRIGGER_SUSPEND:
1052 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1053 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1054 if (ret < 0)
1055 break;
1056
1057 ret = snd_soc_pcm_component_trigger(substream, cmd);
1058 if (ret < 0)
1059 break;
1060
1061 ret = snd_soc_link_trigger(substream, cmd);
1062 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063 }
1064
Olivier Deprez157378f2022-04-04 15:47:50 +02001065 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001066}
1067
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068/*
1069 * soc level wrapper for pointer callback
1070 * If cpu_dai, codec_dai, component driver has the delay callback, then
1071 * the runtime->delay will be updated accordingly.
1072 */
1073static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1074{
Olivier Deprez157378f2022-04-04 15:47:50 +02001075 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1076 struct snd_soc_dai *cpu_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001077 struct snd_soc_dai *codec_dai;
1078 struct snd_pcm_runtime *runtime = substream->runtime;
1079 snd_pcm_uframes_t offset = 0;
1080 snd_pcm_sframes_t delay = 0;
1081 snd_pcm_sframes_t codec_delay = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001082 snd_pcm_sframes_t cpu_delay = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001083 int i;
1084
1085 /* clearing the previous total delay */
1086 runtime->delay = 0;
1087
David Brazdil0f672f62019-12-10 10:32:29 +00001088 offset = snd_soc_pcm_component_pointer(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001090 /* base delay if assigned in pointer callback */
1091 delay = runtime->delay;
1092
Olivier Deprez157378f2022-04-04 15:47:50 +02001093 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1094 cpu_delay = max(cpu_delay,
1095 snd_soc_dai_delay(cpu_dai, substream));
1096 }
1097 delay += cpu_delay;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001098
Olivier Deprez157378f2022-04-04 15:47:50 +02001099 for_each_rtd_codec_dais(rtd, i, codec_dai) {
David Brazdil0f672f62019-12-10 10:32:29 +00001100 codec_delay = max(codec_delay,
1101 snd_soc_dai_delay(codec_dai, substream));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102 }
1103 delay += codec_delay;
1104
1105 runtime->delay = delay;
1106
1107 return offset;
1108}
1109
1110/* connect a FE and BE */
1111static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1112 struct snd_soc_pcm_runtime *be, int stream)
1113{
1114 struct snd_soc_dpcm *dpcm;
David Brazdil0f672f62019-12-10 10:32:29 +00001115 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116
1117 /* only add new dpcms */
David Brazdil0f672f62019-12-10 10:32:29 +00001118 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001119 if (dpcm->be == be && dpcm->fe == fe)
1120 return 0;
1121 }
1122
1123 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1124 if (!dpcm)
1125 return -ENOMEM;
1126
1127 dpcm->be = be;
1128 dpcm->fe = fe;
1129 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1130 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
David Brazdil0f672f62019-12-10 10:32:29 +00001131 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001132 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1133 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
David Brazdil0f672f62019-12-10 10:32:29 +00001134 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001135
1136 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1137 stream ? "capture" : "playback", fe->dai_link->name,
1138 stream ? "<-" : "->", be->dai_link->name);
1139
Olivier Deprez157378f2022-04-04 15:47:50 +02001140 dpcm_create_debugfs_state(dpcm, stream);
1141
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 return 1;
1143}
1144
1145/* reparent a BE onto another FE */
1146static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1147 struct snd_soc_pcm_runtime *be, int stream)
1148{
1149 struct snd_soc_dpcm *dpcm;
1150 struct snd_pcm_substream *fe_substream, *be_substream;
1151
1152 /* reparent if BE is connected to other FEs */
1153 if (!be->dpcm[stream].users)
1154 return;
1155
1156 be_substream = snd_soc_dpcm_get_substream(be, stream);
1157
David Brazdil0f672f62019-12-10 10:32:29 +00001158 for_each_dpcm_fe(be, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001159 if (dpcm->fe == fe)
1160 continue;
1161
1162 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1163 stream ? "capture" : "playback",
1164 dpcm->fe->dai_link->name,
1165 stream ? "<-" : "->", dpcm->be->dai_link->name);
1166
1167 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1168 be_substream->runtime = fe_substream->runtime;
1169 break;
1170 }
1171}
1172
1173/* disconnect a BE and FE */
1174void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1175{
1176 struct snd_soc_dpcm *dpcm, *d;
David Brazdil0f672f62019-12-10 10:32:29 +00001177 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001178
David Brazdil0f672f62019-12-10 10:32:29 +00001179 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1181 stream ? "capture" : "playback",
1182 dpcm->be->dai_link->name);
1183
1184 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1185 continue;
1186
1187 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1188 stream ? "capture" : "playback", fe->dai_link->name,
1189 stream ? "<-" : "->", dpcm->be->dai_link->name);
1190
1191 /* BEs still alive need new FE */
1192 dpcm_be_reparent(fe, dpcm->be, stream);
1193
Olivier Deprez157378f2022-04-04 15:47:50 +02001194 dpcm_remove_debugfs_state(dpcm);
1195
David Brazdil0f672f62019-12-10 10:32:29 +00001196 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001197 list_del(&dpcm->list_be);
1198 list_del(&dpcm->list_fe);
David Brazdil0f672f62019-12-10 10:32:29 +00001199 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001200 kfree(dpcm);
1201 }
1202}
1203
1204/* get BE for DAI widget and stream */
1205static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1206 struct snd_soc_dapm_widget *widget, int stream)
1207{
1208 struct snd_soc_pcm_runtime *be;
Olivier Deprez157378f2022-04-04 15:47:50 +02001209 struct snd_soc_dapm_widget *w;
David Brazdil0f672f62019-12-10 10:32:29 +00001210 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001211 int i;
1212
1213 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1214
Olivier Deprez157378f2022-04-04 15:47:50 +02001215 for_each_card_rtds(card, be) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001216
Olivier Deprez157378f2022-04-04 15:47:50 +02001217 if (!be->dai_link->no_pcm)
1218 continue;
1219
1220 for_each_rtd_dais(be, i, dai) {
1221 w = snd_soc_dai_get_widget(dai, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001222
1223 dev_dbg(card->dev, "ASoC: try BE : %s\n",
Olivier Deprez157378f2022-04-04 15:47:50 +02001224 w ? w->name : "(not set)");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225
Olivier Deprez157378f2022-04-04 15:47:50 +02001226 if (w == widget)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001227 return be;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001228 }
1229 }
1230
Olivier Deprez157378f2022-04-04 15:47:50 +02001231 /* Widget provided is not a BE */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001232 return NULL;
1233}
1234
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001235static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1236 struct snd_soc_dapm_widget *widget)
1237{
Olivier Deprez157378f2022-04-04 15:47:50 +02001238 struct snd_soc_dapm_widget *w;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239 int i;
1240
Olivier Deprez157378f2022-04-04 15:47:50 +02001241 for_each_dapm_widgets(list, i, w)
1242 if (widget == w)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 return 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001244
1245 return 0;
1246}
1247
1248static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1249 enum snd_soc_dapm_direction dir)
1250{
1251 struct snd_soc_card *card = widget->dapm->card;
1252 struct snd_soc_pcm_runtime *rtd;
Olivier Deprez157378f2022-04-04 15:47:50 +02001253 int stream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001254
Olivier Deprez157378f2022-04-04 15:47:50 +02001255 /* adjust dir to stream */
1256 if (dir == SND_SOC_DAPM_DIR_OUT)
1257 stream = SNDRV_PCM_STREAM_PLAYBACK;
1258 else
1259 stream = SNDRV_PCM_STREAM_CAPTURE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001260
Olivier Deprez157378f2022-04-04 15:47:50 +02001261 rtd = dpcm_get_be(card, widget, stream);
1262 if (rtd)
1263 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001264
1265 return false;
1266}
1267
1268int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1269 int stream, struct snd_soc_dapm_widget_list **list)
1270{
Olivier Deprez157378f2022-04-04 15:47:50 +02001271 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001272 int paths;
1273
Olivier Deprez157378f2022-04-04 15:47:50 +02001274 if (fe->num_cpus > 1) {
1275 dev_err(fe->dev,
1276 "%s doesn't support Multi CPU yet\n", __func__);
1277 return -EINVAL;
1278 }
1279
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001280 /* get number of valid DAI paths and their widgets */
1281 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1282 dpcm_end_walk_at_be);
1283
1284 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1285 stream ? "capture" : "playback");
1286
1287 return paths;
1288}
1289
Olivier Deprez157378f2022-04-04 15:47:50 +02001290void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291{
Olivier Deprez157378f2022-04-04 15:47:50 +02001292 snd_soc_dapm_dai_free_widgets(list);
1293}
1294
1295static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1296 struct snd_soc_dapm_widget_list *list)
1297{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001298 struct snd_soc_dapm_widget *widget;
David Brazdil0f672f62019-12-10 10:32:29 +00001299 struct snd_soc_dai *dai;
Olivier Deprez157378f2022-04-04 15:47:50 +02001300 unsigned int i;
1301
1302 /* is there a valid DAI widget for this BE */
1303 for_each_rtd_dais(dpcm->be, i, dai) {
1304 widget = snd_soc_dai_get_widget(dai, stream);
1305
1306 /*
1307 * The BE is pruned only if none of the dai
1308 * widgets are in the active list.
1309 */
1310 if (widget && widget_in_list(list, widget))
1311 return true;
1312 }
1313
1314 return false;
1315}
1316
1317static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1318 struct snd_soc_dapm_widget_list **list_)
1319{
1320 struct snd_soc_dpcm *dpcm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001321 int prune = 0;
1322
1323 /* Destroy any old FE <--> BE connections */
David Brazdil0f672f62019-12-10 10:32:29 +00001324 for_each_dpcm_be(fe, stream, dpcm) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001325 if (dpcm_be_is_active(dpcm, stream, *list_))
Olivier Deprez0e641232021-09-23 10:07:05 +02001326 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001327
1328 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1329 stream ? "capture" : "playback",
1330 dpcm->be->dai_link->name, fe->dai_link->name);
1331 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1332 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1333 prune++;
1334 }
1335
1336 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1337 return prune;
1338}
1339
1340static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1341 struct snd_soc_dapm_widget_list **list_)
1342{
1343 struct snd_soc_card *card = fe->card;
1344 struct snd_soc_dapm_widget_list *list = *list_;
1345 struct snd_soc_pcm_runtime *be;
Olivier Deprez157378f2022-04-04 15:47:50 +02001346 struct snd_soc_dapm_widget *widget;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001347 int i, new = 0, err;
1348
1349 /* Create any new FE <--> BE connections */
Olivier Deprez157378f2022-04-04 15:47:50 +02001350 for_each_dapm_widgets(list, i, widget) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001351
Olivier Deprez157378f2022-04-04 15:47:50 +02001352 switch (widget->id) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001353 case snd_soc_dapm_dai_in:
1354 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1355 continue;
1356 break;
1357 case snd_soc_dapm_dai_out:
1358 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1359 continue;
1360 break;
1361 default:
1362 continue;
1363 }
1364
1365 /* is there a valid BE rtd for this widget */
Olivier Deprez157378f2022-04-04 15:47:50 +02001366 be = dpcm_get_be(card, widget, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001367 if (!be) {
1368 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Olivier Deprez157378f2022-04-04 15:47:50 +02001369 widget->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001370 continue;
1371 }
1372
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001373 /* don't connect if FE is not running */
1374 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1375 continue;
1376
1377 /* newly connected FE and BE */
1378 err = dpcm_be_connect(fe, be, stream);
1379 if (err < 0) {
1380 dev_err(fe->dev, "ASoC: can't connect %s\n",
Olivier Deprez157378f2022-04-04 15:47:50 +02001381 widget->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001382 break;
1383 } else if (err == 0) /* already connected */
1384 continue;
1385
1386 /* new */
1387 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1388 new++;
1389 }
1390
1391 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1392 return new;
1393}
1394
1395/*
1396 * Find the corresponding BE DAIs that source or sink audio to this
1397 * FE substream.
1398 */
1399int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1400 int stream, struct snd_soc_dapm_widget_list **list, int new)
1401{
1402 if (new)
1403 return dpcm_add_paths(fe, stream, list);
1404 else
1405 return dpcm_prune_paths(fe, stream, list);
1406}
1407
1408void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1409{
1410 struct snd_soc_dpcm *dpcm;
David Brazdil0f672f62019-12-10 10:32:29 +00001411 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001412
David Brazdil0f672f62019-12-10 10:32:29 +00001413 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1414 for_each_dpcm_be(fe, stream, dpcm)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001415 dpcm->be->dpcm[stream].runtime_update =
1416 SND_SOC_DPCM_UPDATE_NO;
David Brazdil0f672f62019-12-10 10:32:29 +00001417 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418}
1419
1420static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1421 int stream)
1422{
1423 struct snd_soc_dpcm *dpcm;
1424
1425 /* disable any enabled and non active backends */
David Brazdil0f672f62019-12-10 10:32:29 +00001426 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001427
1428 struct snd_soc_pcm_runtime *be = dpcm->be;
1429 struct snd_pcm_substream *be_substream =
1430 snd_soc_dpcm_get_substream(be, stream);
1431
1432 if (be->dpcm[stream].users == 0)
1433 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1434 stream ? "capture" : "playback",
1435 be->dpcm[stream].state);
1436
1437 if (--be->dpcm[stream].users != 0)
1438 continue;
1439
1440 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1441 continue;
1442
1443 soc_pcm_close(be_substream);
1444 be_substream->runtime = NULL;
1445 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1446 }
1447}
1448
1449int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1450{
1451 struct snd_soc_dpcm *dpcm;
1452 int err, count = 0;
1453
1454 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
David Brazdil0f672f62019-12-10 10:32:29 +00001455 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456
1457 struct snd_soc_pcm_runtime *be = dpcm->be;
1458 struct snd_pcm_substream *be_substream =
1459 snd_soc_dpcm_get_substream(be, stream);
1460
1461 if (!be_substream) {
1462 dev_err(be->dev, "ASoC: no backend %s stream\n",
1463 stream ? "capture" : "playback");
1464 continue;
1465 }
1466
1467 /* is this op for this BE ? */
1468 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1469 continue;
1470
1471 /* first time the dpcm is open ? */
1472 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1473 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1474 stream ? "capture" : "playback",
1475 be->dpcm[stream].state);
1476
1477 if (be->dpcm[stream].users++ != 0)
1478 continue;
1479
1480 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1481 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1482 continue;
1483
1484 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1485 stream ? "capture" : "playback", be->dai_link->name);
1486
1487 be_substream->runtime = be->dpcm[stream].runtime;
1488 err = soc_pcm_open(be_substream);
1489 if (err < 0) {
1490 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1491 be->dpcm[stream].users--;
1492 if (be->dpcm[stream].users < 0)
1493 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1494 stream ? "capture" : "playback",
1495 be->dpcm[stream].state);
1496
1497 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1498 goto unwind;
1499 }
1500
1501 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1502 count++;
1503 }
1504
1505 return count;
1506
1507unwind:
1508 /* disable any enabled and non active backends */
David Brazdil0f672f62019-12-10 10:32:29 +00001509 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001510 struct snd_soc_pcm_runtime *be = dpcm->be;
1511 struct snd_pcm_substream *be_substream =
1512 snd_soc_dpcm_get_substream(be, stream);
1513
1514 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1515 continue;
1516
1517 if (be->dpcm[stream].users == 0)
1518 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1519 stream ? "capture" : "playback",
1520 be->dpcm[stream].state);
1521
1522 if (--be->dpcm[stream].users != 0)
1523 continue;
1524
1525 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1526 continue;
1527
1528 soc_pcm_close(be_substream);
1529 be_substream->runtime = NULL;
1530 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1531 }
1532
1533 return err;
1534}
1535
1536static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1537 struct snd_soc_pcm_stream *stream)
1538{
1539 runtime->hw.rate_min = stream->rate_min;
David Brazdil0f672f62019-12-10 10:32:29 +00001540 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001541 runtime->hw.channels_min = stream->channels_min;
1542 runtime->hw.channels_max = stream->channels_max;
1543 if (runtime->hw.formats)
1544 runtime->hw.formats &= stream->formats;
1545 else
1546 runtime->hw.formats = stream->formats;
1547 runtime->hw.rates = stream->rates;
1548}
1549
1550static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1551 u64 *formats)
1552{
Olivier Deprez157378f2022-04-04 15:47:50 +02001553 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554 struct snd_soc_dpcm *dpcm;
David Brazdil0f672f62019-12-10 10:32:29 +00001555 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001556 int stream = substream->stream;
1557
1558 if (!fe->dai_link->dpcm_merged_format)
1559 return;
1560
1561 /*
1562 * It returns merged BE codec format
1563 * if FE want to use it (= dpcm_merged_format)
1564 */
1565
David Brazdil0f672f62019-12-10 10:32:29 +00001566 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001567 struct snd_soc_pcm_runtime *be = dpcm->be;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001568 struct snd_soc_pcm_stream *codec_stream;
1569 int i;
1570
Olivier Deprez157378f2022-04-04 15:47:50 +02001571 for_each_rtd_codec_dais(be, i, dai) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001572 /*
1573 * Skip CODECs which don't support the current stream
1574 * type. See soc_pcm_init_runtime_hw() for more details
1575 */
David Brazdil0f672f62019-12-10 10:32:29 +00001576 if (!snd_soc_dai_stream_valid(dai, stream))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001577 continue;
1578
Olivier Deprez157378f2022-04-04 15:47:50 +02001579 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001580
1581 *formats &= codec_stream->formats;
1582 }
1583 }
1584}
1585
1586static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1587 unsigned int *channels_min,
1588 unsigned int *channels_max)
1589{
Olivier Deprez157378f2022-04-04 15:47:50 +02001590 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001591 struct snd_soc_dpcm *dpcm;
1592 int stream = substream->stream;
1593
1594 if (!fe->dai_link->dpcm_merged_chan)
1595 return;
1596
1597 /*
1598 * It returns merged BE codec channel;
1599 * if FE want to use it (= dpcm_merged_chan)
1600 */
1601
David Brazdil0f672f62019-12-10 10:32:29 +00001602 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001603 struct snd_soc_pcm_runtime *be = dpcm->be;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001604 struct snd_soc_pcm_stream *codec_stream;
1605 struct snd_soc_pcm_stream *cpu_stream;
Olivier Deprez157378f2022-04-04 15:47:50 +02001606 struct snd_soc_dai *dai;
1607 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001608
Olivier Deprez157378f2022-04-04 15:47:50 +02001609 for_each_rtd_cpu_dais(be, i, dai) {
1610 /*
1611 * Skip CPUs which don't support the current stream
1612 * type. See soc_pcm_init_runtime_hw() for more details
1613 */
1614 if (!snd_soc_dai_stream_valid(dai, stream))
1615 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001616
Olivier Deprez157378f2022-04-04 15:47:50 +02001617 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1618
1619 *channels_min = max(*channels_min,
1620 cpu_stream->channels_min);
1621 *channels_max = min(*channels_max,
1622 cpu_stream->channels_max);
1623 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001624
1625 /*
1626 * chan min/max cannot be enforced if there are multiple CODEC
1627 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1628 */
1629 if (be->num_codecs == 1) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001630 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001631
1632 *channels_min = max(*channels_min,
1633 codec_stream->channels_min);
1634 *channels_max = min(*channels_max,
1635 codec_stream->channels_max);
1636 }
1637 }
1638}
1639
1640static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1641 unsigned int *rates,
1642 unsigned int *rate_min,
1643 unsigned int *rate_max)
1644{
Olivier Deprez157378f2022-04-04 15:47:50 +02001645 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646 struct snd_soc_dpcm *dpcm;
1647 int stream = substream->stream;
1648
1649 if (!fe->dai_link->dpcm_merged_rate)
1650 return;
1651
1652 /*
1653 * It returns merged BE codec channel;
1654 * if FE want to use it (= dpcm_merged_chan)
1655 */
1656
David Brazdil0f672f62019-12-10 10:32:29 +00001657 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001658 struct snd_soc_pcm_runtime *be = dpcm->be;
Olivier Deprez157378f2022-04-04 15:47:50 +02001659 struct snd_soc_pcm_stream *pcm;
David Brazdil0f672f62019-12-10 10:32:29 +00001660 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001661 int i;
1662
Olivier Deprez157378f2022-04-04 15:47:50 +02001663 for_each_rtd_dais(be, i, dai) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001664 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02001665 * Skip DAIs which don't support the current stream
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001666 * type. See soc_pcm_init_runtime_hw() for more details
1667 */
David Brazdil0f672f62019-12-10 10:32:29 +00001668 if (!snd_soc_dai_stream_valid(dai, stream))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001669 continue;
1670
Olivier Deprez157378f2022-04-04 15:47:50 +02001671 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001672
Olivier Deprez157378f2022-04-04 15:47:50 +02001673 *rate_min = max(*rate_min, pcm->rate_min);
1674 *rate_max = min_not_zero(*rate_max, pcm->rate_max);
1675 *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001676 }
1677 }
1678}
1679
1680static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1681{
1682 struct snd_pcm_runtime *runtime = substream->runtime;
Olivier Deprez157378f2022-04-04 15:47:50 +02001683 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1684 struct snd_soc_dai *cpu_dai;
1685 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001686
Olivier Deprez157378f2022-04-04 15:47:50 +02001687 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1688 /*
1689 * Skip CPUs which don't support the current stream
1690 * type. See soc_pcm_init_runtime_hw() for more details
1691 */
1692 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1693 continue;
1694
1695 dpcm_init_runtime_hw(runtime,
1696 snd_soc_dai_get_pcm_stream(cpu_dai,
1697 substream->stream));
1698 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001699
1700 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1701 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1702 &runtime->hw.channels_max);
1703 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1704 &runtime->hw.rate_min, &runtime->hw.rate_max);
1705}
1706
1707static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1708
1709/* Set FE's runtime_update state; the state is protected via PCM stream lock
1710 * for avoiding the race with trigger callback.
1711 * If the state is unset and a trigger is pending while the previous operation,
1712 * process the pending trigger action here.
1713 */
1714static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1715 int stream, enum snd_soc_dpcm_update state)
1716{
1717 struct snd_pcm_substream *substream =
1718 snd_soc_dpcm_get_substream(fe, stream);
1719
1720 snd_pcm_stream_lock_irq(substream);
1721 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1722 dpcm_fe_dai_do_trigger(substream,
1723 fe->dpcm[stream].trigger_pending - 1);
1724 fe->dpcm[stream].trigger_pending = 0;
1725 }
1726 fe->dpcm[stream].runtime_update = state;
1727 snd_pcm_stream_unlock_irq(substream);
1728}
1729
1730static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1731 int stream)
1732{
1733 struct snd_soc_dpcm *dpcm;
Olivier Deprez157378f2022-04-04 15:47:50 +02001734 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1735 struct snd_soc_dai *fe_cpu_dai;
1736 int err = 0;
1737 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001738
1739 /* apply symmetry for FE */
1740 if (soc_pcm_has_symmetry(fe_substream))
1741 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1742
Olivier Deprez157378f2022-04-04 15:47:50 +02001743 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1744 /* Symmetry only applies if we've got an active stream. */
1745 if (snd_soc_dai_active(fe_cpu_dai)) {
1746 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1747 if (err < 0)
1748 return err;
1749 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001750 }
1751
1752 /* apply symmetry for BE */
David Brazdil0f672f62019-12-10 10:32:29 +00001753 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001754 struct snd_soc_pcm_runtime *be = dpcm->be;
1755 struct snd_pcm_substream *be_substream =
1756 snd_soc_dpcm_get_substream(be, stream);
David Brazdil0f672f62019-12-10 10:32:29 +00001757 struct snd_soc_pcm_runtime *rtd;
Olivier Deprez157378f2022-04-04 15:47:50 +02001758 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001759 int i;
1760
David Brazdil0f672f62019-12-10 10:32:29 +00001761 /* A backend may not have the requested substream */
1762 if (!be_substream)
1763 continue;
1764
Olivier Deprez157378f2022-04-04 15:47:50 +02001765 rtd = asoc_substream_to_rtd(be_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001766 if (rtd->dai_link->be_hw_params_fixup)
1767 continue;
1768
1769 if (soc_pcm_has_symmetry(be_substream))
1770 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1771
1772 /* Symmetry only applies if we've got an active stream. */
Olivier Deprez157378f2022-04-04 15:47:50 +02001773 for_each_rtd_dais(rtd, i, dai) {
1774 if (snd_soc_dai_active(dai)) {
1775 err = soc_pcm_apply_symmetry(fe_substream, dai);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001776 if (err < 0)
1777 return err;
1778 }
1779 }
1780 }
1781
1782 return 0;
1783}
1784
1785static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1786{
Olivier Deprez157378f2022-04-04 15:47:50 +02001787 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1789 int stream = fe_substream->stream, ret = 0;
1790
1791 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1792
Olivier Deprez157378f2022-04-04 15:47:50 +02001793 ret = dpcm_be_dai_startup(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001794 if (ret < 0) {
1795 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1796 goto be_err;
1797 }
1798
1799 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1800
1801 /* start the DAI frontend */
1802 ret = soc_pcm_open(fe_substream);
1803 if (ret < 0) {
1804 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1805 goto unwind;
1806 }
1807
1808 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1809
1810 dpcm_set_fe_runtime(fe_substream);
1811 snd_pcm_limit_hw_rates(runtime);
1812
1813 ret = dpcm_apply_symmetry(fe_substream, stream);
Olivier Deprez157378f2022-04-04 15:47:50 +02001814 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001815 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1816 ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817
1818unwind:
Olivier Deprez157378f2022-04-04 15:47:50 +02001819 if (ret < 0)
1820 dpcm_be_dai_startup_unwind(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001821be_err:
1822 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1823 return ret;
1824}
1825
1826int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1827{
1828 struct snd_soc_dpcm *dpcm;
1829
1830 /* only shutdown BEs that are either sinks or sources to this FE DAI */
David Brazdil0f672f62019-12-10 10:32:29 +00001831 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832
1833 struct snd_soc_pcm_runtime *be = dpcm->be;
1834 struct snd_pcm_substream *be_substream =
1835 snd_soc_dpcm_get_substream(be, stream);
1836
1837 /* is this op for this BE ? */
1838 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1839 continue;
1840
1841 if (be->dpcm[stream].users == 0)
1842 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1843 stream ? "capture" : "playback",
1844 be->dpcm[stream].state);
1845
1846 if (--be->dpcm[stream].users != 0)
1847 continue;
1848
1849 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1850 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1851 soc_pcm_hw_free(be_substream);
1852 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1853 }
1854
1855 dev_dbg(be->dev, "ASoC: close BE %s\n",
1856 be->dai_link->name);
1857
1858 soc_pcm_close(be_substream);
1859 be_substream->runtime = NULL;
1860
1861 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1862 }
1863 return 0;
1864}
1865
1866static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1867{
Olivier Deprez157378f2022-04-04 15:47:50 +02001868 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001869 int stream = substream->stream;
1870
1871 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1872
1873 /* shutdown the BEs */
Olivier Deprez157378f2022-04-04 15:47:50 +02001874 dpcm_be_dai_shutdown(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001875
1876 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1877
1878 /* now shutdown the frontend */
1879 soc_pcm_close(substream);
1880
1881 /* run the stream event for each BE */
1882 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1883
1884 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1885 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1886 return 0;
1887}
1888
1889int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1890{
1891 struct snd_soc_dpcm *dpcm;
1892
1893 /* only hw_params backends that are either sinks or sources
1894 * to this frontend DAI */
David Brazdil0f672f62019-12-10 10:32:29 +00001895 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001896
1897 struct snd_soc_pcm_runtime *be = dpcm->be;
1898 struct snd_pcm_substream *be_substream =
1899 snd_soc_dpcm_get_substream(be, stream);
1900
1901 /* is this op for this BE ? */
1902 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1903 continue;
1904
1905 /* only free hw when no longer used - check all FEs */
1906 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1907 continue;
1908
1909 /* do not free hw if this BE is used by other FE */
1910 if (be->dpcm[stream].users > 1)
1911 continue;
1912
1913 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1914 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1915 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1916 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1917 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1918 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1919 continue;
1920
1921 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1922 be->dai_link->name);
1923
1924 soc_pcm_hw_free(be_substream);
1925
1926 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1927 }
1928
1929 return 0;
1930}
1931
1932static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1933{
Olivier Deprez157378f2022-04-04 15:47:50 +02001934 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001935 int err, stream = substream->stream;
1936
1937 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1938 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1939
1940 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1941
1942 /* call hw_free on the frontend */
1943 err = soc_pcm_hw_free(substream);
1944 if (err < 0)
1945 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1946 fe->dai_link->name);
1947
1948 /* only hw_params backends that are either sinks or sources
1949 * to this frontend DAI */
1950 err = dpcm_be_dai_hw_free(fe, stream);
1951
1952 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1953 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1954
1955 mutex_unlock(&fe->card->mutex);
1956 return 0;
1957}
1958
1959int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1960{
1961 struct snd_soc_dpcm *dpcm;
1962 int ret;
1963
David Brazdil0f672f62019-12-10 10:32:29 +00001964 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001965
1966 struct snd_soc_pcm_runtime *be = dpcm->be;
1967 struct snd_pcm_substream *be_substream =
1968 snd_soc_dpcm_get_substream(be, stream);
1969
1970 /* is this op for this BE ? */
1971 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1972 continue;
1973
1974 /* copy params for each dpcm */
1975 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1976 sizeof(struct snd_pcm_hw_params));
1977
1978 /* perform any hw_params fixups */
Olivier Deprez157378f2022-04-04 15:47:50 +02001979 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1980 if (ret < 0)
1981 goto unwind;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001982
David Brazdil0f672f62019-12-10 10:32:29 +00001983 /* copy the fixed-up hw params for BE dai */
1984 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1985 sizeof(struct snd_pcm_hw_params));
1986
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001987 /* only allow hw_params() if no connected FEs are running */
1988 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1989 continue;
1990
1991 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1992 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1993 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1994 continue;
1995
1996 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1997 be->dai_link->name);
1998
1999 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2000 if (ret < 0) {
2001 dev_err(dpcm->be->dev,
2002 "ASoC: hw_params BE failed %d\n", ret);
2003 goto unwind;
2004 }
2005
2006 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2007 }
2008 return 0;
2009
2010unwind:
2011 /* disable any enabled and non active backends */
David Brazdil0f672f62019-12-10 10:32:29 +00002012 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002013 struct snd_soc_pcm_runtime *be = dpcm->be;
2014 struct snd_pcm_substream *be_substream =
2015 snd_soc_dpcm_get_substream(be, stream);
2016
2017 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2018 continue;
2019
2020 /* only allow hw_free() if no connected FEs are running */
2021 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2022 continue;
2023
2024 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2025 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2026 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2027 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2028 continue;
2029
2030 soc_pcm_hw_free(be_substream);
2031 }
2032
2033 return ret;
2034}
2035
2036static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2037 struct snd_pcm_hw_params *params)
2038{
Olivier Deprez157378f2022-04-04 15:47:50 +02002039 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002040 int ret, stream = substream->stream;
2041
2042 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2043 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2044
Olivier Deprez157378f2022-04-04 15:47:50 +02002045 memcpy(&fe->dpcm[stream].hw_params, params,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002046 sizeof(struct snd_pcm_hw_params));
Olivier Deprez157378f2022-04-04 15:47:50 +02002047 ret = dpcm_be_dai_hw_params(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002048 if (ret < 0) {
2049 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2050 goto out;
2051 }
2052
2053 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2054 fe->dai_link->name, params_rate(params),
2055 params_channels(params), params_format(params));
2056
2057 /* call hw_params on the frontend */
2058 ret = soc_pcm_hw_params(substream, params);
2059 if (ret < 0) {
2060 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2061 dpcm_be_dai_hw_free(fe, stream);
2062 } else
2063 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2064
2065out:
2066 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2067 mutex_unlock(&fe->card->mutex);
2068 return ret;
2069}
2070
2071static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2072 struct snd_pcm_substream *substream, int cmd)
2073{
2074 int ret;
2075
2076 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2077 dpcm->be->dai_link->name, cmd);
2078
2079 ret = soc_pcm_trigger(substream, cmd);
2080 if (ret < 0)
2081 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2082
2083 return ret;
2084}
2085
2086int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2087 int cmd)
2088{
2089 struct snd_soc_dpcm *dpcm;
2090 int ret = 0;
2091
David Brazdil0f672f62019-12-10 10:32:29 +00002092 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002093
2094 struct snd_soc_pcm_runtime *be = dpcm->be;
2095 struct snd_pcm_substream *be_substream =
2096 snd_soc_dpcm_get_substream(be, stream);
2097
2098 /* is this op for this BE ? */
2099 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2100 continue;
2101
2102 switch (cmd) {
2103 case SNDRV_PCM_TRIGGER_START:
2104 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
Olivier Deprez0e641232021-09-23 10:07:05 +02002105 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2106 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002107 continue;
2108
2109 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2110 if (ret)
2111 return ret;
2112
2113 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2114 break;
2115 case SNDRV_PCM_TRIGGER_RESUME:
2116 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2117 continue;
2118
2119 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2120 if (ret)
2121 return ret;
2122
2123 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2124 break;
2125 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2126 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2127 continue;
2128
2129 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2130 if (ret)
2131 return ret;
2132
2133 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2134 break;
2135 case SNDRV_PCM_TRIGGER_STOP:
Olivier Deprez0e641232021-09-23 10:07:05 +02002136 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2137 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002138 continue;
2139
2140 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2141 continue;
2142
2143 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2144 if (ret)
2145 return ret;
2146
2147 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2148 break;
2149 case SNDRV_PCM_TRIGGER_SUSPEND:
2150 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2151 continue;
2152
2153 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2154 continue;
2155
2156 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2157 if (ret)
2158 return ret;
2159
2160 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2161 break;
2162 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2163 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2164 continue;
2165
2166 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2167 continue;
2168
2169 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2170 if (ret)
2171 return ret;
2172
2173 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2174 break;
2175 }
2176 }
2177
2178 return ret;
2179}
2180EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2181
Olivier Deprez0e641232021-09-23 10:07:05 +02002182static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2183 int cmd, bool fe_first)
2184{
Olivier Deprez157378f2022-04-04 15:47:50 +02002185 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Olivier Deprez0e641232021-09-23 10:07:05 +02002186 int ret;
2187
2188 /* call trigger on the frontend before the backend. */
2189 if (fe_first) {
2190 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2191 fe->dai_link->name, cmd);
2192
2193 ret = soc_pcm_trigger(substream, cmd);
2194 if (ret < 0)
2195 return ret;
2196
2197 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2198 return ret;
2199 }
2200
2201 /* call trigger on the frontend after the backend. */
2202 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2203 if (ret < 0)
2204 return ret;
2205
2206 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2207 fe->dai_link->name, cmd);
2208
2209 ret = soc_pcm_trigger(substream, cmd);
2210
2211 return ret;
2212}
2213
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002214static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2215{
Olivier Deprez157378f2022-04-04 15:47:50 +02002216 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Olivier Deprez0e641232021-09-23 10:07:05 +02002217 int stream = substream->stream;
2218 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002219 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2220
2221 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2222
2223 switch (trigger) {
2224 case SND_SOC_DPCM_TRIGGER_PRE:
Olivier Deprez0e641232021-09-23 10:07:05 +02002225 switch (cmd) {
2226 case SNDRV_PCM_TRIGGER_START:
2227 case SNDRV_PCM_TRIGGER_RESUME:
2228 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2229 case SNDRV_PCM_TRIGGER_DRAIN:
2230 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2231 break;
2232 case SNDRV_PCM_TRIGGER_STOP:
2233 case SNDRV_PCM_TRIGGER_SUSPEND:
2234 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2235 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2236 break;
2237 default:
2238 ret = -EINVAL;
2239 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002240 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002241 break;
2242 case SND_SOC_DPCM_TRIGGER_POST:
Olivier Deprez0e641232021-09-23 10:07:05 +02002243 switch (cmd) {
2244 case SNDRV_PCM_TRIGGER_START:
2245 case SNDRV_PCM_TRIGGER_RESUME:
2246 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2247 case SNDRV_PCM_TRIGGER_DRAIN:
2248 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2249 break;
2250 case SNDRV_PCM_TRIGGER_STOP:
2251 case SNDRV_PCM_TRIGGER_SUSPEND:
2252 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2253 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2254 break;
2255 default:
2256 ret = -EINVAL;
2257 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002258 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002259 break;
2260 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2261 /* bespoke trigger() - handles both FE and BEs */
2262
2263 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2264 fe->dai_link->name, cmd);
2265
Olivier Deprez157378f2022-04-04 15:47:50 +02002266 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002267 break;
2268 default:
2269 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2270 fe->dai_link->name);
2271 ret = -EINVAL;
2272 goto out;
2273 }
2274
Olivier Deprez0e641232021-09-23 10:07:05 +02002275 if (ret < 0) {
2276 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2277 cmd, ret);
2278 goto out;
2279 }
2280
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002281 switch (cmd) {
2282 case SNDRV_PCM_TRIGGER_START:
2283 case SNDRV_PCM_TRIGGER_RESUME:
2284 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2285 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2286 break;
2287 case SNDRV_PCM_TRIGGER_STOP:
2288 case SNDRV_PCM_TRIGGER_SUSPEND:
2289 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2290 break;
2291 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2292 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2293 break;
2294 }
2295
2296out:
2297 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2298 return ret;
2299}
2300
2301static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2302{
Olivier Deprez157378f2022-04-04 15:47:50 +02002303 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002304 int stream = substream->stream;
2305
2306 /* if FE's runtime_update is already set, we're in race;
2307 * process this trigger later at exit
2308 */
2309 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2310 fe->dpcm[stream].trigger_pending = cmd + 1;
2311 return 0; /* delayed, assuming it's successful */
2312 }
2313
2314 /* we're alone, let's trigger */
2315 return dpcm_fe_dai_do_trigger(substream, cmd);
2316}
2317
2318int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2319{
2320 struct snd_soc_dpcm *dpcm;
2321 int ret = 0;
2322
David Brazdil0f672f62019-12-10 10:32:29 +00002323 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002324
2325 struct snd_soc_pcm_runtime *be = dpcm->be;
2326 struct snd_pcm_substream *be_substream =
2327 snd_soc_dpcm_get_substream(be, stream);
2328
2329 /* is this op for this BE ? */
2330 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2331 continue;
2332
2333 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2334 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002335 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2336 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002337 continue;
2338
2339 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2340 be->dai_link->name);
2341
2342 ret = soc_pcm_prepare(be_substream);
2343 if (ret < 0) {
2344 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2345 ret);
2346 break;
2347 }
2348
2349 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2350 }
2351 return ret;
2352}
2353
2354static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2355{
Olivier Deprez157378f2022-04-04 15:47:50 +02002356 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002357 int stream = substream->stream, ret = 0;
2358
2359 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2360
2361 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2362
2363 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2364
2365 /* there is no point preparing this FE if there are no BEs */
2366 if (list_empty(&fe->dpcm[stream].be_clients)) {
2367 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2368 fe->dai_link->name);
2369 ret = -EINVAL;
2370 goto out;
2371 }
2372
Olivier Deprez157378f2022-04-04 15:47:50 +02002373 ret = dpcm_be_dai_prepare(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002374 if (ret < 0)
2375 goto out;
2376
2377 /* call prepare on the frontend */
2378 ret = soc_pcm_prepare(substream);
2379 if (ret < 0) {
2380 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2381 fe->dai_link->name);
2382 goto out;
2383 }
2384
2385 /* run the stream event for each BE */
2386 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2387 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2388
2389out:
2390 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2391 mutex_unlock(&fe->card->mutex);
2392
2393 return ret;
2394}
2395
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002396static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2397{
2398 struct snd_pcm_substream *substream =
2399 snd_soc_dpcm_get_substream(fe, stream);
2400 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2401 int err;
2402
2403 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2404 stream ? "capture" : "playback", fe->dai_link->name);
2405
2406 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2407 /* call bespoke trigger - FE takes care of all BE triggers */
2408 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2409 fe->dai_link->name);
2410
Olivier Deprez157378f2022-04-04 15:47:50 +02002411 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002412 if (err < 0)
2413 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2414 } else {
2415 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2416 fe->dai_link->name);
2417
2418 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2419 if (err < 0)
2420 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2421 }
2422
2423 err = dpcm_be_dai_hw_free(fe, stream);
2424 if (err < 0)
2425 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2426
2427 err = dpcm_be_dai_shutdown(fe, stream);
2428 if (err < 0)
2429 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2430
2431 /* run the stream event for each BE */
2432 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2433
2434 return 0;
2435}
2436
2437static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2438{
2439 struct snd_pcm_substream *substream =
2440 snd_soc_dpcm_get_substream(fe, stream);
2441 struct snd_soc_dpcm *dpcm;
2442 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2443 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +00002444 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002445
2446 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2447 stream ? "capture" : "playback", fe->dai_link->name);
2448
2449 /* Only start the BE if the FE is ready */
2450 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2451 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2452 return -EINVAL;
2453
2454 /* startup must always be called for new BEs */
2455 ret = dpcm_be_dai_startup(fe, stream);
2456 if (ret < 0)
2457 goto disconnect;
2458
2459 /* keep going if FE state is > open */
2460 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2461 return 0;
2462
2463 ret = dpcm_be_dai_hw_params(fe, stream);
2464 if (ret < 0)
2465 goto close;
2466
2467 /* keep going if FE state is > hw_params */
2468 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2469 return 0;
2470
2471
2472 ret = dpcm_be_dai_prepare(fe, stream);
2473 if (ret < 0)
2474 goto hw_free;
2475
2476 /* run the stream event for each BE */
2477 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2478
2479 /* keep going if FE state is > prepare */
2480 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2481 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2482 return 0;
2483
2484 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2485 /* call trigger on the frontend - FE takes care of all BE triggers */
2486 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2487 fe->dai_link->name);
2488
Olivier Deprez157378f2022-04-04 15:47:50 +02002489 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002490 if (ret < 0) {
2491 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2492 goto hw_free;
2493 }
2494 } else {
2495 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2496 fe->dai_link->name);
2497
2498 ret = dpcm_be_dai_trigger(fe, stream,
2499 SNDRV_PCM_TRIGGER_START);
2500 if (ret < 0) {
2501 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2502 goto hw_free;
2503 }
2504 }
2505
2506 return 0;
2507
2508hw_free:
2509 dpcm_be_dai_hw_free(fe, stream);
2510close:
2511 dpcm_be_dai_shutdown(fe, stream);
2512disconnect:
Olivier Deprez157378f2022-04-04 15:47:50 +02002513 /* disconnect any closed BEs */
David Brazdil0f672f62019-12-10 10:32:29 +00002514 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2515 for_each_dpcm_be(fe, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002516 struct snd_soc_pcm_runtime *be = dpcm->be;
Olivier Deprez157378f2022-04-04 15:47:50 +02002517 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2518 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002519 }
David Brazdil0f672f62019-12-10 10:32:29 +00002520 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002521
2522 return ret;
2523}
2524
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002525static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2526{
2527 struct snd_soc_dapm_widget_list *list;
Olivier Deprez157378f2022-04-04 15:47:50 +02002528 int stream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002529 int count, paths;
Olivier Deprez157378f2022-04-04 15:47:50 +02002530 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002531
2532 if (!fe->dai_link->dynamic)
2533 return 0;
2534
Olivier Deprez157378f2022-04-04 15:47:50 +02002535 if (fe->num_cpus > 1) {
2536 dev_err(fe->dev,
2537 "%s doesn't support Multi CPU yet\n", __func__);
2538 return -EINVAL;
2539 }
2540
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002541 /* only check active links */
Olivier Deprez157378f2022-04-04 15:47:50 +02002542 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002543 return 0;
2544
2545 /* DAPM sync will call this to update DSP paths */
2546 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2547 new ? "new" : "old", fe->dai_link->name);
2548
Olivier Deprez157378f2022-04-04 15:47:50 +02002549 for_each_pcm_streams(stream) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002550
Olivier Deprez157378f2022-04-04 15:47:50 +02002551 /* skip if FE doesn't have playback/capture capability */
2552 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2553 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2554 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002555
Olivier Deprez157378f2022-04-04 15:47:50 +02002556 /* skip if FE isn't currently playing/capturing */
2557 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2558 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2559 continue;
2560
2561 paths = dpcm_path_get(fe, stream, &list);
2562 if (paths < 0) {
2563 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2564 fe->dai_link->name,
2565 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2566 "playback" : "capture");
2567 return paths;
2568 }
2569
2570 /* update any playback/capture paths */
2571 count = dpcm_process_paths(fe, stream, &list, new);
2572 if (count) {
2573 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2574 if (new)
2575 ret = dpcm_run_update_startup(fe, stream);
2576 else
2577 ret = dpcm_run_update_shutdown(fe, stream);
2578 if (ret < 0)
2579 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2580 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2581
2582 dpcm_clear_pending_state(fe, stream);
2583 dpcm_be_disconnect(fe, stream);
2584 }
2585
2586 dpcm_path_put(&list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002587 }
2588
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002589 return 0;
2590}
2591
2592/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2593 * any DAI links.
2594 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002595int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002596{
2597 struct snd_soc_pcm_runtime *fe;
2598 int ret = 0;
2599
2600 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2601 /* shutdown all old paths first */
David Brazdil0f672f62019-12-10 10:32:29 +00002602 for_each_card_rtds(card, fe) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002603 ret = soc_dpcm_fe_runtime_update(fe, 0);
2604 if (ret)
2605 goto out;
2606 }
2607
2608 /* bring new paths up */
David Brazdil0f672f62019-12-10 10:32:29 +00002609 for_each_card_rtds(card, fe) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002610 ret = soc_dpcm_fe_runtime_update(fe, 1);
2611 if (ret)
2612 goto out;
2613 }
2614
2615out:
2616 mutex_unlock(&card->mutex);
2617 return ret;
2618}
Olivier Deprez157378f2022-04-04 15:47:50 +02002619EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2620
2621static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002622{
Olivier Deprez157378f2022-04-04 15:47:50 +02002623 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002624 struct snd_soc_dpcm *dpcm;
Olivier Deprez157378f2022-04-04 15:47:50 +02002625 int stream = fe_substream->stream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002626
Olivier Deprez157378f2022-04-04 15:47:50 +02002627 /* mark FE's links ready to prune */
2628 for_each_dpcm_be(fe, stream, dpcm)
2629 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002630
Olivier Deprez157378f2022-04-04 15:47:50 +02002631 dpcm_be_disconnect(fe, stream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002632
Olivier Deprez157378f2022-04-04 15:47:50 +02002633 fe->dpcm[stream].runtime = NULL;
2634}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002635
Olivier Deprez157378f2022-04-04 15:47:50 +02002636static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2637{
2638 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2639 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002640
Olivier Deprez157378f2022-04-04 15:47:50 +02002641 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2642 ret = dpcm_fe_dai_shutdown(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002643
Olivier Deprez157378f2022-04-04 15:47:50 +02002644 dpcm_fe_dai_cleanup(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002645
Olivier Deprez157378f2022-04-04 15:47:50 +02002646 mutex_unlock(&fe->card->mutex);
2647 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002648}
2649
2650static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2651{
Olivier Deprez157378f2022-04-04 15:47:50 +02002652 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002653 struct snd_soc_dapm_widget_list *list;
2654 int ret;
2655 int stream = fe_substream->stream;
2656
2657 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2658 fe->dpcm[stream].runtime = fe_substream->runtime;
2659
2660 ret = dpcm_path_get(fe, stream, &list);
2661 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002662 goto open_end;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002663 } else if (ret == 0) {
2664 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2665 fe->dai_link->name, stream ? "capture" : "playback");
2666 }
2667
2668 /* calculate valid and active FE <-> BE dpcms */
2669 dpcm_process_paths(fe, stream, &list, 1);
2670
2671 ret = dpcm_fe_dai_startup(fe_substream);
Olivier Deprez157378f2022-04-04 15:47:50 +02002672 if (ret < 0)
2673 dpcm_fe_dai_cleanup(fe_substream);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002674
2675 dpcm_clear_pending_state(fe, stream);
2676 dpcm_path_put(&list);
Olivier Deprez157378f2022-04-04 15:47:50 +02002677open_end:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002678 mutex_unlock(&fe->card->mutex);
2679 return ret;
2680}
2681
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002682/* create a new pcm */
2683int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2684{
2685 struct snd_soc_dai *codec_dai;
Olivier Deprez157378f2022-04-04 15:47:50 +02002686 struct snd_soc_dai *cpu_dai;
2687 struct snd_soc_component *component;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002688 struct snd_pcm *pcm;
2689 char new_name[64];
2690 int ret = 0, playback = 0, capture = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002691 int stream;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002692 int i;
2693
Olivier Deprez157378f2022-04-04 15:47:50 +02002694 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2695 dev_err(rtd->dev,
2696 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2697 return -EINVAL;
2698 }
2699
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002700 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002701 if (rtd->dai_link->dpcm_playback) {
2702 stream = SNDRV_PCM_STREAM_PLAYBACK;
2703
2704 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2705 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2706 playback = 1;
2707 break;
2708 }
2709 }
2710
2711 if (!playback) {
2712 dev_err(rtd->card->dev,
2713 "No CPU DAIs support playback for stream %s\n",
2714 rtd->dai_link->stream_name);
2715 return -EINVAL;
2716 }
2717 }
2718 if (rtd->dai_link->dpcm_capture) {
2719 stream = SNDRV_PCM_STREAM_CAPTURE;
2720
2721 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2722 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2723 capture = 1;
2724 break;
2725 }
2726 }
2727
2728 if (!capture) {
2729 dev_err(rtd->card->dev,
2730 "No CPU DAIs support capture for stream %s\n",
2731 rtd->dai_link->stream_name);
2732 return -EINVAL;
2733 }
2734 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002735 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002736 /* Adapt stream for codec2codec links */
Olivier Deprez157378f2022-04-04 15:47:50 +02002737 int cpu_capture = rtd->dai_link->params ?
2738 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2739 int cpu_playback = rtd->dai_link->params ?
2740 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
David Brazdil0f672f62019-12-10 10:32:29 +00002741
Olivier Deprez157378f2022-04-04 15:47:50 +02002742 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2743 if (rtd->num_cpus == 1) {
2744 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2745 } else if (rtd->num_cpus == rtd->num_codecs) {
2746 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2747 } else {
2748 dev_err(rtd->card->dev,
2749 "N cpus to M codecs link is not supported yet\n");
2750 return -EINVAL;
2751 }
2752
David Brazdil0f672f62019-12-10 10:32:29 +00002753 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002754 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002755 playback = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00002756 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002757 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002758 capture = 1;
2759 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002760 }
2761
2762 if (rtd->dai_link->playback_only) {
2763 playback = 1;
2764 capture = 0;
2765 }
2766
2767 if (rtd->dai_link->capture_only) {
2768 playback = 0;
2769 capture = 1;
2770 }
2771
2772 /* create the PCM */
David Brazdil0f672f62019-12-10 10:32:29 +00002773 if (rtd->dai_link->params) {
2774 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2775 rtd->dai_link->stream_name);
2776
2777 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2778 playback, capture, &pcm);
2779 } else if (rtd->dai_link->no_pcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002780 snprintf(new_name, sizeof(new_name), "(%s)",
2781 rtd->dai_link->stream_name);
2782
2783 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2784 playback, capture, &pcm);
2785 } else {
2786 if (rtd->dai_link->dynamic)
2787 snprintf(new_name, sizeof(new_name), "%s (*)",
2788 rtd->dai_link->stream_name);
2789 else
2790 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2791 rtd->dai_link->stream_name,
2792 (rtd->num_codecs > 1) ?
Olivier Deprez157378f2022-04-04 15:47:50 +02002793 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002794
2795 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2796 capture, &pcm);
2797 }
2798 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002799 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2800 new_name, rtd->dai_link->name, ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002801 return ret;
2802 }
2803 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2804
2805 /* DAPM dai link stream work */
David Brazdil0f672f62019-12-10 10:32:29 +00002806 if (rtd->dai_link->params)
Olivier Deprez157378f2022-04-04 15:47:50 +02002807 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
David Brazdil0f672f62019-12-10 10:32:29 +00002808 else
Olivier Deprez157378f2022-04-04 15:47:50 +02002809 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002810
2811 pcm->nonatomic = rtd->dai_link->nonatomic;
2812 rtd->pcm = pcm;
2813 pcm->private_data = rtd;
2814
David Brazdil0f672f62019-12-10 10:32:29 +00002815 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002816 if (playback)
2817 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2818 if (capture)
2819 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2820 goto out;
2821 }
2822
2823 /* ASoC PCM operations */
2824 if (rtd->dai_link->dynamic) {
2825 rtd->ops.open = dpcm_fe_dai_open;
2826 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2827 rtd->ops.prepare = dpcm_fe_dai_prepare;
2828 rtd->ops.trigger = dpcm_fe_dai_trigger;
2829 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2830 rtd->ops.close = dpcm_fe_dai_close;
2831 rtd->ops.pointer = soc_pcm_pointer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002832 } else {
2833 rtd->ops.open = soc_pcm_open;
2834 rtd->ops.hw_params = soc_pcm_hw_params;
2835 rtd->ops.prepare = soc_pcm_prepare;
2836 rtd->ops.trigger = soc_pcm_trigger;
2837 rtd->ops.hw_free = soc_pcm_hw_free;
2838 rtd->ops.close = soc_pcm_close;
2839 rtd->ops.pointer = soc_pcm_pointer;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002840 }
2841
Olivier Deprez157378f2022-04-04 15:47:50 +02002842 for_each_rtd_components(rtd, i, component) {
2843 const struct snd_soc_component_driver *drv = component->driver;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002844
Olivier Deprez157378f2022-04-04 15:47:50 +02002845 if (drv->ioctl)
2846 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2847 if (drv->sync_stop)
2848 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2849 if (drv->copy_user)
David Brazdil0f672f62019-12-10 10:32:29 +00002850 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Olivier Deprez157378f2022-04-04 15:47:50 +02002851 if (drv->page)
David Brazdil0f672f62019-12-10 10:32:29 +00002852 rtd->ops.page = snd_soc_pcm_component_page;
Olivier Deprez157378f2022-04-04 15:47:50 +02002853 if (drv->mmap)
David Brazdil0f672f62019-12-10 10:32:29 +00002854 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002855 }
2856
2857 if (playback)
2858 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2859
2860 if (capture)
2861 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2862
Olivier Deprez157378f2022-04-04 15:47:50 +02002863 ret = snd_soc_pcm_component_new(rtd);
David Brazdil0f672f62019-12-10 10:32:29 +00002864 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002865 dev_err(rtd->dev, "ASoC: pcm %s constructor failed for dailink %s: %d\n",
2866 new_name, rtd->dai_link->name, ret);
David Brazdil0f672f62019-12-10 10:32:29 +00002867 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002868 }
2869
David Brazdil0f672f62019-12-10 10:32:29 +00002870 pcm->no_device_suspend = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002871out:
Olivier Deprez157378f2022-04-04 15:47:50 +02002872 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2873 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2874 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002875 return ret;
2876}
2877
2878/* is the current PCM operation for this FE ? */
2879int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2880{
2881 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2882 return 1;
2883 return 0;
2884}
2885EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2886
2887/* is the current PCM operation for this BE ? */
2888int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2889 struct snd_soc_pcm_runtime *be, int stream)
2890{
2891 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2892 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2893 be->dpcm[stream].runtime_update))
2894 return 1;
2895 return 0;
2896}
2897EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2898
2899/* get the substream for this BE */
2900struct snd_pcm_substream *
2901 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2902{
2903 return be->pcm->streams[stream].substream;
2904}
2905EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2906
Olivier Deprez157378f2022-04-04 15:47:50 +02002907static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2908 struct snd_soc_pcm_runtime *be,
2909 int stream,
2910 const enum snd_soc_dpcm_state *states,
2911 int num_states)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002912{
2913 struct snd_soc_dpcm *dpcm;
2914 int state;
David Brazdil0f672f62019-12-10 10:32:29 +00002915 int ret = 1;
2916 unsigned long flags;
Olivier Deprez157378f2022-04-04 15:47:50 +02002917 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002918
David Brazdil0f672f62019-12-10 10:32:29 +00002919 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2920 for_each_dpcm_fe(be, stream, dpcm) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921
2922 if (dpcm->fe == fe)
2923 continue;
2924
2925 state = dpcm->fe->dpcm[stream].state;
Olivier Deprez157378f2022-04-04 15:47:50 +02002926 for (i = 0; i < num_states; i++) {
2927 if (state == states[i]) {
2928 ret = 0;
2929 break;
2930 }
David Brazdil0f672f62019-12-10 10:32:29 +00002931 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002932 }
David Brazdil0f672f62019-12-10 10:32:29 +00002933 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002934
Olivier Deprez157378f2022-04-04 15:47:50 +02002935 /* it's safe to do this BE DAI */
David Brazdil0f672f62019-12-10 10:32:29 +00002936 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002937}
Olivier Deprez157378f2022-04-04 15:47:50 +02002938
2939/*
2940 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2941 * are not running, paused or suspended for the specified stream direction.
2942 */
2943int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2944 struct snd_soc_pcm_runtime *be, int stream)
2945{
2946 const enum snd_soc_dpcm_state state[] = {
2947 SND_SOC_DPCM_STATE_START,
2948 SND_SOC_DPCM_STATE_PAUSED,
2949 SND_SOC_DPCM_STATE_SUSPEND,
2950 };
2951
2952 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2953}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002954EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2955
2956/*
2957 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2958 * running, paused or suspended for the specified stream direction.
2959 */
2960int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2961 struct snd_soc_pcm_runtime *be, int stream)
2962{
Olivier Deprez157378f2022-04-04 15:47:50 +02002963 const enum snd_soc_dpcm_state state[] = {
2964 SND_SOC_DPCM_STATE_START,
2965 SND_SOC_DPCM_STATE_PAUSED,
2966 SND_SOC_DPCM_STATE_SUSPEND,
2967 SND_SOC_DPCM_STATE_PREPARE,
2968 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002969
Olivier Deprez157378f2022-04-04 15:47:50 +02002970 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002971}
2972EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);