blob: 04c1c0aec9b030c8c4c812943d4527b5091daa62 [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 Morimotoee39d772021-04-16 11:00:11 +0900387 d.name = __func__;
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900388 soc_pcm_set_dai_params(&d, params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800389
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900390#define __soc_pcm_params_symmetry(xxx) \
391 symmetry = rtd->dai_link->symmetric_##xxx; \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900392 for_each_rtd_dais(rtd, i, dai) \
Kuninori Morimoto1cacbac2021-04-16 10:59:48 +0900393 symmetry |= dai->driver->symmetric_##xxx; \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900394 \
395 if (symmetry) \
396 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
Kuninori Morimoto9c2ae362021-04-16 11:00:32 +0900397 if (!snd_soc_dai_is_dummy(cpu_dai) && \
398 cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
Kuninori Morimotoee39d772021-04-16 11:00:11 +0900399 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
400 #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900401 return -EINVAL; \
402 }
403
Nicolin Chen3635bf02013-11-13 18:56:24 +0800404 /* reject unmatched parameters when applying symmetry */
Kuninori Morimoto3a906722021-01-15 13:56:39 +0900405 __soc_pcm_params_symmetry(rate);
406 __soc_pcm_params_symmetry(channels);
407 __soc_pcm_params_symmetry(sample_bits);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100408
409 return 0;
410}
411
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900412static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100413{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900414 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100415 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900416 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200417 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100418
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900419 symmetry = link->symmetric_rate ||
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800420 link->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900421 link->symmetric_sample_bits;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800422
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900423 for_each_rtd_dais(rtd, i, dai)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800424 symmetry = symmetry ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900425 dai->driver->symmetric_rate ||
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900426 dai->driver->symmetric_channels ||
Kuninori Morimotof14654d2021-01-15 13:52:54 +0900427 dai->driver->symmetric_sample_bits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200428
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900429 if (symmetry)
430 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100431}
432
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200433static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000434{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900435 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwaic6068d32014-12-31 17:10:34 +0100436 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000437
438 if (!bits)
439 return;
440
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100441 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
442 if (ret != 0)
443 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
444 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000445}
446
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200447static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200448{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900449 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800450 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200451 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900452 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200453 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800454 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200455
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900456 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto2bc3e1f2021-07-27 11:05:34 +0900457 struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900458
459 if (pcm_codec->sig_bits == 0) {
460 bits = 0;
461 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200462 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900463 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200464 }
465
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900466 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Kuninori Morimoto2bc3e1f2021-07-27 11:05:34 +0900467 struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800468
469 if (pcm_cpu->sig_bits == 0) {
470 cpu_bits = 0;
471 break;
472 }
473 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
474 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900475
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200476 soc_pcm_set_msb(substream, bits);
477 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200478}
479
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900480static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
481{
482 hw->rates = UINT_MAX;
483 hw->rate_min = 0;
484 hw->rate_max = UINT_MAX;
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900485 hw->channels_min = 0;
486 hw->channels_max = UINT_MAX;
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900487 hw->formats = ULLONG_MAX;
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900488}
489
490static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
491 struct snd_soc_pcm_stream *p)
492{
493 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
494
495 /* setup hw->rate_min/max via hw->rates first */
496 snd_pcm_hw_limit_rates(hw);
497
498 /* update hw->rate_min/max by snd_soc_pcm_stream */
499 hw->rate_min = max(hw->rate_min, p->rate_min);
500 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
501}
502
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900503static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
504 struct snd_soc_pcm_stream *p)
505{
506 hw->channels_min = max(hw->channels_min, p->channels_min);
507 hw->channels_max = min(hw->channels_max, p->channels_max);
508}
509
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900510static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
511 struct snd_soc_pcm_stream *p)
512{
513 hw->formats &= p->formats;
514}
515
Samuel Holland5854a462020-03-04 23:11:42 -0600516/**
517 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
518 * @rtd: ASoC PCM runtime
519 * @hw: PCM hardware parameters (output)
520 * @stream: Direction of the PCM stream
521 *
522 * Calculates the subset of stream parameters supported by all DAIs
523 * associated with the PCM stream.
524 */
525int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
526 struct snd_pcm_hardware *hw, int stream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200527{
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000528 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800529 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200530 struct snd_soc_pcm_stream *codec_stream;
531 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800532 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200533 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100534
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900535 soc_pcm_hw_init(hw);
536
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800537 /* first calculate min/max only for CPUs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900538 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800539
540 /*
541 * Skip CPUs which don't support the current stream type.
542 * Otherwise, since the rate, channel, and format values will
543 * zero in that case, we would have no usable settings left,
544 * causing the resulting setup to fail.
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800545 */
Samuel Holland5854a462020-03-04 23:11:42 -0600546 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800547 continue;
548
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800549 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100550
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900551 soc_pcm_hw_update_chan(hw, cpu_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900552 soc_pcm_hw_update_rate(hw, cpu_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900553 soc_pcm_hw_update_format(hw, cpu_stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800554 }
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900555 cpu_chan_min = hw->channels_min;
556 cpu_chan_max = hw->channels_max;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800557
558 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900559 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200560
561 /*
562 * Skip CODECs which don't support the current stream type.
563 * Otherwise, since the rate, channel, and format values will
564 * zero in that case, we would have no usable settings left,
565 * causing the resulting setup to fail.
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200566 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900567 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200568 continue;
569
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900570 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
571
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900572 soc_pcm_hw_update_chan(hw, codec_stream);
Kuninori Morimotof6c04af2021-02-04 08:50:31 +0900573 soc_pcm_hw_update_rate(hw, codec_stream);
Kuninori Morimotodebc71f2021-02-04 08:52:04 +0900574 soc_pcm_hw_update_format(hw, codec_stream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200575 }
576
Samuel Holland5854a462020-03-04 23:11:42 -0600577 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900578 if (!hw->channels_min)
Samuel Holland5854a462020-03-04 23:11:42 -0600579 return -EINVAL;
580
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200581 /*
582 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800583 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200584 * channel allocation be fixed up later
585 */
586 if (rtd->num_codecs > 1) {
Kuninori Morimoto6cb56a42021-02-04 08:51:49 +0900587 hw->channels_min = cpu_chan_min;
588 hw->channels_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200589 }
590
Samuel Holland5854a462020-03-04 23:11:42 -0600591 return 0;
592}
593EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
594
595static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
596{
597 struct snd_pcm_hardware *hw = &substream->runtime->hw;
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900598 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Samuel Holland5854a462020-03-04 23:11:42 -0600599 u64 formats = hw->formats;
600
601 /*
602 * At least one CPU and one CODEC should match. Otherwise, we should
603 * have bailed out on a higher level, since there would be no CPU or
604 * CODEC to support the transfer direction in that case.
605 */
606 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
607
608 if (formats)
609 hw->formats &= formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200610}
611
Kuninori Morimotodd039072020-02-10 12:14:37 +0900612static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900613{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900614 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900615 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900616 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900617
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900618 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900619 ret = snd_soc_component_module_get_when_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900620 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200621 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900622
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900623 ret = snd_soc_component_open(component, substream);
Kuninori Morimotobcae16312020-09-28 09:01:36 +0900624 if (ret < 0)
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200625 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900626 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900627
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200628 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900629}
630
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900631static int soc_pcm_components_close(struct snd_pcm_substream *substream,
632 int rollback)
Charles Keepax244e2932018-06-19 16:22:09 +0100633{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900634 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Charles Keepax244e2932018-06-19 16:22:09 +0100635 struct snd_soc_component *component;
Kuninori Morimoto33be10b2021-07-27 11:05:38 +0900636 int i, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100637
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900638 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto33be10b2021-07-27 11:05:38 +0900639 int r = snd_soc_component_close(component, substream, rollback);
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900640 if (r < 0)
641 ret = r; /* use last ret */
642
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900643 snd_soc_component_module_put_when_close(component, substream, rollback);
Charles Keepax244e2932018-06-19 16:22:09 +0100644 }
645
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900646 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100647}
648
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900649static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900650{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900651 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900652 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900653 struct snd_soc_dai *dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900654 int i;
655
656 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
657
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900658 if (!rollback)
659 snd_soc_runtime_deactivate(rtd, substream->stream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900660
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900661 for_each_rtd_dais(rtd, i, dai)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900662 snd_soc_dai_shutdown(dai, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900663
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900664 snd_soc_link_shutdown(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900665
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900666 soc_pcm_components_close(substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900667
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900668
669 mutex_unlock(&rtd->card->pcm_mutex);
670
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900671 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900672
673 for_each_rtd_components(rtd, i, component)
Kuninori Morimotob3dea622020-05-15 09:46:51 +0900674 if (!snd_soc_component_active(component))
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900675 pinctrl_pm_select_sleep_state(component->dev);
676
677 return 0;
678}
679
680/*
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900681 * Called by ALSA when a PCM substream is closed. Private data can be
682 * freed here. The cpu DAI, codec DAI, machine and components are also
683 * shutdown.
684 */
685static int soc_pcm_close(struct snd_pcm_substream *substream)
686{
687 return soc_pcm_clean(substream, 0);
688}
689
Kuninori Morimotoc3932812021-03-09 10:08:02 +0900690static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
691{
692 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
693 struct snd_pcm_hardware *hw = &substream->runtime->hw;
694 const char *name_cpu = soc_cpu_dai_name(rtd);
695 const char *name_codec = soc_codec_dai_name(rtd);
696 const char *err_msg;
697 struct device *dev = rtd->dev;
698
699 err_msg = "rates";
700 if (!hw->rates)
701 goto config_err;
702
703 err_msg = "formats";
704 if (!hw->formats)
705 goto config_err;
706
707 err_msg = "channels";
708 if (!hw->channels_min || !hw->channels_max ||
709 hw->channels_min > hw->channels_max)
710 goto config_err;
711
712 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
713 name_cpu);
714 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
715 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
716 hw->channels_max);
717 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
718 hw->rate_max);
719
720 return 0;
721
722config_err:
723 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
724 name_codec, name_cpu, err_msg);
725 return -EINVAL;
726}
727
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900728/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
730 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100731 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100732 */
733static int soc_pcm_open(struct snd_pcm_substream *substream)
734{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900735 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000736 struct snd_soc_component *component;
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900737 struct snd_soc_dai *dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100738 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900740 for_each_rtd_components(rtd, i, component)
741 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000742
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +0900743 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
744 if (ret < 0)
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900745 goto pm_err;
Mark Brownd6652ef2011-12-03 20:14:31 +0000746
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300747 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100748
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900749 ret = soc_pcm_components_open(substream);
750 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900751 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900752
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900753 ret = snd_soc_link_startup(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900754 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900755 goto err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900756
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 /* startup the audio subsystem */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900758 for_each_rtd_dais(rtd, i, dai) {
759 ret = snd_soc_dai_startup(dai, substream);
Kuninori Morimotoce820142020-09-28 09:01:29 +0900760 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900761 goto err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200762
763 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900764 dai->tx_mask = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200765 else
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900766 dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100767 }
768
Liam Girdwood01d75842012-04-25 12:12:49 +0100769 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
770 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
771 goto dynamic;
772
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200774 soc_pcm_init_runtime_hw(substream);
775
Kuninori Morimoto68cbc552021-03-09 10:07:57 +0900776 soc_pcm_update_symmetry(substream);
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100777
Kuninori Morimotoc3932812021-03-09 10:08:02 +0900778 ret = soc_hw_sanity_check(substream);
779 if (ret < 0)
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900780 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200782 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000783
Liam Girdwoodddee6272011-06-09 14:45:53 +0100784 /* Symmetry only applies if we've already got an active stream. */
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900785 for_each_rtd_dais(rtd, i, dai) {
Kuninori Morimotof8fc9ec2021-03-09 10:07:42 +0900786 ret = soc_pcm_apply_symmetry(substream, dai);
787 if (ret != 0)
788 goto err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100789 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100790dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100791 snd_soc_runtime_activate(rtd, substream->stream);
Kuninori Morimoto8e7875a2020-10-01 14:07:41 +0900792 ret = 0;
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900793err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300794 mutex_unlock(&rtd->card->pcm_mutex);
Kuninori Morimotocb2fce92020-10-01 10:32:48 +0900795pm_err:
Kuninori Morimotoe4b044f2021-03-15 09:57:37 +0900796 if (ret < 0) {
Kuninori Morimoto140a4532020-09-28 09:01:24 +0900797 soc_pcm_clean(substream, 1);
Kuninori Morimotoe4b044f2021-03-15 09:57:37 +0900798 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
799 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000800
Liam Girdwoodddee6272011-06-09 14:45:53 +0100801 return ret;
802}
803
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800804static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200805{
806 /*
807 * Currently nothing to do for c2c links
808 * Since c2c links are internal nodes in the DAPM graph and
809 * don't interface with the outside world or application layer
810 * we don't have to do any special handling on close.
811 */
812}
813
Liam Girdwoodddee6272011-06-09 14:45:53 +0100814/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815 * Called by ALSA when the PCM substream is prepared, can set format, sample
816 * rate, etc. This function is non atomic and can be called multiple times,
817 * it can refer to the runtime info.
818 */
819static int soc_pcm_prepare(struct snd_pcm_substream *substream)
820{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900821 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900822 struct snd_soc_dai *dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200823 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100824
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300825 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100826
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900827 ret = snd_soc_link_prepare(substream);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900828 if (ret < 0)
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900829 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100830
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900831 ret = snd_soc_pcm_component_prepare(substream);
832 if (ret < 0)
833 goto out;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000834
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900835 ret = snd_soc_pcm_dai_prepare(substream);
Kuninori Morimoto62462e02021-03-15 09:58:37 +0900836 if (ret < 0)
Kuninori Morimotod108c7f2020-04-24 08:14:53 +0900837 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100838
839 /* cancel any delayed stream shutdown that is pending */
840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600841 rtd->pop_wait) {
842 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100843 cancel_delayed_work(&rtd->delayed_work);
844 }
845
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000846 snd_soc_dapm_stream_event(rtd, substream->stream,
847 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100848
Kuninori Morimotoc840f762020-03-16 15:37:14 +0900849 for_each_rtd_dais(rtd, i, dai)
850 snd_soc_dai_digital_mute(dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100851
852out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300853 mutex_unlock(&rtd->card->pcm_mutex);
Kuninori Morimotodab7eeb2021-03-15 09:57:48 +0900854
855 if (ret < 0)
856 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
857
Liam Girdwoodddee6272011-06-09 14:45:53 +0100858 return ret;
859}
860
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200861static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
862 unsigned int mask)
863{
864 struct snd_interval *interval;
865 int channels = hweight_long(mask);
866
867 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
868 interval->min = channels;
869 interval->max = channels;
870}
871
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900872static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
Kuninori Morimotoab494362020-09-29 13:31:19 +0900873{
874 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
875 struct snd_soc_dai *dai;
876 int i;
877
878 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
879
880 /* clear the corresponding DAIs parameters when going to be inactive */
881 for_each_rtd_dais(rtd, i, dai) {
882 int active = snd_soc_dai_stream_active(dai, substream->stream);
883
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900884 if (snd_soc_dai_active(dai) == 1)
885 soc_pcm_set_dai_params(dai, NULL);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900886
887 if (active == 1)
888 snd_soc_dai_digital_mute(dai, 1, substream->stream);
889 }
890
Ranjani Sridharana27b4212020-11-17 13:50:01 -0800891 /* run the stream event */
892 snd_soc_dapm_stream_stop(rtd, substream->stream);
893
Kuninori Morimotoab494362020-09-29 13:31:19 +0900894 /* free any machine hw params */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900895 snd_soc_link_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900896
897 /* free any component resources */
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900898 snd_soc_pcm_component_hw_free(substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900899
900 /* now free hw params for the DAIs */
901 for_each_rtd_dais(rtd, i, dai) {
902 if (!snd_soc_dai_stream_valid(dai, substream->stream))
903 continue;
904
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900905 snd_soc_dai_hw_free(dai, substream, rollback);
Kuninori Morimotoab494362020-09-29 13:31:19 +0900906 }
907
908 mutex_unlock(&rtd->card->pcm_mutex);
909 return 0;
910}
911
912/*
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900913 * Frees resources allocated by hw_params, can be called multiple times
914 */
915static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
916{
917 return soc_pcm_hw_clean(substream, 0);
918}
919
920/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100921 * Called by ALSA when the hardware params are set by application. This
922 * function can also be called multiple times and can allocate buffers
923 * (using snd_pcm_lib_* ). It's non-atomic.
924 */
925static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
926 struct snd_pcm_hw_params *params)
927{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900928 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800929 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000930 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100931 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100932
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300933 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800934
935 ret = soc_pcm_params_symmetry(substream, params);
936 if (ret)
937 goto out;
938
Kuninori Morimoto7cf3c5b2020-05-25 09:57:31 +0900939 ret = snd_soc_link_hw_params(substream, params);
Kuninori Morimotoa5e6c102020-05-25 09:57:19 +0900940 if (ret < 0)
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900941 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900943 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200944 struct snd_pcm_hw_params codec_params;
945
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200946 /*
947 * Skip CODECs which don't support the current stream type,
948 * the idea being that if a CODEC is not used for the currently
949 * set up transfer direction, it should not need to be
950 * configured, especially since the configuration used might
951 * not even be supported by that CODEC. There may be cases
952 * however where a CODEC needs to be set up although it is
953 * actually not being used for the transfer, e.g. if a
954 * capture-only CODEC is acting as an LRCLK and/or BCLK master
955 * for the DAI link including a playback-only CODEC.
956 * If this becomes necessary, we will have to augment the
957 * machine driver setup with information on how to act, so
958 * we can do the right thing here.
959 */
960 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
961 continue;
962
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200963 /* copy params for each codec */
964 codec_params = *params;
965
966 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800967 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
968 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200969 soc_pcm_codec_params_fixup(&codec_params,
970 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800971
972 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
973 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200974 soc_pcm_codec_params_fixup(&codec_params,
975 codec_dai->rx_mask);
976
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900977 ret = snd_soc_dai_hw_params(codec_dai, substream,
978 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200979 if(ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900980 goto out;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200981
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900982 soc_pcm_set_dai_params(codec_dai, &codec_params);
Charles Keepax078a85f2019-01-31 13:30:18 +0000983 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100984 }
985
Kuninori Morimotoa4be4182020-03-09 13:08:04 +0900986 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800987 /*
988 * Skip CPUs which don't support the current stream
989 * type. See soc_pcm_init_runtime_hw() for more details
990 */
991 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
992 continue;
993
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800994 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
995 if (ret < 0)
Kuninori Morimoto4662c592020-09-29 13:32:01 +0900996 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100997
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800998 /* store the parameters for each DAI */
Kuninori Morimoto2805b8b2020-12-11 14:55:27 +0900999 soc_pcm_set_dai_params(cpu_dai, params);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001000 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1001 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001002
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001003 ret = snd_soc_pcm_component_hw_params(substream, params);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001004out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001005 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001006
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001007 if (ret < 0) {
Kuninori Morimoto4662c592020-09-29 13:32:01 +09001008 soc_pcm_hw_clean(substream, 1);
Kuninori Morimotocb11f792021-03-15 09:57:42 +09001009 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1010 }
Kuninori Morimotob8135862017-10-11 01:37:23 +00001011
Liam Girdwoodddee6272011-06-09 14:45:53 +01001012 return ret;
1013}
1014
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001015static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1016{
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001017 int ret = -EINVAL, _ret = 0;
1018 int rollback = 0;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001019
1020 switch (cmd) {
1021 case SNDRV_PCM_TRIGGER_START:
1022 case SNDRV_PCM_TRIGGER_RESUME:
1023 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001024 ret = snd_soc_link_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001025 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001026 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001027
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001028 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001029 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001030 goto start_err;
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001031
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001032 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1033start_err:
1034 if (ret < 0)
1035 rollback = 1;
1036 }
1037
1038 if (rollback) {
1039 _ret = ret;
1040 switch (cmd) {
1041 case SNDRV_PCM_TRIGGER_START:
1042 cmd = SNDRV_PCM_TRIGGER_STOP;
1043 break;
1044 case SNDRV_PCM_TRIGGER_RESUME:
1045 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1046 break;
1047 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1048 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1049 break;
1050 }
1051 }
1052
1053 switch (cmd) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001054 case SNDRV_PCM_TRIGGER_STOP:
1055 case SNDRV_PCM_TRIGGER_SUSPEND:
1056 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001057 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001058 if (ret < 0)
1059 break;
1060
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001061 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
Kuninori Morimoto836367b2020-06-04 17:08:12 +09001062 if (ret < 0)
1063 break;
1064
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001065 ret = snd_soc_link_trigger(substream, cmd, rollback);
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001066 break;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001067 }
1068
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001069 if (_ret)
1070 ret = _ret;
1071
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001072 return ret;
1073}
1074
Liam Girdwoodddee6272011-06-09 14:45:53 +01001075/*
1076 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001077 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001078 * the runtime->delay will be updated accordingly.
1079 */
1080static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1081{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001082 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001083 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001084 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001085 struct snd_pcm_runtime *runtime = substream->runtime;
1086 snd_pcm_uframes_t offset = 0;
1087 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001088 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001089 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001090 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001091
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301092 /* clearing the previous total delay */
1093 runtime->delay = 0;
1094
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001095 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001096
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301097 /* base delay if assigned in pointer callback */
1098 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001099
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001100 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001101 cpu_delay = max(cpu_delay,
1102 snd_soc_dai_delay(cpu_dai, substream));
1103 }
1104 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001105
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09001106 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001107 codec_delay = max(codec_delay,
1108 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001109 }
1110 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001111
Liam Girdwoodddee6272011-06-09 14:45:53 +01001112 runtime->delay = delay;
1113
1114 return offset;
1115}
1116
Liam Girdwood01d75842012-04-25 12:12:49 +01001117/* connect a FE and BE */
1118static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1119 struct snd_soc_pcm_runtime *be, int stream)
1120{
1121 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001122 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001123
1124 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001125 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001126 if (dpcm->be == be && dpcm->fe == fe)
1127 return 0;
1128 }
1129
1130 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1131 if (!dpcm)
1132 return -ENOMEM;
1133
1134 dpcm->be = be;
1135 dpcm->fe = fe;
1136 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1137 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001138 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001139 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1140 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001141 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001142
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001143 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001144 stream ? "capture" : "playback", fe->dai_link->name,
1145 stream ? "<-" : "->", be->dai_link->name);
1146
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001147 dpcm_create_debugfs_state(dpcm, stream);
1148
Liam Girdwood01d75842012-04-25 12:12:49 +01001149 return 1;
1150}
1151
1152/* reparent a BE onto another FE */
1153static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1154 struct snd_soc_pcm_runtime *be, int stream)
1155{
1156 struct snd_soc_dpcm *dpcm;
1157 struct snd_pcm_substream *fe_substream, *be_substream;
1158
1159 /* reparent if BE is connected to other FEs */
1160 if (!be->dpcm[stream].users)
1161 return;
1162
1163 be_substream = snd_soc_dpcm_get_substream(be, stream);
1164
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001165 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001166 if (dpcm->fe == fe)
1167 continue;
1168
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001169 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001170 stream ? "capture" : "playback",
1171 dpcm->fe->dai_link->name,
1172 stream ? "<-" : "->", dpcm->be->dai_link->name);
1173
1174 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1175 be_substream->runtime = fe_substream->runtime;
1176 break;
1177 }
1178}
1179
1180/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001181void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001182{
1183 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001184 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001185
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001186 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001187 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001188 stream ? "capture" : "playback",
1189 dpcm->be->dai_link->name);
1190
1191 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1192 continue;
1193
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001194 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001195 stream ? "capture" : "playback", fe->dai_link->name,
1196 stream ? "<-" : "->", dpcm->be->dai_link->name);
1197
1198 /* BEs still alive need new FE */
1199 dpcm_be_reparent(fe, dpcm->be, stream);
1200
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001201 dpcm_remove_debugfs_state(dpcm);
1202
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001203 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001204 list_del(&dpcm->list_be);
1205 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001206 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001207 kfree(dpcm);
1208 }
1209}
1210
1211/* get BE for DAI widget and stream */
1212static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1213 struct snd_soc_dapm_widget *widget, int stream)
1214{
1215 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001216 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001217 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001218 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001219
Liam Girdwood3c146462018-03-14 20:43:51 +00001220 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1221
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001222 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001223
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001224 if (!be->dai_link->no_pcm)
1225 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001226
Kuninori Morimotoc840f762020-03-16 15:37:14 +09001227 for_each_rtd_dais(be, i, dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001228 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001229
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001230 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1231 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001232
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001233 if (w == widget)
1234 return be;
1235 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001236 }
1237
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001238 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001239 return NULL;
1240}
1241
Liam Girdwood01d75842012-04-25 12:12:49 +01001242static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1243 struct snd_soc_dapm_widget *widget)
1244{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001245 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001246 int i;
1247
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001248 for_each_dapm_widgets(list, i, w)
1249 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001250 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001251
1252 return 0;
1253}
1254
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001255static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1256 enum snd_soc_dapm_direction dir)
1257{
1258 struct snd_soc_card *card = widget->dapm->card;
1259 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001260 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001261
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001262 /* adjust dir to stream */
1263 if (dir == SND_SOC_DAPM_DIR_OUT)
1264 stream = SNDRV_PCM_STREAM_PLAYBACK;
1265 else
1266 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001267
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001268 rtd = dpcm_get_be(card, widget, stream);
1269 if (rtd)
1270 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001271
1272 return false;
1273}
1274
Liam Girdwood23607022014-01-17 17:03:55 +00001275int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001276 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001277{
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09001278 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
Liam Girdwood01d75842012-04-25 12:12:49 +01001279 int paths;
1280
Bard Liao6e1276a2020-02-25 21:39:16 +08001281 if (fe->num_cpus > 1) {
1282 dev_err(fe->dev,
1283 "%s doesn't support Multi CPU yet\n", __func__);
1284 return -EINVAL;
1285 }
1286
Liam Girdwood01d75842012-04-25 12:12:49 +01001287 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001288 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Sameer Pujaraa293772020-11-02 20:40:09 +05301289 fe->card->component_chaining ?
1290 NULL : dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001291
Kuninori Morimotod479f002021-03-15 09:57:52 +09001292 if (paths > 0)
1293 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001294 stream ? "capture" : "playback");
Kuninori Morimotod479f002021-03-15 09:57:52 +09001295 else if (paths == 0)
1296 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1297 stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001298
Liam Girdwood01d75842012-04-25 12:12:49 +01001299 return paths;
1300}
1301
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001302void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1303{
1304 snd_soc_dapm_dai_free_widgets(list);
1305}
1306
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001307static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1308 struct snd_soc_dapm_widget_list *list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001309{
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) {
Kuninori Morimoto7931df92021-07-27 11:05:47 +09001315 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
Guennadi Liakhovetski8cce6562020-03-12 10:52:13 +01001316
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;
Jaroslav Kysela12ffd722021-06-14 09:17:46 +02001701 int err = 0;
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{
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002582 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002583 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002584
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002585 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2586 dev_err(rtd->dev,
2587 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2588 return -EINVAL;
2589 }
Stephan Gerhold9b5db052020-04-15 12:49:28 +02002590
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002591 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Kuninori Morimoto940a1f42021-07-27 11:05:43 +09002592 int stream;
2593
Pierre-Louis Bossartb73287f2020-06-08 14:44:12 -05002594 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 {
Kuninori Morimoto940a1f42021-07-27 11:05:43 +09002628 struct snd_soc_dai *codec_dai;
2629
Jerome Bruneta3420312019-07-25 18:59:47 +02002630 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002631 int cpu_capture = rtd->dai_link->params ?
2632 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2633 int cpu_playback = rtd->dai_link->params ?
2634 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002635
Kuninori Morimotoa4be4182020-03-09 13:08:04 +09002636 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002637 if (rtd->num_cpus == 1) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002638 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002639 } else if (rtd->num_cpus == rtd->num_codecs) {
Kuninori Morimotoc2233a22020-03-30 10:47:37 +09002640 cpu_dai = asoc_rtd_to_cpu(rtd, i);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002641 } else {
2642 dev_err(rtd->card->dev,
2643 "N cpus to M codecs link is not supported yet\n");
2644 return -EINVAL;
2645 }
2646
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002647 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002648 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002649 *playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002650 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002651 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002652 *capture = 1;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002653 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002654 }
Sangsu Parka5002312012-01-02 17:15:10 +09002655
Fabio Estevamd6bead02013-08-29 10:32:13 -03002656 if (rtd->dai_link->playback_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002657 *playback = 1;
2658 *capture = 0;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002659 }
2660
2661 if (rtd->dai_link->capture_only) {
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002662 *playback = 0;
2663 *capture = 1;
Fabio Estevamd6bead02013-08-29 10:32:13 -03002664 }
2665
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002666 return 0;
2667}
2668
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002669static int soc_create_pcm(struct snd_pcm **pcm,
2670 struct snd_soc_pcm_runtime *rtd,
2671 int playback, int capture, int num)
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002672{
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002673 char new_name[64];
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002674 int ret;
Kuninori Morimoto7fc6beb2021-01-22 10:13:38 +09002675
Liam Girdwood01d75842012-04-25 12:12:49 +01002676 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002677 if (rtd->dai_link->params) {
2678 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2679 rtd->dai_link->stream_name);
2680
2681 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002682 playback, capture, pcm);
Jerome Bruneta3420312019-07-25 18:59:47 +02002683 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002684 snprintf(new_name, sizeof(new_name), "(%s)",
2685 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002686
Liam Girdwood01d75842012-04-25 12:12:49 +01002687 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002688 playback, capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002689 } else {
2690 if (rtd->dai_link->dynamic)
2691 snprintf(new_name, sizeof(new_name), "%s (*)",
2692 rtd->dai_link->stream_name);
2693 else
2694 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002695 rtd->dai_link->stream_name,
Kuninori Morimoto6fb89442021-03-09 10:07:48 +09002696 soc_codec_dai_name(rtd), num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002697
Liam Girdwood01d75842012-04-25 12:12:49 +01002698 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002699 capture, pcm);
Liam Girdwood01d75842012-04-25 12:12:49 +01002700 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002701 if (ret < 0) {
Pierre-Louis Bossart799827a2020-06-12 15:40:49 -05002702 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2703 new_name, rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002704 return ret;
2705 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002706 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002707
Kuninori Morimoto2b391232021-01-22 10:13:43 +09002708 return 0;
2709}
2710
2711/* create a new pcm */
2712int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2713{
2714 struct snd_soc_component *component;
2715 struct snd_pcm *pcm;
2716 int ret = 0, playback = 0, capture = 0;
2717 int i;
2718
2719 ret = soc_get_playback_capture(rtd, &playback, &capture);
2720 if (ret < 0)
2721 return ret;
2722
2723 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2724 if (ret < 0)
2725 return ret;
2726
Liam Girdwoodddee6272011-06-09 14:45:53 +01002727 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002728 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002729 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002730 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002731 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002732
2733 rtd->pcm = pcm;
Kuninori Morimotoe04e7b82021-01-22 10:13:32 +09002734 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002735 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002736
Jerome Bruneta3420312019-07-25 18:59:47 +02002737 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002738 if (playback)
2739 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2740 if (capture)
2741 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2742 goto out;
2743 }
2744
2745 /* ASoC PCM operations */
2746 if (rtd->dai_link->dynamic) {
2747 rtd->ops.open = dpcm_fe_dai_open;
2748 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2749 rtd->ops.prepare = dpcm_fe_dai_prepare;
2750 rtd->ops.trigger = dpcm_fe_dai_trigger;
2751 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2752 rtd->ops.close = dpcm_fe_dai_close;
2753 rtd->ops.pointer = soc_pcm_pointer;
2754 } else {
2755 rtd->ops.open = soc_pcm_open;
2756 rtd->ops.hw_params = soc_pcm_hw_params;
2757 rtd->ops.prepare = soc_pcm_prepare;
2758 rtd->ops.trigger = soc_pcm_trigger;
2759 rtd->ops.hw_free = soc_pcm_hw_free;
2760 rtd->ops.close = soc_pcm_close;
2761 rtd->ops.pointer = soc_pcm_pointer;
2762 }
2763
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002764 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002765 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002766
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002767 if (drv->ioctl)
2768 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002769 if (drv->sync_stop)
2770 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002771 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002772 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002773 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002774 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002775 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002776 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Shengjiu Wang8bdfc042021-03-12 10:38:40 +08002777 if (drv->ack)
2778 rtd->ops.ack = snd_soc_pcm_component_ack;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002779 }
2780
Liam Girdwoodddee6272011-06-09 14:45:53 +01002781 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002782 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002783
2784 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002785 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002786
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002787 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto60adbd82021-03-15 09:58:41 +09002788 if (ret < 0)
Kuninori Morimoto74842912019-07-26 13:52:08 +09002789 return ret;
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002790
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002791 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002792out:
Pierre-Louis Bossart1d5cd522020-06-12 15:40:50 -05002793 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
Kuninori Morimoto6fb89442021-03-09 10:07:48 +09002794 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
Liam Girdwoodddee6272011-06-09 14:45:53 +01002795 return ret;
2796}
Liam Girdwood01d75842012-04-25 12:12:49 +01002797
2798/* is the current PCM operation for this FE ? */
2799int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2800{
2801 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2802 return 1;
2803 return 0;
2804}
2805EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2806
2807/* is the current PCM operation for this BE ? */
2808int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2809 struct snd_soc_pcm_runtime *be, int stream)
2810{
2811 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2812 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2813 be->dpcm[stream].runtime_update))
2814 return 1;
2815 return 0;
2816}
2817EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2818
2819/* get the substream for this BE */
2820struct snd_pcm_substream *
2821 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2822{
2823 return be->pcm->streams[stream].substream;
2824}
2825EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2826
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002827static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2828 struct snd_soc_pcm_runtime *be,
2829 int stream,
2830 const enum snd_soc_dpcm_state *states,
2831 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01002832{
2833 struct snd_soc_dpcm *dpcm;
2834 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002835 int ret = 1;
2836 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002837 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002838
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002839 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00002840 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002841
2842 if (dpcm->fe == fe)
2843 continue;
2844
2845 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002846 for (i = 0; i < num_states; i++) {
2847 if (state == states[i]) {
2848 ret = 0;
2849 break;
2850 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002851 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002852 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002853 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01002854
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002855 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002856 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01002857}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002858
2859/*
2860 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2861 * are not running, paused or suspended for the specified stream direction.
2862 */
2863int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2864 struct snd_soc_pcm_runtime *be, int stream)
2865{
2866 const enum snd_soc_dpcm_state state[] = {
2867 SND_SOC_DPCM_STATE_START,
2868 SND_SOC_DPCM_STATE_PAUSED,
2869 SND_SOC_DPCM_STATE_SUSPEND,
2870 };
2871
2872 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2873}
Liam Girdwood01d75842012-04-25 12:12:49 +01002874EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2875
2876/*
2877 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2878 * running, paused or suspended for the specified stream direction.
2879 */
2880int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2881 struct snd_soc_pcm_runtime *be, int stream)
2882{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002883 const enum snd_soc_dpcm_state state[] = {
2884 SND_SOC_DPCM_STATE_START,
2885 SND_SOC_DPCM_STATE_PAUSED,
2886 SND_SOC_DPCM_STATE_SUSPEND,
2887 SND_SOC_DPCM_STATE_PREPARE,
2888 };
Liam Girdwood01d75842012-04-25 12:12:49 +01002889
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002890 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01002891}
2892EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);