blob: c517064f5391b20d9e912e91025d35623b2b3067 [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>
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +090027#include <sound/soc-link.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/initval.h>
29
Liam Girdwood01d75842012-04-25 12:12:49 +010030#define DPCM_MAX_BE_USERS 8
31
Kuninori Morimotoc3212822020-02-19 15:56:57 +090032#ifdef CONFIG_DEBUG_FS
33static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
34{
35 switch (state) {
36 case SND_SOC_DPCM_STATE_NEW:
37 return "new";
38 case SND_SOC_DPCM_STATE_OPEN:
39 return "open";
40 case SND_SOC_DPCM_STATE_HW_PARAMS:
41 return "hw_params";
42 case SND_SOC_DPCM_STATE_PREPARE:
43 return "prepare";
44 case SND_SOC_DPCM_STATE_START:
45 return "start";
46 case SND_SOC_DPCM_STATE_STOP:
47 return "stop";
48 case SND_SOC_DPCM_STATE_SUSPEND:
49 return "suspend";
50 case SND_SOC_DPCM_STATE_PAUSED:
51 return "paused";
52 case SND_SOC_DPCM_STATE_HW_FREE:
53 return "hw_free";
54 case SND_SOC_DPCM_STATE_CLOSE:
55 return "close";
56 }
57
58 return "unknown";
59}
60
61static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
62 int stream, char *buf, size_t size)
63{
64 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
65 struct snd_soc_dpcm *dpcm;
66 ssize_t offset = 0;
67 unsigned long flags;
68
69 /* FE state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010070 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090071 "[%s - %s]\n", fe->dai_link->name,
72 stream ? "Capture" : "Playback");
73
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010074 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
Kuninori Morimotoc3212822020-02-19 15:56:57 +090075 dpcm_state_string(fe->dpcm[stream].state));
76
77 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
78 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010079 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090080 "Hardware Params: "
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params)),
83 params_channels(params),
84 params_rate(params));
85
86 /* BEs state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010087 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
Kuninori Morimotoc3212822020-02-19 15:56:57 +090088
89 if (list_empty(&fe->dpcm[stream].be_clients)) {
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010090 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090091 " No active DSP links\n");
92 goto out;
93 }
94
95 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
96 for_each_dpcm_be(fe, stream, dpcm) {
97 struct snd_soc_pcm_runtime *be = dpcm->be;
98 params = &dpcm->hw_params;
99
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100100 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900101 "- %s\n", be->dai_link->name);
102
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100103 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900104 " State: %s\n",
105 dpcm_state_string(be->dpcm[stream].state));
106
107 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
108 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100109 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900110 " Hardware Params: "
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params)),
113 params_channels(params),
114 params_rate(params));
115 }
116 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
117out:
118 return offset;
119}
120
121static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
122 size_t count, loff_t *ppos)
123{
124 struct snd_soc_pcm_runtime *fe = file->private_data;
125 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
126 int stream;
127 char *buf;
128
Bard Liao6e1276a2020-02-25 21:39:16 +0800129 if (fe->num_cpus > 1) {
130 dev_err(fe->dev,
131 "%s doesn't support Multi CPU yet\n", __func__);
132 return -EINVAL;
133 }
134
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900135 buf = kmalloc(out_count, GFP_KERNEL);
136 if (!buf)
137 return -ENOMEM;
138
139 for_each_pcm_streams(stream)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900141 offset += dpcm_show_state(fe, stream,
142 buf + offset,
143 out_count - offset);
144
145 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
146
147 kfree(buf);
148 return ret;
149}
150
151static const struct file_operations dpcm_state_fops = {
152 .open = simple_open,
153 .read = dpcm_state_read_file,
154 .llseek = default_llseek,
155};
156
157void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
158{
159 if (!rtd->dai_link)
160 return;
161
162 if (!rtd->dai_link->dynamic)
163 return;
164
165 if (!rtd->card->debugfs_card_root)
166 return;
167
168 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
169 rtd->card->debugfs_card_root);
170
171 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
172 rtd, &dpcm_state_fops);
173}
Kuninori Morimoto154dae82020-02-19 15:57:06 +0900174
175static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
176{
177 char *name;
178
179 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
180 stream ? "capture" : "playback");
181 if (name) {
182 dpcm->debugfs_state = debugfs_create_dir(
183 name, dpcm->fe->debugfs_dpcm_root);
184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
185 &dpcm->state);
186 kfree(name);
187 }
188}
189
190static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
191{
192 debugfs_remove_recursive(dpcm->debugfs_state);
193}
194
195#else
196static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
197 int stream)
198{
199}
200
201static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
202{
203}
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900204#endif
205
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900206/**
207 * snd_soc_runtime_action() - Increment/Decrement active count for
208 * PCM runtime components
209 * @rtd: ASoC PCM runtime that is activated
210 * @stream: Direction of the PCM stream
211 *
212 * Increments/Decrements the active count for all the DAIs and components
213 * attached to a PCM runtime.
214 * Should typically be called when a stream is opened.
215 *
216 * Must be called with the rtd->card->pcm_mutex being held
217 */
218void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
219 int stream, int action)
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900220{
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900221 struct snd_soc_dai *dai;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900222 int i;
223
224 lockdep_assert_held(&rtd->card->pcm_mutex);
225
Kuninori Morimotodc829102020-05-15 09:46:27 +0900226 for_each_rtd_dais(rtd, i, dai)
227 snd_soc_dai_action(dai, stream, action);
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900228}
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900229EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100230
231/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100232 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
233 * @rtd: The ASoC PCM runtime that should be checked.
234 *
235 * This function checks whether the power down delay should be ignored for a
236 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
237 * been configured to ignore the delay, or if none of the components benefits
238 * from having the delay.
239 */
240bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
241{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000242 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200243 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900244 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200245
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100246 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
247 return true;
248
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900249 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000250 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000251
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000252 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100253}
254
255/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200256 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
257 * @substream: the pcm substream
258 * @hw: the hardware parameters
259 *
260 * Sets the substream runtime hardware parameters.
261 */
262int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
263 const struct snd_pcm_hardware *hw)
264{
265 struct snd_pcm_runtime *runtime = substream->runtime;
266 runtime->hw.info = hw->info;
267 runtime->hw.formats = hw->formats;
268 runtime->hw.period_bytes_min = hw->period_bytes_min;
269 runtime->hw.period_bytes_max = hw->period_bytes_max;
270 runtime->hw.periods_min = hw->periods_min;
271 runtime->hw.periods_max = hw->periods_max;
272 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
273 runtime->hw.fifo_size = hw->fifo_size;
274 return 0;
275}
276EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
277
Liam Girdwood01d75842012-04-25 12:12:49 +0100278/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000279int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100280 int event)
281{
282 struct snd_soc_dpcm *dpcm;
283
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000284 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100285
286 struct snd_soc_pcm_runtime *be = dpcm->be;
287
Liam Girdwood103d84a2012-11-19 14:39:15 +0000288 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100289 be->dai_link->name, event, dir);
290
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700291 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
292 (be->dpcm[dir].users >= 1))
293 continue;
294
Liam Girdwood01d75842012-04-25 12:12:49 +0100295 snd_soc_dapm_stream_event(be, dir, event);
296 }
297
298 snd_soc_dapm_stream_event(fe, dir, event);
299
300 return 0;
301}
302
Dong Aisheng17841022011-08-29 17:15:14 +0800303static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
304 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100307 int ret;
308
Nicolin Chen3635bf02013-11-13 18:56:24 +0800309 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
310 rtd->dai_link->symmetric_rates)) {
311 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
312 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100313
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200314 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800315 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200316 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800317 if (ret < 0) {
318 dev_err(soc_dai->dev,
319 "ASoC: Unable to apply rate constraint: %d\n",
320 ret);
321 return ret;
322 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100323 }
324
Nicolin Chen3635bf02013-11-13 18:56:24 +0800325 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
326 rtd->dai_link->symmetric_channels)) {
327 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
328 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100329
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200330 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800331 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800332 soc_dai->channels);
333 if (ret < 0) {
334 dev_err(soc_dai->dev,
335 "ASoC: Unable to apply channel symmetry constraint: %d\n",
336 ret);
337 return ret;
338 }
339 }
340
341 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
342 rtd->dai_link->symmetric_samplebits)) {
343 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
344 soc_dai->sample_bits);
345
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200346 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800347 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800348 soc_dai->sample_bits);
349 if (ret < 0) {
350 dev_err(soc_dai->dev,
351 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
352 ret);
353 return ret;
354 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100355 }
356
357 return 0;
358}
359
Nicolin Chen3635bf02013-11-13 18:56:24 +0800360static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
361 struct snd_pcm_hw_params *params)
362{
363 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900364 struct snd_soc_dai *dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800365 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200366 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800367
368 rate = params_rate(params);
369 channels = params_channels(params);
370 sample_bits = snd_pcm_format_physical_width(params_format(params));
371
372 /* reject unmatched parameters when applying symmetry */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800373 symmetry = rtd->dai_link->symmetric_rates;
374
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900375 for_each_rtd_cpu_dais(rtd, i, dai)
376 symmetry |= dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200377
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800378 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900379 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800380 if (cpu_dai->rate && cpu_dai->rate != rate) {
381 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
382 cpu_dai->rate, rate);
383 return -EINVAL;
384 }
385 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800386 }
387
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800388 symmetry = rtd->dai_link->symmetric_channels;
389
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900390 for_each_rtd_dais(rtd, i, dai)
391 symmetry |= dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800393 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900394 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800395 if (cpu_dai->channels &&
396 cpu_dai->channels != channels) {
397 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
398 cpu_dai->channels, channels);
399 return -EINVAL;
400 }
401 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800402 }
403
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800404 symmetry = rtd->dai_link->symmetric_samplebits;
405
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900406 for_each_rtd_dais(rtd, i, dai)
407 symmetry |= dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800409 if (symmetry) {
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900410 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800411 if (cpu_dai->sample_bits &&
412 cpu_dai->sample_bits != sample_bits) {
413 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
414 cpu_dai->sample_bits, sample_bits);
415 return -EINVAL;
416 }
417 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 }
419
420 return 0;
421}
422
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100423static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
424{
425 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100426 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900427 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200428 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100429
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800430 symmetry = link->symmetric_rates ||
431 link->symmetric_channels ||
432 link->symmetric_samplebits;
433
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900434 for_each_rtd_dais(rtd, i, dai)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800435 symmetry = symmetry ||
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900436 dai->driver->symmetric_rates ||
437 dai->driver->symmetric_channels ||
438 dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200439
440 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100441}
442
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200443static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000444{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200445 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100446 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000447
448 if (!bits)
449 return;
450
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100451 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
452 if (ret != 0)
453 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
454 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000455}
456
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200457static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200458{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200459 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800460 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200461 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900462 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
463 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200464 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800465 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200466
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900467 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900468 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
469
470 if (pcm_codec->sig_bits == 0) {
471 bits = 0;
472 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200473 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900474 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200475 }
476
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900477 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800478 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
479
480 if (pcm_cpu->sig_bits == 0) {
481 cpu_bits = 0;
482 break;
483 }
484 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
485 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900486
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200487 soc_pcm_set_msb(substream, bits);
488 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200489}
490
Samuel Holland5854a462020-03-04 23:11:42 -0600491/**
492 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
493 * @rtd: ASoC PCM runtime
494 * @hw: PCM hardware parameters (output)
495 * @stream: Direction of the PCM stream
496 *
497 * Calculates the subset of stream parameters supported by all DAIs
498 * associated with the PCM stream.
499 */
500int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
501 struct snd_pcm_hardware *hw, int stream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200502{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000503 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800504 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200505 struct snd_soc_pcm_stream *codec_stream;
506 struct snd_soc_pcm_stream *cpu_stream;
507 unsigned int chan_min = 0, chan_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800508 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200509 unsigned int rate_min = 0, rate_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800510 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
511 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200512 u64 formats = ULLONG_MAX;
513 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100514
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800515 /* first calculate min/max only for CPUs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900516 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800517
518 /*
519 * Skip CPUs which don't support the current stream type.
520 * Otherwise, since the rate, channel, and format values will
521 * zero in that case, we would have no usable settings left,
522 * causing the resulting setup to fail.
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800523 */
Samuel Holland5854a462020-03-04 23:11:42 -0600524 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800525 continue;
526
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800527 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100528
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800529 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
530 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
531 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
532 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
533 formats &= cpu_stream->formats;
534 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
535 cpu_rates);
536 }
537
538 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900539 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200540
541 /*
542 * Skip CODECs which don't support the current stream type.
543 * Otherwise, since the rate, channel, and format values will
544 * zero in that case, we would have no usable settings left,
545 * causing the resulting setup to fail.
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200546 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900547 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200548 continue;
549
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900550 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
551
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200552 chan_min = max(chan_min, codec_stream->channels_min);
553 chan_max = min(chan_max, codec_stream->channels_max);
554 rate_min = max(rate_min, codec_stream->rate_min);
555 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
556 formats &= codec_stream->formats;
557 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
558 }
559
Samuel Holland5854a462020-03-04 23:11:42 -0600560 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
561 if (!chan_min || !cpu_chan_min)
562 return -EINVAL;
563
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200564 /*
565 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800566 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200567 * channel allocation be fixed up later
568 */
569 if (rtd->num_codecs > 1) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800570 chan_min = cpu_chan_min;
571 chan_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200572 }
573
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800574 /* finally find a intersection between CODECs and CPUs */
575 hw->channels_min = max(chan_min, cpu_chan_min);
576 hw->channels_max = min(chan_max, cpu_chan_max);
Samuel Holland5854a462020-03-04 23:11:42 -0600577 hw->formats = formats;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800578 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100579
Samuel Holland5854a462020-03-04 23:11:42 -0600580 snd_pcm_hw_limit_rates(hw);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100581
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800582 hw->rate_min = max(hw->rate_min, cpu_rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200583 hw->rate_min = max(hw->rate_min, rate_min);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800584 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200585 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Samuel Holland5854a462020-03-04 23:11:42 -0600586
587 return 0;
588}
589EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
590
591static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
592{
593 struct snd_pcm_hardware *hw = &substream->runtime->hw;
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 u64 formats = hw->formats;
596
597 /*
598 * At least one CPU and one CODEC should match. Otherwise, we should
599 * have bailed out on a higher level, since there would be no CPU or
600 * CODEC to support the transfer direction in that case.
601 */
602 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
603
604 if (formats)
605 hw->formats &= formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200606}
607
Kuninori Morimotodd039072020-02-10 12:14:37 +0900608static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900609{
610 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200611 struct snd_soc_component *last = NULL;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900612 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900613 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900614
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900615 for_each_rtd_components(rtd, i, component) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200616 last = component;
617
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900618 ret = snd_soc_component_module_get_when_open(component);
619 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900620 dev_err(component->dev,
621 "ASoC: can't get module %s\n",
622 component->name);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200623 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900624 }
625
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900626 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900627 if (ret < 0) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200628 snd_soc_component_module_put_when_close(component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900629 dev_err(component->dev,
630 "ASoC: can't open component %s: %d\n",
631 component->name, ret);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200632 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900633 }
634 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900635
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200636 if (ret < 0) {
637 /* rollback on error */
638 for_each_rtd_components(rtd, i, component) {
639 if (component == last)
640 break;
641
642 snd_soc_component_close(component, substream);
643 snd_soc_component_module_put_when_close(component);
644 }
645 }
646
647 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900648}
649
Kuninori Morimotodd039072020-02-10 12:14:37 +0900650static int soc_pcm_components_close(struct snd_pcm_substream *substream)
Charles Keepax244e2932018-06-19 16:22:09 +0100651{
652 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100653 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900654 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100655
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900656 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900657 r = snd_soc_component_close(component, substream);
658 if (r < 0)
659 ret = r; /* use last ret */
660
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900661 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100662 }
663
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900664 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100665}
666
Mark Brown58ba9b22012-01-16 18:38:51 +0000667/*
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900668 * Called by ALSA when a PCM substream is closed. Private data can be
669 * freed here. The cpu DAI, codec DAI, machine and components are also
670 * shutdown.
671 */
672static int soc_pcm_close(struct snd_pcm_substream *substream)
673{
674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
675 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900676 struct snd_soc_dai *dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900677 int i;
678
679 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
680
681 snd_soc_runtime_deactivate(rtd, substream->stream);
682
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900683 for_each_rtd_dais(rtd, i, dai)
684 snd_soc_dai_shutdown(dai, substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900685
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900686 snd_soc_link_shutdown(substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900687
688 soc_pcm_components_close(substream);
689
690 snd_soc_dapm_stream_stop(rtd, substream->stream);
691
692 mutex_unlock(&rtd->card->pcm_mutex);
693
694 for_each_rtd_components(rtd, i, component) {
695 pm_runtime_mark_last_busy(component->dev);
696 pm_runtime_put_autosuspend(component->dev);
697 }
698
699 for_each_rtd_components(rtd, i, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900700 if (!snd_soc_component_active(component))
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900701 pinctrl_pm_select_sleep_state(component->dev);
702
703 return 0;
704}
705
706/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
708 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100709 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100710 */
711static int soc_pcm_open(struct snd_pcm_substream *substream)
712{
713 struct snd_soc_pcm_runtime *rtd = substream->private_data;
714 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000715 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900716 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200717 const char *codec_dai_name = "multicodec";
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800718 const char *cpu_dai_name = "multicpu";
Charles Keepax244e2932018-06-19 16:22:09 +0100719 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900721 for_each_rtd_components(rtd, i, component)
722 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000723
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900724 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000725 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000726
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300727 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100728
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900729 ret = soc_pcm_components_open(substream);
730 if (ret < 0)
731 goto component_err;
732
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900733 ret = snd_soc_link_startup(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900734 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200735 goto rtd_startup_err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900736
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 /* startup the audio subsystem */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900738 for_each_rtd_dais(rtd, i, dai) {
739 ret = snd_soc_dai_startup(dai, substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800740 if (ret < 0) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900741 dev_err(dai->dev,
742 "ASoC: can't open DAI %s: %d\n",
743 dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900744 goto config_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200746
747 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900748 dai->tx_mask = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200749 else
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900750 dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100751 }
752
Liam Girdwood01d75842012-04-25 12:12:49 +0100753 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
754 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
755 goto dynamic;
756
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200758 soc_pcm_init_runtime_hw(substream);
759
760 if (rtd->num_codecs == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900761 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100762
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800763 if (rtd->num_cpus == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900764 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800765
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100766 if (soc_pcm_has_symmetry(substream))
767 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
768
Liam Girdwoodddee6272011-06-09 14:45:53 +0100769 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100770 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000771 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800772 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773 goto config_err;
774 }
775 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000776 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800777 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100778 goto config_err;
779 }
780 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
781 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000782 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800783 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100784 goto config_err;
785 }
786
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200787 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000788
Liam Girdwoodddee6272011-06-09 14:45:53 +0100789 /* Symmetry only applies if we've already got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900790 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900791 if (snd_soc_dai_active(dai)) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900792 ret = soc_pcm_apply_symmetry(substream, dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200793 if (ret != 0)
794 goto config_err;
795 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100796 }
797
Liam Girdwood103d84a2012-11-19 14:39:15 +0000798 pr_debug("ASoC: %s <-> %s info:\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800799 codec_dai_name, cpu_dai_name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000800 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
801 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100802 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000803 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100804 runtime->hw.rate_max);
805
Liam Girdwood01d75842012-04-25 12:12:49 +0100806dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100807
808 snd_soc_runtime_activate(rtd, substream->stream);
809
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300810 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100811 return 0;
812
813config_err:
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900814 for_each_rtd_dais(rtd, i, dai)
815 snd_soc_dai_shutdown(dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200816
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900817 snd_soc_link_shutdown(substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200818rtd_startup_err:
Kuninori Morimotodd039072020-02-10 12:14:37 +0900819 soc_pcm_components_close(substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200820component_err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300821 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000822
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900823 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000824 pm_runtime_mark_last_busy(component->dev);
825 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530826 }
827
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900828 for_each_rtd_components(rtd, i, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900829 if (!snd_soc_component_active(component))
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900830 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000831
Liam Girdwoodddee6272011-06-09 14:45:53 +0100832 return ret;
833}
834
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800835static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200836{
837 /*
838 * Currently nothing to do for c2c links
839 * Since c2c links are internal nodes in the DAPM graph and
840 * don't interface with the outside world or application layer
841 * we don't have to do any special handling on close.
842 */
843}
844
Liam Girdwoodddee6272011-06-09 14:45:53 +0100845/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100846 * Called by ALSA when the PCM substream is prepared, can set format, sample
847 * rate, etc. This function is non atomic and can be called multiple times,
848 * it can refer to the runtime info.
849 */
850static int soc_pcm_prepare(struct snd_pcm_substream *substream)
851{
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000853 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900854 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200855 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100856
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300857 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100858
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900859 ret = snd_soc_link_prepare(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900860 if (ret < 0)
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900861 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100862
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900863 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900864 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000865 if (ret < 0) {
866 dev_err(component->dev,
867 "ASoC: platform prepare error: %d\n", ret);
868 goto out;
869 }
870 }
871
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900872 ret = snd_soc_pcm_dai_prepare(substream);
873 if (ret < 0) {
874 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
875 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100876 }
877
878 /* cancel any delayed stream shutdown that is pending */
879 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600880 rtd->pop_wait) {
881 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100882 cancel_delayed_work(&rtd->delayed_work);
883 }
884
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000885 snd_soc_dapm_stream_event(rtd, substream->stream,
886 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100887
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900888 for_each_rtd_dais(rtd, i, dai)
889 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100890
891out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300892 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100893 return ret;
894}
895
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200896static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
897 unsigned int mask)
898{
899 struct snd_interval *interval;
900 int channels = hweight_long(mask);
901
902 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
903 interval->min = channels;
904 interval->max = channels;
905}
906
Charles Keepax244e2932018-06-19 16:22:09 +0100907static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
908 struct snd_soc_component *last)
909{
910 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100911 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900912 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100913
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900914 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100915 if (component == last)
916 break;
917
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900918 r = snd_soc_component_hw_free(component, substream);
919 if (r < 0)
920 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +0100921 }
922
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900923 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100924}
925
Liam Girdwoodddee6272011-06-09 14:45:53 +0100926/*
927 * Called by ALSA when the hardware params are set by application. This
928 * function can also be called multiple times and can allocate buffers
929 * (using snd_pcm_lib_* ). It's non-atomic.
930 */
931static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
932 struct snd_pcm_hw_params *params)
933{
934 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000935 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800936 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000937 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100938 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100939
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300940 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800941
942 ret = soc_pcm_params_symmetry(substream, params);
943 if (ret)
944 goto out;
945
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900946 ret = snd_soc_link_hw_params(substream, params);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900947 if (ret < 0)
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900948 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100949
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900950 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200951 struct snd_pcm_hw_params codec_params;
952
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200953 /*
954 * Skip CODECs which don't support the current stream type,
955 * the idea being that if a CODEC is not used for the currently
956 * set up transfer direction, it should not need to be
957 * configured, especially since the configuration used might
958 * not even be supported by that CODEC. There may be cases
959 * however where a CODEC needs to be set up although it is
960 * actually not being used for the transfer, e.g. if a
961 * capture-only CODEC is acting as an LRCLK and/or BCLK master
962 * for the DAI link including a playback-only CODEC.
963 * If this becomes necessary, we will have to augment the
964 * machine driver setup with information on how to act, so
965 * we can do the right thing here.
966 */
967 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
968 continue;
969
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200970 /* copy params for each codec */
971 codec_params = *params;
972
973 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800974 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
975 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200976 soc_pcm_codec_params_fixup(&codec_params,
977 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800978
979 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
980 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200981 soc_pcm_codec_params_fixup(&codec_params,
982 codec_dai->rx_mask);
983
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900984 ret = snd_soc_dai_hw_params(codec_dai, substream,
985 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200986 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100987 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200988
989 codec_dai->rate = params_rate(&codec_params);
990 codec_dai->channels = params_channels(&codec_params);
991 codec_dai->sample_bits = snd_pcm_format_physical_width(
992 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000993
994 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100995 }
996
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900997 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800998 /*
999 * Skip CPUs which don't support the current stream
1000 * type. See soc_pcm_init_runtime_hw() for more details
1001 */
1002 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1003 continue;
1004
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001005 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1006 if (ret < 0)
1007 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001008
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001009 /* store the parameters for each DAI */
1010 cpu_dai->rate = params_rate(params);
1011 cpu_dai->channels = params_channels(params);
1012 cpu_dai->sample_bits =
1013 snd_pcm_format_physical_width(params_format(params));
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001014
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001015 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1016 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001017
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001018 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +09001019 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +01001020 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +00001021 dev_err(component->dev,
1022 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +01001023 component->name, ret);
1024 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001025 }
1026 }
Charles Keepax244e2932018-06-19 16:22:09 +01001027 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001028
Liam Girdwoodddee6272011-06-09 14:45:53 +01001029out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001030 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001031 return ret;
1032
Kuninori Morimotob8135862017-10-11 01:37:23 +00001033component_err:
Charles Keepax244e2932018-06-19 16:22:09 +01001034 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001035
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001036 i = rtd->num_cpus;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001037
1038interface_err:
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001039 for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001040 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1041 continue;
1042
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001043 snd_soc_dai_hw_free(cpu_dai, substream);
1044 cpu_dai->rate = 0;
1045 }
1046
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001047 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001048
1049codec_err:
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001050 for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001051 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1052 continue;
1053
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001054 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001055 codec_dai->rate = 0;
1056 }
1057
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +09001058 snd_soc_link_hw_free(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001059
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001060 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001061 return ret;
1062}
1063
1064/*
1065 * Frees resources allocated by hw_params, can be called multiple times
1066 */
1067static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1068{
1069 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001070 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001071 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001072
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001073 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001074
Nicolin Chend3383422013-11-20 18:37:09 +08001075 /* clear the corresponding DAIs parameters when going to be inactive */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001076 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001077 int active = snd_soc_dai_stream_active(dai, substream->stream);
Nicolin Chend3383422013-11-20 18:37:09 +08001078
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001079 if (snd_soc_dai_active(dai) == 1) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001080 dai->rate = 0;
1081 dai->channels = 0;
1082 dai->sample_bits = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001083 }
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +09001084
Kuninori Morimoto67ad8772020-03-06 10:10:04 +09001085 if (active == 1)
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001086 snd_soc_dai_digital_mute(dai, 1, substream->stream);
Kuninori Morimotoa9ee3312020-03-06 10:10:17 +09001087 }
1088
Liam Girdwoodddee6272011-06-09 14:45:53 +01001089 /* free any machine hw params */
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +09001090 snd_soc_link_hw_free(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001091
Kuninori Morimotob8135862017-10-11 01:37:23 +00001092 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001093 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001094
Liam Girdwoodddee6272011-06-09 14:45:53 +01001095 /* now free hw params for the DAIs */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001096 for_each_rtd_dais(rtd, i, dai) {
1097 if (!snd_soc_dai_stream_valid(dai, substream->stream))
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001098 continue;
1099
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001100 snd_soc_dai_hw_free(dai, substream);
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001101 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001102
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001103 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001104 return 0;
1105}
1106
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001107static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001108{
1109 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001110 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001111 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001112
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +09001113 ret = snd_soc_link_trigger(substream, cmd);
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001114 if (ret < 0)
1115 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001116
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001117 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001118 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001119 if (ret < 0)
1120 return ret;
1121 }
1122
Kuninori Morimoto42f24722020-04-24 08:15:04 +09001123 return snd_soc_pcm_dai_trigger(substream, cmd);
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001124}
1125
1126static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1127{
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1129 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001130 int i, ret;
1131
Kuninori Morimoto42f24722020-04-24 08:15:04 +09001132 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1133 if (ret < 0)
1134 return ret;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001135
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001136 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001137 ret = snd_soc_component_trigger(component, substream, cmd);
1138 if (ret < 0)
1139 return ret;
1140 }
1141
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +09001142 ret = snd_soc_link_trigger(substream, cmd);
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001143 if (ret < 0)
1144 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001145
Liam Girdwoodddee6272011-06-09 14:45:53 +01001146 return 0;
1147}
1148
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001149static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1150{
1151 int ret;
1152
1153 switch (cmd) {
1154 case SNDRV_PCM_TRIGGER_START:
1155 case SNDRV_PCM_TRIGGER_RESUME:
1156 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1157 ret = soc_pcm_trigger_start(substream, cmd);
1158 break;
1159 case SNDRV_PCM_TRIGGER_STOP:
1160 case SNDRV_PCM_TRIGGER_SUSPEND:
1161 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1162 ret = soc_pcm_trigger_stop(substream, cmd);
1163 break;
1164 default:
1165 return -EINVAL;
1166 }
1167
1168 return ret;
1169}
1170
Liam Girdwoodddee6272011-06-09 14:45:53 +01001171/*
1172 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001173 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001174 * the runtime->delay will be updated accordingly.
1175 */
1176static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1177{
1178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001179 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001180 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001181 struct snd_pcm_runtime *runtime = substream->runtime;
1182 snd_pcm_uframes_t offset = 0;
1183 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001184 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001185 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001186 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001187
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301188 /* clearing the previous total delay */
1189 runtime->delay = 0;
1190
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001191 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001192
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301193 /* base delay if assigned in pointer callback */
1194 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001195
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001196 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001197 cpu_delay = max(cpu_delay,
1198 snd_soc_dai_delay(cpu_dai, substream));
1199 }
1200 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001201
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001202 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001203 codec_delay = max(codec_delay,
1204 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001205 }
1206 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001207
Liam Girdwoodddee6272011-06-09 14:45:53 +01001208 runtime->delay = delay;
1209
1210 return offset;
1211}
1212
Liam Girdwood01d75842012-04-25 12:12:49 +01001213/* connect a FE and BE */
1214static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1215 struct snd_soc_pcm_runtime *be, int stream)
1216{
1217 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001218 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001219
1220 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001221 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001222 if (dpcm->be == be && dpcm->fe == fe)
1223 return 0;
1224 }
1225
1226 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1227 if (!dpcm)
1228 return -ENOMEM;
1229
1230 dpcm->be = be;
1231 dpcm->fe = fe;
1232 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1233 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001234 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1236 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001237 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001238
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001239 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001240 stream ? "capture" : "playback", fe->dai_link->name,
1241 stream ? "<-" : "->", be->dai_link->name);
1242
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001243 dpcm_create_debugfs_state(dpcm, stream);
1244
Liam Girdwood01d75842012-04-25 12:12:49 +01001245 return 1;
1246}
1247
1248/* reparent a BE onto another FE */
1249static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1250 struct snd_soc_pcm_runtime *be, int stream)
1251{
1252 struct snd_soc_dpcm *dpcm;
1253 struct snd_pcm_substream *fe_substream, *be_substream;
1254
1255 /* reparent if BE is connected to other FEs */
1256 if (!be->dpcm[stream].users)
1257 return;
1258
1259 be_substream = snd_soc_dpcm_get_substream(be, stream);
1260
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001261 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001262 if (dpcm->fe == fe)
1263 continue;
1264
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001265 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001266 stream ? "capture" : "playback",
1267 dpcm->fe->dai_link->name,
1268 stream ? "<-" : "->", dpcm->be->dai_link->name);
1269
1270 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1271 be_substream->runtime = fe_substream->runtime;
1272 break;
1273 }
1274}
1275
1276/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001277void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001278{
1279 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001280 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001281
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001282 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001283 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001284 stream ? "capture" : "playback",
1285 dpcm->be->dai_link->name);
1286
1287 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1288 continue;
1289
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001290 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001291 stream ? "capture" : "playback", fe->dai_link->name,
1292 stream ? "<-" : "->", dpcm->be->dai_link->name);
1293
1294 /* BEs still alive need new FE */
1295 dpcm_be_reparent(fe, dpcm->be, stream);
1296
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001297 dpcm_remove_debugfs_state(dpcm);
1298
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001299 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001300 list_del(&dpcm->list_be);
1301 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001302 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001303 kfree(dpcm);
1304 }
1305}
1306
1307/* get BE for DAI widget and stream */
1308static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1309 struct snd_soc_dapm_widget *widget, int stream)
1310{
1311 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001312 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001313 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001314 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001315
Liam Girdwood3c146462018-03-14 20:43:51 +00001316 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1317
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001318 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001319
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001320 if (!be->dai_link->no_pcm)
1321 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001322
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001323 for_each_rtd_dais(be, i, dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001324 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001325
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001326 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1327 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001328
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001329 if (w == widget)
1330 return be;
1331 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001332 }
1333
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001334 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001335 return NULL;
1336}
1337
Liam Girdwood01d75842012-04-25 12:12:49 +01001338static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1339 struct snd_soc_dapm_widget *widget)
1340{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001341 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001342 int i;
1343
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001344 for_each_dapm_widgets(list, i, w)
1345 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001346 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001347
1348 return 0;
1349}
1350
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001351static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1352 enum snd_soc_dapm_direction dir)
1353{
1354 struct snd_soc_card *card = widget->dapm->card;
1355 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001356 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001357
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001358 /* adjust dir to stream */
1359 if (dir == SND_SOC_DAPM_DIR_OUT)
1360 stream = SNDRV_PCM_STREAM_PLAYBACK;
1361 else
1362 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001363
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001364 rtd = dpcm_get_be(card, widget, stream);
1365 if (rtd)
1366 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001367
1368 return false;
1369}
1370
Liam Girdwood23607022014-01-17 17:03:55 +00001371int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001372 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001373{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001374 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood01d75842012-04-25 12:12:49 +01001375 int paths;
1376
Bard Liao6e1276a2020-02-25 21:39:16 +08001377 if (fe->num_cpus > 1) {
1378 dev_err(fe->dev,
1379 "%s doesn't support Multi CPU yet\n", __func__);
1380 return -EINVAL;
1381 }
1382
Liam Girdwood01d75842012-04-25 12:12:49 +01001383 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001384 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001385 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001386
Liam Girdwood103d84a2012-11-19 14:39:15 +00001387 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001388 stream ? "capture" : "playback");
1389
Liam Girdwood01d75842012-04-25 12:12:49 +01001390 return paths;
1391}
1392
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001393void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1394{
1395 snd_soc_dapm_dai_free_widgets(list);
1396}
1397
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001398static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1399 struct snd_soc_dapm_widget_list *list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001400{
Liam Girdwood01d75842012-04-25 12:12:49 +01001401 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001402 struct snd_soc_dai *dai;
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001403 unsigned int i;
1404
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001405 /* is there a valid DAI widget for this BE */
1406 for_each_rtd_dais(dpcm->be, i, dai) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001407 widget = snd_soc_dai_get_widget(dai, stream);
1408
1409 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001410 * The BE is pruned only if none of the dai
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001411 * widgets are in the active list.
1412 */
1413 if (widget && widget_in_list(list, widget))
1414 return true;
1415 }
1416
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001417 return false;
1418}
1419
1420static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1421 struct snd_soc_dapm_widget_list **list_)
1422{
1423 struct snd_soc_dpcm *dpcm;
Liam Girdwood01d75842012-04-25 12:12:49 +01001424 int prune = 0;
1425
1426 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001427 for_each_dpcm_be(fe, stream, dpcm) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001428 if (dpcm_be_is_active(dpcm, stream, *list_))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001429 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001430
Liam Girdwood103d84a2012-11-19 14:39:15 +00001431 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001432 stream ? "capture" : "playback",
1433 dpcm->be->dai_link->name, fe->dai_link->name);
1434 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1435 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1436 prune++;
1437 }
1438
Liam Girdwood103d84a2012-11-19 14:39:15 +00001439 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001440 return prune;
1441}
1442
1443static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1444 struct snd_soc_dapm_widget_list **list_)
1445{
1446 struct snd_soc_card *card = fe->card;
1447 struct snd_soc_dapm_widget_list *list = *list_;
1448 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001449 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001450 int i, new = 0, err;
1451
1452 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001453 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001454
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001455 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001456 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001457 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1458 continue;
1459 break;
Mark Brown46162742013-06-05 19:36:11 +01001460 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001461 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1462 continue;
Mark Brown46162742013-06-05 19:36:11 +01001463 break;
1464 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001465 continue;
Mark Brown46162742013-06-05 19:36:11 +01001466 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001467
1468 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001469 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001470 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001471 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001472 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001473 continue;
1474 }
1475
Liam Girdwood01d75842012-04-25 12:12:49 +01001476 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001477 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001478 continue;
1479
1480 /* newly connected FE and BE */
1481 err = dpcm_be_connect(fe, be, stream);
1482 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001483 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001484 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001485 break;
1486 } else if (err == 0) /* already connected */
1487 continue;
1488
1489 /* new */
1490 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1491 new++;
1492 }
1493
Liam Girdwood103d84a2012-11-19 14:39:15 +00001494 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001495 return new;
1496}
1497
1498/*
1499 * Find the corresponding BE DAIs that source or sink audio to this
1500 * FE substream.
1501 */
Liam Girdwood23607022014-01-17 17:03:55 +00001502int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001503 int stream, struct snd_soc_dapm_widget_list **list, int new)
1504{
1505 if (new)
1506 return dpcm_add_paths(fe, stream, list);
1507 else
1508 return dpcm_prune_paths(fe, stream, list);
1509}
1510
Liam Girdwood23607022014-01-17 17:03:55 +00001511void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001512{
1513 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001514 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001515
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001516 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001517 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001518 dpcm->be->dpcm[stream].runtime_update =
1519 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001520 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001521}
1522
1523static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1524 int stream)
1525{
1526 struct snd_soc_dpcm *dpcm;
1527
1528 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001529 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001530
1531 struct snd_soc_pcm_runtime *be = dpcm->be;
1532 struct snd_pcm_substream *be_substream =
1533 snd_soc_dpcm_get_substream(be, stream);
1534
1535 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001536 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001537 stream ? "capture" : "playback",
1538 be->dpcm[stream].state);
1539
1540 if (--be->dpcm[stream].users != 0)
1541 continue;
1542
1543 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1544 continue;
1545
1546 soc_pcm_close(be_substream);
1547 be_substream->runtime = NULL;
1548 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1549 }
1550}
1551
Liam Girdwood23607022014-01-17 17:03:55 +00001552int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001553{
1554 struct snd_soc_dpcm *dpcm;
1555 int err, count = 0;
1556
1557 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001558 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001559
1560 struct snd_soc_pcm_runtime *be = dpcm->be;
1561 struct snd_pcm_substream *be_substream =
1562 snd_soc_dpcm_get_substream(be, stream);
1563
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001564 if (!be_substream) {
1565 dev_err(be->dev, "ASoC: no backend %s stream\n",
1566 stream ? "capture" : "playback");
1567 continue;
1568 }
1569
Liam Girdwood01d75842012-04-25 12:12:49 +01001570 /* is this op for this BE ? */
1571 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1572 continue;
1573
1574 /* first time the dpcm is open ? */
1575 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001576 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001577 stream ? "capture" : "playback",
1578 be->dpcm[stream].state);
1579
1580 if (be->dpcm[stream].users++ != 0)
1581 continue;
1582
1583 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1584 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1585 continue;
1586
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001587 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1588 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001589
1590 be_substream->runtime = be->dpcm[stream].runtime;
1591 err = soc_pcm_open(be_substream);
1592 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001593 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001594 be->dpcm[stream].users--;
1595 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001596 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001597 stream ? "capture" : "playback",
1598 be->dpcm[stream].state);
1599
1600 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1601 goto unwind;
1602 }
1603
1604 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1605 count++;
1606 }
1607
1608 return count;
1609
1610unwind:
1611 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001612 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001613 struct snd_soc_pcm_runtime *be = dpcm->be;
1614 struct snd_pcm_substream *be_substream =
1615 snd_soc_dpcm_get_substream(be, stream);
1616
1617 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1618 continue;
1619
1620 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001621 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001622 stream ? "capture" : "playback",
1623 be->dpcm[stream].state);
1624
1625 if (--be->dpcm[stream].users != 0)
1626 continue;
1627
1628 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1629 continue;
1630
1631 soc_pcm_close(be_substream);
1632 be_substream->runtime = NULL;
1633 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1634 }
1635
1636 return err;
1637}
1638
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001639static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001640 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001641{
1642 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001643 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001644 runtime->hw.channels_min = stream->channels_min;
1645 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001646 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001647 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001648 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001649 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001650 runtime->hw.rates = stream->rates;
1651}
1652
Jerome Brunet435ffb72018-07-05 12:13:48 +02001653static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1654 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001655{
1656 struct snd_soc_pcm_runtime *fe = substream->private_data;
1657 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001658 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001659 int stream = substream->stream;
1660
1661 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001662 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001663
1664 /*
1665 * It returns merged BE codec format
1666 * if FE want to use it (= dpcm_merged_format)
1667 */
1668
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001669 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001670 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001671 struct snd_soc_pcm_stream *codec_stream;
1672 int i;
1673
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001674 for_each_rtd_codec_dais(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001675 /*
1676 * Skip CODECs which don't support the current stream
1677 * type. See soc_pcm_init_runtime_hw() for more details
1678 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001679 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001680 continue;
1681
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001682 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001683
Jerome Brunet435ffb72018-07-05 12:13:48 +02001684 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001685 }
1686 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001687}
1688
Jerome Brunet435ffb72018-07-05 12:13:48 +02001689static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1690 unsigned int *channels_min,
1691 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001692{
1693 struct snd_soc_pcm_runtime *fe = substream->private_data;
1694 struct snd_soc_dpcm *dpcm;
1695 int stream = substream->stream;
1696
1697 if (!fe->dai_link->dpcm_merged_chan)
1698 return;
1699
Jiada Wangf4c277b2018-06-20 18:25:20 +09001700 /*
1701 * It returns merged BE codec channel;
1702 * if FE want to use it (= dpcm_merged_chan)
1703 */
1704
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001705 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001706 struct snd_soc_pcm_runtime *be = dpcm->be;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001707 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001708 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001709 struct snd_soc_dai *dai;
1710 int i;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001711
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001712 for_each_rtd_cpu_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001713 /*
1714 * Skip CPUs which don't support the current stream
1715 * type. See soc_pcm_init_runtime_hw() for more details
1716 */
1717 if (!snd_soc_dai_stream_valid(dai, stream))
1718 continue;
1719
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001720 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001721
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001722 *channels_min = max(*channels_min,
1723 cpu_stream->channels_min);
1724 *channels_max = min(*channels_max,
1725 cpu_stream->channels_max);
1726 }
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001727
1728 /*
1729 * chan min/max cannot be enforced if there are multiple CODEC
1730 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1731 */
1732 if (be->num_codecs == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001733 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001734
1735 *channels_min = max(*channels_min,
1736 codec_stream->channels_min);
1737 *channels_max = min(*channels_max,
1738 codec_stream->channels_max);
1739 }
1740 }
1741}
1742
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001743static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1744 unsigned int *rates,
1745 unsigned int *rate_min,
1746 unsigned int *rate_max)
1747{
1748 struct snd_soc_pcm_runtime *fe = substream->private_data;
1749 struct snd_soc_dpcm *dpcm;
1750 int stream = substream->stream;
1751
1752 if (!fe->dai_link->dpcm_merged_rate)
1753 return;
1754
1755 /*
1756 * It returns merged BE codec channel;
1757 * if FE want to use it (= dpcm_merged_chan)
1758 */
1759
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001760 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001761 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001762 struct snd_soc_pcm_stream *pcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001763 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001764 int i;
1765
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001766 for_each_rtd_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001767 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001768 * Skip DAIs which don't support the current stream
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001769 * type. See soc_pcm_init_runtime_hw() for more details
1770 */
1771 if (!snd_soc_dai_stream_valid(dai, stream))
1772 continue;
1773
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001774 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001775
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001776 *rate_min = max(*rate_min, pcm->rate_min);
1777 *rate_max = min_not_zero(*rate_max, pcm->rate_max);
1778 *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001779 }
1780 }
1781}
1782
Mark Brown45c0a182012-05-09 21:46:27 +01001783static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001784{
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1786 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001787 struct snd_soc_dai *cpu_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001788 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001789
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001790 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001791 /*
1792 * Skip CPUs which don't support the current stream
1793 * type. See soc_pcm_init_runtime_hw() for more details
1794 */
1795 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1796 continue;
1797
Kuninori Morimoto0c9ba722020-03-06 10:09:54 +09001798 dpcm_init_runtime_hw(runtime,
1799 snd_soc_dai_get_pcm_stream(cpu_dai,
1800 substream->stream));
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001801 }
Jiada Wangf4c277b2018-06-20 18:25:20 +09001802
Jerome Brunet435ffb72018-07-05 12:13:48 +02001803 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1804 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1805 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001806 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1807 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001808}
1809
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001810static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1811
1812/* Set FE's runtime_update state; the state is protected via PCM stream lock
1813 * for avoiding the race with trigger callback.
1814 * If the state is unset and a trigger is pending while the previous operation,
1815 * process the pending trigger action here.
1816 */
1817static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1818 int stream, enum snd_soc_dpcm_update state)
1819{
1820 struct snd_pcm_substream *substream =
1821 snd_soc_dpcm_get_substream(fe, stream);
1822
1823 snd_pcm_stream_lock_irq(substream);
1824 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1825 dpcm_fe_dai_do_trigger(substream,
1826 fe->dpcm[stream].trigger_pending - 1);
1827 fe->dpcm[stream].trigger_pending = 0;
1828 }
1829 fe->dpcm[stream].runtime_update = state;
1830 snd_pcm_stream_unlock_irq(substream);
1831}
1832
PC Liao906c7d62015-12-11 11:33:51 +08001833static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1834 int stream)
1835{
1836 struct snd_soc_dpcm *dpcm;
1837 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001838 struct snd_soc_dai *fe_cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001839 int err;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001840 int i;
PC Liao906c7d62015-12-11 11:33:51 +08001841
1842 /* apply symmetry for FE */
1843 if (soc_pcm_has_symmetry(fe_substream))
1844 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1845
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001846 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001847 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001848 if (snd_soc_dai_active(fe_cpu_dai)) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001849 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1850 if (err < 0)
1851 return err;
1852 }
PC Liao906c7d62015-12-11 11:33:51 +08001853 }
1854
1855 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001856 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001857 struct snd_soc_pcm_runtime *be = dpcm->be;
1858 struct snd_pcm_substream *be_substream =
1859 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001860 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001861 struct snd_soc_dai *dai;
PC Liao906c7d62015-12-11 11:33:51 +08001862 int i;
1863
Jerome Brunet6246f282019-04-01 15:03:54 +02001864 /* A backend may not have the requested substream */
1865 if (!be_substream)
1866 continue;
1867
1868 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301869 if (rtd->dai_link->be_hw_params_fixup)
1870 continue;
1871
PC Liao906c7d62015-12-11 11:33:51 +08001872 if (soc_pcm_has_symmetry(be_substream))
1873 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1874
1875 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001876 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001877 if (snd_soc_dai_active(dai)) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001878 err = soc_pcm_apply_symmetry(fe_substream, dai);
PC Liao906c7d62015-12-11 11:33:51 +08001879 if (err < 0)
1880 return err;
1881 }
1882 }
1883 }
1884
1885 return 0;
1886}
1887
Liam Girdwood01d75842012-04-25 12:12:49 +01001888static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1889{
1890 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1891 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1892 int stream = fe_substream->stream, ret = 0;
1893
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001894 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001895
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001896 ret = dpcm_be_dai_startup(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001897 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001899 goto be_err;
1900 }
1901
Liam Girdwood103d84a2012-11-19 14:39:15 +00001902 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001903
1904 /* start the DAI frontend */
1905 ret = soc_pcm_open(fe_substream);
1906 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001907 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001908 goto unwind;
1909 }
1910
1911 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1912
1913 dpcm_set_fe_runtime(fe_substream);
1914 snd_pcm_limit_hw_rates(runtime);
1915
PC Liao906c7d62015-12-11 11:33:51 +08001916 ret = dpcm_apply_symmetry(fe_substream, stream);
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09001917 if (ret < 0)
PC Liao906c7d62015-12-11 11:33:51 +08001918 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1919 ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001920
1921unwind:
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09001922 if (ret < 0)
1923 dpcm_be_dai_startup_unwind(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001924be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001925 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001926 return ret;
1927}
1928
Liam Girdwood23607022014-01-17 17:03:55 +00001929int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001930{
1931 struct snd_soc_dpcm *dpcm;
1932
1933 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001934 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001935
1936 struct snd_soc_pcm_runtime *be = dpcm->be;
1937 struct snd_pcm_substream *be_substream =
1938 snd_soc_dpcm_get_substream(be, stream);
1939
1940 /* is this op for this BE ? */
1941 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1942 continue;
1943
1944 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001945 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001946 stream ? "capture" : "playback",
1947 be->dpcm[stream].state);
1948
1949 if (--be->dpcm[stream].users != 0)
1950 continue;
1951
1952 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001953 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1954 soc_pcm_hw_free(be_substream);
1955 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1956 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001957
Liam Girdwood103d84a2012-11-19 14:39:15 +00001958 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001959 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001960
1961 soc_pcm_close(be_substream);
1962 be_substream->runtime = NULL;
1963
1964 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1965 }
1966 return 0;
1967}
1968
1969static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1970{
1971 struct snd_soc_pcm_runtime *fe = substream->private_data;
1972 int stream = substream->stream;
1973
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001974 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001975
1976 /* shutdown the BEs */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001977 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001978
Liam Girdwood103d84a2012-11-19 14:39:15 +00001979 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001980
1981 /* now shutdown the frontend */
1982 soc_pcm_close(substream);
1983
1984 /* run the stream event for each BE */
Kuninori Morimoto1c531232020-02-21 10:25:18 +09001985 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood01d75842012-04-25 12:12:49 +01001986
1987 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001988 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001989 return 0;
1990}
1991
Liam Girdwood23607022014-01-17 17:03:55 +00001992int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001993{
1994 struct snd_soc_dpcm *dpcm;
1995
1996 /* only hw_params backends that are either sinks or sources
1997 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001998 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001999
2000 struct snd_soc_pcm_runtime *be = dpcm->be;
2001 struct snd_pcm_substream *be_substream =
2002 snd_soc_dpcm_get_substream(be, stream);
2003
2004 /* is this op for this BE ? */
2005 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2006 continue;
2007
2008 /* only free hw when no longer used - check all FEs */
2009 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2010 continue;
2011
Qiao Zhou36fba622014-12-03 10:13:43 +08002012 /* do not free hw if this BE is used by other FE */
2013 if (be->dpcm[stream].users > 1)
2014 continue;
2015
Liam Girdwood01d75842012-04-25 12:12:49 +01002016 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2017 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2018 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002019 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2021 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002022 continue;
2023
Liam Girdwood103d84a2012-11-19 14:39:15 +00002024 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002025 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002026
2027 soc_pcm_hw_free(be_substream);
2028
2029 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2030 }
2031
2032 return 0;
2033}
2034
Mark Brown45c0a182012-05-09 21:46:27 +01002035static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002036{
2037 struct snd_soc_pcm_runtime *fe = substream->private_data;
2038 int err, stream = substream->stream;
2039
2040 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002041 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002042
Liam Girdwood103d84a2012-11-19 14:39:15 +00002043 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002044
2045 /* call hw_free on the frontend */
2046 err = soc_pcm_hw_free(substream);
2047 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002048 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002049 fe->dai_link->name);
2050
2051 /* only hw_params backends that are either sinks or sources
2052 * to this frontend DAI */
2053 err = dpcm_be_dai_hw_free(fe, stream);
2054
2055 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002056 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002057
2058 mutex_unlock(&fe->card->mutex);
2059 return 0;
2060}
2061
Liam Girdwood23607022014-01-17 17:03:55 +00002062int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002063{
2064 struct snd_soc_dpcm *dpcm;
2065 int ret;
2066
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002067 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002068
2069 struct snd_soc_pcm_runtime *be = dpcm->be;
2070 struct snd_pcm_substream *be_substream =
2071 snd_soc_dpcm_get_substream(be, stream);
2072
2073 /* is this op for this BE ? */
2074 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2075 continue;
2076
Liam Girdwood01d75842012-04-25 12:12:49 +01002077 /* copy params for each dpcm */
2078 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2079 sizeof(struct snd_pcm_hw_params));
2080
2081 /* perform any hw_params fixups */
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +09002082 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
2083 if (ret < 0)
2084 goto unwind;
Liam Girdwood01d75842012-04-25 12:12:49 +01002085
Libin Yangae061d22019-04-19 09:53:12 +08002086 /* copy the fixed-up hw params for BE dai */
2087 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2088 sizeof(struct snd_pcm_hw_params));
2089
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002090 /* only allow hw_params() if no connected FEs are running */
2091 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2092 continue;
2093
2094 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2096 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2097 continue;
2098
2099 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002100 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002101
Liam Girdwood01d75842012-04-25 12:12:49 +01002102 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2103 if (ret < 0) {
2104 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002105 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002106 goto unwind;
2107 }
2108
2109 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2110 }
2111 return 0;
2112
2113unwind:
2114 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002115 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002116 struct snd_soc_pcm_runtime *be = dpcm->be;
2117 struct snd_pcm_substream *be_substream =
2118 snd_soc_dpcm_get_substream(be, stream);
2119
2120 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2121 continue;
2122
2123 /* only allow hw_free() if no connected FEs are running */
2124 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2125 continue;
2126
2127 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2128 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2129 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2130 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2131 continue;
2132
2133 soc_pcm_hw_free(be_substream);
2134 }
2135
2136 return ret;
2137}
2138
Mark Brown45c0a182012-05-09 21:46:27 +01002139static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2140 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002141{
2142 struct snd_soc_pcm_runtime *fe = substream->private_data;
2143 int ret, stream = substream->stream;
2144
2145 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002146 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002147
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002148 memcpy(&fe->dpcm[stream].hw_params, params,
Liam Girdwood01d75842012-04-25 12:12:49 +01002149 sizeof(struct snd_pcm_hw_params));
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002150 ret = dpcm_be_dai_hw_params(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002151 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002152 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002153 goto out;
2154 }
2155
Liam Girdwood103d84a2012-11-19 14:39:15 +00002156 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002157 fe->dai_link->name, params_rate(params),
2158 params_channels(params), params_format(params));
2159
2160 /* call hw_params on the frontend */
2161 ret = soc_pcm_hw_params(substream, params);
2162 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002163 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002164 dpcm_be_dai_hw_free(fe, stream);
2165 } else
2166 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2167
2168out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002169 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002170 mutex_unlock(&fe->card->mutex);
2171 return ret;
2172}
2173
2174static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2175 struct snd_pcm_substream *substream, int cmd)
2176{
2177 int ret;
2178
Liam Girdwood103d84a2012-11-19 14:39:15 +00002179 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002180 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002181
2182 ret = soc_pcm_trigger(substream, cmd);
2183 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002184 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002185
2186 return ret;
2187}
2188
Liam Girdwood23607022014-01-17 17:03:55 +00002189int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002190 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002191{
2192 struct snd_soc_dpcm *dpcm;
2193 int ret = 0;
2194
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002195 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002196
2197 struct snd_soc_pcm_runtime *be = dpcm->be;
2198 struct snd_pcm_substream *be_substream =
2199 snd_soc_dpcm_get_substream(be, stream);
2200
2201 /* is this op for this BE ? */
2202 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2203 continue;
2204
2205 switch (cmd) {
2206 case SNDRV_PCM_TRIGGER_START:
2207 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
이경택21fca8b2020-04-01 10:04:21 +09002208 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2209 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002210 continue;
2211
2212 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2213 if (ret)
2214 return ret;
2215
2216 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2217 break;
2218 case SNDRV_PCM_TRIGGER_RESUME:
2219 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2220 continue;
2221
2222 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2223 if (ret)
2224 return ret;
2225
2226 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2227 break;
2228 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2229 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2230 continue;
2231
2232 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2233 if (ret)
2234 return ret;
2235
2236 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2237 break;
2238 case SNDRV_PCM_TRIGGER_STOP:
이경택21fca8b2020-04-01 10:04:21 +09002239 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2240 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002241 continue;
2242
2243 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2244 continue;
2245
2246 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2247 if (ret)
2248 return ret;
2249
2250 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2251 break;
2252 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002253 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002254 continue;
2255
2256 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2257 continue;
2258
2259 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2260 if (ret)
2261 return ret;
2262
2263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2264 break;
2265 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2266 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2267 continue;
2268
2269 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2270 continue;
2271
2272 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2273 if (ret)
2274 return ret;
2275
2276 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2277 break;
2278 }
2279 }
2280
2281 return ret;
2282}
2283EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2284
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002285static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2286 int cmd, bool fe_first)
2287{
2288 struct snd_soc_pcm_runtime *fe = substream->private_data;
2289 int ret;
2290
2291 /* call trigger on the frontend before the backend. */
2292 if (fe_first) {
2293 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2294 fe->dai_link->name, cmd);
2295
2296 ret = soc_pcm_trigger(substream, cmd);
2297 if (ret < 0)
2298 return ret;
2299
2300 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2301 return ret;
2302 }
2303
2304 /* call trigger on the frontend after the backend. */
2305 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2306 if (ret < 0)
2307 return ret;
2308
2309 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2310 fe->dai_link->name, cmd);
2311
2312 ret = soc_pcm_trigger(substream, cmd);
2313
2314 return ret;
2315}
2316
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002317static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002318{
2319 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002320 int stream = substream->stream;
2321 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002322 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2323
2324 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2325
2326 switch (trigger) {
2327 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002328 switch (cmd) {
2329 case SNDRV_PCM_TRIGGER_START:
2330 case SNDRV_PCM_TRIGGER_RESUME:
2331 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2332 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2333 break;
2334 case SNDRV_PCM_TRIGGER_STOP:
2335 case SNDRV_PCM_TRIGGER_SUSPEND:
2336 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2337 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2338 break;
2339 default:
2340 ret = -EINVAL;
2341 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002342 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002343 break;
2344 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002345 switch (cmd) {
2346 case SNDRV_PCM_TRIGGER_START:
2347 case SNDRV_PCM_TRIGGER_RESUME:
2348 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2349 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2350 break;
2351 case SNDRV_PCM_TRIGGER_STOP:
2352 case SNDRV_PCM_TRIGGER_SUSPEND:
2353 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2354 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2355 break;
2356 default:
2357 ret = -EINVAL;
2358 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002359 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002360 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002361 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2362 /* bespoke trigger() - handles both FE and BEs */
2363
Liam Girdwood103d84a2012-11-19 14:39:15 +00002364 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002365 fe->dai_link->name, cmd);
2366
Kuninori Morimoto308193582020-04-24 08:15:09 +09002367 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002368 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002369 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002370 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002371 fe->dai_link->name);
2372 ret = -EINVAL;
2373 goto out;
2374 }
2375
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002376 if (ret < 0) {
2377 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2378 cmd, ret);
2379 goto out;
2380 }
2381
Liam Girdwood01d75842012-04-25 12:12:49 +01002382 switch (cmd) {
2383 case SNDRV_PCM_TRIGGER_START:
2384 case SNDRV_PCM_TRIGGER_RESUME:
2385 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2386 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2387 break;
2388 case SNDRV_PCM_TRIGGER_STOP:
2389 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002390 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2391 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002392 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2393 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2394 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002395 }
2396
2397out:
2398 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2399 return ret;
2400}
2401
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002402static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2403{
2404 struct snd_soc_pcm_runtime *fe = substream->private_data;
2405 int stream = substream->stream;
2406
2407 /* if FE's runtime_update is already set, we're in race;
2408 * process this trigger later at exit
2409 */
2410 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2411 fe->dpcm[stream].trigger_pending = cmd + 1;
2412 return 0; /* delayed, assuming it's successful */
2413 }
2414
2415 /* we're alone, let's trigger */
2416 return dpcm_fe_dai_do_trigger(substream, cmd);
2417}
2418
Liam Girdwood23607022014-01-17 17:03:55 +00002419int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002420{
2421 struct snd_soc_dpcm *dpcm;
2422 int ret = 0;
2423
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002424 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002425
2426 struct snd_soc_pcm_runtime *be = dpcm->be;
2427 struct snd_pcm_substream *be_substream =
2428 snd_soc_dpcm_get_substream(be, stream);
2429
2430 /* is this op for this BE ? */
2431 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2432 continue;
2433
2434 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002435 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002436 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002438 continue;
2439
Liam Girdwood103d84a2012-11-19 14:39:15 +00002440 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002441 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002442
2443 ret = soc_pcm_prepare(be_substream);
2444 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002445 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002446 ret);
2447 break;
2448 }
2449
2450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2451 }
2452 return ret;
2453}
2454
Mark Brown45c0a182012-05-09 21:46:27 +01002455static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002456{
2457 struct snd_soc_pcm_runtime *fe = substream->private_data;
2458 int stream = substream->stream, ret = 0;
2459
2460 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2461
Liam Girdwood103d84a2012-11-19 14:39:15 +00002462 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002463
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002464 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002465
2466 /* there is no point preparing this FE if there are no BEs */
2467 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002468 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002469 fe->dai_link->name);
2470 ret = -EINVAL;
2471 goto out;
2472 }
2473
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002474 ret = dpcm_be_dai_prepare(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002475 if (ret < 0)
2476 goto out;
2477
2478 /* call prepare on the frontend */
2479 ret = soc_pcm_prepare(substream);
2480 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002481 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002482 fe->dai_link->name);
2483 goto out;
2484 }
2485
2486 /* run the stream event for each BE */
2487 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2488 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2489
2490out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002491 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002492 mutex_unlock(&fe->card->mutex);
2493
2494 return ret;
2495}
2496
Liam Girdwood618dae12012-04-25 12:12:51 +01002497static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2498{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002499 struct snd_pcm_substream *substream =
2500 snd_soc_dpcm_get_substream(fe, stream);
2501 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002502 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002503
Liam Girdwood103d84a2012-11-19 14:39:15 +00002504 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002505 stream ? "capture" : "playback", fe->dai_link->name);
2506
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002507 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2508 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002509 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002510 fe->dai_link->name);
2511
Kuninori Morimoto308193582020-04-24 08:15:09 +09002512 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002513 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002514 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002515 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002516 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002517 fe->dai_link->name);
2518
2519 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2520 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002521 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002522 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002523
2524 err = dpcm_be_dai_hw_free(fe, stream);
2525 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002526 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002527
2528 err = dpcm_be_dai_shutdown(fe, stream);
2529 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002530 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002531
2532 /* run the stream event for each BE */
2533 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2534
2535 return 0;
2536}
2537
2538static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2539{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002540 struct snd_pcm_substream *substream =
2541 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002542 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002543 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002544 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002545 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002546
Liam Girdwood103d84a2012-11-19 14:39:15 +00002547 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002548 stream ? "capture" : "playback", fe->dai_link->name);
2549
2550 /* Only start the BE if the FE is ready */
2551 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2552 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2553 return -EINVAL;
2554
2555 /* startup must always be called for new BEs */
2556 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002557 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002558 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002559
2560 /* keep going if FE state is > open */
2561 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2562 return 0;
2563
2564 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002565 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002566 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002567
2568 /* keep going if FE state is > hw_params */
2569 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2570 return 0;
2571
2572
2573 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002574 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002575 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002576
2577 /* run the stream event for each BE */
2578 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2579
2580 /* keep going if FE state is > prepare */
2581 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2582 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2583 return 0;
2584
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002585 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2586 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002587 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002588 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002589
Kuninori Morimoto308193582020-04-24 08:15:09 +09002590 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002591 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002592 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002593 goto hw_free;
2594 }
2595 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002596 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002597 fe->dai_link->name);
2598
2599 ret = dpcm_be_dai_trigger(fe, stream,
2600 SNDRV_PCM_TRIGGER_START);
2601 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002602 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002603 goto hw_free;
2604 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002605 }
2606
2607 return 0;
2608
2609hw_free:
2610 dpcm_be_dai_hw_free(fe, stream);
2611close:
2612 dpcm_be_dai_shutdown(fe, stream);
2613disconnect:
朱灿灿68f8043e2020-05-29 18:12:44 +08002614 /* disconnect any closed BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002615 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002616 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002617 struct snd_soc_pcm_runtime *be = dpcm->be;
朱灿灿68f8043e2020-05-29 18:12:44 +08002618 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2619 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Liam Girdwood618dae12012-04-25 12:12:51 +01002620 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002621 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002622
2623 return ret;
2624}
2625
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002626static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2627{
2628 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002629 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002630 int count, paths;
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002631 int ret;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002632
Pierre-Louis Bossart96bf62f2020-06-12 15:35:07 -05002633 if (!fe->dai_link->dynamic)
2634 return 0;
2635
Bard Liao6e1276a2020-02-25 21:39:16 +08002636 if (fe->num_cpus > 1) {
2637 dev_err(fe->dev,
2638 "%s doesn't support Multi CPU yet\n", __func__);
2639 return -EINVAL;
2640 }
2641
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002642 /* only check active links */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002643 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002644 return 0;
2645
2646 /* DAPM sync will call this to update DSP paths */
2647 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2648 new ? "new" : "old", fe->dai_link->name);
2649
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002650 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002651
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002652 /* skip if FE doesn't have playback/capture capability */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002653 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2654 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002655 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002656
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002657 /* skip if FE isn't currently playing/capturing */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002658 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2659 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002660 continue;
2661
2662 paths = dpcm_path_get(fe, stream, &list);
2663 if (paths < 0) {
2664 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2665 fe->dai_link->name,
2666 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2667 "playback" : "capture");
2668 return paths;
2669 }
2670
2671 /* update any playback/capture paths */
2672 count = dpcm_process_paths(fe, stream, &list, new);
2673 if (count) {
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002674 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002675 if (new)
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002676 ret = dpcm_run_update_startup(fe, stream);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002677 else
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002678 ret = dpcm_run_update_shutdown(fe, stream);
2679 if (ret < 0)
2680 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2681 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002682
2683 dpcm_clear_pending_state(fe, stream);
2684 dpcm_be_disconnect(fe, stream);
2685 }
2686
2687 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002688 }
2689
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002690 return 0;
2691}
2692
Liam Girdwood618dae12012-04-25 12:12:51 +01002693/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2694 * any DAI links.
2695 */
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002696int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002697{
Mengdong Lin1a497982015-11-18 02:34:11 -05002698 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002699 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002700
Liam Girdwood618dae12012-04-25 12:12:51 +01002701 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002702 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002703 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002704 ret = soc_dpcm_fe_runtime_update(fe, 0);
2705 if (ret)
2706 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002707 }
2708
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002709 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002710 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002711 ret = soc_dpcm_fe_runtime_update(fe, 1);
2712 if (ret)
2713 goto out;
2714 }
2715
2716out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002717 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002718 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002719}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002720EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
Liam Girdwood01d75842012-04-25 12:12:49 +01002721
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002722static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002723{
2724 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2725 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002726 int stream = fe_substream->stream;
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002727
2728 /* mark FE's links ready to prune */
2729 for_each_dpcm_be(fe, stream, dpcm)
2730 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2731
2732 dpcm_be_disconnect(fe, stream);
2733
2734 fe->dpcm[stream].runtime = NULL;
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002735}
2736
2737static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2738{
2739 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2740 int ret;
2741
2742 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2743 ret = dpcm_fe_dai_shutdown(fe_substream);
2744
2745 dpcm_fe_dai_cleanup(fe_substream);
2746
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002747 mutex_unlock(&fe->card->mutex);
2748 return ret;
2749}
2750
Liam Girdwood01d75842012-04-25 12:12:49 +01002751static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2752{
2753 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Liam Girdwood01d75842012-04-25 12:12:49 +01002754 struct snd_soc_dapm_widget_list *list;
2755 int ret;
2756 int stream = fe_substream->stream;
2757
2758 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2759 fe->dpcm[stream].runtime = fe_substream->runtime;
2760
Qiao Zhou8f70e512014-09-10 17:54:07 +08002761 ret = dpcm_path_get(fe, stream, &list);
2762 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002763 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08002764 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002765 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002766 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002767 }
2768
2769 /* calculate valid and active FE <-> BE dpcms */
2770 dpcm_process_paths(fe, stream, &list, 1);
2771
2772 ret = dpcm_fe_dai_startup(fe_substream);
Kuninori Morimoto265694b62020-03-06 10:09:49 +09002773 if (ret < 0)
2774 dpcm_fe_dai_cleanup(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002775
2776 dpcm_clear_pending_state(fe, stream);
2777 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002778open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002779 mutex_unlock(&fe->card->mutex);
2780 return ret;
2781}
2782
Liam Girdwoodddee6272011-06-09 14:45:53 +01002783/* create a new pcm */
2784int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2785{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002786 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002787 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002788 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002789 struct snd_pcm *pcm;
2790 char new_name[64];
2791 int ret = 0, playback = 0, capture = 0;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002792 int stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002793 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002794
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002795 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2796 dev_err(rtd->dev,
2797 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2798 return -EINVAL;
2799 }
Stephan Gerhold9b5db052020-04-15 12:49:28 +02002800
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002801 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2802 if (rtd->dai_link->dpcm_playback) {
2803 stream = SNDRV_PCM_STREAM_PLAYBACK;
2804
2805 for_each_rtd_cpu_dais(rtd, i, cpu_dai)
2806 if (!snd_soc_dai_stream_valid(cpu_dai,
2807 stream)) {
2808 dev_err(rtd->card->dev,
2809 "CPU DAI %s for rtd %s does not support playback\n",
2810 cpu_dai->name,
2811 rtd->dai_link->stream_name);
2812 return -EINVAL;
2813 }
2814 playback = 1;
2815 }
2816 if (rtd->dai_link->dpcm_capture) {
2817 stream = SNDRV_PCM_STREAM_CAPTURE;
2818
2819 for_each_rtd_cpu_dais(rtd, i, cpu_dai)
2820 if (!snd_soc_dai_stream_valid(cpu_dai,
2821 stream)) {
2822 dev_err(rtd->card->dev,
2823 "CPU DAI %s for rtd %s does not support capture\n",
2824 cpu_dai->name,
2825 rtd->dai_link->stream_name);
2826 return -EINVAL;
2827 }
2828 capture = 1;
2829 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002830 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002831 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002832 int cpu_capture = rtd->dai_link->params ?
2833 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2834 int cpu_playback = rtd->dai_link->params ?
2835 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002836
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002837 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002838 if (rtd->num_cpus == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002839 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002840 } else if (rtd->num_cpus == rtd->num_codecs) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002841 cpu_dai = asoc_rtd_to_cpu(rtd, i);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002842 } else {
2843 dev_err(rtd->card->dev,
2844 "N cpus to M codecs link is not supported yet\n");
2845 return -EINVAL;
2846 }
2847
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002848 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002849 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002850 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002851 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002852 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002853 capture = 1;
2854 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002855 }
Sangsu Parka5002312012-01-02 17:15:10 +09002856
Fabio Estevamd6bead02013-08-29 10:32:13 -03002857 if (rtd->dai_link->playback_only) {
2858 playback = 1;
2859 capture = 0;
2860 }
2861
2862 if (rtd->dai_link->capture_only) {
2863 playback = 0;
2864 capture = 1;
2865 }
2866
Liam Girdwood01d75842012-04-25 12:12:49 +01002867 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002868 if (rtd->dai_link->params) {
2869 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2870 rtd->dai_link->stream_name);
2871
2872 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2873 playback, capture, &pcm);
2874 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002875 snprintf(new_name, sizeof(new_name), "(%s)",
2876 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002877
Liam Girdwood01d75842012-04-25 12:12:49 +01002878 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2879 playback, capture, &pcm);
2880 } else {
2881 if (rtd->dai_link->dynamic)
2882 snprintf(new_name, sizeof(new_name), "%s (*)",
2883 rtd->dai_link->stream_name);
2884 else
2885 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002886 rtd->dai_link->stream_name,
2887 (rtd->num_codecs > 1) ?
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002888 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002889
Liam Girdwood01d75842012-04-25 12:12:49 +01002890 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2891 capture, &pcm);
2892 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002893 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002894 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002895 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002896 return ret;
2897 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002898 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002899
2900 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002901 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002902 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002903 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002904 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002905
Vinod Koul48c76992015-02-12 09:59:53 +05302906 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002907 rtd->pcm = pcm;
2908 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002909
Jerome Bruneta3420312019-07-25 18:59:47 +02002910 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002911 if (playback)
2912 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2913 if (capture)
2914 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2915 goto out;
2916 }
2917
2918 /* ASoC PCM operations */
2919 if (rtd->dai_link->dynamic) {
2920 rtd->ops.open = dpcm_fe_dai_open;
2921 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2922 rtd->ops.prepare = dpcm_fe_dai_prepare;
2923 rtd->ops.trigger = dpcm_fe_dai_trigger;
2924 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2925 rtd->ops.close = dpcm_fe_dai_close;
2926 rtd->ops.pointer = soc_pcm_pointer;
2927 } else {
2928 rtd->ops.open = soc_pcm_open;
2929 rtd->ops.hw_params = soc_pcm_hw_params;
2930 rtd->ops.prepare = soc_pcm_prepare;
2931 rtd->ops.trigger = soc_pcm_trigger;
2932 rtd->ops.hw_free = soc_pcm_hw_free;
2933 rtd->ops.close = soc_pcm_close;
2934 rtd->ops.pointer = soc_pcm_pointer;
2935 }
2936
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002937 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002938 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002939
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002940 if (drv->ioctl)
2941 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002942 if (drv->sync_stop)
2943 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002944 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002945 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002946 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002947 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002948 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002949 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002950 }
2951
Liam Girdwoodddee6272011-06-09 14:45:53 +01002952 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002953 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002954
2955 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002957
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002958 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002959 if (ret < 0) {
2960 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2961 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002962 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002963
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002964 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002965out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002966 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002967 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2968 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002969 return ret;
2970}
Liam Girdwood01d75842012-04-25 12:12:49 +01002971
2972/* is the current PCM operation for this FE ? */
2973int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2974{
2975 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2976 return 1;
2977 return 0;
2978}
2979EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2980
2981/* is the current PCM operation for this BE ? */
2982int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2983 struct snd_soc_pcm_runtime *be, int stream)
2984{
2985 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2986 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2987 be->dpcm[stream].runtime_update))
2988 return 1;
2989 return 0;
2990}
2991EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2992
2993/* get the substream for this BE */
2994struct snd_pcm_substream *
2995 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2996{
2997 return be->pcm->streams[stream].substream;
2998}
2999EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3000
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003001static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3002 struct snd_soc_pcm_runtime *be,
3003 int stream,
3004 const enum snd_soc_dpcm_state *states,
3005 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01003006{
3007 struct snd_soc_dpcm *dpcm;
3008 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003009 int ret = 1;
3010 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003011 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01003012
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003013 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003014 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003015
3016 if (dpcm->fe == fe)
3017 continue;
3018
3019 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003020 for (i = 0; i < num_states; i++) {
3021 if (state == states[i]) {
3022 ret = 0;
3023 break;
3024 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003025 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003026 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003027 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003028
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003029 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003030 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003031}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003032
3033/*
3034 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3035 * are not running, paused or suspended for the specified stream direction.
3036 */
3037int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3038 struct snd_soc_pcm_runtime *be, int stream)
3039{
3040 const enum snd_soc_dpcm_state state[] = {
3041 SND_SOC_DPCM_STATE_START,
3042 SND_SOC_DPCM_STATE_PAUSED,
3043 SND_SOC_DPCM_STATE_SUSPEND,
3044 };
3045
3046 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3047}
Liam Girdwood01d75842012-04-25 12:12:49 +01003048EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3049
3050/*
3051 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3052 * running, paused or suspended for the specified stream direction.
3053 */
3054int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3055 struct snd_soc_pcm_runtime *be, int stream)
3056{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003057 const enum snd_soc_dpcm_state state[] = {
3058 SND_SOC_DPCM_STATE_START,
3059 SND_SOC_DPCM_STATE_PAUSED,
3060 SND_SOC_DPCM_STATE_SUSPEND,
3061 SND_SOC_DPCM_STATE_PREPARE,
3062 };
Liam Girdwood01d75842012-04-25 12:12:49 +01003063
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003064 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01003065}
3066EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);