blob: 7fc7e3e24444883e15cc1f6365470e837dcb09d1 [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 Morimoto9c6d7f92020-12-11 14:55:16 +0900206/* Set FE's runtime_update state; the state is protected via PCM stream lock
207 * for avoiding the race with trigger callback.
208 * If the state is unset and a trigger is pending while the previous operation,
209 * process the pending trigger action here.
210 */
211static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
212static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
213 int stream, enum snd_soc_dpcm_update state)
214{
215 struct snd_pcm_substream *substream =
216 snd_soc_dpcm_get_substream(fe, stream);
217
218 snd_pcm_stream_lock_irq(substream);
219 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
220 dpcm_fe_dai_do_trigger(substream,
221 fe->dpcm[stream].trigger_pending - 1);
222 fe->dpcm[stream].trigger_pending = 0;
223 }
224 fe->dpcm[stream].runtime_update = state;
225 snd_pcm_stream_unlock_irq(substream);
226}
227
Kuninori Morimotoa7e204442020-12-11 14:55:22 +0900228static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
229 int stream, enum snd_soc_dpcm_update state)
230{
231 be->dpcm[stream].runtime_update = state;
232}
233
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900234/**
235 * snd_soc_runtime_action() - Increment/Decrement active count for
236 * PCM runtime components
237 * @rtd: ASoC PCM runtime that is activated
238 * @stream: Direction of the PCM stream
Colton Lewisb6d6e9e2020-06-26 05:40:24 +0000239 * @action: Activate stream if 1. Deactivate if -1.
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900240 *
241 * Increments/Decrements the active count for all the DAIs and components
242 * attached to a PCM runtime.
243 * Should typically be called when a stream is opened.
244 *
245 * Must be called with the rtd->card->pcm_mutex being held
246 */
247void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
248 int stream, int action)
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900249{
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900250 struct snd_soc_dai *dai;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900251 int i;
252
253 lockdep_assert_held(&rtd->card->pcm_mutex);
254
Kuninori Morimotodc829102020-05-15 09:46:27 +0900255 for_each_rtd_dais(rtd, i, dai)
256 snd_soc_dai_action(dai, stream, action);
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900257}
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900258EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100259
260/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100261 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
262 * @rtd: The ASoC PCM runtime that should be checked.
263 *
264 * This function checks whether the power down delay should be ignored for a
265 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
266 * been configured to ignore the delay, or if none of the components benefits
267 * from having the delay.
268 */
269bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
270{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000271 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200272 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900273 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200274
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100275 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
276 return true;
277
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900278 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000279 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000280
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000281 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100282}
283
284/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200285 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
286 * @substream: the pcm substream
287 * @hw: the hardware parameters
288 *
289 * Sets the substream runtime hardware parameters.
290 */
291int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
292 const struct snd_pcm_hardware *hw)
293{
294 struct snd_pcm_runtime *runtime = substream->runtime;
295 runtime->hw.info = hw->info;
296 runtime->hw.formats = hw->formats;
297 runtime->hw.period_bytes_min = hw->period_bytes_min;
298 runtime->hw.period_bytes_max = hw->period_bytes_max;
299 runtime->hw.periods_min = hw->periods_min;
300 runtime->hw.periods_max = hw->periods_max;
301 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
302 runtime->hw.fifo_size = hw->fifo_size;
303 return 0;
304}
305EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
306
Liam Girdwood01d75842012-04-25 12:12:49 +0100307/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000308int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100309 int event)
310{
311 struct snd_soc_dpcm *dpcm;
312
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000313 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100314
315 struct snd_soc_pcm_runtime *be = dpcm->be;
316
Liam Girdwood103d84a2012-11-19 14:39:15 +0000317 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100318 be->dai_link->name, event, dir);
319
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700320 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
321 (be->dpcm[dir].users >= 1))
322 continue;
323
Liam Girdwood01d75842012-04-25 12:12:49 +0100324 snd_soc_dapm_stream_event(be, dir, event);
325 }
326
327 snd_soc_dapm_stream_event(fe, dir, event);
328
329 return 0;
330}
331
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900332static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
333 struct snd_pcm_hw_params *params)
334{
335 if (params) {
336 dai->rate = params_rate(params);
337 dai->channels = params_channels(params);
338 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
339 } else {
340 dai->rate = 0;
341 dai->channels = 0;
342 dai->sample_bits = 0;
343 }
344}
345
Dong Aisheng17841022011-08-29 17:15:14 +0800346static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
347 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100348{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900349 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100350 int ret;
351
Kuninori Morimotofac110c2021-01-15 13:56:35 +0900352#define __soc_pcm_apply_symmetry(name, NAME) \
353 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
354 rtd->dai_link->symmetric_##name)) { \
355 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
356 #name, soc_dai->name); \
357 \
358 ret = snd_pcm_hw_constraint_single(substream->runtime, \
359 SNDRV_PCM_HW_PARAM_##NAME,\
360 soc_dai->name); \
361 if (ret < 0) { \
362 dev_err(soc_dai->dev, \
363 "ASoC: Unable to apply %s constraint: %d\n",\
364 #name, ret); \
365 return ret; \
366 } \
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 }
368
Kuninori Morimotofac110c2021-01-15 13:56:35 +0900369 __soc_pcm_apply_symmetry(rate, RATE);
370 __soc_pcm_apply_symmetry(channels, CHANNELS);
371 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100372
373 return 0;
374}
375
Nicolin Chen3635bf02013-11-13 18:56:24 +0800376static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
377 struct snd_pcm_hw_params *params)
378{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900379 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900380 struct snd_soc_dai d;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900381 struct snd_soc_dai *dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800382 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900383 unsigned int symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800384
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900385 soc_pcm_set_dai_params(&d, params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800386
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900387#define __soc_pcm_params_symmetry(name) \
388 symmetry = rtd->dai_link->symmetric_##name; \
389 for_each_rtd_dais(rtd, i, dai) \
390 symmetry |= dai->driver->symmetric_##name; \
391 \
392 if (symmetry) \
393 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
394 if (cpu_dai->name && cpu_dai->name != d.name) { \
395 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %d - %d\n", \
396 #name, cpu_dai->name, d.name); \
397 return -EINVAL; \
398 }
399
Nicolin Chen3635bf02013-11-13 18:56:24 +0800400 /* reject unmatched parameters when applying symmetry */
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900401 __soc_pcm_params_symmetry(rate);
402 __soc_pcm_params_symmetry(channels);
403 __soc_pcm_params_symmetry(sample_bits);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100404
405 return 0;
406}
407
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100408static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
409{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900410 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100411 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900412 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200413 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100414
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900415 symmetry = link->symmetric_rate ||
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800416 link->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900417 link->symmetric_sample_bits;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800418
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900419 for_each_rtd_dais(rtd, i, dai)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800420 symmetry = symmetry ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900421 dai->driver->symmetric_rate ||
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900422 dai->driver->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900423 dai->driver->symmetric_sample_bits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200424
425 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100426}
427
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200428static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000429{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900430 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwaic6068d32014-12-31 17:10:34 +0100431 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000432
433 if (!bits)
434 return;
435
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100436 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
437 if (ret != 0)
438 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
439 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000440}
441
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200442static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200443{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900444 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800445 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200446 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900447 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
448 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200449 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800450 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200451
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900452 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900453 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
454
455 if (pcm_codec->sig_bits == 0) {
456 bits = 0;
457 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200458 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900459 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200460 }
461
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900462 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800463 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
464
465 if (pcm_cpu->sig_bits == 0) {
466 cpu_bits = 0;
467 break;
468 }
469 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
470 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900471
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200472 soc_pcm_set_msb(substream, bits);
473 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200474}
475
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900476static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
477{
478 hw->rates = UINT_MAX;
479 hw->rate_min = 0;
480 hw->rate_max = UINT_MAX;
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900481 hw->channels_min = 0;
482 hw->channels_max = UINT_MAX;
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900483 hw->formats = ULLONG_MAX;
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900484}
485
486static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
487 struct snd_soc_pcm_stream *p)
488{
489 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
490
491 /* setup hw->rate_min/max via hw->rates first */
492 snd_pcm_hw_limit_rates(hw);
493
494 /* update hw->rate_min/max by snd_soc_pcm_stream */
495 hw->rate_min = max(hw->rate_min, p->rate_min);
496 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
497}
498
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900499static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
500 struct snd_soc_pcm_stream *p)
501{
502 hw->channels_min = max(hw->channels_min, p->channels_min);
503 hw->channels_max = min(hw->channels_max, p->channels_max);
504}
505
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900506static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
507 struct snd_soc_pcm_stream *p)
508{
509 hw->formats &= p->formats;
510}
511
Samuel Holland5854a462020-03-04 23:11:42 -0600512/**
513 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
514 * @rtd: ASoC PCM runtime
515 * @hw: PCM hardware parameters (output)
516 * @stream: Direction of the PCM stream
517 *
518 * Calculates the subset of stream parameters supported by all DAIs
519 * associated with the PCM stream.
520 */
521int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
522 struct snd_pcm_hardware *hw, int stream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200523{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000524 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800525 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200526 struct snd_soc_pcm_stream *codec_stream;
527 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800528 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200529 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100530
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900531 soc_pcm_hw_init(hw);
532
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800533 /* first calculate min/max only for CPUs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900534 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800535
536 /*
537 * Skip CPUs which don't support the current stream type.
538 * Otherwise, since the rate, channel, and format values will
539 * zero in that case, we would have no usable settings left,
540 * causing the resulting setup to fail.
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800541 */
Samuel Holland5854a462020-03-04 23:11:42 -0600542 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800543 continue;
544
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800545 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100546
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900547 soc_pcm_hw_update_chan(hw, cpu_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900548 soc_pcm_hw_update_rate(hw, cpu_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900549 soc_pcm_hw_update_format(hw, cpu_stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800550 }
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900551 cpu_chan_min = hw->channels_min;
552 cpu_chan_max = hw->channels_max;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800553
554 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900555 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200556
557 /*
558 * Skip CODECs which don't support the current stream type.
559 * Otherwise, since the rate, channel, and format values will
560 * zero in that case, we would have no usable settings left,
561 * causing the resulting setup to fail.
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200562 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900563 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200564 continue;
565
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900566 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
567
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900568 soc_pcm_hw_update_chan(hw, codec_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900569 soc_pcm_hw_update_rate(hw, codec_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900570 soc_pcm_hw_update_format(hw, codec_stream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200571 }
572
Samuel Holland5854a462020-03-04 23:11:42 -0600573 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900574 if (!hw->channels_min)
Samuel Holland5854a462020-03-04 23:11:42 -0600575 return -EINVAL;
576
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200577 /*
578 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800579 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200580 * channel allocation be fixed up later
581 */
582 if (rtd->num_codecs > 1) {
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900583 hw->channels_min = cpu_chan_min;
584 hw->channels_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200585 }
586
Samuel Holland5854a462020-03-04 23:11:42 -0600587 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;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900594 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Samuel Holland5854a462020-03-04 23:11:42 -0600595 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{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900610 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900611 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900612 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900613
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900614 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900615 ret = snd_soc_component_module_get_when_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900616 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200617 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900618
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900619 ret = snd_soc_component_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900620 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200621 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900622 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900623
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200624 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900625}
626
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900627static int soc_pcm_components_close(struct snd_pcm_substream *substream,
628 int rollback)
Charles Keepax244e2932018-06-19 16:22:09 +0100629{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900630 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax244e2932018-06-19 16:22:09 +0100631 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900632 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100633
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900634 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900635 r = snd_soc_component_close(component, substream, rollback);
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900636 if (r < 0)
637 ret = r; /* use last ret */
638
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900639 snd_soc_component_module_put_when_close(component, substream, rollback);
Charles Keepax244e2932018-06-19 16:22:09 +0100640 }
641
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900642 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100643}
644
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900645static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900646{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900647 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900648 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900649 struct snd_soc_dai *dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900650 int i;
651
652 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
653
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900654 if (!rollback)
655 snd_soc_runtime_deactivate(rtd, substream->stream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900656
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900657 for_each_rtd_dais(rtd, i, dai)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900658 snd_soc_dai_shutdown(dai, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900659
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900660 snd_soc_link_shutdown(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900661
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900662 soc_pcm_components_close(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900663
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900664
665 mutex_unlock(&rtd->card->pcm_mutex);
666
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900667 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900668
669 for_each_rtd_components(rtd, i, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900670 if (!snd_soc_component_active(component))
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900671 pinctrl_pm_select_sleep_state(component->dev);
672
673 return 0;
674}
675
676/*
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900677 * Called by ALSA when a PCM substream is closed. Private data can be
678 * freed here. The cpu DAI, codec DAI, machine and components are also
679 * shutdown.
680 */
681static int soc_pcm_close(struct snd_pcm_substream *substream)
682{
683 return soc_pcm_clean(substream, 0);
684}
685
686/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100687 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
688 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100689 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690 */
691static int soc_pcm_open(struct snd_pcm_substream *substream)
692{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900693 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100694 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000695 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900696 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200697 const char *codec_dai_name = "multicodec";
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800698 const char *cpu_dai_name = "multicpu";
Charles Keepax244e2932018-06-19 16:22:09 +0100699 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100700
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900701 for_each_rtd_components(rtd, i, component)
702 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000703
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900704 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
705 if (ret < 0)
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900706 goto pm_err;
Mark Brownd6652ef2011-12-03 20:14:31 +0000707
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300708 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100709
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900710 ret = soc_pcm_components_open(substream);
711 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900712 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900713
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900714 ret = snd_soc_link_startup(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900715 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900716 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900717
Liam Girdwoodddee6272011-06-09 14:45:53 +0100718 /* startup the audio subsystem */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900719 for_each_rtd_dais(rtd, i, dai) {
720 ret = snd_soc_dai_startup(dai, substream);
Kuninori Morimotoce820142020-09-28 09:01:29 +0900721 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900722 goto err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200723
724 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900725 dai->tx_mask = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200726 else
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900727 dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100728 }
729
Liam Girdwood01d75842012-04-25 12:12:49 +0100730 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
731 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
732 goto dynamic;
733
Liam Girdwoodddee6272011-06-09 14:45:53 +0100734 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200735 soc_pcm_init_runtime_hw(substream);
736
737 if (rtd->num_codecs == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900738 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800740 if (rtd->num_cpus == 1)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900741 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800742
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100743 if (soc_pcm_has_symmetry(substream))
744 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
745
Liam Girdwoodddee6272011-06-09 14:45:53 +0100746 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100747 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000748 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800749 codec_dai_name, cpu_dai_name);
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900750 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100751 }
752 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000753 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800754 codec_dai_name, cpu_dai_name);
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900755 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756 }
757 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
758 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000759 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800760 codec_dai_name, cpu_dai_name);
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900761 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100762 }
763
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200764 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000765
Liam Girdwoodddee6272011-06-09 14:45:53 +0100766 /* Symmetry only applies if we've already got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900767 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900768 if (snd_soc_dai_active(dai)) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900769 ret = soc_pcm_apply_symmetry(substream, dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200770 if (ret != 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900771 goto err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200772 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773 }
774
Liam Girdwood103d84a2012-11-19 14:39:15 +0000775 pr_debug("ASoC: %s <-> %s info:\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800776 codec_dai_name, cpu_dai_name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000777 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
778 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100779 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000780 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781 runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +0100782dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100783 snd_soc_runtime_activate(rtd, substream->stream);
Kuninori Morimoto8e7875a2020-10-01 14:07:41 +0900784 ret = 0;
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900785err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300786 mutex_unlock(&rtd->card->pcm_mutex);
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900787pm_err:
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900788 if (ret < 0)
789 soc_pcm_clean(substream, 1);
Mark Brownd6652ef2011-12-03 20:14:31 +0000790
Liam Girdwoodddee6272011-06-09 14:45:53 +0100791 return ret;
792}
793
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800794static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200795{
796 /*
797 * Currently nothing to do for c2c links
798 * Since c2c links are internal nodes in the DAPM graph and
799 * don't interface with the outside world or application layer
800 * we don't have to do any special handling on close.
801 */
802}
803
Liam Girdwoodddee6272011-06-09 14:45:53 +0100804/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100805 * Called by ALSA when the PCM substream is prepared, can set format, sample
806 * rate, etc. This function is non atomic and can be called multiple times,
807 * it can refer to the runtime info.
808 */
809static int soc_pcm_prepare(struct snd_pcm_substream *substream)
810{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900811 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900812 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200813 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100814
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300815 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100816
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900817 ret = snd_soc_link_prepare(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900818 if (ret < 0)
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900819 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100820
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900821 ret = snd_soc_pcm_component_prepare(substream);
822 if (ret < 0)
823 goto out;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000824
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900825 ret = snd_soc_pcm_dai_prepare(substream);
826 if (ret < 0) {
827 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
828 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100829 }
830
831 /* cancel any delayed stream shutdown that is pending */
832 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600833 rtd->pop_wait) {
834 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100835 cancel_delayed_work(&rtd->delayed_work);
836 }
837
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000838 snd_soc_dapm_stream_event(rtd, substream->stream,
839 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100840
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900841 for_each_rtd_dais(rtd, i, dai)
842 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100843
844out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300845 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100846 return ret;
847}
848
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200849static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
850 unsigned int mask)
851{
852 struct snd_interval *interval;
853 int channels = hweight_long(mask);
854
855 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
856 interval->min = channels;
857 interval->max = channels;
858}
859
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900860static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimotoab494362020-09-29 13:31:19 +0900861{
862 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
863 struct snd_soc_dai *dai;
864 int i;
865
866 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
867
868 /* clear the corresponding DAIs parameters when going to be inactive */
869 for_each_rtd_dais(rtd, i, dai) {
870 int active = snd_soc_dai_stream_active(dai, substream->stream);
871
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900872 if (snd_soc_dai_active(dai) == 1)
873 soc_pcm_set_dai_params(dai, NULL);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900874
875 if (active == 1)
876 snd_soc_dai_digital_mute(dai, 1, substream->stream);
877 }
878
Ranjani Sridharana27b4212020-11-17 13:50:01 -0800879 /* run the stream event */
880 snd_soc_dapm_stream_stop(rtd, substream->stream);
881
Kuninori Morimotoab494362020-09-29 13:31:19 +0900882 /* free any machine hw params */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900883 snd_soc_link_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900884
885 /* free any component resources */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900886 snd_soc_pcm_component_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900887
888 /* now free hw params for the DAIs */
889 for_each_rtd_dais(rtd, i, dai) {
890 if (!snd_soc_dai_stream_valid(dai, substream->stream))
891 continue;
892
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900893 snd_soc_dai_hw_free(dai, substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900894 }
895
896 mutex_unlock(&rtd->card->pcm_mutex);
897 return 0;
898}
899
900/*
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900901 * Frees resources allocated by hw_params, can be called multiple times
902 */
903static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
904{
905 return soc_pcm_hw_clean(substream, 0);
906}
907
908/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100909 * Called by ALSA when the hardware params are set by application. This
910 * function can also be called multiple times and can allocate buffers
911 * (using snd_pcm_lib_* ). It's non-atomic.
912 */
913static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
914 struct snd_pcm_hw_params *params)
915{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900916 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800917 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000918 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100919 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100920
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300921 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800922
923 ret = soc_pcm_params_symmetry(substream, params);
924 if (ret)
925 goto out;
926
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900927 ret = snd_soc_link_hw_params(substream, params);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900928 if (ret < 0)
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900929 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100930
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900931 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200932 struct snd_pcm_hw_params codec_params;
933
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200934 /*
935 * Skip CODECs which don't support the current stream type,
936 * the idea being that if a CODEC is not used for the currently
937 * set up transfer direction, it should not need to be
938 * configured, especially since the configuration used might
939 * not even be supported by that CODEC. There may be cases
940 * however where a CODEC needs to be set up although it is
941 * actually not being used for the transfer, e.g. if a
942 * capture-only CODEC is acting as an LRCLK and/or BCLK master
943 * for the DAI link including a playback-only CODEC.
944 * If this becomes necessary, we will have to augment the
945 * machine driver setup with information on how to act, so
946 * we can do the right thing here.
947 */
948 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
949 continue;
950
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200951 /* copy params for each codec */
952 codec_params = *params;
953
954 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800955 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
956 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200957 soc_pcm_codec_params_fixup(&codec_params,
958 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800959
960 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
961 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200962 soc_pcm_codec_params_fixup(&codec_params,
963 codec_dai->rx_mask);
964
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900965 ret = snd_soc_dai_hw_params(codec_dai, substream,
966 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200967 if(ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900968 goto out;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200969
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900970 soc_pcm_set_dai_params(codec_dai, &codec_params);
Charles Keepax078a85f2019-01-31 13:30:18 +0000971 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100972 }
973
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900974 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800975 /*
976 * Skip CPUs which don't support the current stream
977 * type. See soc_pcm_init_runtime_hw() for more details
978 */
979 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
980 continue;
981
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800982 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
983 if (ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900984 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100985
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800986 /* store the parameters for each DAI */
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900987 soc_pcm_set_dai_params(cpu_dai, params);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800988 snd_soc_dapm_update_dai(substream, params, cpu_dai);
989 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900990
Kuninori Morimoto3a36a642020-09-29 13:31:41 +0900991 ret = snd_soc_pcm_component_hw_params(substream, params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100992out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300993 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100994
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900995 if (ret < 0)
996 soc_pcm_hw_clean(substream, 1);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000997
Liam Girdwoodddee6272011-06-09 14:45:53 +0100998 return ret;
999}
1000
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001001static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1002{
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001003 int ret = -EINVAL, _ret = 0;
1004 int rollback = 0;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001005
1006 switch (cmd) {
1007 case SNDRV_PCM_TRIGGER_START:
1008 case SNDRV_PCM_TRIGGER_RESUME:
1009 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001010 ret = snd_soc_link_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001011 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001012 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001013
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001014 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001015 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001016 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001017
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001018 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1019start_err:
1020 if (ret < 0)
1021 rollback = 1;
1022 }
1023
1024 if (rollback) {
1025 _ret = ret;
1026 switch (cmd) {
1027 case SNDRV_PCM_TRIGGER_START:
1028 cmd = SNDRV_PCM_TRIGGER_STOP;
1029 break;
1030 case SNDRV_PCM_TRIGGER_RESUME:
1031 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1032 break;
1033 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1034 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1035 break;
1036 }
1037 }
1038
1039 switch (cmd) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001040 case SNDRV_PCM_TRIGGER_STOP:
1041 case SNDRV_PCM_TRIGGER_SUSPEND:
1042 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001043 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001044 if (ret < 0)
1045 break;
1046
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001047 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001048 if (ret < 0)
1049 break;
1050
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001051 ret = snd_soc_link_trigger(substream, cmd, rollback);
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001052 break;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001053 }
1054
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001055 if (_ret)
1056 ret = _ret;
1057
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001058 return ret;
1059}
1060
Liam Girdwoodddee6272011-06-09 14:45:53 +01001061/*
1062 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001063 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001064 * the runtime->delay will be updated accordingly.
1065 */
1066static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1067{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001068 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001069 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001070 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001071 struct snd_pcm_runtime *runtime = substream->runtime;
1072 snd_pcm_uframes_t offset = 0;
1073 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001074 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001075 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001076 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001077
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301078 /* clearing the previous total delay */
1079 runtime->delay = 0;
1080
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001081 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001082
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301083 /* base delay if assigned in pointer callback */
1084 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001085
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001086 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001087 cpu_delay = max(cpu_delay,
1088 snd_soc_dai_delay(cpu_dai, substream));
1089 }
1090 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001091
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001092 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001093 codec_delay = max(codec_delay,
1094 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001095 }
1096 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001097
Liam Girdwoodddee6272011-06-09 14:45:53 +01001098 runtime->delay = delay;
1099
1100 return offset;
1101}
1102
Liam Girdwood01d75842012-04-25 12:12:49 +01001103/* connect a FE and BE */
1104static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1105 struct snd_soc_pcm_runtime *be, int stream)
1106{
1107 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001108 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001109
1110 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001111 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001112 if (dpcm->be == be && dpcm->fe == fe)
1113 return 0;
1114 }
1115
1116 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1117 if (!dpcm)
1118 return -ENOMEM;
1119
1120 dpcm->be = be;
1121 dpcm->fe = fe;
1122 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1123 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001124 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001125 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1126 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001127 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001128
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001129 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001130 stream ? "capture" : "playback", fe->dai_link->name,
1131 stream ? "<-" : "->", be->dai_link->name);
1132
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001133 dpcm_create_debugfs_state(dpcm, stream);
1134
Liam Girdwood01d75842012-04-25 12:12:49 +01001135 return 1;
1136}
1137
1138/* reparent a BE onto another FE */
1139static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1140 struct snd_soc_pcm_runtime *be, int stream)
1141{
1142 struct snd_soc_dpcm *dpcm;
1143 struct snd_pcm_substream *fe_substream, *be_substream;
1144
1145 /* reparent if BE is connected to other FEs */
1146 if (!be->dpcm[stream].users)
1147 return;
1148
1149 be_substream = snd_soc_dpcm_get_substream(be, stream);
1150
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001151 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001152 if (dpcm->fe == fe)
1153 continue;
1154
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001155 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001156 stream ? "capture" : "playback",
1157 dpcm->fe->dai_link->name,
1158 stream ? "<-" : "->", dpcm->be->dai_link->name);
1159
1160 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1161 be_substream->runtime = fe_substream->runtime;
1162 break;
1163 }
1164}
1165
1166/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001167void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001168{
1169 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001170 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001171
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001172 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001173 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001174 stream ? "capture" : "playback",
1175 dpcm->be->dai_link->name);
1176
1177 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1178 continue;
1179
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001180 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001181 stream ? "capture" : "playback", fe->dai_link->name,
1182 stream ? "<-" : "->", dpcm->be->dai_link->name);
1183
1184 /* BEs still alive need new FE */
1185 dpcm_be_reparent(fe, dpcm->be, stream);
1186
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001187 dpcm_remove_debugfs_state(dpcm);
1188
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001189 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001190 list_del(&dpcm->list_be);
1191 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001192 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001193 kfree(dpcm);
1194 }
1195}
1196
1197/* get BE for DAI widget and stream */
1198static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1199 struct snd_soc_dapm_widget *widget, int stream)
1200{
1201 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001202 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001203 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001204 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001205
Liam Girdwood3c146462018-03-14 20:43:51 +00001206 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1207
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001208 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001209
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001210 if (!be->dai_link->no_pcm)
1211 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001212
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001213 for_each_rtd_dais(be, i, dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001214 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001215
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001216 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1217 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001218
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001219 if (w == widget)
1220 return be;
1221 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001222 }
1223
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001224 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001225 return NULL;
1226}
1227
Liam Girdwood01d75842012-04-25 12:12:49 +01001228static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1229 struct snd_soc_dapm_widget *widget)
1230{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001231 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001232 int i;
1233
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001234 for_each_dapm_widgets(list, i, w)
1235 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001236 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001237
1238 return 0;
1239}
1240
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001241static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1242 enum snd_soc_dapm_direction dir)
1243{
1244 struct snd_soc_card *card = widget->dapm->card;
1245 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001246 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001247
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001248 /* adjust dir to stream */
1249 if (dir == SND_SOC_DAPM_DIR_OUT)
1250 stream = SNDRV_PCM_STREAM_PLAYBACK;
1251 else
1252 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001253
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001254 rtd = dpcm_get_be(card, widget, stream);
1255 if (rtd)
1256 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001257
1258 return false;
1259}
1260
Liam Girdwood23607022014-01-17 17:03:55 +00001261int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001262 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001263{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001264 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood01d75842012-04-25 12:12:49 +01001265 int paths;
1266
Bard Liao6e1276a2020-02-25 21:39:16 +08001267 if (fe->num_cpus > 1) {
1268 dev_err(fe->dev,
1269 "%s doesn't support Multi CPU yet\n", __func__);
1270 return -EINVAL;
1271 }
1272
Liam Girdwood01d75842012-04-25 12:12:49 +01001273 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001274 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Sameer Pujaraa293772020-11-02 20:40:09 +05301275 fe->card->component_chaining ?
1276 NULL : dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001277
Liam Girdwood103d84a2012-11-19 14:39:15 +00001278 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001279 stream ? "capture" : "playback");
1280
Liam Girdwood01d75842012-04-25 12:12:49 +01001281 return paths;
1282}
1283
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001284void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1285{
1286 snd_soc_dapm_dai_free_widgets(list);
1287}
1288
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001289static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1290 struct snd_soc_dapm_widget_list *list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001291{
Liam Girdwood01d75842012-04-25 12:12:49 +01001292 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001293 struct snd_soc_dai *dai;
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001294 unsigned int i;
1295
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001296 /* is there a valid DAI widget for this BE */
1297 for_each_rtd_dais(dpcm->be, i, dai) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001298 widget = snd_soc_dai_get_widget(dai, stream);
1299
1300 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001301 * The BE is pruned only if none of the dai
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001302 * widgets are in the active list.
1303 */
1304 if (widget && widget_in_list(list, widget))
1305 return true;
1306 }
1307
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001308 return false;
1309}
1310
1311static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1312 struct snd_soc_dapm_widget_list **list_)
1313{
1314 struct snd_soc_dpcm *dpcm;
Liam Girdwood01d75842012-04-25 12:12:49 +01001315 int prune = 0;
1316
1317 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001318 for_each_dpcm_be(fe, stream, dpcm) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001319 if (dpcm_be_is_active(dpcm, stream, *list_))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001320 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001321
Liam Girdwood103d84a2012-11-19 14:39:15 +00001322 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001323 stream ? "capture" : "playback",
1324 dpcm->be->dai_link->name, fe->dai_link->name);
1325 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001326 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001327 prune++;
1328 }
1329
Liam Girdwood103d84a2012-11-19 14:39:15 +00001330 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001331 return prune;
1332}
1333
1334static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1335 struct snd_soc_dapm_widget_list **list_)
1336{
1337 struct snd_soc_card *card = fe->card;
1338 struct snd_soc_dapm_widget_list *list = *list_;
1339 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001340 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001341 int i, new = 0, err;
1342
1343 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001344 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001345
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001346 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001347 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001348 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1349 continue;
1350 break;
Mark Brown46162742013-06-05 19:36:11 +01001351 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001352 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1353 continue;
Mark Brown46162742013-06-05 19:36:11 +01001354 break;
1355 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001356 continue;
Mark Brown46162742013-06-05 19:36:11 +01001357 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001358
1359 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001360 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001361 if (!be) {
Shengjiu Wangb6eabd22021-02-08 16:12:45 +08001362 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1363 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001364 continue;
1365 }
1366
Liam Girdwood01d75842012-04-25 12:12:49 +01001367 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001368 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001369 continue;
1370
1371 /* newly connected FE and BE */
1372 err = dpcm_be_connect(fe, be, stream);
1373 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001374 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001375 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001376 break;
1377 } else if (err == 0) /* already connected */
1378 continue;
1379
1380 /* new */
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001381 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001382 new++;
1383 }
1384
Liam Girdwood103d84a2012-11-19 14:39:15 +00001385 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 return new;
1387}
1388
1389/*
1390 * Find the corresponding BE DAIs that source or sink audio to this
1391 * FE substream.
1392 */
Liam Girdwood23607022014-01-17 17:03:55 +00001393int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001394 int stream, struct snd_soc_dapm_widget_list **list, int new)
1395{
1396 if (new)
1397 return dpcm_add_paths(fe, stream, list);
1398 else
1399 return dpcm_prune_paths(fe, stream, list);
1400}
1401
Liam Girdwood23607022014-01-17 17:03:55 +00001402void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001403{
1404 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001405 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001406
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001407 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001408 for_each_dpcm_be(fe, stream, dpcm)
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001409 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001410 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001411}
1412
1413static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1414 int stream)
1415{
1416 struct snd_soc_dpcm *dpcm;
1417
1418 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001419 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001420
1421 struct snd_soc_pcm_runtime *be = dpcm->be;
1422 struct snd_pcm_substream *be_substream =
1423 snd_soc_dpcm_get_substream(be, stream);
1424
1425 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001426 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001427 stream ? "capture" : "playback",
1428 be->dpcm[stream].state);
1429
1430 if (--be->dpcm[stream].users != 0)
1431 continue;
1432
1433 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1434 continue;
1435
1436 soc_pcm_close(be_substream);
1437 be_substream->runtime = NULL;
1438 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1439 }
1440}
1441
Liam Girdwood23607022014-01-17 17:03:55 +00001442int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001443{
1444 struct snd_soc_dpcm *dpcm;
1445 int err, count = 0;
1446
1447 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001448 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001449
1450 struct snd_soc_pcm_runtime *be = dpcm->be;
1451 struct snd_pcm_substream *be_substream =
1452 snd_soc_dpcm_get_substream(be, stream);
1453
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001454 if (!be_substream) {
1455 dev_err(be->dev, "ASoC: no backend %s stream\n",
1456 stream ? "capture" : "playback");
1457 continue;
1458 }
1459
Liam Girdwood01d75842012-04-25 12:12:49 +01001460 /* is this op for this BE ? */
1461 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1462 continue;
1463
1464 /* first time the dpcm is open ? */
1465 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001466 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001467 stream ? "capture" : "playback",
1468 be->dpcm[stream].state);
1469
1470 if (be->dpcm[stream].users++ != 0)
1471 continue;
1472
1473 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1474 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1475 continue;
1476
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001477 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1478 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001479
1480 be_substream->runtime = be->dpcm[stream].runtime;
1481 err = soc_pcm_open(be_substream);
1482 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001483 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001484 be->dpcm[stream].users--;
1485 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001486 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001487 stream ? "capture" : "playback",
1488 be->dpcm[stream].state);
1489
1490 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1491 goto unwind;
1492 }
1493
1494 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1495 count++;
1496 }
1497
1498 return count;
1499
1500unwind:
1501 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001502 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001503 struct snd_soc_pcm_runtime *be = dpcm->be;
1504 struct snd_pcm_substream *be_substream =
1505 snd_soc_dpcm_get_substream(be, stream);
1506
1507 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1508 continue;
1509
1510 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001511 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001512 stream ? "capture" : "playback",
1513 be->dpcm[stream].state);
1514
1515 if (--be->dpcm[stream].users != 0)
1516 continue;
1517
1518 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1519 continue;
1520
1521 soc_pcm_close(be_substream);
1522 be_substream->runtime = NULL;
1523 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1524 }
1525
1526 return err;
1527}
1528
Kuninori Morimoto5f538982021-02-22 09:47:26 +09001529static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1530{
1531 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1532 struct snd_pcm_runtime *runtime = substream->runtime;
1533 struct snd_pcm_hardware *hw = &runtime->hw;
1534 struct snd_soc_dai *dai;
1535 int stream = substream->stream;
1536 int i;
1537
1538 soc_pcm_hw_init(hw);
1539
1540 for_each_rtd_cpu_dais(fe, i, dai) {
1541 struct snd_soc_pcm_stream *cpu_stream;
1542
1543 /*
1544 * Skip CPUs which don't support the current stream
1545 * type. See soc_pcm_init_runtime_hw() for more details
1546 */
1547 if (!snd_soc_dai_stream_valid(dai, stream))
1548 continue;
1549
1550 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1551
1552 soc_pcm_hw_update_rate(hw, cpu_stream);
1553 soc_pcm_hw_update_chan(hw, cpu_stream);
1554 soc_pcm_hw_update_format(hw, cpu_stream);
1555 }
1556
1557}
1558
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001559static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001560{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001561 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001562 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001563 struct snd_pcm_hardware *hw = &runtime->hw;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001564 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001565 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001566 int stream = substream->stream;
1567
1568 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001569 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001570
1571 /*
1572 * It returns merged BE codec format
1573 * if FE want to use it (= dpcm_merged_format)
1574 */
1575
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001576 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001577 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001578 struct snd_soc_pcm_stream *codec_stream;
1579 int i;
1580
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001581 for_each_rtd_codec_dais(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001582 /*
1583 * Skip CODECs which don't support the current stream
1584 * type. See soc_pcm_init_runtime_hw() for more details
1585 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001586 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001587 continue;
1588
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001589 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001590
Kuninori Morimotodebc71f2021-02-04 08:52:04 +09001591 soc_pcm_hw_update_format(hw, codec_stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001592 }
1593 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001594}
1595
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001596static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001597{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001598 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001599 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001600 struct snd_pcm_hardware *hw = &runtime->hw;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001601 struct snd_soc_dpcm *dpcm;
1602 int stream = substream->stream;
1603
1604 if (!fe->dai_link->dpcm_merged_chan)
1605 return;
1606
Jiada Wangf4c277b2018-06-20 18:25:20 +09001607 /*
1608 * It returns merged BE codec channel;
1609 * if FE want to use it (= dpcm_merged_chan)
1610 */
1611
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001612 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001613 struct snd_soc_pcm_runtime *be = dpcm->be;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001614 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001615 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001616 struct snd_soc_dai *dai;
1617 int i;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001618
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001619 for_each_rtd_cpu_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001620 /*
1621 * Skip CPUs which don't support the current stream
1622 * type. See soc_pcm_init_runtime_hw() for more details
1623 */
1624 if (!snd_soc_dai_stream_valid(dai, stream))
1625 continue;
1626
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001627 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001628
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +09001629 soc_pcm_hw_update_chan(hw, cpu_stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001630 }
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001631
1632 /*
1633 * chan min/max cannot be enforced if there are multiple CODEC
1634 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1635 */
1636 if (be->num_codecs == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001637 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001638
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +09001639 soc_pcm_hw_update_chan(hw, codec_stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001640 }
1641 }
1642}
1643
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001644static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001645{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001646 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001647 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001648 struct snd_pcm_hardware *hw = &runtime->hw;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001649 struct snd_soc_dpcm *dpcm;
1650 int stream = substream->stream;
1651
1652 if (!fe->dai_link->dpcm_merged_rate)
1653 return;
1654
1655 /*
1656 * It returns merged BE codec channel;
1657 * if FE want to use it (= dpcm_merged_chan)
1658 */
1659
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001660 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001661 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001662 struct snd_soc_pcm_stream *pcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001663 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001664 int i;
1665
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001666 for_each_rtd_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001667 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001668 * Skip DAIs which don't support the current stream
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001669 * type. See soc_pcm_init_runtime_hw() for more details
1670 */
1671 if (!snd_soc_dai_stream_valid(dai, stream))
1672 continue;
1673
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001674 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001675
Kuninori Morimotof6c04af2021-02-04 08:50:31 +09001676 soc_pcm_hw_update_rate(hw, pcm);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001677 }
1678 }
1679}
1680
Mark Brown45c0a182012-05-09 21:46:27 +01001681static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001682{
Kuninori Morimoto5f538982021-02-22 09:47:26 +09001683 dpcm_runtime_setup_fe(substream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001684
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001685 dpcm_runtime_setup_be_format(substream);
1686 dpcm_runtime_setup_be_chan(substream);
1687 dpcm_runtime_setup_be_rate(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001688}
1689
PC Liao906c7d62015-12-11 11:33:51 +08001690static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1691 int stream)
1692{
1693 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001694 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001695 struct snd_soc_dai *fe_cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001696 int err;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001697 int i;
PC Liao906c7d62015-12-11 11:33:51 +08001698
1699 /* apply symmetry for FE */
1700 if (soc_pcm_has_symmetry(fe_substream))
1701 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1702
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001703 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001704 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001705 if (snd_soc_dai_active(fe_cpu_dai)) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001706 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1707 if (err < 0)
1708 return err;
1709 }
PC Liao906c7d62015-12-11 11:33:51 +08001710 }
1711
1712 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001713 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001714 struct snd_soc_pcm_runtime *be = dpcm->be;
1715 struct snd_pcm_substream *be_substream =
1716 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001717 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001718 struct snd_soc_dai *dai;
PC Liao906c7d62015-12-11 11:33:51 +08001719 int i;
1720
Jerome Brunet6246f282019-04-01 15:03:54 +02001721 /* A backend may not have the requested substream */
1722 if (!be_substream)
1723 continue;
1724
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001725 rtd = asoc_substream_to_rtd(be_substream);
Jeeja KPf1176612016-09-06 14:17:55 +05301726 if (rtd->dai_link->be_hw_params_fixup)
1727 continue;
1728
PC Liao906c7d62015-12-11 11:33:51 +08001729 if (soc_pcm_has_symmetry(be_substream))
1730 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1731
1732 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001733 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotob3dea622020-05-15 09:46:51 +09001734 if (snd_soc_dai_active(dai)) {
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001735 err = soc_pcm_apply_symmetry(fe_substream, dai);
PC Liao906c7d62015-12-11 11:33:51 +08001736 if (err < 0)
1737 return err;
1738 }
1739 }
1740 }
1741
1742 return 0;
1743}
1744
Liam Girdwood01d75842012-04-25 12:12:49 +01001745static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1746{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001747 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001748 int stream = fe_substream->stream, ret = 0;
1749
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001750 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001751
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001752 ret = dpcm_be_dai_startup(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001753 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001754 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001755 goto be_err;
1756 }
1757
Liam Girdwood103d84a2012-11-19 14:39:15 +00001758 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001759
1760 /* start the DAI frontend */
1761 ret = soc_pcm_open(fe_substream);
1762 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001763 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001764 goto unwind;
1765 }
1766
1767 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1768
1769 dpcm_set_fe_runtime(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001770
PC Liao906c7d62015-12-11 11:33:51 +08001771 ret = dpcm_apply_symmetry(fe_substream, stream);
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09001772 if (ret < 0)
PC Liao906c7d62015-12-11 11:33:51 +08001773 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1774 ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001775
1776unwind:
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09001777 if (ret < 0)
1778 dpcm_be_dai_startup_unwind(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001779be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001780 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001781 return ret;
1782}
1783
Liam Girdwood23607022014-01-17 17:03:55 +00001784int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001785{
1786 struct snd_soc_dpcm *dpcm;
1787
1788 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001789 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001790
1791 struct snd_soc_pcm_runtime *be = dpcm->be;
1792 struct snd_pcm_substream *be_substream =
1793 snd_soc_dpcm_get_substream(be, stream);
1794
1795 /* is this op for this BE ? */
1796 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1797 continue;
1798
1799 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001800 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001801 stream ? "capture" : "playback",
1802 be->dpcm[stream].state);
1803
1804 if (--be->dpcm[stream].users != 0)
1805 continue;
1806
1807 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001808 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1809 soc_pcm_hw_free(be_substream);
1810 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1811 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001812
Liam Girdwood103d84a2012-11-19 14:39:15 +00001813 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001814 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001815
1816 soc_pcm_close(be_substream);
1817 be_substream->runtime = NULL;
1818
1819 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1820 }
1821 return 0;
1822}
1823
1824static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1825{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001826 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001827 int stream = substream->stream;
1828
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001829 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001830
1831 /* shutdown the BEs */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001832 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001833
Liam Girdwood103d84a2012-11-19 14:39:15 +00001834 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001835
1836 /* now shutdown the frontend */
1837 soc_pcm_close(substream);
1838
Ranjani Sridharanbb9dd3c2020-12-02 11:33:43 -08001839 /* run the stream stop event */
1840 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1841
Liam Girdwood01d75842012-04-25 12:12:49 +01001842 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001843 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001844 return 0;
1845}
1846
Liam Girdwood23607022014-01-17 17:03:55 +00001847int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001848{
1849 struct snd_soc_dpcm *dpcm;
1850
1851 /* only hw_params backends that are either sinks or sources
1852 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001853 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001854
1855 struct snd_soc_pcm_runtime *be = dpcm->be;
1856 struct snd_pcm_substream *be_substream =
1857 snd_soc_dpcm_get_substream(be, stream);
1858
1859 /* is this op for this BE ? */
1860 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1861 continue;
1862
1863 /* only free hw when no longer used - check all FEs */
1864 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1865 continue;
1866
Qiao Zhou36fba622014-12-03 10:13:43 +08001867 /* do not free hw if this BE is used by other FE */
1868 if (be->dpcm[stream].users > 1)
1869 continue;
1870
Liam Girdwood01d75842012-04-25 12:12:49 +01001871 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1872 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1873 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001874 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301875 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1876 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001877 continue;
1878
Liam Girdwood103d84a2012-11-19 14:39:15 +00001879 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001880 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001881
1882 soc_pcm_hw_free(be_substream);
1883
1884 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1885 }
1886
1887 return 0;
1888}
1889
Mark Brown45c0a182012-05-09 21:46:27 +01001890static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001891{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001892 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001893 int err, stream = substream->stream;
1894
1895 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001896 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001897
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001899
1900 /* call hw_free on the frontend */
1901 err = soc_pcm_hw_free(substream);
1902 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001903 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001904 fe->dai_link->name);
1905
1906 /* only hw_params backends that are either sinks or sources
1907 * to this frontend DAI */
1908 err = dpcm_be_dai_hw_free(fe, stream);
1909
1910 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001911 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001912
1913 mutex_unlock(&fe->card->mutex);
1914 return 0;
1915}
1916
Liam Girdwood23607022014-01-17 17:03:55 +00001917int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001918{
1919 struct snd_soc_dpcm *dpcm;
1920 int ret;
1921
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001922 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001923
1924 struct snd_soc_pcm_runtime *be = dpcm->be;
1925 struct snd_pcm_substream *be_substream =
1926 snd_soc_dpcm_get_substream(be, stream);
1927
1928 /* is this op for this BE ? */
1929 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1930 continue;
1931
Liam Girdwood01d75842012-04-25 12:12:49 +01001932 /* copy params for each dpcm */
1933 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1934 sizeof(struct snd_pcm_hw_params));
1935
1936 /* perform any hw_params fixups */
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +09001937 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1938 if (ret < 0)
1939 goto unwind;
Liam Girdwood01d75842012-04-25 12:12:49 +01001940
Libin Yangae061d22019-04-19 09:53:12 +08001941 /* copy the fixed-up hw params for BE dai */
1942 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1943 sizeof(struct snd_pcm_hw_params));
1944
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001945 /* only allow hw_params() if no connected FEs are running */
1946 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1947 continue;
1948
1949 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1950 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1951 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1952 continue;
1953
1954 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001955 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001956
Liam Girdwood01d75842012-04-25 12:12:49 +01001957 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1958 if (ret < 0) {
1959 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001960 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001961 goto unwind;
1962 }
1963
1964 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1965 }
1966 return 0;
1967
1968unwind:
1969 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001970 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001971 struct snd_soc_pcm_runtime *be = dpcm->be;
1972 struct snd_pcm_substream *be_substream =
1973 snd_soc_dpcm_get_substream(be, stream);
1974
1975 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1976 continue;
1977
1978 /* only allow hw_free() if no connected FEs are running */
1979 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1980 continue;
1981
1982 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1983 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1984 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1985 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1986 continue;
1987
1988 soc_pcm_hw_free(be_substream);
1989 }
1990
1991 return ret;
1992}
1993
Mark Brown45c0a182012-05-09 21:46:27 +01001994static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1995 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001996{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001997 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001998 int ret, stream = substream->stream;
1999
2000 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002001 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002003 memcpy(&fe->dpcm[stream].hw_params, params,
Liam Girdwood01d75842012-04-25 12:12:49 +01002004 sizeof(struct snd_pcm_hw_params));
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002005 ret = dpcm_be_dai_hw_params(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002006 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002007 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002008 goto out;
2009 }
2010
Liam Girdwood103d84a2012-11-19 14:39:15 +00002011 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002012 fe->dai_link->name, params_rate(params),
2013 params_channels(params), params_format(params));
2014
2015 /* call hw_params on the frontend */
2016 ret = soc_pcm_hw_params(substream, params);
2017 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002018 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002019 dpcm_be_dai_hw_free(fe, stream);
2020 } else
2021 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2022
2023out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002024 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002025 mutex_unlock(&fe->card->mutex);
2026 return ret;
2027}
2028
Liam Girdwood23607022014-01-17 17:03:55 +00002029int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002030 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002031{
2032 struct snd_soc_dpcm *dpcm;
2033 int ret = 0;
2034
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002035 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002036
2037 struct snd_soc_pcm_runtime *be = dpcm->be;
2038 struct snd_pcm_substream *be_substream =
2039 snd_soc_dpcm_get_substream(be, stream);
2040
2041 /* is this op for this BE ? */
2042 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2043 continue;
2044
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002045 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2046 be->dai_link->name, cmd);
2047
Liam Girdwood01d75842012-04-25 12:12:49 +01002048 switch (cmd) {
2049 case SNDRV_PCM_TRIGGER_START:
2050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
이경택21fca8b2020-04-01 10:04:21 +09002051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2052 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002053 continue;
2054
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002055 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002056 if (ret)
2057 return ret;
2058
2059 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2060 break;
2061 case SNDRV_PCM_TRIGGER_RESUME:
2062 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2063 continue;
2064
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002065 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002066 if (ret)
2067 return ret;
2068
2069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2070 break;
2071 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2072 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2073 continue;
2074
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002075 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002076 if (ret)
2077 return ret;
2078
2079 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2080 break;
2081 case SNDRV_PCM_TRIGGER_STOP:
이경택21fca8b2020-04-01 10:04:21 +09002082 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2083 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002084 continue;
2085
2086 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2087 continue;
2088
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002089 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002090 if (ret)
2091 return ret;
2092
2093 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2094 break;
2095 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002096 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002097 continue;
2098
2099 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2100 continue;
2101
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002102 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002103 if (ret)
2104 return ret;
2105
2106 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2107 break;
2108 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2109 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2110 continue;
2111
2112 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2113 continue;
2114
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002115 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002116 if (ret)
2117 return ret;
2118
2119 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2120 break;
2121 }
2122 }
2123
2124 return ret;
2125}
2126EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2127
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002128static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2129 int cmd, bool fe_first)
2130{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002131 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002132 int ret;
2133
2134 /* call trigger on the frontend before the backend. */
2135 if (fe_first) {
2136 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2137 fe->dai_link->name, cmd);
2138
2139 ret = soc_pcm_trigger(substream, cmd);
2140 if (ret < 0)
2141 return ret;
2142
2143 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2144 return ret;
2145 }
2146
2147 /* call trigger on the frontend after the backend. */
2148 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2149 if (ret < 0)
2150 return ret;
2151
2152 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2153 fe->dai_link->name, cmd);
2154
2155 ret = soc_pcm_trigger(substream, cmd);
2156
2157 return ret;
2158}
2159
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002160static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002161{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002162 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002163 int stream = substream->stream;
2164 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002165 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2166
2167 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2168
2169 switch (trigger) {
2170 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002171 switch (cmd) {
2172 case SNDRV_PCM_TRIGGER_START:
2173 case SNDRV_PCM_TRIGGER_RESUME:
2174 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Cezary Rojewski4c22b802020-10-26 11:01:29 +01002175 case SNDRV_PCM_TRIGGER_DRAIN:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002176 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2177 break;
2178 case SNDRV_PCM_TRIGGER_STOP:
2179 case SNDRV_PCM_TRIGGER_SUSPEND:
2180 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2181 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2182 break;
2183 default:
2184 ret = -EINVAL;
2185 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002186 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002187 break;
2188 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002189 switch (cmd) {
2190 case SNDRV_PCM_TRIGGER_START:
2191 case SNDRV_PCM_TRIGGER_RESUME:
2192 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Cezary Rojewski4c22b802020-10-26 11:01:29 +01002193 case SNDRV_PCM_TRIGGER_DRAIN:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002194 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2195 break;
2196 case SNDRV_PCM_TRIGGER_STOP:
2197 case SNDRV_PCM_TRIGGER_SUSPEND:
2198 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2199 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2200 break;
2201 default:
2202 ret = -EINVAL;
2203 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002204 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002205 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002206 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2207 /* bespoke trigger() - handles both FE and BEs */
2208
Liam Girdwood103d84a2012-11-19 14:39:15 +00002209 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002210 fe->dai_link->name, cmd);
2211
Kuninori Morimoto308193582020-04-24 08:15:09 +09002212 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002213 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002214 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002215 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002216 fe->dai_link->name);
2217 ret = -EINVAL;
2218 goto out;
2219 }
2220
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002221 if (ret < 0) {
2222 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2223 cmd, ret);
2224 goto out;
2225 }
2226
Liam Girdwood01d75842012-04-25 12:12:49 +01002227 switch (cmd) {
2228 case SNDRV_PCM_TRIGGER_START:
2229 case SNDRV_PCM_TRIGGER_RESUME:
2230 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2231 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2232 break;
2233 case SNDRV_PCM_TRIGGER_STOP:
2234 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002235 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2236 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002237 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2238 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2239 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002240 }
2241
2242out:
2243 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2244 return ret;
2245}
2246
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002247static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2248{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002249 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002250 int stream = substream->stream;
2251
2252 /* if FE's runtime_update is already set, we're in race;
2253 * process this trigger later at exit
2254 */
2255 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2256 fe->dpcm[stream].trigger_pending = cmd + 1;
2257 return 0; /* delayed, assuming it's successful */
2258 }
2259
2260 /* we're alone, let's trigger */
2261 return dpcm_fe_dai_do_trigger(substream, cmd);
2262}
2263
Liam Girdwood23607022014-01-17 17:03:55 +00002264int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002265{
2266 struct snd_soc_dpcm *dpcm;
2267 int ret = 0;
2268
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002269 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002270
2271 struct snd_soc_pcm_runtime *be = dpcm->be;
2272 struct snd_pcm_substream *be_substream =
2273 snd_soc_dpcm_get_substream(be, stream);
2274
2275 /* is this op for this BE ? */
2276 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2277 continue;
2278
2279 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002280 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002281 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2282 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002283 continue;
2284
Liam Girdwood103d84a2012-11-19 14:39:15 +00002285 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002286 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002287
2288 ret = soc_pcm_prepare(be_substream);
2289 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002290 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002291 ret);
2292 break;
2293 }
2294
2295 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2296 }
2297 return ret;
2298}
2299
Mark Brown45c0a182012-05-09 21:46:27 +01002300static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002301{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002302 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002303 int stream = substream->stream, ret = 0;
2304
2305 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2306
Liam Girdwood103d84a2012-11-19 14:39:15 +00002307 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002308
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002309 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002310
2311 /* there is no point preparing this FE if there are no BEs */
2312 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002313 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002314 fe->dai_link->name);
2315 ret = -EINVAL;
2316 goto out;
2317 }
2318
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002319 ret = dpcm_be_dai_prepare(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002320 if (ret < 0)
2321 goto out;
2322
2323 /* call prepare on the frontend */
2324 ret = soc_pcm_prepare(substream);
2325 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002326 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002327 fe->dai_link->name);
2328 goto out;
2329 }
2330
Liam Girdwood01d75842012-04-25 12:12:49 +01002331 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2332
2333out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002334 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002335 mutex_unlock(&fe->card->mutex);
2336
2337 return ret;
2338}
2339
Liam Girdwood618dae12012-04-25 12:12:51 +01002340static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2341{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002342 struct snd_pcm_substream *substream =
2343 snd_soc_dpcm_get_substream(fe, stream);
2344 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002345 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002346
Liam Girdwood103d84a2012-11-19 14:39:15 +00002347 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002348 stream ? "capture" : "playback", fe->dai_link->name);
2349
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002350 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2351 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002352 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002353 fe->dai_link->name);
2354
Kuninori Morimoto308193582020-04-24 08:15:09 +09002355 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002356 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002357 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002358 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002359 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002360 fe->dai_link->name);
2361
2362 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2363 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002364 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002365 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002366
2367 err = dpcm_be_dai_hw_free(fe, stream);
2368 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002369 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002370
2371 err = dpcm_be_dai_shutdown(fe, stream);
2372 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002373 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002374
2375 /* run the stream event for each BE */
2376 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2377
2378 return 0;
2379}
2380
2381static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2382{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002383 struct snd_pcm_substream *substream =
2384 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002385 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002386 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Souptick Joarder4eeed5f2021-01-09 09:15:01 +05302387 int ret = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002388 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002389
Liam Girdwood103d84a2012-11-19 14:39:15 +00002390 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002391 stream ? "capture" : "playback", fe->dai_link->name);
2392
2393 /* Only start the BE if the FE is ready */
2394 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
朱灿灿2c138282020-12-25 16:42:46 +08002395 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
Souptick Joarder4eeed5f2021-01-09 09:15:01 +05302396 ret = -EINVAL;
朱灿灿2c138282020-12-25 16:42:46 +08002397 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2398 fe->dai_link->name, fe->dpcm[stream].state);
Dan Carpentere91b65b2021-01-11 12:50:21 +03002399 ret = -EINVAL;
朱灿灿2c138282020-12-25 16:42:46 +08002400 goto disconnect;
2401 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002402
2403 /* startup must always be called for new BEs */
2404 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002405 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002406 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002407
2408 /* keep going if FE state is > open */
2409 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2410 return 0;
2411
2412 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002413 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002414 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002415
2416 /* keep going if FE state is > hw_params */
2417 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2418 return 0;
2419
2420
2421 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002422 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002423 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002424
2425 /* run the stream event for each BE */
2426 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2427
2428 /* keep going if FE state is > prepare */
2429 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2430 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2431 return 0;
2432
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002433 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2434 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002435 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002436 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002437
Kuninori Morimoto308193582020-04-24 08:15:09 +09002438 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002439 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002440 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002441 goto hw_free;
2442 }
2443 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002444 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002445 fe->dai_link->name);
2446
2447 ret = dpcm_be_dai_trigger(fe, stream,
2448 SNDRV_PCM_TRIGGER_START);
2449 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002450 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002451 goto hw_free;
2452 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002453 }
2454
2455 return 0;
2456
2457hw_free:
2458 dpcm_be_dai_hw_free(fe, stream);
2459close:
2460 dpcm_be_dai_shutdown(fe, stream);
2461disconnect:
朱灿灿2c138282020-12-25 16:42:46 +08002462 /* disconnect any pending BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002463 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002464 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002465 struct snd_soc_pcm_runtime *be = dpcm->be;
朱灿灿2c138282020-12-25 16:42:46 +08002466
2467 /* is this op for this BE ? */
2468 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2469 continue;
2470
2471 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2472 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2473 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Liam Girdwood618dae12012-04-25 12:12:51 +01002474 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002475 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002476
2477 return ret;
2478}
2479
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002480static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2481{
2482 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002483 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002484 int count, paths;
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002485 int ret;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002486
Pierre-Louis Bossart96bf62f2020-06-12 15:35:07 -05002487 if (!fe->dai_link->dynamic)
2488 return 0;
2489
Bard Liao6e1276a2020-02-25 21:39:16 +08002490 if (fe->num_cpus > 1) {
2491 dev_err(fe->dev,
2492 "%s doesn't support Multi CPU yet\n", __func__);
2493 return -EINVAL;
2494 }
2495
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002496 /* only check active links */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002497 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002498 return 0;
2499
2500 /* DAPM sync will call this to update DSP paths */
2501 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2502 new ? "new" : "old", fe->dai_link->name);
2503
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002504 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002505
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002506 /* skip if FE doesn't have playback/capture capability */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002507 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2508 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002509 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002510
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002511 /* skip if FE isn't currently playing/capturing */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002512 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2513 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002514 continue;
2515
2516 paths = dpcm_path_get(fe, stream, &list);
2517 if (paths < 0) {
2518 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2519 fe->dai_link->name,
2520 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2521 "playback" : "capture");
2522 return paths;
2523 }
2524
2525 /* update any playback/capture paths */
2526 count = dpcm_process_paths(fe, stream, &list, new);
2527 if (count) {
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002528 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002529 if (new)
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002530 ret = dpcm_run_update_startup(fe, stream);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002531 else
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002532 ret = dpcm_run_update_shutdown(fe, stream);
2533 if (ret < 0)
2534 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2535 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002536
2537 dpcm_clear_pending_state(fe, stream);
2538 dpcm_be_disconnect(fe, stream);
2539 }
2540
2541 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002542 }
2543
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002544 return 0;
2545}
2546
Liam Girdwood618dae12012-04-25 12:12:51 +01002547/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2548 * any DAI links.
2549 */
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002550int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002551{
Mengdong Lin1a497982015-11-18 02:34:11 -05002552 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002553 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002554
Liam Girdwood618dae12012-04-25 12:12:51 +01002555 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002556 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002557 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002558 ret = soc_dpcm_fe_runtime_update(fe, 0);
2559 if (ret)
2560 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002561 }
2562
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002563 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002564 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002565 ret = soc_dpcm_fe_runtime_update(fe, 1);
2566 if (ret)
2567 goto out;
2568 }
2569
2570out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002571 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002572 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002573}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002574EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
Liam Girdwood01d75842012-04-25 12:12:49 +01002575
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002576static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002577{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002578 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002579 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002580 int stream = fe_substream->stream;
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002581
2582 /* mark FE's links ready to prune */
2583 for_each_dpcm_be(fe, stream, dpcm)
2584 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2585
2586 dpcm_be_disconnect(fe, stream);
2587
2588 fe->dpcm[stream].runtime = NULL;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002589}
2590
2591static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2592{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002593 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002594 int ret;
2595
2596 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2597 ret = dpcm_fe_dai_shutdown(fe_substream);
2598
2599 dpcm_fe_dai_cleanup(fe_substream);
2600
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002601 mutex_unlock(&fe->card->mutex);
2602 return ret;
2603}
2604
Liam Girdwood01d75842012-04-25 12:12:49 +01002605static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2606{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002607 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002608 struct snd_soc_dapm_widget_list *list;
2609 int ret;
2610 int stream = fe_substream->stream;
2611
2612 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2613 fe->dpcm[stream].runtime = fe_substream->runtime;
2614
Qiao Zhou8f70e512014-09-10 17:54:07 +08002615 ret = dpcm_path_get(fe, stream, &list);
2616 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002617 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08002618 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002619 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002620 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002621 }
2622
2623 /* calculate valid and active FE <-> BE dpcms */
2624 dpcm_process_paths(fe, stream, &list, 1);
2625
2626 ret = dpcm_fe_dai_startup(fe_substream);
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002627 if (ret < 0)
2628 dpcm_fe_dai_cleanup(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002629
2630 dpcm_clear_pending_state(fe, stream);
2631 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002632open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002633 mutex_unlock(&fe->card->mutex);
2634 return ret;
2635}
2636
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002637static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2638 int *playback, int *capture)
Liam Girdwoodddee6272011-06-09 14:45:53 +01002639{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002640 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002641 struct snd_soc_dai *cpu_dai;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002642 int stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002643 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002644
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002645 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2646 dev_err(rtd->dev,
2647 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2648 return -EINVAL;
2649 }
Stephan Gerhold9b5db052020-04-15 12:49:28 +02002650
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002651 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2652 if (rtd->dai_link->dpcm_playback) {
2653 stream = SNDRV_PCM_STREAM_PLAYBACK;
2654
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002655 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2656 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002657 *playback = 1;
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002658 break;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002659 }
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002660 }
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002661 if (!*playback) {
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002662 dev_err(rtd->card->dev,
2663 "No CPU DAIs support playback for stream %s\n",
2664 rtd->dai_link->stream_name);
2665 return -EINVAL;
2666 }
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002667 }
2668 if (rtd->dai_link->dpcm_capture) {
2669 stream = SNDRV_PCM_STREAM_CAPTURE;
2670
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002671 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2672 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002673 *capture = 1;
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002674 break;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002675 }
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002676 }
2677
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002678 if (!*capture) {
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002679 dev_err(rtd->card->dev,
2680 "No CPU DAIs support capture for stream %s\n",
2681 rtd->dai_link->stream_name);
2682 return -EINVAL;
2683 }
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002684 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002685 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002686 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002687 int cpu_capture = rtd->dai_link->params ?
2688 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2689 int cpu_playback = rtd->dai_link->params ?
2690 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002691
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002692 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002693 if (rtd->num_cpus == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002694 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002695 } else if (rtd->num_cpus == rtd->num_codecs) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002696 cpu_dai = asoc_rtd_to_cpu(rtd, i);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002697 } else {
2698 dev_err(rtd->card->dev,
2699 "N cpus to M codecs link is not supported yet\n");
2700 return -EINVAL;
2701 }
2702
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002703 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002704 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002705 *playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002706 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002707 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002708 *capture = 1;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002709 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002710 }
Sangsu Parka5002312012-01-02 17:15:10 +09002711
Fabio Estevamd6bead02013-08-29 10:32:13 -03002712 if (rtd->dai_link->playback_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002713 *playback = 1;
2714 *capture = 0;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002715 }
2716
2717 if (rtd->dai_link->capture_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002718 *playback = 0;
2719 *capture = 1;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002720 }
2721
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002722 return 0;
2723}
2724
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002725static int soc_create_pcm(struct snd_pcm **pcm,
2726 struct snd_soc_pcm_runtime *rtd,
2727 int playback, int capture, int num)
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002728{
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002729 char new_name[64];
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002730 int ret;
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002731
Liam Girdwood01d75842012-04-25 12:12:49 +01002732 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002733 if (rtd->dai_link->params) {
2734 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2735 rtd->dai_link->stream_name);
2736
2737 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002738 playback, capture, pcm);
Jerome Bruneta3420312019-07-25 18:59:47 +02002739 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002740 snprintf(new_name, sizeof(new_name), "(%s)",
2741 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002742
Liam Girdwood01d75842012-04-25 12:12:49 +01002743 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002744 playback, capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002745 } else {
2746 if (rtd->dai_link->dynamic)
2747 snprintf(new_name, sizeof(new_name), "%s (*)",
2748 rtd->dai_link->stream_name);
2749 else
2750 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002751 rtd->dai_link->stream_name,
2752 (rtd->num_codecs > 1) ?
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002753 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002754
Liam Girdwood01d75842012-04-25 12:12:49 +01002755 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002756 capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002757 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002758 if (ret < 0) {
Pierre-Louis Bossart799827a2020-06-12 15:40:49 -05002759 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2760 new_name, rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002761 return ret;
2762 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002763 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002764
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002765 return 0;
2766}
2767
2768/* create a new pcm */
2769int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2770{
2771 struct snd_soc_component *component;
2772 struct snd_pcm *pcm;
2773 int ret = 0, playback = 0, capture = 0;
2774 int i;
2775
2776 ret = soc_get_playback_capture(rtd, &playback, &capture);
2777 if (ret < 0)
2778 return ret;
2779
2780 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2781 if (ret < 0)
2782 return ret;
2783
Liam Girdwoodddee6272011-06-09 14:45:53 +01002784 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002785 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002786 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002787 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002788 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002789
2790 rtd->pcm = pcm;
Kuninori Morimotoe04e7b82021-01-22 10:13:32 +09002791 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002792 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002793
Jerome Bruneta3420312019-07-25 18:59:47 +02002794 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002795 if (playback)
2796 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2797 if (capture)
2798 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2799 goto out;
2800 }
2801
2802 /* ASoC PCM operations */
2803 if (rtd->dai_link->dynamic) {
2804 rtd->ops.open = dpcm_fe_dai_open;
2805 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2806 rtd->ops.prepare = dpcm_fe_dai_prepare;
2807 rtd->ops.trigger = dpcm_fe_dai_trigger;
2808 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2809 rtd->ops.close = dpcm_fe_dai_close;
2810 rtd->ops.pointer = soc_pcm_pointer;
2811 } else {
2812 rtd->ops.open = soc_pcm_open;
2813 rtd->ops.hw_params = soc_pcm_hw_params;
2814 rtd->ops.prepare = soc_pcm_prepare;
2815 rtd->ops.trigger = soc_pcm_trigger;
2816 rtd->ops.hw_free = soc_pcm_hw_free;
2817 rtd->ops.close = soc_pcm_close;
2818 rtd->ops.pointer = soc_pcm_pointer;
2819 }
2820
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002821 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002822 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002823
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002824 if (drv->ioctl)
2825 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002826 if (drv->sync_stop)
2827 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002828 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002829 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002830 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002831 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002832 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002833 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002834 }
2835
Liam Girdwoodddee6272011-06-09 14:45:53 +01002836 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002837 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002838
2839 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002840 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002841
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002842 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002843 if (ret < 0) {
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002844 dev_err(rtd->dev, "ASoC: pcm constructor failed for dailink %s: %d\n",
2845 rtd->dai_link->name, ret);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002846 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002847 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002848
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002849 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002850out:
Pierre-Louis Bossart1d5cd522020-06-12 15:40:50 -05002851 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2852 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2853 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002854 return ret;
2855}
Liam Girdwood01d75842012-04-25 12:12:49 +01002856
2857/* is the current PCM operation for this FE ? */
2858int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2859{
2860 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2861 return 1;
2862 return 0;
2863}
2864EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2865
2866/* is the current PCM operation for this BE ? */
2867int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2868 struct snd_soc_pcm_runtime *be, int stream)
2869{
2870 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2871 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2872 be->dpcm[stream].runtime_update))
2873 return 1;
2874 return 0;
2875}
2876EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2877
2878/* get the substream for this BE */
2879struct snd_pcm_substream *
2880 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2881{
2882 return be->pcm->streams[stream].substream;
2883}
2884EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2885
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002886static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2887 struct snd_soc_pcm_runtime *be,
2888 int stream,
2889 const enum snd_soc_dpcm_state *states,
2890 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01002891{
2892 struct snd_soc_dpcm *dpcm;
2893 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002894 int ret = 1;
2895 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002896 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002897
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002898 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00002899 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002900
2901 if (dpcm->fe == fe)
2902 continue;
2903
2904 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002905 for (i = 0; i < num_states; i++) {
2906 if (state == states[i]) {
2907 ret = 0;
2908 break;
2909 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002910 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002911 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002912 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01002913
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002914 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002915 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01002916}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002917
2918/*
2919 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2920 * are not running, paused or suspended for the specified stream direction.
2921 */
2922int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2923 struct snd_soc_pcm_runtime *be, int stream)
2924{
2925 const enum snd_soc_dpcm_state state[] = {
2926 SND_SOC_DPCM_STATE_START,
2927 SND_SOC_DPCM_STATE_PAUSED,
2928 SND_SOC_DPCM_STATE_SUSPEND,
2929 };
2930
2931 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2932}
Liam Girdwood01d75842012-04-25 12:12:49 +01002933EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2934
2935/*
2936 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2937 * running, paused or suspended for the specified stream direction.
2938 */
2939int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2940 struct snd_soc_pcm_runtime *be, int stream)
2941{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002942 const enum snd_soc_dpcm_state state[] = {
2943 SND_SOC_DPCM_STATE_START,
2944 SND_SOC_DPCM_STATE_PAUSED,
2945 SND_SOC_DPCM_STATE_SUSPEND,
2946 SND_SOC_DPCM_STATE_PREPARE,
2947 };
Liam Girdwood01d75842012-04-25 12:12:49 +01002948
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002949 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01002950}
2951EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);