blob: e56500212c0c1fbe003c98eb2db67402917949f1 [file] [log] [blame]
Kuninori Morimotoed517582018-07-02 06:22:44 +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>
Liam Girdwoodddee6272011-06-09 14:45:53 +010012
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080016#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000017#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010018#include <linux/slab.h>
19#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010020#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010021#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/initval.h>
28
Liam Girdwood01d75842012-04-25 12:12:49 +010029#define DPCM_MAX_BE_USERS 8
30
Kuninori Morimotoc3212822020-02-19 15:56:57 +090031#ifdef CONFIG_DEBUG_FS
32static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
33{
34 switch (state) {
35 case SND_SOC_DPCM_STATE_NEW:
36 return "new";
37 case SND_SOC_DPCM_STATE_OPEN:
38 return "open";
39 case SND_SOC_DPCM_STATE_HW_PARAMS:
40 return "hw_params";
41 case SND_SOC_DPCM_STATE_PREPARE:
42 return "prepare";
43 case SND_SOC_DPCM_STATE_START:
44 return "start";
45 case SND_SOC_DPCM_STATE_STOP:
46 return "stop";
47 case SND_SOC_DPCM_STATE_SUSPEND:
48 return "suspend";
49 case SND_SOC_DPCM_STATE_PAUSED:
50 return "paused";
51 case SND_SOC_DPCM_STATE_HW_FREE:
52 return "hw_free";
53 case SND_SOC_DPCM_STATE_CLOSE:
54 return "close";
55 }
56
57 return "unknown";
58}
59
60static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
61 int stream, char *buf, size_t size)
62{
63 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
64 struct snd_soc_dpcm *dpcm;
65 ssize_t offset = 0;
66 unsigned long flags;
67
68 /* FE state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010069 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090070 "[%s - %s]\n", fe->dai_link->name,
71 stream ? "Capture" : "Playback");
72
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010073 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
Kuninori Morimotoc3212822020-02-19 15:56:57 +090074 dpcm_state_string(fe->dpcm[stream].state));
75
76 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
77 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010078 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090079 "Hardware Params: "
80 "Format = %s, Channels = %d, Rate = %d\n",
81 snd_pcm_format_name(params_format(params)),
82 params_channels(params),
83 params_rate(params));
84
85 /* BEs state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010086 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
Kuninori Morimotoc3212822020-02-19 15:56:57 +090087
88 if (list_empty(&fe->dpcm[stream].be_clients)) {
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010089 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090090 " No active DSP links\n");
91 goto out;
92 }
93
94 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
95 for_each_dpcm_be(fe, stream, dpcm) {
96 struct snd_soc_pcm_runtime *be = dpcm->be;
97 params = &dpcm->hw_params;
98
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010099 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900100 "- %s\n", be->dai_link->name);
101
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100102 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900103 " State: %s\n",
104 dpcm_state_string(be->dpcm[stream].state));
105
106 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
107 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100108 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900109 " Hardware Params: "
110 "Format = %s, Channels = %d, Rate = %d\n",
111 snd_pcm_format_name(params_format(params)),
112 params_channels(params),
113 params_rate(params));
114 }
115 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116out:
117 return offset;
118}
119
120static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
121 size_t count, loff_t *ppos)
122{
123 struct snd_soc_pcm_runtime *fe = file->private_data;
124 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
125 int stream;
126 char *buf;
127
Bard Liao6e1276a2020-02-25 21:39:16 +0800128 if (fe->num_cpus > 1) {
129 dev_err(fe->dev,
130 "%s doesn't support Multi CPU yet\n", __func__);
131 return -EINVAL;
132 }
133
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900134 buf = kmalloc(out_count, GFP_KERNEL);
135 if (!buf)
136 return -ENOMEM;
137
138 for_each_pcm_streams(stream)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900139 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900140 offset += dpcm_show_state(fe, stream,
141 buf + offset,
142 out_count - offset);
143
144 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
145
146 kfree(buf);
147 return ret;
148}
149
150static const struct file_operations dpcm_state_fops = {
151 .open = simple_open,
152 .read = dpcm_state_read_file,
153 .llseek = default_llseek,
154};
155
156void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
157{
158 if (!rtd->dai_link)
159 return;
160
161 if (!rtd->dai_link->dynamic)
162 return;
163
164 if (!rtd->card->debugfs_card_root)
165 return;
166
167 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
168 rtd->card->debugfs_card_root);
169
170 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
171 rtd, &dpcm_state_fops);
172}
Kuninori Morimoto154dae82020-02-19 15:57:06 +0900173
174static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
175{
176 char *name;
177
178 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
179 stream ? "capture" : "playback");
180 if (name) {
181 dpcm->debugfs_state = debugfs_create_dir(
182 name, dpcm->fe->debugfs_dpcm_root);
183 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
184 &dpcm->state);
185 kfree(name);
186 }
187}
188
189static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
190{
191 debugfs_remove_recursive(dpcm->debugfs_state);
192}
193
194#else
195static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
196 int stream)
197{
198}
199
200static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
201{
202}
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900203#endif
204
Kuninori Morimotof183f922020-01-22 09:44:35 +0900205static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
206 struct snd_pcm_substream *substream)
207{
208 if (rtd->dai_link->ops &&
209 rtd->dai_link->ops->startup)
210 return rtd->dai_link->ops->startup(substream);
211 return 0;
212}
213
Kuninori Morimoto0be429f2020-01-22 09:44:40 +0900214static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
215 struct snd_pcm_substream *substream)
216{
217 if (rtd->dai_link->ops &&
218 rtd->dai_link->ops->shutdown)
219 rtd->dai_link->ops->shutdown(substream);
220}
221
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900222static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
223 struct snd_pcm_substream *substream)
224{
225 if (rtd->dai_link->ops &&
226 rtd->dai_link->ops->prepare)
227 return rtd->dai_link->ops->prepare(substream);
228 return 0;
229}
230
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900231static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
232 struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *params)
234{
235 if (rtd->dai_link->ops &&
236 rtd->dai_link->ops->hw_params)
237 return rtd->dai_link->ops->hw_params(substream, params);
238 return 0;
239}
240
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900241static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
242 struct snd_pcm_substream *substream)
243{
244 if (rtd->dai_link->ops &&
245 rtd->dai_link->ops->hw_free)
246 rtd->dai_link->ops->hw_free(substream);
247}
248
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +0900249static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
250 struct snd_pcm_substream *substream,
251 int cmd)
252{
253 if (rtd->dai_link->ops &&
254 rtd->dai_link->ops->trigger)
255 return rtd->dai_link->ops->trigger(substream, cmd);
256 return 0;
257}
258
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900259static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
260 int stream, int action)
261{
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900262 struct snd_soc_dai *dai;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900263 int i;
264
265 lockdep_assert_held(&rtd->card->pcm_mutex);
266
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900267 for_each_rtd_dais(rtd, i, dai) {
268 dai->stream_active[stream] += action;
269 dai->active += action;
270 dai->component->active += action;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900271 }
272}
273
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200274/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100275 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
276 * @rtd: ASoC PCM runtime that is activated
277 * @stream: Direction of the PCM stream
278 *
279 * Increments the active count for all the DAIs and components attached to a PCM
280 * runtime. Should typically be called when a stream is opened.
281 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300282 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100283 */
284void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
285{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900286 snd_soc_runtime_action(rtd, stream, 1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100287}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +0100288EXPORT_SYMBOL_GPL(snd_soc_runtime_activate);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100289
290/**
291 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
292 * @rtd: ASoC PCM runtime that is deactivated
293 * @stream: Direction of the PCM stream
294 *
295 * Decrements the active count for all the DAIs and components attached to a PCM
296 * runtime. Should typically be called when a stream is closed.
297 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300298 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100299 */
300void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
301{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900302 snd_soc_runtime_action(rtd, stream, -1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100303}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +0100304EXPORT_SYMBOL_GPL(snd_soc_runtime_deactivate);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100305
306/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100307 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
308 * @rtd: The ASoC PCM runtime that should be checked.
309 *
310 * This function checks whether the power down delay should be ignored for a
311 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
312 * been configured to ignore the delay, or if none of the components benefits
313 * from having the delay.
314 */
315bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
316{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000317 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200318 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900319 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200320
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100321 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
322 return true;
323
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900324 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000325 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000326
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000327 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100328}
329
330/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200331 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
332 * @substream: the pcm substream
333 * @hw: the hardware parameters
334 *
335 * Sets the substream runtime hardware parameters.
336 */
337int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
338 const struct snd_pcm_hardware *hw)
339{
340 struct snd_pcm_runtime *runtime = substream->runtime;
341 runtime->hw.info = hw->info;
342 runtime->hw.formats = hw->formats;
343 runtime->hw.period_bytes_min = hw->period_bytes_min;
344 runtime->hw.period_bytes_max = hw->period_bytes_max;
345 runtime->hw.periods_min = hw->periods_min;
346 runtime->hw.periods_max = hw->periods_max;
347 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
348 runtime->hw.fifo_size = hw->fifo_size;
349 return 0;
350}
351EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
352
Liam Girdwood01d75842012-04-25 12:12:49 +0100353/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000354int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100355 int event)
356{
357 struct snd_soc_dpcm *dpcm;
358
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000359 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100360
361 struct snd_soc_pcm_runtime *be = dpcm->be;
362
Liam Girdwood103d84a2012-11-19 14:39:15 +0000363 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100364 be->dai_link->name, event, dir);
365
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700366 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
367 (be->dpcm[dir].users >= 1))
368 continue;
369
Liam Girdwood01d75842012-04-25 12:12:49 +0100370 snd_soc_dapm_stream_event(be, dir, event);
371 }
372
373 snd_soc_dapm_stream_event(fe, dir, event);
374
375 return 0;
376}
377
Dong Aisheng17841022011-08-29 17:15:14 +0800378static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
379 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100380{
381 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100382 int ret;
383
Nicolin Chen3635bf02013-11-13 18:56:24 +0800384 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
385 rtd->dai_link->symmetric_rates)) {
386 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
387 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100388
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200389 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800390 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200391 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800392 if (ret < 0) {
393 dev_err(soc_dai->dev,
394 "ASoC: Unable to apply rate constraint: %d\n",
395 ret);
396 return ret;
397 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100398 }
399
Nicolin Chen3635bf02013-11-13 18:56:24 +0800400 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
401 rtd->dai_link->symmetric_channels)) {
402 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
403 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100404
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200405 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800406 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800407 soc_dai->channels);
408 if (ret < 0) {
409 dev_err(soc_dai->dev,
410 "ASoC: Unable to apply channel symmetry constraint: %d\n",
411 ret);
412 return ret;
413 }
414 }
415
416 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
417 rtd->dai_link->symmetric_samplebits)) {
418 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
419 soc_dai->sample_bits);
420
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200421 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800422 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800423 soc_dai->sample_bits);
424 if (ret < 0) {
425 dev_err(soc_dai->dev,
426 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
427 ret);
428 return ret;
429 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100430 }
431
432 return 0;
433}
434
Nicolin Chen3635bf02013-11-13 18:56:24 +0800435static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
436 struct snd_pcm_hw_params *params)
437{
438 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900439 struct snd_soc_dai *dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800440 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200441 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800442
443 rate = params_rate(params);
444 channels = params_channels(params);
445 sample_bits = snd_pcm_format_physical_width(params_format(params));
446
447 /* reject unmatched parameters when applying symmetry */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800448 symmetry = rtd->dai_link->symmetric_rates;
449
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900450 for_each_rtd_cpu_dais(rtd, i, dai)
451 symmetry |= dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200452
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800453 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900454 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800455 if (cpu_dai->rate && cpu_dai->rate != rate) {
456 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
457 cpu_dai->rate, rate);
458 return -EINVAL;
459 }
460 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800461 }
462
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800463 symmetry = rtd->dai_link->symmetric_channels;
464
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900465 for_each_rtd_dais(rtd, i, dai)
466 symmetry |= dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200467
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800468 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900469 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800470 if (cpu_dai->channels &&
471 cpu_dai->channels != channels) {
472 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
473 cpu_dai->channels, channels);
474 return -EINVAL;
475 }
476 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800477 }
478
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800479 symmetry = rtd->dai_link->symmetric_samplebits;
480
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900481 for_each_rtd_dais(rtd, i, dai)
482 symmetry |= dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200483
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800484 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900485 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800486 if (cpu_dai->sample_bits &&
487 cpu_dai->sample_bits != sample_bits) {
488 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
489 cpu_dai->sample_bits, sample_bits);
490 return -EINVAL;
491 }
492 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493 }
494
495 return 0;
496}
497
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100498static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
499{
500 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100501 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900502 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200503 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100504
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800505 symmetry = link->symmetric_rates ||
506 link->symmetric_channels ||
507 link->symmetric_samplebits;
508
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900509 for_each_rtd_dais(rtd, i, dai)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800510 symmetry = symmetry ||
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900511 dai->driver->symmetric_rates ||
512 dai->driver->symmetric_channels ||
513 dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200514
515 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100516}
517
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200518static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000519{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100521 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000522
523 if (!bits)
524 return;
525
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100526 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
527 if (ret != 0)
528 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
529 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000530}
531
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200532static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200533{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200534 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800535 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200536 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900537 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
538 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200539 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800540 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200541
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900542 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900543 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
544
545 if (pcm_codec->sig_bits == 0) {
546 bits = 0;
547 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200548 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900549 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200550 }
551
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900552 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800553 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
554
555 if (pcm_cpu->sig_bits == 0) {
556 cpu_bits = 0;
557 break;
558 }
559 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
560 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900561
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200562 soc_pcm_set_msb(substream, bits);
563 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200564}
565
Samuel Holland5854a462020-03-04 23:11:42 -0600566/**
567 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
568 * @rtd: ASoC PCM runtime
569 * @hw: PCM hardware parameters (output)
570 * @stream: Direction of the PCM stream
571 *
572 * Calculates the subset of stream parameters supported by all DAIs
573 * associated with the PCM stream.
574 */
575int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
576 struct snd_pcm_hardware *hw, int stream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200577{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000578 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800579 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200580 struct snd_soc_pcm_stream *codec_stream;
581 struct snd_soc_pcm_stream *cpu_stream;
582 unsigned int chan_min = 0, chan_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800583 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200584 unsigned int rate_min = 0, rate_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800585 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
586 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200587 u64 formats = ULLONG_MAX;
588 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100589
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800590 /* first calculate min/max only for CPUs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900591 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800592
593 /*
594 * Skip CPUs which don't support the current stream type.
595 * Otherwise, since the rate, channel, and format values will
596 * zero in that case, we would have no usable settings left,
597 * causing the resulting setup to fail.
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800598 */
Samuel Holland5854a462020-03-04 23:11:42 -0600599 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800600 continue;
601
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800602 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100603
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800604 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
605 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
606 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
607 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
608 formats &= cpu_stream->formats;
609 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
610 cpu_rates);
611 }
612
613 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900614 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200615
616 /*
617 * Skip CODECs which don't support the current stream type.
618 * Otherwise, since the rate, channel, and format values will
619 * zero in that case, we would have no usable settings left,
620 * causing the resulting setup to fail.
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200621 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900622 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200623 continue;
624
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900625 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
626
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200627 chan_min = max(chan_min, codec_stream->channels_min);
628 chan_max = min(chan_max, codec_stream->channels_max);
629 rate_min = max(rate_min, codec_stream->rate_min);
630 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
631 formats &= codec_stream->formats;
632 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
633 }
634
Samuel Holland5854a462020-03-04 23:11:42 -0600635 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
636 if (!chan_min || !cpu_chan_min)
637 return -EINVAL;
638
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200639 /*
640 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800641 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200642 * channel allocation be fixed up later
643 */
644 if (rtd->num_codecs > 1) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800645 chan_min = cpu_chan_min;
646 chan_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200647 }
648
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800649 /* finally find a intersection between CODECs and CPUs */
650 hw->channels_min = max(chan_min, cpu_chan_min);
651 hw->channels_max = min(chan_max, cpu_chan_max);
Samuel Holland5854a462020-03-04 23:11:42 -0600652 hw->formats = formats;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800653 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100654
Samuel Holland5854a462020-03-04 23:11:42 -0600655 snd_pcm_hw_limit_rates(hw);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100656
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800657 hw->rate_min = max(hw->rate_min, cpu_rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200658 hw->rate_min = max(hw->rate_min, rate_min);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800659 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200660 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Samuel Holland5854a462020-03-04 23:11:42 -0600661
662 return 0;
663}
664EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
665
666static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
667{
668 struct snd_pcm_hardware *hw = &substream->runtime->hw;
669 struct snd_soc_pcm_runtime *rtd = substream->private_data;
670 u64 formats = hw->formats;
671
672 /*
673 * At least one CPU and one CODEC should match. Otherwise, we should
674 * have bailed out on a higher level, since there would be no CPU or
675 * CODEC to support the transfer direction in that case.
676 */
677 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
678
679 if (formats)
680 hw->formats &= formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200681}
682
Kuninori Morimotodd039072020-02-10 12:14:37 +0900683static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900684{
685 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200686 struct snd_soc_component *last = NULL;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900687 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900688 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900689
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900690 for_each_rtd_components(rtd, i, component) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200691 last = component;
692
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900693 ret = snd_soc_component_module_get_when_open(component);
694 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900695 dev_err(component->dev,
696 "ASoC: can't get module %s\n",
697 component->name);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200698 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900699 }
700
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900701 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900702 if (ret < 0) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200703 snd_soc_component_module_put_when_close(component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900704 dev_err(component->dev,
705 "ASoC: can't open component %s: %d\n",
706 component->name, ret);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200707 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900708 }
709 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900710
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200711 if (ret < 0) {
712 /* rollback on error */
713 for_each_rtd_components(rtd, i, component) {
714 if (component == last)
715 break;
716
717 snd_soc_component_close(component, substream);
718 snd_soc_component_module_put_when_close(component);
719 }
720 }
721
722 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900723}
724
Kuninori Morimotodd039072020-02-10 12:14:37 +0900725static int soc_pcm_components_close(struct snd_pcm_substream *substream)
Charles Keepax244e2932018-06-19 16:22:09 +0100726{
727 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100728 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900729 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100730
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900731 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900732 r = snd_soc_component_close(component, substream);
733 if (r < 0)
734 ret = r; /* use last ret */
735
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900736 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100737 }
738
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900739 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100740}
741
Mark Brown58ba9b22012-01-16 18:38:51 +0000742/*
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900743 * Called by ALSA when a PCM substream is closed. Private data can be
744 * freed here. The cpu DAI, codec DAI, machine and components are also
745 * shutdown.
746 */
747static int soc_pcm_close(struct snd_pcm_substream *substream)
748{
749 struct snd_soc_pcm_runtime *rtd = substream->private_data;
750 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900751 struct snd_soc_dai *dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900752 int i;
753
754 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
755
756 snd_soc_runtime_deactivate(rtd, substream->stream);
757
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900758 for_each_rtd_dais(rtd, i, dai)
759 snd_soc_dai_shutdown(dai, substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900760
761 soc_rtd_shutdown(rtd, substream);
762
763 soc_pcm_components_close(substream);
764
765 snd_soc_dapm_stream_stop(rtd, substream->stream);
766
767 mutex_unlock(&rtd->card->pcm_mutex);
768
769 for_each_rtd_components(rtd, i, component) {
770 pm_runtime_mark_last_busy(component->dev);
771 pm_runtime_put_autosuspend(component->dev);
772 }
773
774 for_each_rtd_components(rtd, i, component)
775 if (!component->active)
776 pinctrl_pm_select_sleep_state(component->dev);
777
778 return 0;
779}
780
781/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100782 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
783 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100784 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100785 */
786static int soc_pcm_open(struct snd_pcm_substream *substream)
787{
788 struct snd_soc_pcm_runtime *rtd = substream->private_data;
789 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000790 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900791 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200792 const char *codec_dai_name = "multicodec";
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800793 const char *cpu_dai_name = "multicpu";
Charles Keepax244e2932018-06-19 16:22:09 +0100794 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100795
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900796 for_each_rtd_components(rtd, i, component)
797 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000798
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900799 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000800 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000801
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300802 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900804 ret = soc_pcm_components_open(substream);
805 if (ret < 0)
806 goto component_err;
807
808 ret = soc_rtd_startup(rtd, substream);
809 if (ret < 0) {
810 pr_err("ASoC: %s startup failed: %d\n",
811 rtd->dai_link->name, ret);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200812 goto rtd_startup_err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900813 }
814
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815 /* startup the audio subsystem */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900816 for_each_rtd_dais(rtd, i, dai) {
817 ret = snd_soc_dai_startup(dai, substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800818 if (ret < 0) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900819 dev_err(dai->dev,
820 "ASoC: can't open DAI %s: %d\n",
821 dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900822 goto config_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100823 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200824
825 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900826 dai->tx_mask = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200827 else
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900828 dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100829 }
830
Liam Girdwood01d75842012-04-25 12:12:49 +0100831 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
832 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
833 goto dynamic;
834
Liam Girdwoodddee6272011-06-09 14:45:53 +0100835 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200836 soc_pcm_init_runtime_hw(substream);
837
838 if (rtd->num_codecs == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900839 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100840
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800841 if (rtd->num_cpus == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900842 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800843
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100844 if (soc_pcm_has_symmetry(substream))
845 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
846
Liam Girdwoodddee6272011-06-09 14:45:53 +0100847 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100848 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000849 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800850 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100851 goto config_err;
852 }
853 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000854 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800855 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100856 goto config_err;
857 }
858 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
859 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000860 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800861 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100862 goto config_err;
863 }
864
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200865 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000866
Liam Girdwoodddee6272011-06-09 14:45:53 +0100867 /* Symmetry only applies if we've already got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900868 for_each_rtd_dais(rtd, i, dai) {
869 if (dai->active) {
870 ret = soc_pcm_apply_symmetry(substream, dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200871 if (ret != 0)
872 goto config_err;
873 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100874 }
875
Liam Girdwood103d84a2012-11-19 14:39:15 +0000876 pr_debug("ASoC: %s <-> %s info:\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800877 codec_dai_name, cpu_dai_name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000878 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
879 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100880 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000881 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100882 runtime->hw.rate_max);
883
Liam Girdwood01d75842012-04-25 12:12:49 +0100884dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100885
886 snd_soc_runtime_activate(rtd, substream->stream);
887
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300888 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100889 return 0;
890
891config_err:
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900892 for_each_rtd_dais(rtd, i, dai)
893 snd_soc_dai_shutdown(dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200894
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900895 soc_rtd_shutdown(rtd, substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200896rtd_startup_err:
Kuninori Morimotodd039072020-02-10 12:14:37 +0900897 soc_pcm_components_close(substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200898component_err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300899 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000900
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900901 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000902 pm_runtime_mark_last_busy(component->dev);
903 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530904 }
905
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900906 for_each_rtd_components(rtd, i, component)
907 if (!component->active)
908 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000909
Liam Girdwoodddee6272011-06-09 14:45:53 +0100910 return ret;
911}
912
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800913static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200914{
915 /*
916 * Currently nothing to do for c2c links
917 * Since c2c links are internal nodes in the DAPM graph and
918 * don't interface with the outside world or application layer
919 * we don't have to do any special handling on close.
920 */
921}
922
Liam Girdwoodddee6272011-06-09 14:45:53 +0100923/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100924 * Called by ALSA when the PCM substream is prepared, can set format, sample
925 * rate, etc. This function is non atomic and can be called multiple times,
926 * it can refer to the runtime info.
927 */
928static int soc_pcm_prepare(struct snd_pcm_substream *substream)
929{
930 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000931 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900932 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200933 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100934
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300935 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100936
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900937 ret = soc_rtd_prepare(rtd, substream);
938 if (ret < 0) {
939 dev_err(rtd->card->dev,
940 "ASoC: machine prepare error: %d\n", ret);
941 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942 }
943
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900944 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900945 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000946 if (ret < 0) {
947 dev_err(component->dev,
948 "ASoC: platform prepare error: %d\n", ret);
949 goto out;
950 }
951 }
952
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900953 ret = snd_soc_pcm_dai_prepare(substream);
954 if (ret < 0) {
955 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
956 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100957 }
958
959 /* cancel any delayed stream shutdown that is pending */
960 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600961 rtd->pop_wait) {
962 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100963 cancel_delayed_work(&rtd->delayed_work);
964 }
965
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000966 snd_soc_dapm_stream_event(rtd, substream->stream,
967 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100968
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900969 for_each_rtd_dais(rtd, i, dai)
970 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100971
972out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300973 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100974 return ret;
975}
976
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200977static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
978 unsigned int mask)
979{
980 struct snd_interval *interval;
981 int channels = hweight_long(mask);
982
983 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
984 interval->min = channels;
985 interval->max = channels;
986}
987
Charles Keepax244e2932018-06-19 16:22:09 +0100988static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
989 struct snd_soc_component *last)
990{
991 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100992 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900993 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100994
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900995 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100996 if (component == last)
997 break;
998
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900999 r = snd_soc_component_hw_free(component, substream);
1000 if (r < 0)
1001 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +01001002 }
1003
Kuninori Morimotoeae71362019-07-26 13:50:24 +09001004 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +01001005}
1006
Liam Girdwoodddee6272011-06-09 14:45:53 +01001007/*
1008 * Called by ALSA when the hardware params are set by application. This
1009 * function can also be called multiple times and can allocate buffers
1010 * (using snd_pcm_lib_* ). It's non-atomic.
1011 */
1012static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1013 struct snd_pcm_hw_params *params)
1014{
1015 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001016 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001017 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001018 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +01001019 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001020
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001021 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +08001022
1023 ret = soc_pcm_params_symmetry(substream, params);
1024 if (ret)
1025 goto out;
1026
Kuninori Morimotode9ad992020-01-22 09:44:48 +09001027 ret = soc_rtd_hw_params(rtd, substream, params);
1028 if (ret < 0) {
1029 dev_err(rtd->card->dev,
1030 "ASoC: machine hw_params failed: %d\n", ret);
1031 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001032 }
1033
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001034 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001035 struct snd_pcm_hw_params codec_params;
1036
Ricard Wanderlofcde79032015-08-24 14:16:51 +02001037 /*
1038 * Skip CODECs which don't support the current stream type,
1039 * the idea being that if a CODEC is not used for the currently
1040 * set up transfer direction, it should not need to be
1041 * configured, especially since the configuration used might
1042 * not even be supported by that CODEC. There may be cases
1043 * however where a CODEC needs to be set up although it is
1044 * actually not being used for the transfer, e.g. if a
1045 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1046 * for the DAI link including a playback-only CODEC.
1047 * If this becomes necessary, we will have to augment the
1048 * machine driver setup with information on how to act, so
1049 * we can do the right thing here.
1050 */
1051 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1052 continue;
1053
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001054 /* copy params for each codec */
1055 codec_params = *params;
1056
1057 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +08001058 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1059 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001060 soc_pcm_codec_params_fixup(&codec_params,
1061 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +08001062
1063 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1064 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001065 soc_pcm_codec_params_fixup(&codec_params,
1066 codec_dai->rx_mask);
1067
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +09001068 ret = snd_soc_dai_hw_params(codec_dai, substream,
1069 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +02001070 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001071 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001072
1073 codec_dai->rate = params_rate(&codec_params);
1074 codec_dai->channels = params_channels(&codec_params);
1075 codec_dai->sample_bits = snd_pcm_format_physical_width(
1076 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +00001077
1078 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001079 }
1080
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001081 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001082 /*
1083 * Skip CPUs which don't support the current stream
1084 * type. See soc_pcm_init_runtime_hw() for more details
1085 */
1086 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1087 continue;
1088
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001089 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1090 if (ret < 0)
1091 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001092
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001093 /* store the parameters for each DAI */
1094 cpu_dai->rate = params_rate(params);
1095 cpu_dai->channels = params_channels(params);
1096 cpu_dai->sample_bits =
1097 snd_pcm_format_physical_width(params_format(params));
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001098
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001099 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1100 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001101
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001102 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +09001103 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +01001104 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +00001105 dev_err(component->dev,
1106 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +01001107 component->name, ret);
1108 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001109 }
1110 }
Charles Keepax244e2932018-06-19 16:22:09 +01001111 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001112
Liam Girdwoodddee6272011-06-09 14:45:53 +01001113out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001114 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115 return ret;
1116
Kuninori Morimotob8135862017-10-11 01:37:23 +00001117component_err:
Charles Keepax244e2932018-06-19 16:22:09 +01001118 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001119
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001120 i = rtd->num_cpus;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001121
1122interface_err:
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001123 for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001124 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1125 continue;
1126
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001127 snd_soc_dai_hw_free(cpu_dai, substream);
1128 cpu_dai->rate = 0;
1129 }
1130
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001131 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001132
1133codec_err:
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001134 for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001135 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1136 continue;
1137
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001138 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001139 codec_dai->rate = 0;
1140 }
1141
Kuninori Morimoto49f020e2020-01-22 09:44:52 +09001142 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001143
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001144 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001145 return ret;
1146}
1147
1148/*
1149 * Frees resources allocated by hw_params, can be called multiple times
1150 */
1151static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1152{
1153 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001154 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001155 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001156
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001157 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001158
Nicolin Chend3383422013-11-20 18:37:09 +08001159 /* clear the corresponding DAIs parameters when going to be inactive */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001160 for_each_rtd_dais(rtd, i, dai) {
1161 int active = dai->stream_active[substream->stream];
Nicolin Chend3383422013-11-20 18:37:09 +08001162
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001163 if (dai->active == 1) {
1164 dai->rate = 0;
1165 dai->channels = 0;
1166 dai->sample_bits = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001167 }
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +09001168
Kuninori Morimoto67ad8772020-03-06 10:10:04 +09001169 if (active == 1)
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001170 snd_soc_dai_digital_mute(dai, 1, substream->stream);
Kuninori Morimotoa9ee3312020-03-06 10:10:17 +09001171 }
1172
Liam Girdwoodddee6272011-06-09 14:45:53 +01001173 /* free any machine hw params */
Kuninori Morimoto49f020e2020-01-22 09:44:52 +09001174 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001175
Kuninori Morimotob8135862017-10-11 01:37:23 +00001176 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001177 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001178
Liam Girdwoodddee6272011-06-09 14:45:53 +01001179 /* now free hw params for the DAIs */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001180 for_each_rtd_dais(rtd, i, dai) {
1181 if (!snd_soc_dai_stream_valid(dai, substream->stream))
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001182 continue;
1183
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001184 snd_soc_dai_hw_free(dai, substream);
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001185 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001186
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001187 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001188 return 0;
1189}
1190
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001191static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001192{
1193 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001194 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001195 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001196
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001197 ret = soc_rtd_trigger(rtd, substream, cmd);
1198 if (ret < 0)
1199 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001200
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001201 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001202 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001203 if (ret < 0)
1204 return ret;
1205 }
1206
Kuninori Morimoto42f24722020-04-24 08:15:04 +09001207 return snd_soc_pcm_dai_trigger(substream, cmd);
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001208}
1209
1210static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1211{
1212 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1213 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001214 int i, ret;
1215
Kuninori Morimoto42f24722020-04-24 08:15:04 +09001216 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1217 if (ret < 0)
1218 return ret;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001219
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001220 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001221 ret = snd_soc_component_trigger(component, substream, cmd);
1222 if (ret < 0)
1223 return ret;
1224 }
1225
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001226 ret = soc_rtd_trigger(rtd, substream, cmd);
1227 if (ret < 0)
1228 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001229
Liam Girdwoodddee6272011-06-09 14:45:53 +01001230 return 0;
1231}
1232
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001233static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1234{
1235 int ret;
1236
1237 switch (cmd) {
1238 case SNDRV_PCM_TRIGGER_START:
1239 case SNDRV_PCM_TRIGGER_RESUME:
1240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1241 ret = soc_pcm_trigger_start(substream, cmd);
1242 break;
1243 case SNDRV_PCM_TRIGGER_STOP:
1244 case SNDRV_PCM_TRIGGER_SUSPEND:
1245 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1246 ret = soc_pcm_trigger_stop(substream, cmd);
1247 break;
1248 default:
1249 return -EINVAL;
1250 }
1251
1252 return ret;
1253}
1254
Mark Brown45c0a182012-05-09 21:46:27 +01001255static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1256 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001257{
1258 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001259 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001260 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001261
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001262 for_each_rtd_dais(rtd, i, dai) {
1263 ret = snd_soc_dai_bespoke_trigger(dai, substream, cmd);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001264 if (ret < 0)
1265 return ret;
1266 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001267
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001268 return 0;
1269}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001270/*
1271 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001272 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001273 * the runtime->delay will be updated accordingly.
1274 */
1275static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1276{
1277 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001278 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001279 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001280 struct snd_pcm_runtime *runtime = substream->runtime;
1281 snd_pcm_uframes_t offset = 0;
1282 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001283 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001284 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001285 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001286
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301287 /* clearing the previous total delay */
1288 runtime->delay = 0;
1289
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001290 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001291
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301292 /* base delay if assigned in pointer callback */
1293 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001294
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001295 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001296 cpu_delay = max(cpu_delay,
1297 snd_soc_dai_delay(cpu_dai, substream));
1298 }
1299 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001300
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001301 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001302 codec_delay = max(codec_delay,
1303 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001304 }
1305 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001306
Liam Girdwoodddee6272011-06-09 14:45:53 +01001307 runtime->delay = delay;
1308
1309 return offset;
1310}
1311
Liam Girdwood01d75842012-04-25 12:12:49 +01001312/* connect a FE and BE */
1313static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1314 struct snd_soc_pcm_runtime *be, int stream)
1315{
1316 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001317 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001318
1319 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001320 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001321 if (dpcm->be == be && dpcm->fe == fe)
1322 return 0;
1323 }
1324
1325 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1326 if (!dpcm)
1327 return -ENOMEM;
1328
1329 dpcm->be = be;
1330 dpcm->fe = fe;
1331 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1332 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001333 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001334 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1335 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001336 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001337
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001338 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001339 stream ? "capture" : "playback", fe->dai_link->name,
1340 stream ? "<-" : "->", be->dai_link->name);
1341
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001342 dpcm_create_debugfs_state(dpcm, stream);
1343
Liam Girdwood01d75842012-04-25 12:12:49 +01001344 return 1;
1345}
1346
1347/* reparent a BE onto another FE */
1348static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1349 struct snd_soc_pcm_runtime *be, int stream)
1350{
1351 struct snd_soc_dpcm *dpcm;
1352 struct snd_pcm_substream *fe_substream, *be_substream;
1353
1354 /* reparent if BE is connected to other FEs */
1355 if (!be->dpcm[stream].users)
1356 return;
1357
1358 be_substream = snd_soc_dpcm_get_substream(be, stream);
1359
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001360 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001361 if (dpcm->fe == fe)
1362 continue;
1363
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001364 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 stream ? "capture" : "playback",
1366 dpcm->fe->dai_link->name,
1367 stream ? "<-" : "->", dpcm->be->dai_link->name);
1368
1369 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1370 be_substream->runtime = fe_substream->runtime;
1371 break;
1372 }
1373}
1374
1375/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001376void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001377{
1378 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001379 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001380
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001381 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001382 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001383 stream ? "capture" : "playback",
1384 dpcm->be->dai_link->name);
1385
1386 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1387 continue;
1388
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001389 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001390 stream ? "capture" : "playback", fe->dai_link->name,
1391 stream ? "<-" : "->", dpcm->be->dai_link->name);
1392
1393 /* BEs still alive need new FE */
1394 dpcm_be_reparent(fe, dpcm->be, stream);
1395
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001396 dpcm_remove_debugfs_state(dpcm);
1397
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001398 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001399 list_del(&dpcm->list_be);
1400 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001401 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001402 kfree(dpcm);
1403 }
1404}
1405
1406/* get BE for DAI widget and stream */
1407static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1408 struct snd_soc_dapm_widget *widget, int stream)
1409{
1410 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001411 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001412 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001413 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001414
Liam Girdwood3c146462018-03-14 20:43:51 +00001415 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1416
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001417 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001418
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001419 if (!be->dai_link->no_pcm)
1420 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001421
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001422 for_each_rtd_dais(be, i, dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001423 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001424
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001425 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1426 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001427
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001428 if (w == widget)
1429 return be;
1430 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001431 }
1432
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001433 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001434 return NULL;
1435}
1436
Liam Girdwood01d75842012-04-25 12:12:49 +01001437static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1438 struct snd_soc_dapm_widget *widget)
1439{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001440 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001441 int i;
1442
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001443 for_each_dapm_widgets(list, i, w)
1444 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001445 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001446
1447 return 0;
1448}
1449
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001450static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1451 enum snd_soc_dapm_direction dir)
1452{
1453 struct snd_soc_card *card = widget->dapm->card;
1454 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001455 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001456
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001457 /* adjust dir to stream */
1458 if (dir == SND_SOC_DAPM_DIR_OUT)
1459 stream = SNDRV_PCM_STREAM_PLAYBACK;
1460 else
1461 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001462
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001463 rtd = dpcm_get_be(card, widget, stream);
1464 if (rtd)
1465 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001466
1467 return false;
1468}
1469
Liam Girdwood23607022014-01-17 17:03:55 +00001470int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001471 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001472{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001473 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood01d75842012-04-25 12:12:49 +01001474 int paths;
1475
Bard Liao6e1276a2020-02-25 21:39:16 +08001476 if (fe->num_cpus > 1) {
1477 dev_err(fe->dev,
1478 "%s doesn't support Multi CPU yet\n", __func__);
1479 return -EINVAL;
1480 }
1481
Liam Girdwood01d75842012-04-25 12:12:49 +01001482 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001483 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001484 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001485
Liam Girdwood103d84a2012-11-19 14:39:15 +00001486 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001487 stream ? "capture" : "playback");
1488
Liam Girdwood01d75842012-04-25 12:12:49 +01001489 return paths;
1490}
1491
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001492void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1493{
1494 snd_soc_dapm_dai_free_widgets(list);
1495}
1496
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001497static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1498 struct snd_soc_dapm_widget_list *list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001499{
Liam Girdwood01d75842012-04-25 12:12:49 +01001500 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001501 struct snd_soc_dai *dai;
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001502 unsigned int i;
1503
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001504 /* is there a valid DAI widget for this BE */
1505 for_each_rtd_dais(dpcm->be, i, dai) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001506 widget = snd_soc_dai_get_widget(dai, stream);
1507
1508 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001509 * The BE is pruned only if none of the dai
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001510 * widgets are in the active list.
1511 */
1512 if (widget && widget_in_list(list, widget))
1513 return true;
1514 }
1515
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001516 return false;
1517}
1518
1519static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1520 struct snd_soc_dapm_widget_list **list_)
1521{
1522 struct snd_soc_dpcm *dpcm;
Liam Girdwood01d75842012-04-25 12:12:49 +01001523 int prune = 0;
1524
1525 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001526 for_each_dpcm_be(fe, stream, dpcm) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001527 if (dpcm_be_is_active(dpcm, stream, *list_))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001528 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001529
Liam Girdwood103d84a2012-11-19 14:39:15 +00001530 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001531 stream ? "capture" : "playback",
1532 dpcm->be->dai_link->name, fe->dai_link->name);
1533 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1534 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1535 prune++;
1536 }
1537
Liam Girdwood103d84a2012-11-19 14:39:15 +00001538 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001539 return prune;
1540}
1541
1542static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1543 struct snd_soc_dapm_widget_list **list_)
1544{
1545 struct snd_soc_card *card = fe->card;
1546 struct snd_soc_dapm_widget_list *list = *list_;
1547 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001548 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001549 int i, new = 0, err;
1550
1551 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001552 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001553
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001554 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001555 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001556 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1557 continue;
1558 break;
Mark Brown46162742013-06-05 19:36:11 +01001559 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001560 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1561 continue;
Mark Brown46162742013-06-05 19:36:11 +01001562 break;
1563 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001564 continue;
Mark Brown46162742013-06-05 19:36:11 +01001565 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001566
1567 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001568 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001569 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001570 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001571 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001572 continue;
1573 }
1574
Liam Girdwood01d75842012-04-25 12:12:49 +01001575 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001576 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001577 continue;
1578
1579 /* newly connected FE and BE */
1580 err = dpcm_be_connect(fe, be, stream);
1581 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001582 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001583 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001584 break;
1585 } else if (err == 0) /* already connected */
1586 continue;
1587
1588 /* new */
1589 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1590 new++;
1591 }
1592
Liam Girdwood103d84a2012-11-19 14:39:15 +00001593 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001594 return new;
1595}
1596
1597/*
1598 * Find the corresponding BE DAIs that source or sink audio to this
1599 * FE substream.
1600 */
Liam Girdwood23607022014-01-17 17:03:55 +00001601int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001602 int stream, struct snd_soc_dapm_widget_list **list, int new)
1603{
1604 if (new)
1605 return dpcm_add_paths(fe, stream, list);
1606 else
1607 return dpcm_prune_paths(fe, stream, list);
1608}
1609
Liam Girdwood23607022014-01-17 17:03:55 +00001610void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001611{
1612 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001613 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001614
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001615 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001616 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001617 dpcm->be->dpcm[stream].runtime_update =
1618 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001619 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001620}
1621
1622static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1623 int stream)
1624{
1625 struct snd_soc_dpcm *dpcm;
1626
1627 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001628 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001629
1630 struct snd_soc_pcm_runtime *be = dpcm->be;
1631 struct snd_pcm_substream *be_substream =
1632 snd_soc_dpcm_get_substream(be, stream);
1633
1634 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001636 stream ? "capture" : "playback",
1637 be->dpcm[stream].state);
1638
1639 if (--be->dpcm[stream].users != 0)
1640 continue;
1641
1642 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1643 continue;
1644
1645 soc_pcm_close(be_substream);
1646 be_substream->runtime = NULL;
1647 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1648 }
1649}
1650
Liam Girdwood23607022014-01-17 17:03:55 +00001651int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001652{
1653 struct snd_soc_dpcm *dpcm;
1654 int err, count = 0;
1655
1656 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001657 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001658
1659 struct snd_soc_pcm_runtime *be = dpcm->be;
1660 struct snd_pcm_substream *be_substream =
1661 snd_soc_dpcm_get_substream(be, stream);
1662
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001663 if (!be_substream) {
1664 dev_err(be->dev, "ASoC: no backend %s stream\n",
1665 stream ? "capture" : "playback");
1666 continue;
1667 }
1668
Liam Girdwood01d75842012-04-25 12:12:49 +01001669 /* is this op for this BE ? */
1670 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1671 continue;
1672
1673 /* first time the dpcm is open ? */
1674 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001675 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001676 stream ? "capture" : "playback",
1677 be->dpcm[stream].state);
1678
1679 if (be->dpcm[stream].users++ != 0)
1680 continue;
1681
1682 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1683 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1684 continue;
1685
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001686 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1687 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001688
1689 be_substream->runtime = be->dpcm[stream].runtime;
1690 err = soc_pcm_open(be_substream);
1691 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001692 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001693 be->dpcm[stream].users--;
1694 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001695 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001696 stream ? "capture" : "playback",
1697 be->dpcm[stream].state);
1698
1699 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1700 goto unwind;
1701 }
1702
1703 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1704 count++;
1705 }
1706
1707 return count;
1708
1709unwind:
1710 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001711 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001712 struct snd_soc_pcm_runtime *be = dpcm->be;
1713 struct snd_pcm_substream *be_substream =
1714 snd_soc_dpcm_get_substream(be, stream);
1715
1716 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1717 continue;
1718
1719 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001720 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001721 stream ? "capture" : "playback",
1722 be->dpcm[stream].state);
1723
1724 if (--be->dpcm[stream].users != 0)
1725 continue;
1726
1727 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1728 continue;
1729
1730 soc_pcm_close(be_substream);
1731 be_substream->runtime = NULL;
1732 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1733 }
1734
1735 return err;
1736}
1737
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001738static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001739 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001740{
1741 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001742 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001743 runtime->hw.channels_min = stream->channels_min;
1744 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001745 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001746 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001747 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001748 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001749 runtime->hw.rates = stream->rates;
1750}
1751
Jerome Brunet435ffb72018-07-05 12:13:48 +02001752static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1753 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001754{
1755 struct snd_soc_pcm_runtime *fe = substream->private_data;
1756 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001757 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001758 int stream = substream->stream;
1759
1760 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001761 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001762
1763 /*
1764 * It returns merged BE codec format
1765 * if FE want to use it (= dpcm_merged_format)
1766 */
1767
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001768 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001769 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001770 struct snd_soc_pcm_stream *codec_stream;
1771 int i;
1772
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001773 for_each_rtd_codec_dais(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001774 /*
1775 * Skip CODECs which don't support the current stream
1776 * type. See soc_pcm_init_runtime_hw() for more details
1777 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001778 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001779 continue;
1780
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001781 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001782
Jerome Brunet435ffb72018-07-05 12:13:48 +02001783 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001784 }
1785 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001786}
1787
Jerome Brunet435ffb72018-07-05 12:13:48 +02001788static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1789 unsigned int *channels_min,
1790 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001791{
1792 struct snd_soc_pcm_runtime *fe = substream->private_data;
1793 struct snd_soc_dpcm *dpcm;
1794 int stream = substream->stream;
1795
1796 if (!fe->dai_link->dpcm_merged_chan)
1797 return;
1798
Jiada Wangf4c277b2018-06-20 18:25:20 +09001799 /*
1800 * It returns merged BE codec channel;
1801 * if FE want to use it (= dpcm_merged_chan)
1802 */
1803
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001804 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001805 struct snd_soc_pcm_runtime *be = dpcm->be;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001806 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001807 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001808 struct snd_soc_dai *dai;
1809 int i;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001810
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001811 for_each_rtd_cpu_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001812 /*
1813 * Skip CPUs which don't support the current stream
1814 * type. See soc_pcm_init_runtime_hw() for more details
1815 */
1816 if (!snd_soc_dai_stream_valid(dai, stream))
1817 continue;
1818
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001819 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001820
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001821 *channels_min = max(*channels_min,
1822 cpu_stream->channels_min);
1823 *channels_max = min(*channels_max,
1824 cpu_stream->channels_max);
1825 }
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001826
1827 /*
1828 * chan min/max cannot be enforced if there are multiple CODEC
1829 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1830 */
1831 if (be->num_codecs == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001832 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001833
1834 *channels_min = max(*channels_min,
1835 codec_stream->channels_min);
1836 *channels_max = min(*channels_max,
1837 codec_stream->channels_max);
1838 }
1839 }
1840}
1841
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001842static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1843 unsigned int *rates,
1844 unsigned int *rate_min,
1845 unsigned int *rate_max)
1846{
1847 struct snd_soc_pcm_runtime *fe = substream->private_data;
1848 struct snd_soc_dpcm *dpcm;
1849 int stream = substream->stream;
1850
1851 if (!fe->dai_link->dpcm_merged_rate)
1852 return;
1853
1854 /*
1855 * It returns merged BE codec channel;
1856 * if FE want to use it (= dpcm_merged_chan)
1857 */
1858
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001859 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001860 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001861 struct snd_soc_pcm_stream *pcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001862 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001863 int i;
1864
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001865 for_each_rtd_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001866 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001867 * Skip DAIs which don't support the current stream
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001868 * type. See soc_pcm_init_runtime_hw() for more details
1869 */
1870 if (!snd_soc_dai_stream_valid(dai, stream))
1871 continue;
1872
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001873 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001874
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001875 *rate_min = max(*rate_min, pcm->rate_min);
1876 *rate_max = min_not_zero(*rate_max, pcm->rate_max);
1877 *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001878 }
1879 }
1880}
1881
Mark Brown45c0a182012-05-09 21:46:27 +01001882static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001883{
1884 struct snd_pcm_runtime *runtime = substream->runtime;
1885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001886 struct snd_soc_dai *cpu_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001887 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001888
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001889 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001890 /*
1891 * Skip CPUs which don't support the current stream
1892 * type. See soc_pcm_init_runtime_hw() for more details
1893 */
1894 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1895 continue;
1896
Kuninori Morimoto0c9ba722020-03-06 10:09:54 +09001897 dpcm_init_runtime_hw(runtime,
1898 snd_soc_dai_get_pcm_stream(cpu_dai,
1899 substream->stream));
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001900 }
Jiada Wangf4c277b2018-06-20 18:25:20 +09001901
Jerome Brunet435ffb72018-07-05 12:13:48 +02001902 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1903 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1904 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001905 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1906 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001907}
1908
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001909static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1910
1911/* Set FE's runtime_update state; the state is protected via PCM stream lock
1912 * for avoiding the race with trigger callback.
1913 * If the state is unset and a trigger is pending while the previous operation,
1914 * process the pending trigger action here.
1915 */
1916static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1917 int stream, enum snd_soc_dpcm_update state)
1918{
1919 struct snd_pcm_substream *substream =
1920 snd_soc_dpcm_get_substream(fe, stream);
1921
1922 snd_pcm_stream_lock_irq(substream);
1923 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1924 dpcm_fe_dai_do_trigger(substream,
1925 fe->dpcm[stream].trigger_pending - 1);
1926 fe->dpcm[stream].trigger_pending = 0;
1927 }
1928 fe->dpcm[stream].runtime_update = state;
1929 snd_pcm_stream_unlock_irq(substream);
1930}
1931
PC Liao906c7d62015-12-11 11:33:51 +08001932static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1933 int stream)
1934{
1935 struct snd_soc_dpcm *dpcm;
1936 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001937 struct snd_soc_dai *fe_cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001938 int err;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001939 int i;
PC Liao906c7d62015-12-11 11:33:51 +08001940
1941 /* apply symmetry for FE */
1942 if (soc_pcm_has_symmetry(fe_substream))
1943 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1944
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001945 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001946 /* Symmetry only applies if we've got an active stream. */
1947 if (fe_cpu_dai->active) {
1948 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1949 if (err < 0)
1950 return err;
1951 }
PC Liao906c7d62015-12-11 11:33:51 +08001952 }
1953
1954 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001955 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001956 struct snd_soc_pcm_runtime *be = dpcm->be;
1957 struct snd_pcm_substream *be_substream =
1958 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001959 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001960 struct snd_soc_dai *dai;
PC Liao906c7d62015-12-11 11:33:51 +08001961 int i;
1962
Jerome Brunet6246f282019-04-01 15:03:54 +02001963 /* A backend may not have the requested substream */
1964 if (!be_substream)
1965 continue;
1966
1967 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301968 if (rtd->dai_link->be_hw_params_fixup)
1969 continue;
1970
PC Liao906c7d62015-12-11 11:33:51 +08001971 if (soc_pcm_has_symmetry(be_substream))
1972 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1973
1974 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001975 for_each_rtd_dais(rtd, i, dai) {
1976 if (dai->active) {
1977 err = soc_pcm_apply_symmetry(fe_substream, dai);
PC Liao906c7d62015-12-11 11:33:51 +08001978 if (err < 0)
1979 return err;
1980 }
1981 }
1982 }
1983
1984 return 0;
1985}
1986
Liam Girdwood01d75842012-04-25 12:12:49 +01001987static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1988{
1989 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1990 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1991 int stream = fe_substream->stream, ret = 0;
1992
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001993 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001994
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001995 ret = dpcm_be_dai_startup(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001996 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001997 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001998 goto be_err;
1999 }
2000
Liam Girdwood103d84a2012-11-19 14:39:15 +00002001 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
2003 /* start the DAI frontend */
2004 ret = soc_pcm_open(fe_substream);
2005 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002006 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002007 goto unwind;
2008 }
2009
2010 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2011
2012 dpcm_set_fe_runtime(fe_substream);
2013 snd_pcm_limit_hw_rates(runtime);
2014
PC Liao906c7d62015-12-11 11:33:51 +08002015 ret = dpcm_apply_symmetry(fe_substream, stream);
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09002016 if (ret < 0)
PC Liao906c7d62015-12-11 11:33:51 +08002017 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2018 ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002019
2020unwind:
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09002021 if (ret < 0)
2022 dpcm_be_dai_startup_unwind(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002023be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002024 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002025 return ret;
2026}
2027
Liam Girdwood23607022014-01-17 17:03:55 +00002028int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002029{
2030 struct snd_soc_dpcm *dpcm;
2031
2032 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002033 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002034
2035 struct snd_soc_pcm_runtime *be = dpcm->be;
2036 struct snd_pcm_substream *be_substream =
2037 snd_soc_dpcm_get_substream(be, stream);
2038
2039 /* is this op for this BE ? */
2040 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2041 continue;
2042
2043 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002044 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002045 stream ? "capture" : "playback",
2046 be->dpcm[stream].state);
2047
2048 if (--be->dpcm[stream].users != 0)
2049 continue;
2050
2051 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08002052 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2053 soc_pcm_hw_free(be_substream);
2054 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2055 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002056
Liam Girdwood103d84a2012-11-19 14:39:15 +00002057 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002058 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002059
2060 soc_pcm_close(be_substream);
2061 be_substream->runtime = NULL;
2062
2063 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2064 }
2065 return 0;
2066}
2067
2068static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2069{
2070 struct snd_soc_pcm_runtime *fe = substream->private_data;
2071 int stream = substream->stream;
2072
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002073 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002074
2075 /* shutdown the BEs */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002076 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002077
Liam Girdwood103d84a2012-11-19 14:39:15 +00002078 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002079
2080 /* now shutdown the frontend */
2081 soc_pcm_close(substream);
2082
2083 /* run the stream event for each BE */
Kuninori Morimoto1c531232020-02-21 10:25:18 +09002084 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood01d75842012-04-25 12:12:49 +01002085
2086 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002087 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002088 return 0;
2089}
2090
Liam Girdwood23607022014-01-17 17:03:55 +00002091int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002092{
2093 struct snd_soc_dpcm *dpcm;
2094
2095 /* only hw_params backends that are either sinks or sources
2096 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002097 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002098
2099 struct snd_soc_pcm_runtime *be = dpcm->be;
2100 struct snd_pcm_substream *be_substream =
2101 snd_soc_dpcm_get_substream(be, stream);
2102
2103 /* is this op for this BE ? */
2104 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2105 continue;
2106
2107 /* only free hw when no longer used - check all FEs */
2108 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2109 continue;
2110
Qiao Zhou36fba622014-12-03 10:13:43 +08002111 /* do not free hw if this BE is used by other FE */
2112 if (be->dpcm[stream].users > 1)
2113 continue;
2114
Liam Girdwood01d75842012-04-25 12:12:49 +01002115 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2116 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2117 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002118 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302119 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2120 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002121 continue;
2122
Liam Girdwood103d84a2012-11-19 14:39:15 +00002123 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002124 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002125
2126 soc_pcm_hw_free(be_substream);
2127
2128 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2129 }
2130
2131 return 0;
2132}
2133
Mark Brown45c0a182012-05-09 21:46:27 +01002134static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002135{
2136 struct snd_soc_pcm_runtime *fe = substream->private_data;
2137 int err, stream = substream->stream;
2138
2139 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002140 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002141
Liam Girdwood103d84a2012-11-19 14:39:15 +00002142 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002143
2144 /* call hw_free on the frontend */
2145 err = soc_pcm_hw_free(substream);
2146 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002147 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002148 fe->dai_link->name);
2149
2150 /* only hw_params backends that are either sinks or sources
2151 * to this frontend DAI */
2152 err = dpcm_be_dai_hw_free(fe, stream);
2153
2154 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002155 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002156
2157 mutex_unlock(&fe->card->mutex);
2158 return 0;
2159}
2160
Liam Girdwood23607022014-01-17 17:03:55 +00002161int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002162{
2163 struct snd_soc_dpcm *dpcm;
2164 int ret;
2165
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002166 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002167
2168 struct snd_soc_pcm_runtime *be = dpcm->be;
2169 struct snd_pcm_substream *be_substream =
2170 snd_soc_dpcm_get_substream(be, stream);
2171
2172 /* is this op for this BE ? */
2173 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2174 continue;
2175
Liam Girdwood01d75842012-04-25 12:12:49 +01002176 /* copy params for each dpcm */
2177 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2178 sizeof(struct snd_pcm_hw_params));
2179
2180 /* perform any hw_params fixups */
2181 if (be->dai_link->be_hw_params_fixup) {
2182 ret = be->dai_link->be_hw_params_fixup(be,
2183 &dpcm->hw_params);
2184 if (ret < 0) {
2185 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002186 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002187 ret);
2188 goto unwind;
2189 }
2190 }
2191
Libin Yangae061d22019-04-19 09:53:12 +08002192 /* copy the fixed-up hw params for BE dai */
2193 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2194 sizeof(struct snd_pcm_hw_params));
2195
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002196 /* only allow hw_params() if no connected FEs are running */
2197 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2198 continue;
2199
2200 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2201 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2202 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2203 continue;
2204
2205 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002206 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002207
Liam Girdwood01d75842012-04-25 12:12:49 +01002208 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2209 if (ret < 0) {
2210 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002211 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 goto unwind;
2213 }
2214
2215 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2216 }
2217 return 0;
2218
2219unwind:
2220 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002221 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002222 struct snd_soc_pcm_runtime *be = dpcm->be;
2223 struct snd_pcm_substream *be_substream =
2224 snd_soc_dpcm_get_substream(be, stream);
2225
2226 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2227 continue;
2228
2229 /* only allow hw_free() if no connected FEs are running */
2230 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2231 continue;
2232
2233 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2234 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2235 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2236 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2237 continue;
2238
2239 soc_pcm_hw_free(be_substream);
2240 }
2241
2242 return ret;
2243}
2244
Mark Brown45c0a182012-05-09 21:46:27 +01002245static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2246 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002247{
2248 struct snd_soc_pcm_runtime *fe = substream->private_data;
2249 int ret, stream = substream->stream;
2250
2251 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002252 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002253
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002254 memcpy(&fe->dpcm[stream].hw_params, params,
Liam Girdwood01d75842012-04-25 12:12:49 +01002255 sizeof(struct snd_pcm_hw_params));
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002256 ret = dpcm_be_dai_hw_params(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002257 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002258 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002259 goto out;
2260 }
2261
Liam Girdwood103d84a2012-11-19 14:39:15 +00002262 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002263 fe->dai_link->name, params_rate(params),
2264 params_channels(params), params_format(params));
2265
2266 /* call hw_params on the frontend */
2267 ret = soc_pcm_hw_params(substream, params);
2268 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002269 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002270 dpcm_be_dai_hw_free(fe, stream);
2271 } else
2272 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2273
2274out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002275 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002276 mutex_unlock(&fe->card->mutex);
2277 return ret;
2278}
2279
2280static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2281 struct snd_pcm_substream *substream, int cmd)
2282{
2283 int ret;
2284
Liam Girdwood103d84a2012-11-19 14:39:15 +00002285 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002286 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002287
2288 ret = soc_pcm_trigger(substream, cmd);
2289 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002290 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002291
2292 return ret;
2293}
2294
Liam Girdwood23607022014-01-17 17:03:55 +00002295int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002296 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002297{
2298 struct snd_soc_dpcm *dpcm;
2299 int ret = 0;
2300
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002301 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002302
2303 struct snd_soc_pcm_runtime *be = dpcm->be;
2304 struct snd_pcm_substream *be_substream =
2305 snd_soc_dpcm_get_substream(be, stream);
2306
2307 /* is this op for this BE ? */
2308 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2309 continue;
2310
2311 switch (cmd) {
2312 case SNDRV_PCM_TRIGGER_START:
2313 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
이경택21fca8b2020-04-01 10:04:21 +09002314 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2315 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002316 continue;
2317
2318 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2319 if (ret)
2320 return ret;
2321
2322 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2323 break;
2324 case SNDRV_PCM_TRIGGER_RESUME:
2325 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2326 continue;
2327
2328 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2329 if (ret)
2330 return ret;
2331
2332 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2333 break;
2334 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2335 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2336 continue;
2337
2338 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2339 if (ret)
2340 return ret;
2341
2342 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2343 break;
2344 case SNDRV_PCM_TRIGGER_STOP:
이경택21fca8b2020-04-01 10:04:21 +09002345 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2346 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002347 continue;
2348
2349 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2350 continue;
2351
2352 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2353 if (ret)
2354 return ret;
2355
2356 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2357 break;
2358 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002359 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002360 continue;
2361
2362 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2363 continue;
2364
2365 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2366 if (ret)
2367 return ret;
2368
2369 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2370 break;
2371 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2372 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2373 continue;
2374
2375 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2376 continue;
2377
2378 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2379 if (ret)
2380 return ret;
2381
2382 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2383 break;
2384 }
2385 }
2386
2387 return ret;
2388}
2389EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2390
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002391static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2392 int cmd, bool fe_first)
2393{
2394 struct snd_soc_pcm_runtime *fe = substream->private_data;
2395 int ret;
2396
2397 /* call trigger on the frontend before the backend. */
2398 if (fe_first) {
2399 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2400 fe->dai_link->name, cmd);
2401
2402 ret = soc_pcm_trigger(substream, cmd);
2403 if (ret < 0)
2404 return ret;
2405
2406 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2407 return ret;
2408 }
2409
2410 /* call trigger on the frontend after the backend. */
2411 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2412 if (ret < 0)
2413 return ret;
2414
2415 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2416 fe->dai_link->name, cmd);
2417
2418 ret = soc_pcm_trigger(substream, cmd);
2419
2420 return ret;
2421}
2422
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002423static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002424{
2425 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002426 int stream = substream->stream;
2427 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002428 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2429
2430 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2431
2432 switch (trigger) {
2433 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002434 switch (cmd) {
2435 case SNDRV_PCM_TRIGGER_START:
2436 case SNDRV_PCM_TRIGGER_RESUME:
2437 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2438 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2439 break;
2440 case SNDRV_PCM_TRIGGER_STOP:
2441 case SNDRV_PCM_TRIGGER_SUSPEND:
2442 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2443 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2444 break;
2445 default:
2446 ret = -EINVAL;
2447 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002448 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002449 break;
2450 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002451 switch (cmd) {
2452 case SNDRV_PCM_TRIGGER_START:
2453 case SNDRV_PCM_TRIGGER_RESUME:
2454 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2455 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2456 break;
2457 case SNDRV_PCM_TRIGGER_STOP:
2458 case SNDRV_PCM_TRIGGER_SUSPEND:
2459 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2460 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2461 break;
2462 default:
2463 ret = -EINVAL;
2464 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002465 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002466 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002467 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2468 /* bespoke trigger() - handles both FE and BEs */
2469
Liam Girdwood103d84a2012-11-19 14:39:15 +00002470 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002471 fe->dai_link->name, cmd);
2472
2473 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002474 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002475 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002476 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002477 fe->dai_link->name);
2478 ret = -EINVAL;
2479 goto out;
2480 }
2481
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002482 if (ret < 0) {
2483 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2484 cmd, ret);
2485 goto out;
2486 }
2487
Liam Girdwood01d75842012-04-25 12:12:49 +01002488 switch (cmd) {
2489 case SNDRV_PCM_TRIGGER_START:
2490 case SNDRV_PCM_TRIGGER_RESUME:
2491 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2492 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2493 break;
2494 case SNDRV_PCM_TRIGGER_STOP:
2495 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002496 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2497 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002498 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2499 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2500 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002501 }
2502
2503out:
2504 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2505 return ret;
2506}
2507
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002508static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2509{
2510 struct snd_soc_pcm_runtime *fe = substream->private_data;
2511 int stream = substream->stream;
2512
2513 /* if FE's runtime_update is already set, we're in race;
2514 * process this trigger later at exit
2515 */
2516 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2517 fe->dpcm[stream].trigger_pending = cmd + 1;
2518 return 0; /* delayed, assuming it's successful */
2519 }
2520
2521 /* we're alone, let's trigger */
2522 return dpcm_fe_dai_do_trigger(substream, cmd);
2523}
2524
Liam Girdwood23607022014-01-17 17:03:55 +00002525int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002526{
2527 struct snd_soc_dpcm *dpcm;
2528 int ret = 0;
2529
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002530 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002531
2532 struct snd_soc_pcm_runtime *be = dpcm->be;
2533 struct snd_pcm_substream *be_substream =
2534 snd_soc_dpcm_get_substream(be, stream);
2535
2536 /* is this op for this BE ? */
2537 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2538 continue;
2539
2540 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002541 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002542 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2543 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002544 continue;
2545
Liam Girdwood103d84a2012-11-19 14:39:15 +00002546 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002547 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002548
2549 ret = soc_pcm_prepare(be_substream);
2550 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002551 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002552 ret);
2553 break;
2554 }
2555
2556 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2557 }
2558 return ret;
2559}
2560
Mark Brown45c0a182012-05-09 21:46:27 +01002561static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002562{
2563 struct snd_soc_pcm_runtime *fe = substream->private_data;
2564 int stream = substream->stream, ret = 0;
2565
2566 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2567
Liam Girdwood103d84a2012-11-19 14:39:15 +00002568 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002569
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002570 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002571
2572 /* there is no point preparing this FE if there are no BEs */
2573 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002574 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002575 fe->dai_link->name);
2576 ret = -EINVAL;
2577 goto out;
2578 }
2579
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002580 ret = dpcm_be_dai_prepare(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002581 if (ret < 0)
2582 goto out;
2583
2584 /* call prepare on the frontend */
2585 ret = soc_pcm_prepare(substream);
2586 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002587 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002588 fe->dai_link->name);
2589 goto out;
2590 }
2591
2592 /* run the stream event for each BE */
2593 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2594 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2595
2596out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002597 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002598 mutex_unlock(&fe->card->mutex);
2599
2600 return ret;
2601}
2602
Liam Girdwood618dae12012-04-25 12:12:51 +01002603static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2604{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002605 struct snd_pcm_substream *substream =
2606 snd_soc_dpcm_get_substream(fe, stream);
2607 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002608 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002609
Liam Girdwood103d84a2012-11-19 14:39:15 +00002610 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002611 stream ? "capture" : "playback", fe->dai_link->name);
2612
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002613 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2614 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002615 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002616 fe->dai_link->name);
2617
2618 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2619 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002620 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002621 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002622 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002623 fe->dai_link->name);
2624
2625 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2626 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002627 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002628 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002629
2630 err = dpcm_be_dai_hw_free(fe, stream);
2631 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002632 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002633
2634 err = dpcm_be_dai_shutdown(fe, stream);
2635 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002636 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002637
2638 /* run the stream event for each BE */
2639 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2640
2641 return 0;
2642}
2643
2644static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2645{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002646 struct snd_pcm_substream *substream =
2647 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002648 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002649 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002650 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002651 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002652
Liam Girdwood103d84a2012-11-19 14:39:15 +00002653 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002654 stream ? "capture" : "playback", fe->dai_link->name);
2655
2656 /* Only start the BE if the FE is ready */
2657 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2658 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2659 return -EINVAL;
2660
2661 /* startup must always be called for new BEs */
2662 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002663 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002664 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002665
2666 /* keep going if FE state is > open */
2667 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2668 return 0;
2669
2670 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002671 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002672 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002673
2674 /* keep going if FE state is > hw_params */
2675 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2676 return 0;
2677
2678
2679 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002680 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002681 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002682
2683 /* run the stream event for each BE */
2684 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2685
2686 /* keep going if FE state is > prepare */
2687 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2688 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2689 return 0;
2690
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002691 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2692 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002693 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002694 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002695
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002696 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2697 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002698 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002699 goto hw_free;
2700 }
2701 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002702 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002703 fe->dai_link->name);
2704
2705 ret = dpcm_be_dai_trigger(fe, stream,
2706 SNDRV_PCM_TRIGGER_START);
2707 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002708 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002709 goto hw_free;
2710 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002711 }
2712
2713 return 0;
2714
2715hw_free:
2716 dpcm_be_dai_hw_free(fe, stream);
2717close:
2718 dpcm_be_dai_shutdown(fe, stream);
2719disconnect:
2720 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002721 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002722 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002723 struct snd_soc_pcm_runtime *be = dpcm->be;
2724 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2725 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2726 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002727 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002728
2729 return ret;
2730}
2731
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002732static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2733{
2734 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002735 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002736 int count, paths;
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002737 int ret;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002738
Bard Liao6e1276a2020-02-25 21:39:16 +08002739 if (fe->num_cpus > 1) {
2740 dev_err(fe->dev,
2741 "%s doesn't support Multi CPU yet\n", __func__);
2742 return -EINVAL;
2743 }
2744
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002745 if (!fe->dai_link->dynamic)
2746 return 0;
2747
2748 /* only check active links */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002749 if (!asoc_rtd_to_cpu(fe, 0)->active)
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002750 return 0;
2751
2752 /* DAPM sync will call this to update DSP paths */
2753 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2754 new ? "new" : "old", fe->dai_link->name);
2755
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002756 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002757
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002758 /* skip if FE doesn't have playback/capture capability */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002759 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2760 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002761 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002762
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002763 /* skip if FE isn't currently playing/capturing */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002764 if (!asoc_rtd_to_cpu(fe, 0)->stream_active[stream] ||
2765 !asoc_rtd_to_codec(fe, 0)->stream_active[stream])
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002766 continue;
2767
2768 paths = dpcm_path_get(fe, stream, &list);
2769 if (paths < 0) {
2770 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2771 fe->dai_link->name,
2772 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2773 "playback" : "capture");
2774 return paths;
2775 }
2776
2777 /* update any playback/capture paths */
2778 count = dpcm_process_paths(fe, stream, &list, new);
2779 if (count) {
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002780 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002781 if (new)
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002782 ret = dpcm_run_update_startup(fe, stream);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002783 else
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002784 ret = dpcm_run_update_shutdown(fe, stream);
2785 if (ret < 0)
2786 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2787 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002788
2789 dpcm_clear_pending_state(fe, stream);
2790 dpcm_be_disconnect(fe, stream);
2791 }
2792
2793 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002794 }
2795
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002796 return 0;
2797}
2798
Liam Girdwood618dae12012-04-25 12:12:51 +01002799/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2800 * any DAI links.
2801 */
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002802int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002803{
Mengdong Lin1a497982015-11-18 02:34:11 -05002804 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002805 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002806
Liam Girdwood618dae12012-04-25 12:12:51 +01002807 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002808 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002809 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002810 ret = soc_dpcm_fe_runtime_update(fe, 0);
2811 if (ret)
2812 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002813 }
2814
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002815 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002816 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002817 ret = soc_dpcm_fe_runtime_update(fe, 1);
2818 if (ret)
2819 goto out;
2820 }
2821
2822out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002823 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002824 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002825}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002826EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
Liam Girdwood01d75842012-04-25 12:12:49 +01002827
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002828static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002829{
2830 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2831 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002832 int stream = fe_substream->stream;
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002833
2834 /* mark FE's links ready to prune */
2835 for_each_dpcm_be(fe, stream, dpcm)
2836 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2837
2838 dpcm_be_disconnect(fe, stream);
2839
2840 fe->dpcm[stream].runtime = NULL;
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002841}
2842
2843static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2844{
2845 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2846 int ret;
2847
2848 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2849 ret = dpcm_fe_dai_shutdown(fe_substream);
2850
2851 dpcm_fe_dai_cleanup(fe_substream);
2852
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002853 mutex_unlock(&fe->card->mutex);
2854 return ret;
2855}
2856
Liam Girdwood01d75842012-04-25 12:12:49 +01002857static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2858{
2859 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Liam Girdwood01d75842012-04-25 12:12:49 +01002860 struct snd_soc_dapm_widget_list *list;
2861 int ret;
2862 int stream = fe_substream->stream;
2863
2864 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2865 fe->dpcm[stream].runtime = fe_substream->runtime;
2866
Qiao Zhou8f70e512014-09-10 17:54:07 +08002867 ret = dpcm_path_get(fe, stream, &list);
2868 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002869 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08002870 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002871 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002872 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002873 }
2874
2875 /* calculate valid and active FE <-> BE dpcms */
2876 dpcm_process_paths(fe, stream, &list, 1);
2877
2878 ret = dpcm_fe_dai_startup(fe_substream);
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002879 if (ret < 0)
2880 dpcm_fe_dai_cleanup(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002881
2882 dpcm_clear_pending_state(fe, stream);
2883 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002884open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002885 mutex_unlock(&fe->card->mutex);
2886 return ret;
2887}
2888
Liam Girdwoodddee6272011-06-09 14:45:53 +01002889/* create a new pcm */
2890int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2891{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002892 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002893 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002894 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002895 struct snd_pcm *pcm;
2896 char new_name[64];
2897 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002898 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002899
Liam Girdwood01d75842012-04-25 12:12:49 +01002900 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Stephan Gerhold9b5db052020-04-15 12:49:28 +02002901 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2902 if (rtd->num_cpus > 1) {
2903 dev_err(rtd->dev,
2904 "DPCM doesn't support Multi CPU yet\n");
2905 return -EINVAL;
2906 }
2907
2908 playback = rtd->dai_link->dpcm_playback &&
2909 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK);
2910 capture = rtd->dai_link->dpcm_capture &&
2911 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002912 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002913 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002914 int cpu_capture = rtd->dai_link->params ?
2915 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2916 int cpu_playback = rtd->dai_link->params ?
2917 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002918
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002919 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002920 if (rtd->num_cpus == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002921 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002922 } else if (rtd->num_cpus == rtd->num_codecs) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002923 cpu_dai = asoc_rtd_to_cpu(rtd, i);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002924 } else {
2925 dev_err(rtd->card->dev,
2926 "N cpus to M codecs link is not supported yet\n");
2927 return -EINVAL;
2928 }
2929
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002930 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002931 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002932 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002933 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002934 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002935 capture = 1;
2936 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002937 }
Sangsu Parka5002312012-01-02 17:15:10 +09002938
Fabio Estevamd6bead02013-08-29 10:32:13 -03002939 if (rtd->dai_link->playback_only) {
2940 playback = 1;
2941 capture = 0;
2942 }
2943
2944 if (rtd->dai_link->capture_only) {
2945 playback = 0;
2946 capture = 1;
2947 }
2948
Liam Girdwood01d75842012-04-25 12:12:49 +01002949 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002950 if (rtd->dai_link->params) {
2951 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2952 rtd->dai_link->stream_name);
2953
2954 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2955 playback, capture, &pcm);
2956 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002957 snprintf(new_name, sizeof(new_name), "(%s)",
2958 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002959
Liam Girdwood01d75842012-04-25 12:12:49 +01002960 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2961 playback, capture, &pcm);
2962 } else {
2963 if (rtd->dai_link->dynamic)
2964 snprintf(new_name, sizeof(new_name), "%s (*)",
2965 rtd->dai_link->stream_name);
2966 else
2967 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002968 rtd->dai_link->stream_name,
2969 (rtd->num_codecs > 1) ?
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002970 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002971
Liam Girdwood01d75842012-04-25 12:12:49 +01002972 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2973 capture, &pcm);
2974 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002975 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002976 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002977 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002978 return ret;
2979 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002980 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002981
2982 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002983 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002984 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002985 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002986 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002987
Vinod Koul48c76992015-02-12 09:59:53 +05302988 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002989 rtd->pcm = pcm;
2990 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002991
Jerome Bruneta3420312019-07-25 18:59:47 +02002992 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002993 if (playback)
2994 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2995 if (capture)
2996 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2997 goto out;
2998 }
2999
3000 /* ASoC PCM operations */
3001 if (rtd->dai_link->dynamic) {
3002 rtd->ops.open = dpcm_fe_dai_open;
3003 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3004 rtd->ops.prepare = dpcm_fe_dai_prepare;
3005 rtd->ops.trigger = dpcm_fe_dai_trigger;
3006 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3007 rtd->ops.close = dpcm_fe_dai_close;
3008 rtd->ops.pointer = soc_pcm_pointer;
3009 } else {
3010 rtd->ops.open = soc_pcm_open;
3011 rtd->ops.hw_params = soc_pcm_hw_params;
3012 rtd->ops.prepare = soc_pcm_prepare;
3013 rtd->ops.trigger = soc_pcm_trigger;
3014 rtd->ops.hw_free = soc_pcm_hw_free;
3015 rtd->ops.close = soc_pcm_close;
3016 rtd->ops.pointer = soc_pcm_pointer;
3017 }
3018
Kuninori Morimoto613fb502020-01-10 11:35:21 +09003019 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09003020 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003021
Takashi Iwai3b1c9522019-11-21 20:07:08 +01003022 if (drv->ioctl)
3023 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01003024 if (drv->sync_stop)
3025 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003026 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09003027 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003028 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09003029 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003030 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09003031 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003032 }
3033
Liam Girdwoodddee6272011-06-09 14:45:53 +01003034 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003035 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003036
3037 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003038 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003039
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09003040 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09003041 if (ret < 0) {
3042 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3043 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003044 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003045
Takashi Iwai3d21ef02019-01-11 15:58:39 +01003046 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01003047out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003048 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09003049 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
3050 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003051 return ret;
3052}
Liam Girdwood01d75842012-04-25 12:12:49 +01003053
3054/* is the current PCM operation for this FE ? */
3055int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3056{
3057 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3058 return 1;
3059 return 0;
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3062
3063/* is the current PCM operation for this BE ? */
3064int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3065 struct snd_soc_pcm_runtime *be, int stream)
3066{
3067 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3068 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3069 be->dpcm[stream].runtime_update))
3070 return 1;
3071 return 0;
3072}
3073EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3074
3075/* get the substream for this BE */
3076struct snd_pcm_substream *
3077 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3078{
3079 return be->pcm->streams[stream].substream;
3080}
3081EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3082
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003083static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3084 struct snd_soc_pcm_runtime *be,
3085 int stream,
3086 const enum snd_soc_dpcm_state *states,
3087 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01003088{
3089 struct snd_soc_dpcm *dpcm;
3090 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003091 int ret = 1;
3092 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003093 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01003094
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003095 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003096 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003097
3098 if (dpcm->fe == fe)
3099 continue;
3100
3101 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003102 for (i = 0; i < num_states; i++) {
3103 if (state == states[i]) {
3104 ret = 0;
3105 break;
3106 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003107 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003108 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003109 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003110
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003111 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003112 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003113}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003114
3115/*
3116 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3117 * are not running, paused or suspended for the specified stream direction.
3118 */
3119int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3120 struct snd_soc_pcm_runtime *be, int stream)
3121{
3122 const enum snd_soc_dpcm_state state[] = {
3123 SND_SOC_DPCM_STATE_START,
3124 SND_SOC_DPCM_STATE_PAUSED,
3125 SND_SOC_DPCM_STATE_SUSPEND,
3126 };
3127
3128 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3129}
Liam Girdwood01d75842012-04-25 12:12:49 +01003130EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3131
3132/*
3133 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3134 * running, paused or suspended for the specified stream direction.
3135 */
3136int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3137 struct snd_soc_pcm_runtime *be, int stream)
3138{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003139 const enum snd_soc_dpcm_state state[] = {
3140 SND_SOC_DPCM_STATE_START,
3141 SND_SOC_DPCM_STATE_PAUSED,
3142 SND_SOC_DPCM_STATE_SUSPEND,
3143 SND_SOC_DPCM_STATE_PREPARE,
3144 };
Liam Girdwood01d75842012-04-25 12:12:49 +01003145
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003146 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01003147}
3148EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);