blob: a3fc27fc156f4bbc571f507f7c3c41967c06ac50 [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 Morimoto6fb89442021-03-09 10:07:48 +090032static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
33{
34 return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
35}
36static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
37{
38 return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
39}
40
Kuninori Morimotoc3212822020-02-19 15:56:57 +090041#ifdef CONFIG_DEBUG_FS
42static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
43{
44 switch (state) {
45 case SND_SOC_DPCM_STATE_NEW:
46 return "new";
47 case SND_SOC_DPCM_STATE_OPEN:
48 return "open";
49 case SND_SOC_DPCM_STATE_HW_PARAMS:
50 return "hw_params";
51 case SND_SOC_DPCM_STATE_PREPARE:
52 return "prepare";
53 case SND_SOC_DPCM_STATE_START:
54 return "start";
55 case SND_SOC_DPCM_STATE_STOP:
56 return "stop";
57 case SND_SOC_DPCM_STATE_SUSPEND:
58 return "suspend";
59 case SND_SOC_DPCM_STATE_PAUSED:
60 return "paused";
61 case SND_SOC_DPCM_STATE_HW_FREE:
62 return "hw_free";
63 case SND_SOC_DPCM_STATE_CLOSE:
64 return "close";
65 }
66
67 return "unknown";
68}
69
70static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
71 int stream, char *buf, size_t size)
72{
73 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
74 struct snd_soc_dpcm *dpcm;
75 ssize_t offset = 0;
76 unsigned long flags;
77
78 /* FE state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010079 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090080 "[%s - %s]\n", fe->dai_link->name,
81 stream ? "Capture" : "Playback");
82
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010083 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
Kuninori Morimotoc3212822020-02-19 15:56:57 +090084 dpcm_state_string(fe->dpcm[stream].state));
85
86 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
87 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010088 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +090089 "Hardware Params: "
90 "Format = %s, Channels = %d, Rate = %d\n",
91 snd_pcm_format_name(params_format(params)),
92 params_channels(params),
93 params_rate(params));
94
95 /* BEs state */
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010096 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
Kuninori Morimotoc3212822020-02-19 15:56:57 +090097
98 if (list_empty(&fe->dpcm[stream].be_clients)) {
Takashi Iwaid0c9abb2020-03-10 17:36:25 +010099 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900100 " No active DSP links\n");
101 goto out;
102 }
103
104 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
105 for_each_dpcm_be(fe, stream, dpcm) {
106 struct snd_soc_pcm_runtime *be = dpcm->be;
107 params = &dpcm->hw_params;
108
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100109 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900110 "- %s\n", be->dai_link->name);
111
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100112 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900113 " State: %s\n",
114 dpcm_state_string(be->dpcm[stream].state));
115
116 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
117 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
Takashi Iwaid0c9abb2020-03-10 17:36:25 +0100118 offset += scnprintf(buf + offset, size - offset,
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900119 " Hardware Params: "
120 "Format = %s, Channels = %d, Rate = %d\n",
121 snd_pcm_format_name(params_format(params)),
122 params_channels(params),
123 params_rate(params));
124 }
125 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
126out:
127 return offset;
128}
129
130static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
131 size_t count, loff_t *ppos)
132{
133 struct snd_soc_pcm_runtime *fe = file->private_data;
134 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
135 int stream;
136 char *buf;
137
Bard Liao6e1276a2020-02-25 21:39:16 +0800138 if (fe->num_cpus > 1) {
139 dev_err(fe->dev,
140 "%s doesn't support Multi CPU yet\n", __func__);
141 return -EINVAL;
142 }
143
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900144 buf = kmalloc(out_count, GFP_KERNEL);
145 if (!buf)
146 return -ENOMEM;
147
148 for_each_pcm_streams(stream)
Kuninori Morimotoc2233a22020-03-30 10:47:37 +0900149 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900150 offset += dpcm_show_state(fe, stream,
151 buf + offset,
152 out_count - offset);
153
154 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
155
156 kfree(buf);
157 return ret;
158}
159
160static const struct file_operations dpcm_state_fops = {
161 .open = simple_open,
162 .read = dpcm_state_read_file,
163 .llseek = default_llseek,
164};
165
166void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
167{
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900168 if (!rtd->dai_link->dynamic)
169 return;
170
171 if (!rtd->card->debugfs_card_root)
172 return;
173
174 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
175 rtd->card->debugfs_card_root);
176
177 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
178 rtd, &dpcm_state_fops);
179}
Kuninori Morimoto154dae82020-02-19 15:57:06 +0900180
181static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
182{
183 char *name;
184
185 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
186 stream ? "capture" : "playback");
187 if (name) {
188 dpcm->debugfs_state = debugfs_create_dir(
189 name, dpcm->fe->debugfs_dpcm_root);
190 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
191 &dpcm->state);
192 kfree(name);
193 }
194}
195
196static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
197{
198 debugfs_remove_recursive(dpcm->debugfs_state);
199}
200
201#else
202static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
203 int stream)
204{
205}
206
207static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
208{
209}
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900210#endif
211
Kuninori Morimoto9c6d7f92020-12-11 14:55:16 +0900212/* Set FE's runtime_update state; the state is protected via PCM stream lock
213 * for avoiding the race with trigger callback.
214 * If the state is unset and a trigger is pending while the previous operation,
215 * process the pending trigger action here.
216 */
217static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
218static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
219 int stream, enum snd_soc_dpcm_update state)
220{
221 struct snd_pcm_substream *substream =
222 snd_soc_dpcm_get_substream(fe, stream);
223
224 snd_pcm_stream_lock_irq(substream);
225 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
226 dpcm_fe_dai_do_trigger(substream,
227 fe->dpcm[stream].trigger_pending - 1);
228 fe->dpcm[stream].trigger_pending = 0;
229 }
230 fe->dpcm[stream].runtime_update = state;
231 snd_pcm_stream_unlock_irq(substream);
232}
233
Kuninori Morimotoa7e204442020-12-11 14:55:22 +0900234static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
235 int stream, enum snd_soc_dpcm_update state)
236{
237 be->dpcm[stream].runtime_update = state;
238}
239
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900240/**
241 * snd_soc_runtime_action() - Increment/Decrement active count for
242 * PCM runtime components
243 * @rtd: ASoC PCM runtime that is activated
244 * @stream: Direction of the PCM stream
Colton Lewisb6d6e9e2020-06-26 05:40:24 +0000245 * @action: Activate stream if 1. Deactivate if -1.
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900246 *
247 * Increments/Decrements the active count for all the DAIs and components
248 * attached to a PCM runtime.
249 * Should typically be called when a stream is opened.
250 *
251 * Must be called with the rtd->card->pcm_mutex being held
252 */
253void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
254 int stream, int action)
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900255{
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900256 struct snd_soc_dai *dai;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900257 int i;
258
259 lockdep_assert_held(&rtd->card->pcm_mutex);
260
Kuninori Morimotodc829102020-05-15 09:46:27 +0900261 for_each_rtd_dais(rtd, i, dai)
262 snd_soc_dai_action(dai, stream, action);
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900263}
Kuninori Morimotod9051d82020-05-15 09:46:21 +0900264EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100265
266/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100267 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
268 * @rtd: The ASoC PCM runtime that should be checked.
269 *
270 * This function checks whether the power down delay should be ignored for a
271 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
272 * been configured to ignore the delay, or if none of the components benefits
273 * from having the delay.
274 */
275bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
276{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000277 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200278 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900279 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200280
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100281 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
282 return true;
283
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900284 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000285 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000286
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000287 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100288}
289
290/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200291 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
292 * @substream: the pcm substream
293 * @hw: the hardware parameters
294 *
295 * Sets the substream runtime hardware parameters.
296 */
297int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
298 const struct snd_pcm_hardware *hw)
299{
Kuninori Morimoto56e749b2021-03-09 10:07:53 +0900300 substream->runtime->hw = *hw;
301
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200302 return 0;
303}
304EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
305
Liam Girdwood01d75842012-04-25 12:12:49 +0100306/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000307int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100308 int event)
309{
310 struct snd_soc_dpcm *dpcm;
311
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000312 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100313
314 struct snd_soc_pcm_runtime *be = dpcm->be;
315
Liam Girdwood103d84a2012-11-19 14:39:15 +0000316 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100317 be->dai_link->name, event, dir);
318
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700319 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
320 (be->dpcm[dir].users >= 1))
321 continue;
322
Liam Girdwood01d75842012-04-25 12:12:49 +0100323 snd_soc_dapm_stream_event(be, dir, event);
324 }
325
326 snd_soc_dapm_stream_event(fe, dir, event);
327
328 return 0;
329}
330
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900331static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
332 struct snd_pcm_hw_params *params)
333{
334 if (params) {
335 dai->rate = params_rate(params);
336 dai->channels = params_channels(params);
337 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
338 } else {
339 dai->rate = 0;
340 dai->channels = 0;
341 dai->sample_bits = 0;
342 }
343}
344
Dong Aisheng17841022011-08-29 17:15:14 +0800345static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
346 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100347{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900348 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100349 int ret;
350
Kuninori Morimotof8fc9ec2021-03-09 10:07:42 +0900351 if (!snd_soc_dai_active(soc_dai))
352 return 0;
353
Kuninori Morimotofac110c2021-01-15 13:56:35 +0900354#define __soc_pcm_apply_symmetry(name, NAME) \
355 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
356 rtd->dai_link->symmetric_##name)) { \
357 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
358 #name, soc_dai->name); \
359 \
360 ret = snd_pcm_hw_constraint_single(substream->runtime, \
361 SNDRV_PCM_HW_PARAM_##NAME,\
362 soc_dai->name); \
363 if (ret < 0) { \
364 dev_err(soc_dai->dev, \
365 "ASoC: Unable to apply %s constraint: %d\n",\
366 #name, ret); \
367 return ret; \
368 } \
Liam Girdwoodddee6272011-06-09 14:45:53 +0100369 }
370
Kuninori Morimotofac110c2021-01-15 13:56:35 +0900371 __soc_pcm_apply_symmetry(rate, RATE);
372 __soc_pcm_apply_symmetry(channels, CHANNELS);
373 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100374
375 return 0;
376}
377
Nicolin Chen3635bf02013-11-13 18:56:24 +0800378static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
379 struct snd_pcm_hw_params *params)
380{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900381 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900382 struct snd_soc_dai d;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900383 struct snd_soc_dai *dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800384 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900385 unsigned int symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800386
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900387 soc_pcm_set_dai_params(&d, params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800388
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900389#define __soc_pcm_params_symmetry(xxx) \
390 symmetry = rtd->dai_link->symmetric_##xxx; \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900391 for_each_rtd_dais(rtd, i, dai) \
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900392 symmetry |= dai->driver->symmetric_##xxx; \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900393 \
394 if (symmetry) \
395 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900396 if (cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900397 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %d - %d\n", \
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900398 #xxx, cpu_dai->xxx, d.xxx); \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900399 return -EINVAL; \
400 }
401
Nicolin Chen3635bf02013-11-13 18:56:24 +0800402 /* reject unmatched parameters when applying symmetry */
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900403 __soc_pcm_params_symmetry(rate);
404 __soc_pcm_params_symmetry(channels);
405 __soc_pcm_params_symmetry(sample_bits);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100406
407 return 0;
408}
409
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900410static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100411{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900412 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100413 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900414 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200415 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100416
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900417 symmetry = link->symmetric_rate ||
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800418 link->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900419 link->symmetric_sample_bits;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800420
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900421 for_each_rtd_dais(rtd, i, dai)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800422 symmetry = symmetry ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900423 dai->driver->symmetric_rate ||
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900424 dai->driver->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900425 dai->driver->symmetric_sample_bits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200426
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900427 if (symmetry)
428 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100429}
430
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200431static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000432{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900433 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwaic6068d32014-12-31 17:10:34 +0100434 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000435
436 if (!bits)
437 return;
438
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100439 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
440 if (ret != 0)
441 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
442 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000443}
444
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200445static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200446{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900447 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800448 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200449 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900450 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
451 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200452 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800453 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200454
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900455 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900456 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
457
458 if (pcm_codec->sig_bits == 0) {
459 bits = 0;
460 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200461 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900462 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200463 }
464
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900465 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800466 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
467
468 if (pcm_cpu->sig_bits == 0) {
469 cpu_bits = 0;
470 break;
471 }
472 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
473 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900474
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200475 soc_pcm_set_msb(substream, bits);
476 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200477}
478
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900479static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
480{
481 hw->rates = UINT_MAX;
482 hw->rate_min = 0;
483 hw->rate_max = UINT_MAX;
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900484 hw->channels_min = 0;
485 hw->channels_max = UINT_MAX;
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900486 hw->formats = ULLONG_MAX;
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900487}
488
489static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
490 struct snd_soc_pcm_stream *p)
491{
492 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
493
494 /* setup hw->rate_min/max via hw->rates first */
495 snd_pcm_hw_limit_rates(hw);
496
497 /* update hw->rate_min/max by snd_soc_pcm_stream */
498 hw->rate_min = max(hw->rate_min, p->rate_min);
499 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
500}
501
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900502static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
503 struct snd_soc_pcm_stream *p)
504{
505 hw->channels_min = max(hw->channels_min, p->channels_min);
506 hw->channels_max = min(hw->channels_max, p->channels_max);
507}
508
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900509static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
510 struct snd_soc_pcm_stream *p)
511{
512 hw->formats &= p->formats;
513}
514
Samuel Holland5854a462020-03-04 23:11:42 -0600515/**
516 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
517 * @rtd: ASoC PCM runtime
518 * @hw: PCM hardware parameters (output)
519 * @stream: Direction of the PCM stream
520 *
521 * Calculates the subset of stream parameters supported by all DAIs
522 * associated with the PCM stream.
523 */
524int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
525 struct snd_pcm_hardware *hw, int stream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200526{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000527 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800528 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200529 struct snd_soc_pcm_stream *codec_stream;
530 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800531 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200532 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100533
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900534 soc_pcm_hw_init(hw);
535
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800536 /* first calculate min/max only for CPUs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900537 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800538
539 /*
540 * Skip CPUs which don't support the current stream type.
541 * Otherwise, since the rate, channel, and format values will
542 * zero in that case, we would have no usable settings left,
543 * causing the resulting setup to fail.
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800544 */
Samuel Holland5854a462020-03-04 23:11:42 -0600545 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800546 continue;
547
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800548 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100549
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900550 soc_pcm_hw_update_chan(hw, cpu_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900551 soc_pcm_hw_update_rate(hw, cpu_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900552 soc_pcm_hw_update_format(hw, cpu_stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800553 }
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900554 cpu_chan_min = hw->channels_min;
555 cpu_chan_max = hw->channels_max;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800556
557 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900558 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200559
560 /*
561 * Skip CODECs which don't support the current stream type.
562 * Otherwise, since the rate, channel, and format values will
563 * zero in that case, we would have no usable settings left,
564 * causing the resulting setup to fail.
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200565 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900566 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200567 continue;
568
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900569 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
570
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900571 soc_pcm_hw_update_chan(hw, codec_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900572 soc_pcm_hw_update_rate(hw, codec_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900573 soc_pcm_hw_update_format(hw, codec_stream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200574 }
575
Samuel Holland5854a462020-03-04 23:11:42 -0600576 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900577 if (!hw->channels_min)
Samuel Holland5854a462020-03-04 23:11:42 -0600578 return -EINVAL;
579
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200580 /*
581 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800582 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200583 * channel allocation be fixed up later
584 */
585 if (rtd->num_codecs > 1) {
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900586 hw->channels_min = cpu_chan_min;
587 hw->channels_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200588 }
589
Samuel Holland5854a462020-03-04 23:11:42 -0600590 return 0;
591}
592EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
593
594static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
595{
596 struct snd_pcm_hardware *hw = &substream->runtime->hw;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900597 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Samuel Holland5854a462020-03-04 23:11:42 -0600598 u64 formats = hw->formats;
599
600 /*
601 * At least one CPU and one CODEC should match. Otherwise, we should
602 * have bailed out on a higher level, since there would be no CPU or
603 * CODEC to support the transfer direction in that case.
604 */
605 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
606
607 if (formats)
608 hw->formats &= formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200609}
610
Kuninori Morimotodd039072020-02-10 12:14:37 +0900611static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900612{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900613 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900614 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900615 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900616
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900617 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900618 ret = snd_soc_component_module_get_when_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900619 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200620 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900621
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900622 ret = snd_soc_component_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900623 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200624 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900625 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900626
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200627 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900628}
629
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900630static int soc_pcm_components_close(struct snd_pcm_substream *substream,
631 int rollback)
Charles Keepax244e2932018-06-19 16:22:09 +0100632{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900633 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax244e2932018-06-19 16:22:09 +0100634 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900635 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100636
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900637 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900638 r = snd_soc_component_close(component, substream, rollback);
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900639 if (r < 0)
640 ret = r; /* use last ret */
641
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900642 snd_soc_component_module_put_when_close(component, substream, rollback);
Charles Keepax244e2932018-06-19 16:22:09 +0100643 }
644
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900645 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100646}
647
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900648static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900649{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900650 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900651 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900652 struct snd_soc_dai *dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900653 int i;
654
655 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
656
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900657 if (!rollback)
658 snd_soc_runtime_deactivate(rtd, substream->stream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900659
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900660 for_each_rtd_dais(rtd, i, dai)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900661 snd_soc_dai_shutdown(dai, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900662
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900663 snd_soc_link_shutdown(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900664
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900665 soc_pcm_components_close(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900666
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900667
668 mutex_unlock(&rtd->card->pcm_mutex);
669
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900670 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900671
672 for_each_rtd_components(rtd, i, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900673 if (!snd_soc_component_active(component))
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900674 pinctrl_pm_select_sleep_state(component->dev);
675
676 return 0;
677}
678
679/*
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900680 * Called by ALSA when a PCM substream is closed. Private data can be
681 * freed here. The cpu DAI, codec DAI, machine and components are also
682 * shutdown.
683 */
684static int soc_pcm_close(struct snd_pcm_substream *substream)
685{
686 return soc_pcm_clean(substream, 0);
687}
688
Kuninori Morimotoc3932812021-03-09 10:08:02 +0900689static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
690{
691 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
692 struct snd_pcm_hardware *hw = &substream->runtime->hw;
693 const char *name_cpu = soc_cpu_dai_name(rtd);
694 const char *name_codec = soc_codec_dai_name(rtd);
695 const char *err_msg;
696 struct device *dev = rtd->dev;
697
698 err_msg = "rates";
699 if (!hw->rates)
700 goto config_err;
701
702 err_msg = "formats";
703 if (!hw->formats)
704 goto config_err;
705
706 err_msg = "channels";
707 if (!hw->channels_min || !hw->channels_max ||
708 hw->channels_min > hw->channels_max)
709 goto config_err;
710
711 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
712 name_cpu);
713 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
714 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
715 hw->channels_max);
716 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
717 hw->rate_max);
718
719 return 0;
720
721config_err:
722 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
723 name_codec, name_cpu, err_msg);
724 return -EINVAL;
725}
726
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900727/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100728 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
729 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100730 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100731 */
732static int soc_pcm_open(struct snd_pcm_substream *substream)
733{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900734 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000735 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900736 struct snd_soc_dai *dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100737 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100738
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900739 for_each_rtd_components(rtd, i, component)
740 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000741
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900742 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
743 if (ret < 0)
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900744 goto pm_err;
Mark Brownd6652ef2011-12-03 20:14:31 +0000745
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300746 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100747
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900748 ret = soc_pcm_components_open(substream);
749 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900750 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900751
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900752 ret = snd_soc_link_startup(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900753 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900754 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900755
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756 /* startup the audio subsystem */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900757 for_each_rtd_dais(rtd, i, dai) {
758 ret = snd_soc_dai_startup(dai, substream);
Kuninori Morimotoce820142020-09-28 09:01:29 +0900759 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900760 goto err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200761
762 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900763 dai->tx_mask = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200764 else
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900765 dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100766 }
767
Liam Girdwood01d75842012-04-25 12:12:49 +0100768 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
769 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
770 goto dynamic;
771
Liam Girdwoodddee6272011-06-09 14:45:53 +0100772 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200773 soc_pcm_init_runtime_hw(substream);
774
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900775 soc_pcm_update_symmetry(substream);
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100776
Kuninori Morimotoc3932812021-03-09 10:08:02 +0900777 ret = soc_hw_sanity_check(substream);
778 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900779 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100780
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200781 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000782
Liam Girdwoodddee6272011-06-09 14:45:53 +0100783 /* Symmetry only applies if we've already got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900784 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotof8fc9ec2021-03-09 10:07:42 +0900785 ret = soc_pcm_apply_symmetry(substream, dai);
786 if (ret != 0)
787 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100788 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100789dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100790 snd_soc_runtime_activate(rtd, substream->stream);
Kuninori Morimoto8e7875a2020-10-01 14:07:41 +0900791 ret = 0;
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900792err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300793 mutex_unlock(&rtd->card->pcm_mutex);
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900794pm_err:
Kuninori Morimotoe4b044f2021-03-15 09:57:37 +0900795 if (ret < 0) {
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900796 soc_pcm_clean(substream, 1);
Kuninori Morimotoe4b044f2021-03-15 09:57:37 +0900797 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
798 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000799
Liam Girdwoodddee6272011-06-09 14:45:53 +0100800 return ret;
801}
802
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800803static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200804{
805 /*
806 * Currently nothing to do for c2c links
807 * Since c2c links are internal nodes in the DAPM graph and
808 * don't interface with the outside world or application layer
809 * we don't have to do any special handling on close.
810 */
811}
812
Liam Girdwoodddee6272011-06-09 14:45:53 +0100813/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100814 * Called by ALSA when the PCM substream is prepared, can set format, sample
815 * rate, etc. This function is non atomic and can be called multiple times,
816 * it can refer to the runtime info.
817 */
818static int soc_pcm_prepare(struct snd_pcm_substream *substream)
819{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900820 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900821 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200822 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100823
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300824 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100825
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900826 ret = snd_soc_link_prepare(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900827 if (ret < 0)
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900828 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100829
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900830 ret = snd_soc_pcm_component_prepare(substream);
831 if (ret < 0)
832 goto out;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000833
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900834 ret = snd_soc_pcm_dai_prepare(substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +0900835 if (ret < 0)
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900836 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100837
838 /* cancel any delayed stream shutdown that is pending */
839 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600840 rtd->pop_wait) {
841 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100842 cancel_delayed_work(&rtd->delayed_work);
843 }
844
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000845 snd_soc_dapm_stream_event(rtd, substream->stream,
846 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100847
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900848 for_each_rtd_dais(rtd, i, dai)
849 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100850
851out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300852 mutex_unlock(&rtd->card->pcm_mutex);
Kuninori Morimotodab7eeb2021-03-15 09:57:48 +0900853
854 if (ret < 0)
855 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
856
Liam Girdwoodddee6272011-06-09 14:45:53 +0100857 return ret;
858}
859
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200860static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
861 unsigned int mask)
862{
863 struct snd_interval *interval;
864 int channels = hweight_long(mask);
865
866 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
867 interval->min = channels;
868 interval->max = channels;
869}
870
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900871static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimotoab494362020-09-29 13:31:19 +0900872{
873 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
874 struct snd_soc_dai *dai;
875 int i;
876
877 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
878
879 /* clear the corresponding DAIs parameters when going to be inactive */
880 for_each_rtd_dais(rtd, i, dai) {
881 int active = snd_soc_dai_stream_active(dai, substream->stream);
882
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900883 if (snd_soc_dai_active(dai) == 1)
884 soc_pcm_set_dai_params(dai, NULL);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900885
886 if (active == 1)
887 snd_soc_dai_digital_mute(dai, 1, substream->stream);
888 }
889
Ranjani Sridharana27b4212020-11-17 13:50:01 -0800890 /* run the stream event */
891 snd_soc_dapm_stream_stop(rtd, substream->stream);
892
Kuninori Morimotoab494362020-09-29 13:31:19 +0900893 /* free any machine hw params */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900894 snd_soc_link_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900895
896 /* free any component resources */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900897 snd_soc_pcm_component_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900898
899 /* now free hw params for the DAIs */
900 for_each_rtd_dais(rtd, i, dai) {
901 if (!snd_soc_dai_stream_valid(dai, substream->stream))
902 continue;
903
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900904 snd_soc_dai_hw_free(dai, substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900905 }
906
907 mutex_unlock(&rtd->card->pcm_mutex);
908 return 0;
909}
910
911/*
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900912 * Frees resources allocated by hw_params, can be called multiple times
913 */
914static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
915{
916 return soc_pcm_hw_clean(substream, 0);
917}
918
919/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100920 * Called by ALSA when the hardware params are set by application. This
921 * function can also be called multiple times and can allocate buffers
922 * (using snd_pcm_lib_* ). It's non-atomic.
923 */
924static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
925 struct snd_pcm_hw_params *params)
926{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900927 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800928 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000929 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100930 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300932 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800933
934 ret = soc_pcm_params_symmetry(substream, params);
935 if (ret)
936 goto out;
937
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900938 ret = snd_soc_link_hw_params(substream, params);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900939 if (ret < 0)
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900940 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100941
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900942 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200943 struct snd_pcm_hw_params codec_params;
944
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200945 /*
946 * Skip CODECs which don't support the current stream type,
947 * the idea being that if a CODEC is not used for the currently
948 * set up transfer direction, it should not need to be
949 * configured, especially since the configuration used might
950 * not even be supported by that CODEC. There may be cases
951 * however where a CODEC needs to be set up although it is
952 * actually not being used for the transfer, e.g. if a
953 * capture-only CODEC is acting as an LRCLK and/or BCLK master
954 * for the DAI link including a playback-only CODEC.
955 * If this becomes necessary, we will have to augment the
956 * machine driver setup with information on how to act, so
957 * we can do the right thing here.
958 */
959 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
960 continue;
961
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200962 /* copy params for each codec */
963 codec_params = *params;
964
965 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800966 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
967 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200968 soc_pcm_codec_params_fixup(&codec_params,
969 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800970
971 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
972 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200973 soc_pcm_codec_params_fixup(&codec_params,
974 codec_dai->rx_mask);
975
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900976 ret = snd_soc_dai_hw_params(codec_dai, substream,
977 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200978 if(ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900979 goto out;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200980
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900981 soc_pcm_set_dai_params(codec_dai, &codec_params);
Charles Keepax078a85f2019-01-31 13:30:18 +0000982 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100983 }
984
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900985 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800986 /*
987 * Skip CPUs which don't support the current stream
988 * type. See soc_pcm_init_runtime_hw() for more details
989 */
990 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
991 continue;
992
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800993 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
994 if (ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900995 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100996
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800997 /* store the parameters for each DAI */
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900998 soc_pcm_set_dai_params(cpu_dai, params);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800999 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1000 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001001
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001002 ret = snd_soc_pcm_component_hw_params(substream, params);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001004 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001005
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001006 if (ret < 0) {
Kuninori Morimoto4662c592020-09-29 13:32:01 +09001007 soc_pcm_hw_clean(substream, 1);
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001008 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1009 }
Kuninori Morimotob8135862017-10-11 01:37:23 +00001010
Liam Girdwoodddee6272011-06-09 14:45:53 +01001011 return ret;
1012}
1013
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001014static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1015{
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001016 int ret = -EINVAL, _ret = 0;
1017 int rollback = 0;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001018
1019 switch (cmd) {
1020 case SNDRV_PCM_TRIGGER_START:
1021 case SNDRV_PCM_TRIGGER_RESUME:
1022 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001023 ret = snd_soc_link_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001024 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001025 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001026
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001027 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001028 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001029 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001030
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001031 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1032start_err:
1033 if (ret < 0)
1034 rollback = 1;
1035 }
1036
1037 if (rollback) {
1038 _ret = ret;
1039 switch (cmd) {
1040 case SNDRV_PCM_TRIGGER_START:
1041 cmd = SNDRV_PCM_TRIGGER_STOP;
1042 break;
1043 case SNDRV_PCM_TRIGGER_RESUME:
1044 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1045 break;
1046 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1047 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1048 break;
1049 }
1050 }
1051
1052 switch (cmd) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001053 case SNDRV_PCM_TRIGGER_STOP:
1054 case SNDRV_PCM_TRIGGER_SUSPEND:
1055 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001056 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001057 if (ret < 0)
1058 break;
1059
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001060 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001061 if (ret < 0)
1062 break;
1063
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001064 ret = snd_soc_link_trigger(substream, cmd, rollback);
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001065 break;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001066 }
1067
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001068 if (_ret)
1069 ret = _ret;
1070
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001071 return ret;
1072}
1073
Liam Girdwoodddee6272011-06-09 14:45:53 +01001074/*
1075 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001076 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001077 * the runtime->delay will be updated accordingly.
1078 */
1079static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1080{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001081 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001082 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001083 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001084 struct snd_pcm_runtime *runtime = substream->runtime;
1085 snd_pcm_uframes_t offset = 0;
1086 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001087 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001088 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001089 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001090
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301091 /* clearing the previous total delay */
1092 runtime->delay = 0;
1093
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001094 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001095
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301096 /* base delay if assigned in pointer callback */
1097 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001098
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001099 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001100 cpu_delay = max(cpu_delay,
1101 snd_soc_dai_delay(cpu_dai, substream));
1102 }
1103 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001104
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001105 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001106 codec_delay = max(codec_delay,
1107 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001108 }
1109 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001110
Liam Girdwoodddee6272011-06-09 14:45:53 +01001111 runtime->delay = delay;
1112
1113 return offset;
1114}
1115
Liam Girdwood01d75842012-04-25 12:12:49 +01001116/* connect a FE and BE */
1117static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1118 struct snd_soc_pcm_runtime *be, int stream)
1119{
1120 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001121 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001122
1123 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001124 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001125 if (dpcm->be == be && dpcm->fe == fe)
1126 return 0;
1127 }
1128
1129 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1130 if (!dpcm)
1131 return -ENOMEM;
1132
1133 dpcm->be = be;
1134 dpcm->fe = fe;
1135 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1136 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001137 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001138 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1139 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001140 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001141
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001142 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001143 stream ? "capture" : "playback", fe->dai_link->name,
1144 stream ? "<-" : "->", be->dai_link->name);
1145
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001146 dpcm_create_debugfs_state(dpcm, stream);
1147
Liam Girdwood01d75842012-04-25 12:12:49 +01001148 return 1;
1149}
1150
1151/* reparent a BE onto another FE */
1152static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1153 struct snd_soc_pcm_runtime *be, int stream)
1154{
1155 struct snd_soc_dpcm *dpcm;
1156 struct snd_pcm_substream *fe_substream, *be_substream;
1157
1158 /* reparent if BE is connected to other FEs */
1159 if (!be->dpcm[stream].users)
1160 return;
1161
1162 be_substream = snd_soc_dpcm_get_substream(be, stream);
1163
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001164 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001165 if (dpcm->fe == fe)
1166 continue;
1167
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001168 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001169 stream ? "capture" : "playback",
1170 dpcm->fe->dai_link->name,
1171 stream ? "<-" : "->", dpcm->be->dai_link->name);
1172
1173 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1174 be_substream->runtime = fe_substream->runtime;
1175 break;
1176 }
1177}
1178
1179/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001180void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001181{
1182 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001183 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001184
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001185 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001186 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001187 stream ? "capture" : "playback",
1188 dpcm->be->dai_link->name);
1189
1190 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1191 continue;
1192
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001193 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001194 stream ? "capture" : "playback", fe->dai_link->name,
1195 stream ? "<-" : "->", dpcm->be->dai_link->name);
1196
1197 /* BEs still alive need new FE */
1198 dpcm_be_reparent(fe, dpcm->be, stream);
1199
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001200 dpcm_remove_debugfs_state(dpcm);
1201
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001202 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001203 list_del(&dpcm->list_be);
1204 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001205 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001206 kfree(dpcm);
1207 }
1208}
1209
1210/* get BE for DAI widget and stream */
1211static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1212 struct snd_soc_dapm_widget *widget, int stream)
1213{
1214 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001215 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001216 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001217 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001218
Liam Girdwood3c146462018-03-14 20:43:51 +00001219 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1220
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001221 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001222
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001223 if (!be->dai_link->no_pcm)
1224 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001225
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001226 for_each_rtd_dais(be, i, dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001227 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001228
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001229 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1230 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001231
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001232 if (w == widget)
1233 return be;
1234 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 }
1236
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001237 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001238 return NULL;
1239}
1240
Liam Girdwood01d75842012-04-25 12:12:49 +01001241static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1242 struct snd_soc_dapm_widget *widget)
1243{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001244 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001245 int i;
1246
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001247 for_each_dapm_widgets(list, i, w)
1248 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001249 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001250
1251 return 0;
1252}
1253
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001254static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1255 enum snd_soc_dapm_direction dir)
1256{
1257 struct snd_soc_card *card = widget->dapm->card;
1258 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001259 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001260
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001261 /* adjust dir to stream */
1262 if (dir == SND_SOC_DAPM_DIR_OUT)
1263 stream = SNDRV_PCM_STREAM_PLAYBACK;
1264 else
1265 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001266
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001267 rtd = dpcm_get_be(card, widget, stream);
1268 if (rtd)
1269 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001270
1271 return false;
1272}
1273
Liam Girdwood23607022014-01-17 17:03:55 +00001274int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001275 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001276{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001277 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood01d75842012-04-25 12:12:49 +01001278 int paths;
1279
Bard Liao6e1276a2020-02-25 21:39:16 +08001280 if (fe->num_cpus > 1) {
1281 dev_err(fe->dev,
1282 "%s doesn't support Multi CPU yet\n", __func__);
1283 return -EINVAL;
1284 }
1285
Liam Girdwood01d75842012-04-25 12:12:49 +01001286 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001287 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Sameer Pujaraa293772020-11-02 20:40:09 +05301288 fe->card->component_chaining ?
1289 NULL : dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001290
Kuninori Morimotod479f002021-03-15 09:57:52 +09001291 if (paths > 0)
1292 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001293 stream ? "capture" : "playback");
Kuninori Morimotod479f002021-03-15 09:57:52 +09001294 else if (paths == 0)
1295 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1296 stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001297
Liam Girdwood01d75842012-04-25 12:12:49 +01001298 return paths;
1299}
1300
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001301void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1302{
1303 snd_soc_dapm_dai_free_widgets(list);
1304}
1305
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001306static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1307 struct snd_soc_dapm_widget_list *list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001308{
Liam Girdwood01d75842012-04-25 12:12:49 +01001309 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001310 struct snd_soc_dai *dai;
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001311 unsigned int i;
1312
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001313 /* is there a valid DAI widget for this BE */
1314 for_each_rtd_dais(dpcm->be, i, dai) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001315 widget = snd_soc_dai_get_widget(dai, stream);
1316
1317 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001318 * The BE is pruned only if none of the dai
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001319 * widgets are in the active list.
1320 */
1321 if (widget && widget_in_list(list, widget))
1322 return true;
1323 }
1324
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001325 return false;
1326}
1327
1328static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1329 struct snd_soc_dapm_widget_list **list_)
1330{
1331 struct snd_soc_dpcm *dpcm;
Liam Girdwood01d75842012-04-25 12:12:49 +01001332 int prune = 0;
1333
1334 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001335 for_each_dpcm_be(fe, stream, dpcm) {
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001336 if (dpcm_be_is_active(dpcm, stream, *list_))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001337 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001338
Liam Girdwood103d84a2012-11-19 14:39:15 +00001339 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001340 stream ? "capture" : "playback",
1341 dpcm->be->dai_link->name, fe->dai_link->name);
1342 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001343 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001344 prune++;
1345 }
1346
Liam Girdwood103d84a2012-11-19 14:39:15 +00001347 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001348 return prune;
1349}
1350
1351static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1352 struct snd_soc_dapm_widget_list **list_)
1353{
1354 struct snd_soc_card *card = fe->card;
1355 struct snd_soc_dapm_widget_list *list = *list_;
1356 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001357 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001358 int i, new = 0, err;
1359
1360 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001361 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001362
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001363 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001364 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001365 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1366 continue;
1367 break;
Mark Brown46162742013-06-05 19:36:11 +01001368 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001369 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1370 continue;
Mark Brown46162742013-06-05 19:36:11 +01001371 break;
1372 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001373 continue;
Mark Brown46162742013-06-05 19:36:11 +01001374 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001375
1376 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001377 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001378 if (!be) {
Shengjiu Wangb6eabd22021-02-08 16:12:45 +08001379 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1380 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 continue;
1382 }
1383
Liam Girdwood01d75842012-04-25 12:12:49 +01001384 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001385 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001386 continue;
1387
1388 /* newly connected FE and BE */
1389 err = dpcm_be_connect(fe, be, stream);
1390 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001391 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001392 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 break;
1394 } else if (err == 0) /* already connected */
1395 continue;
1396
1397 /* new */
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001398 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001399 new++;
1400 }
1401
Liam Girdwood103d84a2012-11-19 14:39:15 +00001402 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001403 return new;
1404}
1405
1406/*
1407 * Find the corresponding BE DAIs that source or sink audio to this
1408 * FE substream.
1409 */
Liam Girdwood23607022014-01-17 17:03:55 +00001410int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001411 int stream, struct snd_soc_dapm_widget_list **list, int new)
1412{
1413 if (new)
1414 return dpcm_add_paths(fe, stream, list);
1415 else
1416 return dpcm_prune_paths(fe, stream, list);
1417}
1418
Liam Girdwood23607022014-01-17 17:03:55 +00001419void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001420{
1421 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001422 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001423
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001424 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001425 for_each_dpcm_be(fe, stream, dpcm)
Kuninori Morimotoa7e204442020-12-11 14:55:22 +09001426 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001427 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001428}
1429
Kuninori Morimoto531590b2021-03-09 10:08:17 +09001430void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1431 int do_hw_free, struct snd_soc_dpcm *last)
Liam Girdwood01d75842012-04-25 12:12:49 +01001432{
1433 struct snd_soc_dpcm *dpcm;
1434
1435 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001436 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001437 struct snd_soc_pcm_runtime *be = dpcm->be;
1438 struct snd_pcm_substream *be_substream =
1439 snd_soc_dpcm_get_substream(be, stream);
1440
Kuninori Morimoto531590b2021-03-09 10:08:17 +09001441 if (dpcm == last)
1442 return;
1443
1444 /* is this op for this BE ? */
1445 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1446 continue;
1447
Kuninori Morimoto1db19c12021-03-09 10:08:08 +09001448 if (be->dpcm[stream].users == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001449 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001450 stream ? "capture" : "playback",
1451 be->dpcm[stream].state);
Kuninori Morimoto1db19c12021-03-09 10:08:08 +09001452 continue;
1453 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001454
1455 if (--be->dpcm[stream].users != 0)
1456 continue;
1457
Kuninori Morimoto531590b2021-03-09 10:08:17 +09001458 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1459 if (!do_hw_free)
1460 continue;
1461
1462 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1463 soc_pcm_hw_free(be_substream);
1464 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1465 }
1466 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001467
1468 soc_pcm_close(be_substream);
1469 be_substream->runtime = NULL;
1470 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1471 }
1472}
1473
Liam Girdwood23607022014-01-17 17:03:55 +00001474int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001475{
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001476 struct snd_soc_pcm_runtime *be;
Liam Girdwood01d75842012-04-25 12:12:49 +01001477 struct snd_soc_dpcm *dpcm;
1478 int err, count = 0;
1479
1480 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001481 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001482 struct snd_pcm_substream *be_substream;
Liam Girdwood01d75842012-04-25 12:12:49 +01001483
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001484 be = dpcm->be;
1485 be_substream = snd_soc_dpcm_get_substream(be, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001486
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001487 if (!be_substream) {
1488 dev_err(be->dev, "ASoC: no backend %s stream\n",
1489 stream ? "capture" : "playback");
1490 continue;
1491 }
1492
Liam Girdwood01d75842012-04-25 12:12:49 +01001493 /* is this op for this BE ? */
1494 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1495 continue;
1496
1497 /* first time the dpcm is open ? */
Kuninori Morimoto1db19c12021-03-09 10:08:08 +09001498 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001499 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001500 stream ? "capture" : "playback",
1501 be->dpcm[stream].state);
Kuninori Morimoto1db19c12021-03-09 10:08:08 +09001502 continue;
1503 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001504
1505 if (be->dpcm[stream].users++ != 0)
1506 continue;
1507
1508 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1509 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1510 continue;
1511
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001512 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1513 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001514
1515 be_substream->runtime = be->dpcm[stream].runtime;
1516 err = soc_pcm_open(be_substream);
1517 if (err < 0) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001518 be->dpcm[stream].users--;
1519 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001520 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001521 stream ? "capture" : "playback",
1522 be->dpcm[stream].state);
1523
1524 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1525 goto unwind;
1526 }
1527
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1529 count++;
1530 }
1531
1532 return count;
1533
1534unwind:
Kuninori Morimoto531590b2021-03-09 10:08:17 +09001535 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01001536
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001537 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1538 __func__, be->dai_link->name, err);
1539
Liam Girdwood01d75842012-04-25 12:12:49 +01001540 return err;
1541}
1542
Kuninori Morimoto5f538982021-02-22 09:47:26 +09001543static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1544{
1545 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1546 struct snd_pcm_runtime *runtime = substream->runtime;
1547 struct snd_pcm_hardware *hw = &runtime->hw;
1548 struct snd_soc_dai *dai;
1549 int stream = substream->stream;
1550 int i;
1551
1552 soc_pcm_hw_init(hw);
1553
1554 for_each_rtd_cpu_dais(fe, i, dai) {
1555 struct snd_soc_pcm_stream *cpu_stream;
1556
1557 /*
1558 * Skip CPUs which don't support the current stream
1559 * type. See soc_pcm_init_runtime_hw() for more details
1560 */
1561 if (!snd_soc_dai_stream_valid(dai, stream))
1562 continue;
1563
1564 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1565
1566 soc_pcm_hw_update_rate(hw, cpu_stream);
1567 soc_pcm_hw_update_chan(hw, cpu_stream);
1568 soc_pcm_hw_update_format(hw, cpu_stream);
1569 }
1570
1571}
1572
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001573static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001574{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001575 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001576 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001577 struct snd_pcm_hardware *hw = &runtime->hw;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001578 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001579 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001580 int stream = substream->stream;
1581
1582 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001583 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001584
1585 /*
1586 * It returns merged BE codec format
1587 * if FE want to use it (= dpcm_merged_format)
1588 */
1589
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001590 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001591 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001592 struct snd_soc_pcm_stream *codec_stream;
1593 int i;
1594
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001595 for_each_rtd_codec_dais(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001596 /*
1597 * Skip CODECs which don't support the current stream
1598 * type. See soc_pcm_init_runtime_hw() for more details
1599 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001600 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001601 continue;
1602
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001603 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001604
Kuninori Morimotodebc71f2021-02-04 08:52:04 +09001605 soc_pcm_hw_update_format(hw, codec_stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001606 }
1607 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001608}
1609
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001610static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001611{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001612 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001613 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001614 struct snd_pcm_hardware *hw = &runtime->hw;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001615 struct snd_soc_dpcm *dpcm;
1616 int stream = substream->stream;
1617
1618 if (!fe->dai_link->dpcm_merged_chan)
1619 return;
1620
Jiada Wangf4c277b2018-06-20 18:25:20 +09001621 /*
1622 * It returns merged BE codec channel;
1623 * if FE want to use it (= dpcm_merged_chan)
1624 */
1625
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001626 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001627 struct snd_soc_pcm_runtime *be = dpcm->be;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001628 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001629 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001630 struct snd_soc_dai *dai;
1631 int i;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001632
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001633 for_each_rtd_cpu_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001634 /*
1635 * Skip CPUs which don't support the current stream
1636 * type. See soc_pcm_init_runtime_hw() for more details
1637 */
1638 if (!snd_soc_dai_stream_valid(dai, stream))
1639 continue;
1640
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001641 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001642
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +09001643 soc_pcm_hw_update_chan(hw, cpu_stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001644 }
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001645
1646 /*
1647 * chan min/max cannot be enforced if there are multiple CODEC
1648 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1649 */
1650 if (be->num_codecs == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001651 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001652
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +09001653 soc_pcm_hw_update_chan(hw, codec_stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001654 }
1655 }
1656}
1657
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001658static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001659{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001660 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimoto1b8cb122021-02-22 09:47:34 +09001661 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto4b260f42021-01-22 10:13:48 +09001662 struct snd_pcm_hardware *hw = &runtime->hw;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001663 struct snd_soc_dpcm *dpcm;
1664 int stream = substream->stream;
1665
1666 if (!fe->dai_link->dpcm_merged_rate)
1667 return;
1668
1669 /*
1670 * It returns merged BE codec channel;
1671 * if FE want to use it (= dpcm_merged_chan)
1672 */
1673
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001674 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001675 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001676 struct snd_soc_pcm_stream *pcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001677 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001678 int i;
1679
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001680 for_each_rtd_dais(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001681 /*
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001682 * Skip DAIs which don't support the current stream
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001683 * type. See soc_pcm_init_runtime_hw() for more details
1684 */
1685 if (!snd_soc_dai_stream_valid(dai, stream))
1686 continue;
1687
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001688 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001689
Kuninori Morimotof6c04af2021-02-04 08:50:31 +09001690 soc_pcm_hw_update_rate(hw, pcm);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001691 }
1692 }
1693}
1694
PC Liao906c7d62015-12-11 11:33:51 +08001695static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1696 int stream)
1697{
1698 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001699 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001700 struct snd_soc_dai *fe_cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001701 int err;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001702 int i;
PC Liao906c7d62015-12-11 11:33:51 +08001703
1704 /* apply symmetry for FE */
Kuninori Morimoto68cbc552021-03-09 10:07:57 +09001705 soc_pcm_update_symmetry(fe_substream);
PC Liao906c7d62015-12-11 11:33:51 +08001706
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001707 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001708 /* Symmetry only applies if we've got an active stream. */
Kuninori Morimotof8fc9ec2021-03-09 10:07:42 +09001709 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1710 if (err < 0)
Kuninori Morimotobbd2bac2021-03-15 09:58:02 +09001711 goto error;
PC Liao906c7d62015-12-11 11:33:51 +08001712 }
1713
1714 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001715 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001716 struct snd_soc_pcm_runtime *be = dpcm->be;
1717 struct snd_pcm_substream *be_substream =
1718 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001719 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001720 struct snd_soc_dai *dai;
PC Liao906c7d62015-12-11 11:33:51 +08001721
Jerome Brunet6246f282019-04-01 15:03:54 +02001722 /* A backend may not have the requested substream */
1723 if (!be_substream)
1724 continue;
1725
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001726 rtd = asoc_substream_to_rtd(be_substream);
Jeeja KPf1176612016-09-06 14:17:55 +05301727 if (rtd->dai_link->be_hw_params_fixup)
1728 continue;
1729
Kuninori Morimoto68cbc552021-03-09 10:07:57 +09001730 soc_pcm_update_symmetry(be_substream);
PC Liao906c7d62015-12-11 11:33:51 +08001731
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 Morimotof8fc9ec2021-03-09 10:07:42 +09001734 err = soc_pcm_apply_symmetry(fe_substream, dai);
1735 if (err < 0)
Kuninori Morimotobbd2bac2021-03-15 09:58:02 +09001736 goto error;
PC Liao906c7d62015-12-11 11:33:51 +08001737 }
1738 }
Kuninori Morimotobbd2bac2021-03-15 09:58:02 +09001739error:
1740 if (err < 0)
1741 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
PC Liao906c7d62015-12-11 11:33:51 +08001742
Kuninori Morimotobbd2bac2021-03-15 09:58:02 +09001743 return err;
PC Liao906c7d62015-12-11 11:33:51 +08001744}
1745
Liam Girdwood01d75842012-04-25 12:12:49 +01001746static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1747{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001748 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001749 int stream = fe_substream->stream, ret = 0;
1750
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001751 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001752
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001753 ret = dpcm_be_dai_startup(fe, stream);
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001754 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01001755 goto be_err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001756
Liam Girdwood103d84a2012-11-19 14:39:15 +00001757 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001758
1759 /* start the DAI frontend */
1760 ret = soc_pcm_open(fe_substream);
Kuninori Morimotoe4b044f2021-03-15 09:57:37 +09001761 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01001762 goto unwind;
Liam Girdwood01d75842012-04-25 12:12:49 +01001763
1764 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1765
Kuninori Morimoto4fe28462021-02-22 09:47:36 +09001766 dpcm_runtime_setup_fe(fe_substream);
1767
1768 dpcm_runtime_setup_be_format(fe_substream);
1769 dpcm_runtime_setup_be_chan(fe_substream);
1770 dpcm_runtime_setup_be_rate(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001771
PC Liao906c7d62015-12-11 11:33:51 +08001772 ret = dpcm_apply_symmetry(fe_substream, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001773
1774unwind:
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09001775 if (ret < 0)
1776 dpcm_be_dai_startup_unwind(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001777be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001778 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto06aaeb82021-03-15 09:58:13 +09001779
1780 if (ret < 0)
1781 dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1782
Liam Girdwood01d75842012-04-25 12:12:49 +01001783 return ret;
1784}
1785
Liam Girdwood01d75842012-04-25 12:12:49 +01001786static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1787{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001788 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001789 int stream = substream->stream;
1790
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001791 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001792
1793 /* shutdown the BEs */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001794 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001795
Liam Girdwood103d84a2012-11-19 14:39:15 +00001796 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001797
1798 /* now shutdown the frontend */
1799 soc_pcm_close(substream);
1800
Ranjani Sridharanbb9dd3c2020-12-02 11:33:43 -08001801 /* run the stream stop event */
1802 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1803
Liam Girdwood01d75842012-04-25 12:12:49 +01001804 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001805 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001806 return 0;
1807}
1808
Kuninori Morimotof52366e2021-03-15 09:58:32 +09001809void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001810{
1811 struct snd_soc_dpcm *dpcm;
1812
1813 /* only hw_params backends that are either sinks or sources
1814 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001815 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001816
1817 struct snd_soc_pcm_runtime *be = dpcm->be;
1818 struct snd_pcm_substream *be_substream =
1819 snd_soc_dpcm_get_substream(be, stream);
1820
1821 /* is this op for this BE ? */
1822 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1823 continue;
1824
1825 /* only free hw when no longer used - check all FEs */
1826 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1827 continue;
1828
Qiao Zhou36fba622014-12-03 10:13:43 +08001829 /* do not free hw if this BE is used by other FE */
1830 if (be->dpcm[stream].users > 1)
1831 continue;
1832
Liam Girdwood01d75842012-04-25 12:12:49 +01001833 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1834 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1835 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001836 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301837 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1838 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001839 continue;
1840
Liam Girdwood103d84a2012-11-19 14:39:15 +00001841 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001842 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001843
1844 soc_pcm_hw_free(be_substream);
1845
1846 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1847 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001848}
1849
Mark Brown45c0a182012-05-09 21:46:27 +01001850static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001851{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001852 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Kuninori Morimotof52366e2021-03-15 09:58:32 +09001853 int stream = substream->stream;
Liam Girdwood01d75842012-04-25 12:12:49 +01001854
1855 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001856 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001857
Liam Girdwood103d84a2012-11-19 14:39:15 +00001858 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001859
1860 /* call hw_free on the frontend */
Kuninori Morimotoe20c9c42021-03-15 09:58:28 +09001861 soc_pcm_hw_free(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001862
1863 /* only hw_params backends that are either sinks or sources
1864 * to this frontend DAI */
Kuninori Morimotof52366e2021-03-15 09:58:32 +09001865 dpcm_be_dai_hw_free(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001866
1867 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001868 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001869
1870 mutex_unlock(&fe->card->mutex);
1871 return 0;
1872}
1873
Liam Girdwood23607022014-01-17 17:03:55 +00001874int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001875{
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001876 struct snd_soc_pcm_runtime *be;
1877 struct snd_pcm_substream *be_substream;
Liam Girdwood01d75842012-04-25 12:12:49 +01001878 struct snd_soc_dpcm *dpcm;
1879 int ret;
1880
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001881 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001882 be = dpcm->be;
1883 be_substream = snd_soc_dpcm_get_substream(be, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001884
1885 /* is this op for this BE ? */
1886 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1887 continue;
1888
Liam Girdwood01d75842012-04-25 12:12:49 +01001889 /* copy params for each dpcm */
1890 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1891 sizeof(struct snd_pcm_hw_params));
1892
1893 /* perform any hw_params fixups */
Kuninori Morimoto0cbbf8a2020-05-25 09:57:36 +09001894 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1895 if (ret < 0)
1896 goto unwind;
Liam Girdwood01d75842012-04-25 12:12:49 +01001897
Libin Yangae061d22019-04-19 09:53:12 +08001898 /* copy the fixed-up hw params for BE dai */
1899 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1900 sizeof(struct snd_pcm_hw_params));
1901
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001902 /* only allow hw_params() if no connected FEs are running */
1903 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1904 continue;
1905
1906 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1907 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1908 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1909 continue;
1910
1911 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001912 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00001913
Liam Girdwood01d75842012-04-25 12:12:49 +01001914 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001915 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01001916 goto unwind;
Liam Girdwood01d75842012-04-25 12:12:49 +01001917
1918 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1919 }
1920 return 0;
1921
1922unwind:
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001923 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1924 __func__, be->dai_link->name, ret);
1925
Liam Girdwood01d75842012-04-25 12:12:49 +01001926 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001927 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001928 be = dpcm->be;
1929 be_substream = snd_soc_dpcm_get_substream(be, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001930
1931 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1932 continue;
1933
1934 /* only allow hw_free() if no connected FEs are running */
1935 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1936 continue;
1937
1938 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1941 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1942 continue;
1943
1944 soc_pcm_hw_free(be_substream);
1945 }
1946
1947 return ret;
1948}
1949
Mark Brown45c0a182012-05-09 21:46:27 +01001950static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1951 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001952{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001953 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001954 int ret, stream = substream->stream;
1955
1956 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001957 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001958
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001959 memcpy(&fe->dpcm[stream].hw_params, params,
Liam Girdwood01d75842012-04-25 12:12:49 +01001960 sizeof(struct snd_pcm_hw_params));
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09001961 ret = dpcm_be_dai_hw_params(fe, stream);
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001962 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01001963 goto out;
Liam Girdwood01d75842012-04-25 12:12:49 +01001964
Liam Girdwood103d84a2012-11-19 14:39:15 +00001965 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001966 fe->dai_link->name, params_rate(params),
1967 params_channels(params), params_format(params));
1968
1969 /* call hw_params on the frontend */
1970 ret = soc_pcm_hw_params(substream, params);
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001971 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01001972 dpcm_be_dai_hw_free(fe, stream);
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001973 else
Liam Girdwood01d75842012-04-25 12:12:49 +01001974 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1975
1976out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001977 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001978 mutex_unlock(&fe->card->mutex);
Kuninori Morimoto33b6b942021-03-15 09:58:18 +09001979
1980 if (ret < 0)
1981 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
1982
Liam Girdwood01d75842012-04-25 12:12:49 +01001983 return ret;
1984}
1985
Liam Girdwood23607022014-01-17 17:03:55 +00001986int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001987 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001988{
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09001989 struct snd_soc_pcm_runtime *be;
Liam Girdwood01d75842012-04-25 12:12:49 +01001990 struct snd_soc_dpcm *dpcm;
1991 int ret = 0;
1992
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001993 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09001994 struct snd_pcm_substream *be_substream;
Liam Girdwood01d75842012-04-25 12:12:49 +01001995
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09001996 be = dpcm->be;
1997 be_substream = snd_soc_dpcm_get_substream(be, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001998
1999 /* is this op for this BE ? */
2000 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2001 continue;
2002
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002003 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2004 be->dai_link->name, cmd);
2005
Liam Girdwood01d75842012-04-25 12:12:49 +01002006 switch (cmd) {
2007 case SNDRV_PCM_TRIGGER_START:
2008 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
이경택21fca8b2020-04-01 10:04:21 +09002009 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2010 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002011 continue;
2012
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002013 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002014 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002015 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002016
2017 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2018 break;
2019 case SNDRV_PCM_TRIGGER_RESUME:
2020 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2021 continue;
2022
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002023 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002024 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002025 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002026
2027 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2028 break;
2029 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2030 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2031 continue;
2032
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002033 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002034 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002035 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002036
2037 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2038 break;
2039 case SNDRV_PCM_TRIGGER_STOP:
이경택21fca8b2020-04-01 10:04:21 +09002040 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2041 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002042 continue;
2043
2044 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2045 continue;
2046
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002047 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002048 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002049 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002050
2051 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2052 break;
2053 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002054 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002055 continue;
2056
2057 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2058 continue;
2059
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002060 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002061 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002062 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002063
2064 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2065 break;
2066 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2067 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2068 continue;
2069
2070 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2071 continue;
2072
Kuninori Morimotoa9faca12020-12-01 08:51:25 +09002073 ret = soc_pcm_trigger(be_substream, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002074 if (ret)
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002075 goto end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002076
2077 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2078 break;
2079 }
2080 }
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002081end:
2082 if (ret < 0)
2083 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2084 __func__, be->dai_link->name, ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002085 return ret;
2086}
2087EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2088
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002089static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2090 int cmd, bool fe_first)
2091{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002092 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002093 int ret;
2094
2095 /* call trigger on the frontend before the backend. */
2096 if (fe_first) {
2097 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2098 fe->dai_link->name, cmd);
2099
2100 ret = soc_pcm_trigger(substream, cmd);
2101 if (ret < 0)
2102 return ret;
2103
2104 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2105 return ret;
2106 }
2107
2108 /* call trigger on the frontend after the backend. */
2109 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2110 if (ret < 0)
2111 return ret;
2112
2113 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2114 fe->dai_link->name, cmd);
2115
2116 ret = soc_pcm_trigger(substream, cmd);
2117
2118 return ret;
2119}
2120
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002121static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002122{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002123 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002124 int stream = substream->stream;
2125 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002126 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2127
2128 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2129
2130 switch (trigger) {
2131 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002132 switch (cmd) {
2133 case SNDRV_PCM_TRIGGER_START:
2134 case SNDRV_PCM_TRIGGER_RESUME:
2135 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Cezary Rojewski4c22b802020-10-26 11:01:29 +01002136 case SNDRV_PCM_TRIGGER_DRAIN:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002137 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2138 break;
2139 case SNDRV_PCM_TRIGGER_STOP:
2140 case SNDRV_PCM_TRIGGER_SUSPEND:
2141 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2142 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2143 break;
2144 default:
2145 ret = -EINVAL;
2146 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002147 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002148 break;
2149 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002150 switch (cmd) {
2151 case SNDRV_PCM_TRIGGER_START:
2152 case SNDRV_PCM_TRIGGER_RESUME:
2153 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Cezary Rojewski4c22b802020-10-26 11:01:29 +01002154 case SNDRV_PCM_TRIGGER_DRAIN:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002155 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2156 break;
2157 case SNDRV_PCM_TRIGGER_STOP:
2158 case SNDRV_PCM_TRIGGER_SUSPEND:
2159 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2160 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2161 break;
2162 default:
2163 ret = -EINVAL;
2164 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002165 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002166 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002167 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2168 /* bespoke trigger() - handles both FE and BEs */
2169
Liam Girdwood103d84a2012-11-19 14:39:15 +00002170 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002171 fe->dai_link->name, cmd);
2172
Kuninori Morimoto308193582020-04-24 08:15:09 +09002173 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002174 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002175 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002176 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002177 fe->dai_link->name);
2178 ret = -EINVAL;
2179 goto out;
2180 }
2181
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002182 if (ret < 0) {
2183 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2184 cmd, ret);
2185 goto out;
2186 }
2187
Liam Girdwood01d75842012-04-25 12:12:49 +01002188 switch (cmd) {
2189 case SNDRV_PCM_TRIGGER_START:
2190 case SNDRV_PCM_TRIGGER_RESUME:
2191 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2192 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2193 break;
2194 case SNDRV_PCM_TRIGGER_STOP:
2195 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002196 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2197 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002198 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2199 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2200 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002201 }
2202
2203out:
2204 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2205 return ret;
2206}
2207
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002208static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2209{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002210 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002211 int stream = substream->stream;
2212
2213 /* if FE's runtime_update is already set, we're in race;
2214 * process this trigger later at exit
2215 */
2216 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2217 fe->dpcm[stream].trigger_pending = cmd + 1;
2218 return 0; /* delayed, assuming it's successful */
2219 }
2220
2221 /* we're alone, let's trigger */
2222 return dpcm_fe_dai_do_trigger(substream, cmd);
2223}
2224
Liam Girdwood23607022014-01-17 17:03:55 +00002225int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002226{
2227 struct snd_soc_dpcm *dpcm;
2228 int ret = 0;
2229
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002230 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002231
2232 struct snd_soc_pcm_runtime *be = dpcm->be;
2233 struct snd_pcm_substream *be_substream =
2234 snd_soc_dpcm_get_substream(be, stream);
2235
2236 /* is this op for this BE ? */
2237 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2238 continue;
2239
2240 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002241 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002242 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2243 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002244 continue;
2245
Liam Girdwood103d84a2012-11-19 14:39:15 +00002246 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002247 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002248
2249 ret = soc_pcm_prepare(be_substream);
Kuninori Morimotodab7eeb2021-03-15 09:57:48 +09002250 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01002251 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002252
2253 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2254 }
Kuninori Morimoto273db972021-03-15 09:58:22 +09002255
2256 if (ret < 0)
2257 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2258
Liam Girdwood01d75842012-04-25 12:12:49 +01002259 return ret;
2260}
2261
Mark Brown45c0a182012-05-09 21:46:27 +01002262static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002263{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002264 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002265 int stream = substream->stream, ret = 0;
2266
2267 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2268
Liam Girdwood103d84a2012-11-19 14:39:15 +00002269 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002270
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002271 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002272
2273 /* there is no point preparing this FE if there are no BEs */
2274 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002275 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002276 fe->dai_link->name);
2277 ret = -EINVAL;
2278 goto out;
2279 }
2280
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002281 ret = dpcm_be_dai_prepare(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002282 if (ret < 0)
2283 goto out;
2284
2285 /* call prepare on the frontend */
2286 ret = soc_pcm_prepare(substream);
Kuninori Morimotodab7eeb2021-03-15 09:57:48 +09002287 if (ret < 0)
Liam Girdwood01d75842012-04-25 12:12:49 +01002288 goto out;
Liam Girdwood01d75842012-04-25 12:12:49 +01002289
Liam Girdwood01d75842012-04-25 12:12:49 +01002290 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2291
2292out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002293 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002294 mutex_unlock(&fe->card->mutex);
2295
Kuninori Morimoto273db972021-03-15 09:58:22 +09002296 if (ret < 0)
2297 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2298
Liam Girdwood01d75842012-04-25 12:12:49 +01002299 return ret;
2300}
2301
Liam Girdwood618dae12012-04-25 12:12:51 +01002302static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2303{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002304 struct snd_pcm_substream *substream =
2305 snd_soc_dpcm_get_substream(fe, stream);
2306 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002307 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002308
Liam Girdwood103d84a2012-11-19 14:39:15 +00002309 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002310 stream ? "capture" : "playback", fe->dai_link->name);
2311
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002312 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2313 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002314 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002315 fe->dai_link->name);
2316
Kuninori Morimoto308193582020-04-24 08:15:09 +09002317 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002318 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002319 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002320 fe->dai_link->name);
2321
2322 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002323 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002324
Kuninori Morimotof52366e2021-03-15 09:58:32 +09002325 dpcm_be_dai_hw_free(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002326
Kuninori Morimoto531590b2021-03-09 10:08:17 +09002327 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002328
2329 /* run the stream event for each BE */
2330 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2331
Kuninori Morimoto81c82a92021-03-15 09:58:08 +09002332 if (err < 0)
2333 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2334
2335 return err;
Liam Girdwood618dae12012-04-25 12:12:51 +01002336}
2337
2338static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2339{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002340 struct snd_pcm_substream *substream =
2341 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002342 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002343 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Souptick Joarder4eeed5f2021-01-09 09:15:01 +05302344 int ret = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002345 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002346
Liam Girdwood103d84a2012-11-19 14:39:15 +00002347 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002348 stream ? "capture" : "playback", fe->dai_link->name);
2349
2350 /* Only start the BE if the FE is ready */
2351 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
朱灿灿2c138282020-12-25 16:42:46 +08002352 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2353 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2354 fe->dai_link->name, fe->dpcm[stream].state);
Dan Carpentere91b65b2021-01-11 12:50:21 +03002355 ret = -EINVAL;
朱灿灿2c138282020-12-25 16:42:46 +08002356 goto disconnect;
2357 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002358
2359 /* startup must always be called for new BEs */
2360 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002361 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002362 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002363
2364 /* keep going if FE state is > open */
2365 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2366 return 0;
2367
2368 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002369 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002370 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002371
2372 /* keep going if FE state is > hw_params */
2373 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2374 return 0;
2375
Liam Girdwood618dae12012-04-25 12:12:51 +01002376 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002377 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002378 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002379
2380 /* run the stream event for each BE */
2381 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2382
2383 /* keep going if FE state is > prepare */
2384 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2385 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2386 return 0;
2387
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002388 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2389 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002390 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002391 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002392
Kuninori Morimoto308193582020-04-24 08:15:09 +09002393 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
Kuninori Morimoto62462e02021-03-15 09:58:37 +09002394 if (ret < 0)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002395 goto hw_free;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002396 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002397 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002398 fe->dai_link->name);
2399
2400 ret = dpcm_be_dai_trigger(fe, stream,
2401 SNDRV_PCM_TRIGGER_START);
Kuninori Morimotodb3aa392021-03-15 09:57:57 +09002402 if (ret < 0)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002403 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002404 }
2405
2406 return 0;
2407
2408hw_free:
2409 dpcm_be_dai_hw_free(fe, stream);
2410close:
2411 dpcm_be_dai_shutdown(fe, stream);
2412disconnect:
朱灿灿2c138282020-12-25 16:42:46 +08002413 /* disconnect any pending BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002414 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002415 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002416 struct snd_soc_pcm_runtime *be = dpcm->be;
朱灿灿2c138282020-12-25 16:42:46 +08002417
2418 /* is this op for this BE ? */
2419 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2420 continue;
2421
2422 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2423 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2424 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
Liam Girdwood618dae12012-04-25 12:12:51 +01002425 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002426 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002427
Kuninori Morimoto81c82a92021-03-15 09:58:08 +09002428 if (ret < 0)
2429 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2430
Liam Girdwood618dae12012-04-25 12:12:51 +01002431 return ret;
2432}
2433
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002434static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2435{
2436 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002437 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002438 int count, paths;
2439
Pierre-Louis Bossart96bf62f2020-06-12 15:35:07 -05002440 if (!fe->dai_link->dynamic)
2441 return 0;
2442
Bard Liao6e1276a2020-02-25 21:39:16 +08002443 if (fe->num_cpus > 1) {
2444 dev_err(fe->dev,
2445 "%s doesn't support Multi CPU yet\n", __func__);
2446 return -EINVAL;
2447 }
2448
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002449 /* only check active links */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002450 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002451 return 0;
2452
2453 /* DAPM sync will call this to update DSP paths */
2454 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2455 new ? "new" : "old", fe->dai_link->name);
2456
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002457 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002458
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002459 /* skip if FE doesn't have playback/capture capability */
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002460 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2461 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002462 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002463
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002464 /* skip if FE isn't currently playing/capturing */
Kuninori Morimotob3dea622020-05-15 09:46:51 +09002465 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2466 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002467 continue;
2468
2469 paths = dpcm_path_get(fe, stream, &list);
Kuninori Morimotod479f002021-03-15 09:57:52 +09002470 if (paths < 0)
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002471 return paths;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002472
2473 /* update any playback/capture paths */
2474 count = dpcm_process_paths(fe, stream, &list, new);
2475 if (count) {
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002476 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002477 if (new)
Kuninori Morimoto81c82a92021-03-15 09:58:08 +09002478 dpcm_run_update_startup(fe, stream);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002479 else
Kuninori Morimoto81c82a92021-03-15 09:58:08 +09002480 dpcm_run_update_shutdown(fe, stream);
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002481 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002482
2483 dpcm_clear_pending_state(fe, stream);
2484 dpcm_be_disconnect(fe, stream);
2485 }
2486
2487 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002488 }
2489
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002490 return 0;
2491}
2492
Liam Girdwood618dae12012-04-25 12:12:51 +01002493/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2494 * any DAI links.
2495 */
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002496int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002497{
Mengdong Lin1a497982015-11-18 02:34:11 -05002498 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002499 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002500
Liam Girdwood618dae12012-04-25 12:12:51 +01002501 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002502 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002503 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002504 ret = soc_dpcm_fe_runtime_update(fe, 0);
2505 if (ret)
2506 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002507 }
2508
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002509 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002510 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002511 ret = soc_dpcm_fe_runtime_update(fe, 1);
2512 if (ret)
2513 goto out;
2514 }
2515
2516out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002517 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002518 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002519}
Guennadi Liakhovetskif17a1472020-03-12 10:52:14 +01002520EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
Liam Girdwood01d75842012-04-25 12:12:49 +01002521
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002522static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002523{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002524 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002525 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002526 int stream = fe_substream->stream;
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002527
2528 /* mark FE's links ready to prune */
2529 for_each_dpcm_be(fe, stream, dpcm)
2530 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2531
2532 dpcm_be_disconnect(fe, stream);
2533
2534 fe->dpcm[stream].runtime = NULL;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002535}
2536
2537static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2538{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002539 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002540 int ret;
2541
2542 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2543 ret = dpcm_fe_dai_shutdown(fe_substream);
2544
2545 dpcm_fe_dai_cleanup(fe_substream);
2546
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002547 mutex_unlock(&fe->card->mutex);
2548 return ret;
2549}
2550
Liam Girdwood01d75842012-04-25 12:12:49 +01002551static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2552{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09002553 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002554 struct snd_soc_dapm_widget_list *list;
2555 int ret;
2556 int stream = fe_substream->stream;
2557
2558 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2559 fe->dpcm[stream].runtime = fe_substream->runtime;
2560
Qiao Zhou8f70e512014-09-10 17:54:07 +08002561 ret = dpcm_path_get(fe, stream, &list);
Kuninori Morimotod479f002021-03-15 09:57:52 +09002562 if (ret < 0)
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002563 goto open_end;
Liam Girdwood01d75842012-04-25 12:12:49 +01002564
2565 /* calculate valid and active FE <-> BE dpcms */
2566 dpcm_process_paths(fe, stream, &list, 1);
2567
2568 ret = dpcm_fe_dai_startup(fe_substream);
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002569 if (ret < 0)
2570 dpcm_fe_dai_cleanup(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002571
2572 dpcm_clear_pending_state(fe, stream);
2573 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002574open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002575 mutex_unlock(&fe->card->mutex);
2576 return ret;
2577}
2578
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002579static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2580 int *playback, int *capture)
Liam Girdwoodddee6272011-06-09 14:45:53 +01002581{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002582 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002583 struct snd_soc_dai *cpu_dai;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002584 int stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002585 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002586
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002587 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2588 dev_err(rtd->dev,
2589 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2590 return -EINVAL;
2591 }
Stephan Gerhold9b5db052020-04-15 12:49:28 +02002592
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002593 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2594 if (rtd->dai_link->dpcm_playback) {
2595 stream = SNDRV_PCM_STREAM_PLAYBACK;
2596
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002597 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2598 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002599 *playback = 1;
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002600 break;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002601 }
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002602 }
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002603 if (!*playback) {
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002604 dev_err(rtd->card->dev,
2605 "No CPU DAIs support playback for stream %s\n",
2606 rtd->dai_link->stream_name);
2607 return -EINVAL;
2608 }
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002609 }
2610 if (rtd->dai_link->dpcm_capture) {
2611 stream = SNDRV_PCM_STREAM_CAPTURE;
2612
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002613 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2614 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002615 *capture = 1;
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002616 break;
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002617 }
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002618 }
2619
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002620 if (!*capture) {
Pierre-Louis Bossart4f872152020-07-23 13:05:33 -05002621 dev_err(rtd->card->dev,
2622 "No CPU DAIs support capture for stream %s\n",
2623 rtd->dai_link->stream_name);
2624 return -EINVAL;
2625 }
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002626 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002627 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002628 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002629 int cpu_capture = rtd->dai_link->params ?
2630 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2631 int cpu_playback = rtd->dai_link->params ?
2632 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002633
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002634 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002635 if (rtd->num_cpus == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002636 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002637 } else if (rtd->num_cpus == rtd->num_codecs) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002638 cpu_dai = asoc_rtd_to_cpu(rtd, i);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002639 } else {
2640 dev_err(rtd->card->dev,
2641 "N cpus to M codecs link is not supported yet\n");
2642 return -EINVAL;
2643 }
2644
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002645 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002646 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002647 *playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002648 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002649 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002650 *capture = 1;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002651 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002652 }
Sangsu Parka5002312012-01-02 17:15:10 +09002653
Fabio Estevamd6bead02013-08-29 10:32:13 -03002654 if (rtd->dai_link->playback_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002655 *playback = 1;
2656 *capture = 0;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002657 }
2658
2659 if (rtd->dai_link->capture_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002660 *playback = 0;
2661 *capture = 1;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002662 }
2663
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002664 return 0;
2665}
2666
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002667static int soc_create_pcm(struct snd_pcm **pcm,
2668 struct snd_soc_pcm_runtime *rtd,
2669 int playback, int capture, int num)
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002670{
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002671 char new_name[64];
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002672 int ret;
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002673
Liam Girdwood01d75842012-04-25 12:12:49 +01002674 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002675 if (rtd->dai_link->params) {
2676 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2677 rtd->dai_link->stream_name);
2678
2679 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002680 playback, capture, pcm);
Jerome Bruneta3420312019-07-25 18:59:47 +02002681 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002682 snprintf(new_name, sizeof(new_name), "(%s)",
2683 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002684
Liam Girdwood01d75842012-04-25 12:12:49 +01002685 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002686 playback, capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002687 } else {
2688 if (rtd->dai_link->dynamic)
2689 snprintf(new_name, sizeof(new_name), "%s (*)",
2690 rtd->dai_link->stream_name);
2691 else
2692 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002693 rtd->dai_link->stream_name,
Kuninori Morimoto6fb89442021-03-09 10:07:48 +09002694 soc_codec_dai_name(rtd), num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002695
Liam Girdwood01d75842012-04-25 12:12:49 +01002696 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002697 capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002698 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002699 if (ret < 0) {
Pierre-Louis Bossart799827a2020-06-12 15:40:49 -05002700 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2701 new_name, rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002702 return ret;
2703 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002704 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002705
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002706 return 0;
2707}
2708
2709/* create a new pcm */
2710int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2711{
2712 struct snd_soc_component *component;
2713 struct snd_pcm *pcm;
2714 int ret = 0, playback = 0, capture = 0;
2715 int i;
2716
2717 ret = soc_get_playback_capture(rtd, &playback, &capture);
2718 if (ret < 0)
2719 return ret;
2720
2721 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2722 if (ret < 0)
2723 return ret;
2724
Liam Girdwoodddee6272011-06-09 14:45:53 +01002725 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002726 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002727 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002728 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002729 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002730
2731 rtd->pcm = pcm;
Kuninori Morimotoe04e7b82021-01-22 10:13:32 +09002732 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002733 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002734
Jerome Bruneta3420312019-07-25 18:59:47 +02002735 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002736 if (playback)
2737 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2738 if (capture)
2739 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2740 goto out;
2741 }
2742
2743 /* ASoC PCM operations */
2744 if (rtd->dai_link->dynamic) {
2745 rtd->ops.open = dpcm_fe_dai_open;
2746 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2747 rtd->ops.prepare = dpcm_fe_dai_prepare;
2748 rtd->ops.trigger = dpcm_fe_dai_trigger;
2749 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2750 rtd->ops.close = dpcm_fe_dai_close;
2751 rtd->ops.pointer = soc_pcm_pointer;
2752 } else {
2753 rtd->ops.open = soc_pcm_open;
2754 rtd->ops.hw_params = soc_pcm_hw_params;
2755 rtd->ops.prepare = soc_pcm_prepare;
2756 rtd->ops.trigger = soc_pcm_trigger;
2757 rtd->ops.hw_free = soc_pcm_hw_free;
2758 rtd->ops.close = soc_pcm_close;
2759 rtd->ops.pointer = soc_pcm_pointer;
2760 }
2761
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002762 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002763 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002764
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002765 if (drv->ioctl)
2766 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002767 if (drv->sync_stop)
2768 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002769 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002770 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002771 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002772 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002773 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002774 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Shengjiu Wang8bdfc042021-03-12 10:38:40 +08002775 if (drv->ack)
2776 rtd->ops.ack = snd_soc_pcm_component_ack;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002777 }
2778
Liam Girdwoodddee6272011-06-09 14:45:53 +01002779 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002780 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002781
2782 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002783 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002784
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002785 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto60adbd82021-03-15 09:58:41 +09002786 if (ret < 0)
Kuninori Morimoto74842912019-07-26 13:52:08 +09002787 return ret;
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002788
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002789 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002790out:
Pierre-Louis Bossart1d5cd522020-06-12 15:40:50 -05002791 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
Kuninori Morimoto6fb89442021-03-09 10:07:48 +09002792 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
Liam Girdwoodddee6272011-06-09 14:45:53 +01002793 return ret;
2794}
Liam Girdwood01d75842012-04-25 12:12:49 +01002795
2796/* is the current PCM operation for this FE ? */
2797int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2798{
2799 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2800 return 1;
2801 return 0;
2802}
2803EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2804
2805/* is the current PCM operation for this BE ? */
2806int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2807 struct snd_soc_pcm_runtime *be, int stream)
2808{
2809 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2810 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2811 be->dpcm[stream].runtime_update))
2812 return 1;
2813 return 0;
2814}
2815EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2816
2817/* get the substream for this BE */
2818struct snd_pcm_substream *
2819 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2820{
2821 return be->pcm->streams[stream].substream;
2822}
2823EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2824
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002825static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2826 struct snd_soc_pcm_runtime *be,
2827 int stream,
2828 const enum snd_soc_dpcm_state *states,
2829 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01002830{
2831 struct snd_soc_dpcm *dpcm;
2832 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002833 int ret = 1;
2834 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002835 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002836
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002837 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00002838 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002839
2840 if (dpcm->fe == fe)
2841 continue;
2842
2843 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002844 for (i = 0; i < num_states; i++) {
2845 if (state == states[i]) {
2846 ret = 0;
2847 break;
2848 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002849 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002850 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002851 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01002852
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002853 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002854 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01002855}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002856
2857/*
2858 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2859 * are not running, paused or suspended for the specified stream direction.
2860 */
2861int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2862 struct snd_soc_pcm_runtime *be, int stream)
2863{
2864 const enum snd_soc_dpcm_state state[] = {
2865 SND_SOC_DPCM_STATE_START,
2866 SND_SOC_DPCM_STATE_PAUSED,
2867 SND_SOC_DPCM_STATE_SUSPEND,
2868 };
2869
2870 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2871}
Liam Girdwood01d75842012-04-25 12:12:49 +01002872EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2873
2874/*
2875 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2876 * running, paused or suspended for the specified stream direction.
2877 */
2878int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2879 struct snd_soc_pcm_runtime *be, int stream)
2880{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002881 const enum snd_soc_dpcm_state state[] = {
2882 SND_SOC_DPCM_STATE_START,
2883 SND_SOC_DPCM_STATE_PAUSED,
2884 SND_SOC_DPCM_STATE_SUSPEND,
2885 SND_SOC_DPCM_STATE_PREPARE,
2886 };
Liam Girdwood01d75842012-04-25 12:12:49 +01002887
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002888 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01002889}
2890EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);