blob: af0e17bfeeab4b949623dd242455f562f7bf63e5 [file] [log] [blame]
Kuninori Morimotoed517582018-07-02 06:22:44 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-pcm.c -- ALSA SoC PCM
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Authors: Liam Girdwood <lrg@ti.com>
11// Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010012
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080016#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000017#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010018#include <linux/slab.h>
19#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010020#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010021#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/initval.h>
28
Liam Girdwood01d75842012-04-25 12:12:49 +010029#define DPCM_MAX_BE_USERS 8
30
Kuninori Morimotoc3212822020-02-19 15:56:57 +090031#ifdef CONFIG_DEBUG_FS
32static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
33{
34 switch (state) {
35 case SND_SOC_DPCM_STATE_NEW:
36 return "new";
37 case SND_SOC_DPCM_STATE_OPEN:
38 return "open";
39 case SND_SOC_DPCM_STATE_HW_PARAMS:
40 return "hw_params";
41 case SND_SOC_DPCM_STATE_PREPARE:
42 return "prepare";
43 case SND_SOC_DPCM_STATE_START:
44 return "start";
45 case SND_SOC_DPCM_STATE_STOP:
46 return "stop";
47 case SND_SOC_DPCM_STATE_SUSPEND:
48 return "suspend";
49 case SND_SOC_DPCM_STATE_PAUSED:
50 return "paused";
51 case SND_SOC_DPCM_STATE_HW_FREE:
52 return "hw_free";
53 case SND_SOC_DPCM_STATE_CLOSE:
54 return "close";
55 }
56
57 return "unknown";
58}
59
60static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
61 int stream, char *buf, size_t size)
62{
63 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
64 struct snd_soc_dpcm *dpcm;
65 ssize_t offset = 0;
66 unsigned long flags;
67
68 /* FE state */
69 offset += snprintf(buf + offset, size - offset,
70 "[%s - %s]\n", fe->dai_link->name,
71 stream ? "Capture" : "Playback");
72
73 offset += snprintf(buf + offset, size - offset, "State: %s\n",
74 dpcm_state_string(fe->dpcm[stream].state));
75
76 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
77 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
78 offset += snprintf(buf + offset, size - offset,
79 "Hardware Params: "
80 "Format = %s, Channels = %d, Rate = %d\n",
81 snd_pcm_format_name(params_format(params)),
82 params_channels(params),
83 params_rate(params));
84
85 /* BEs state */
86 offset += snprintf(buf + offset, size - offset, "Backends:\n");
87
88 if (list_empty(&fe->dpcm[stream].be_clients)) {
89 offset += snprintf(buf + offset, size - offset,
90 " No active DSP links\n");
91 goto out;
92 }
93
94 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
95 for_each_dpcm_be(fe, stream, dpcm) {
96 struct snd_soc_pcm_runtime *be = dpcm->be;
97 params = &dpcm->hw_params;
98
99 offset += snprintf(buf + offset, size - offset,
100 "- %s\n", be->dai_link->name);
101
102 offset += snprintf(buf + offset, size - offset,
103 " State: %s\n",
104 dpcm_state_string(be->dpcm[stream].state));
105
106 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
107 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
108 offset += snprintf(buf + offset, size - offset,
109 " Hardware Params: "
110 "Format = %s, Channels = %d, Rate = %d\n",
111 snd_pcm_format_name(params_format(params)),
112 params_channels(params),
113 params_rate(params));
114 }
115 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116out:
117 return offset;
118}
119
120static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
121 size_t count, loff_t *ppos)
122{
123 struct snd_soc_pcm_runtime *fe = file->private_data;
124 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
125 int stream;
126 char *buf;
127
Bard Liao6e1276a2020-02-25 21:39:16 +0800128 if (fe->num_cpus > 1) {
129 dev_err(fe->dev,
130 "%s doesn't support Multi CPU yet\n", __func__);
131 return -EINVAL;
132 }
133
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900134 buf = kmalloc(out_count, GFP_KERNEL);
135 if (!buf)
136 return -ENOMEM;
137
138 for_each_pcm_streams(stream)
139 if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
140 offset += dpcm_show_state(fe, stream,
141 buf + offset,
142 out_count - offset);
143
144 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
145
146 kfree(buf);
147 return ret;
148}
149
150static const struct file_operations dpcm_state_fops = {
151 .open = simple_open,
152 .read = dpcm_state_read_file,
153 .llseek = default_llseek,
154};
155
156void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
157{
158 if (!rtd->dai_link)
159 return;
160
161 if (!rtd->dai_link->dynamic)
162 return;
163
164 if (!rtd->card->debugfs_card_root)
165 return;
166
167 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
168 rtd->card->debugfs_card_root);
169
170 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
171 rtd, &dpcm_state_fops);
172}
Kuninori Morimoto154dae82020-02-19 15:57:06 +0900173
174static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
175{
176 char *name;
177
178 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
179 stream ? "capture" : "playback");
180 if (name) {
181 dpcm->debugfs_state = debugfs_create_dir(
182 name, dpcm->fe->debugfs_dpcm_root);
183 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
184 &dpcm->state);
185 kfree(name);
186 }
187}
188
189static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
190{
191 debugfs_remove_recursive(dpcm->debugfs_state);
192}
193
194#else
195static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
196 int stream)
197{
198}
199
200static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
201{
202}
Kuninori Morimotoc3212822020-02-19 15:56:57 +0900203#endif
204
Kuninori Morimotof183f922020-01-22 09:44:35 +0900205static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
206 struct snd_pcm_substream *substream)
207{
208 if (rtd->dai_link->ops &&
209 rtd->dai_link->ops->startup)
210 return rtd->dai_link->ops->startup(substream);
211 return 0;
212}
213
Kuninori Morimoto0be429f2020-01-22 09:44:40 +0900214static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
215 struct snd_pcm_substream *substream)
216{
217 if (rtd->dai_link->ops &&
218 rtd->dai_link->ops->shutdown)
219 rtd->dai_link->ops->shutdown(substream);
220}
221
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900222static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
223 struct snd_pcm_substream *substream)
224{
225 if (rtd->dai_link->ops &&
226 rtd->dai_link->ops->prepare)
227 return rtd->dai_link->ops->prepare(substream);
228 return 0;
229}
230
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900231static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
232 struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *params)
234{
235 if (rtd->dai_link->ops &&
236 rtd->dai_link->ops->hw_params)
237 return rtd->dai_link->ops->hw_params(substream, params);
238 return 0;
239}
240
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900241static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
242 struct snd_pcm_substream *substream)
243{
244 if (rtd->dai_link->ops &&
245 rtd->dai_link->ops->hw_free)
246 rtd->dai_link->ops->hw_free(substream);
247}
248
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +0900249static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
250 struct snd_pcm_substream *substream,
251 int cmd)
252{
253 if (rtd->dai_link->ops &&
254 rtd->dai_link->ops->trigger)
255 return rtd->dai_link->ops->trigger(substream, cmd);
256 return 0;
257}
258
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900259static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
260 int stream, int action)
261{
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800262 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900263 struct snd_soc_dai *codec_dai;
264 int i;
265
266 lockdep_assert_held(&rtd->card->pcm_mutex);
267
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800268 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
269 cpu_dai->stream_active[stream] += action;
270
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900271 for_each_rtd_codec_dai(rtd, i, codec_dai)
272 codec_dai->stream_active[stream] += action;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900273
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800274 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
275 cpu_dai->active += action;
276 cpu_dai->component->active += action;
277 }
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900278 for_each_rtd_codec_dai(rtd, i, codec_dai) {
279 codec_dai->active += action;
280 codec_dai->component->active += action;
281 }
282}
283
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200284/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100285 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
286 * @rtd: ASoC PCM runtime that is activated
287 * @stream: Direction of the PCM stream
288 *
289 * Increments the active count for all the DAIs and components attached to a PCM
290 * runtime. Should typically be called when a stream is opened.
291 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300292 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100293 */
294void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
295{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900296 snd_soc_runtime_action(rtd, stream, 1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100297}
298
299/**
300 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
301 * @rtd: ASoC PCM runtime that is deactivated
302 * @stream: Direction of the PCM stream
303 *
304 * Decrements the active count for all the DAIs and components attached to a PCM
305 * runtime. Should typically be called when a stream is closed.
306 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300307 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100308 */
309void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
310{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900311 snd_soc_runtime_action(rtd, stream, -1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100312}
313
314/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100315 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
316 * @rtd: The ASoC PCM runtime that should be checked.
317 *
318 * This function checks whether the power down delay should be ignored for a
319 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
320 * been configured to ignore the delay, or if none of the components benefits
321 * from having the delay.
322 */
323bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
324{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000325 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200326 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900327 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200328
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100329 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
330 return true;
331
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900332 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000333 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000334
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000335 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100336}
337
338/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200339 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
340 * @substream: the pcm substream
341 * @hw: the hardware parameters
342 *
343 * Sets the substream runtime hardware parameters.
344 */
345int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
346 const struct snd_pcm_hardware *hw)
347{
348 struct snd_pcm_runtime *runtime = substream->runtime;
349 runtime->hw.info = hw->info;
350 runtime->hw.formats = hw->formats;
351 runtime->hw.period_bytes_min = hw->period_bytes_min;
352 runtime->hw.period_bytes_max = hw->period_bytes_max;
353 runtime->hw.periods_min = hw->periods_min;
354 runtime->hw.periods_max = hw->periods_max;
355 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
356 runtime->hw.fifo_size = hw->fifo_size;
357 return 0;
358}
359EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
360
Liam Girdwood01d75842012-04-25 12:12:49 +0100361/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000362int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100363 int event)
364{
365 struct snd_soc_dpcm *dpcm;
366
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000367 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100368
369 struct snd_soc_pcm_runtime *be = dpcm->be;
370
Liam Girdwood103d84a2012-11-19 14:39:15 +0000371 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100372 be->dai_link->name, event, dir);
373
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700374 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
375 (be->dpcm[dir].users >= 1))
376 continue;
377
Liam Girdwood01d75842012-04-25 12:12:49 +0100378 snd_soc_dapm_stream_event(be, dir, event);
379 }
380
381 snd_soc_dapm_stream_event(fe, dir, event);
382
383 return 0;
384}
385
Dong Aisheng17841022011-08-29 17:15:14 +0800386static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
387 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100388{
389 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100390 int ret;
391
Nicolin Chen3635bf02013-11-13 18:56:24 +0800392 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
393 rtd->dai_link->symmetric_rates)) {
394 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
395 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100396
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200397 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800398 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200399 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800400 if (ret < 0) {
401 dev_err(soc_dai->dev,
402 "ASoC: Unable to apply rate constraint: %d\n",
403 ret);
404 return ret;
405 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100406 }
407
Nicolin Chen3635bf02013-11-13 18:56:24 +0800408 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
409 rtd->dai_link->symmetric_channels)) {
410 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
411 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100412
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200413 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800414 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800415 soc_dai->channels);
416 if (ret < 0) {
417 dev_err(soc_dai->dev,
418 "ASoC: Unable to apply channel symmetry constraint: %d\n",
419 ret);
420 return ret;
421 }
422 }
423
424 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
425 rtd->dai_link->symmetric_samplebits)) {
426 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
427 soc_dai->sample_bits);
428
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200429 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800430 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800431 soc_dai->sample_bits);
432 if (ret < 0) {
433 dev_err(soc_dai->dev,
434 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
435 ret);
436 return ret;
437 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100438 }
439
440 return 0;
441}
442
Nicolin Chen3635bf02013-11-13 18:56:24 +0800443static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
444 struct snd_pcm_hw_params *params)
445{
446 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800447 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000448 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200449 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800450
451 rate = params_rate(params);
452 channels = params_channels(params);
453 sample_bits = snd_pcm_format_physical_width(params_format(params));
454
455 /* reject unmatched parameters when applying symmetry */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800456 symmetry = rtd->dai_link->symmetric_rates;
457
458 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
459 symmetry |= cpu_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200460
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000461 for_each_rtd_codec_dai(rtd, i, codec_dai)
462 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200463
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800464 if (symmetry) {
465 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
466 if (cpu_dai->rate && cpu_dai->rate != rate) {
467 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
468 cpu_dai->rate, rate);
469 return -EINVAL;
470 }
471 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800472 }
473
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800474 symmetry = rtd->dai_link->symmetric_channels;
475
476 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
477 symmetry |= cpu_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200478
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000479 for_each_rtd_codec_dai(rtd, i, codec_dai)
480 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200481
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800482 if (symmetry) {
483 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
484 if (cpu_dai->channels &&
485 cpu_dai->channels != channels) {
486 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
487 cpu_dai->channels, channels);
488 return -EINVAL;
489 }
490 }
Nicolin Chen3635bf02013-11-13 18:56:24 +0800491 }
492
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800493 symmetry = rtd->dai_link->symmetric_samplebits;
494
495 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
496 symmetry |= cpu_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200497
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000498 for_each_rtd_codec_dai(rtd, i, codec_dai)
499 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200500
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800501 if (symmetry) {
502 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
503 if (cpu_dai->sample_bits &&
504 cpu_dai->sample_bits != sample_bits) {
505 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
506 cpu_dai->sample_bits, sample_bits);
507 return -EINVAL;
508 }
509 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100510 }
511
512 return 0;
513}
514
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100515static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
516{
517 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100518 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000519 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800520 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200521 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100522
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800523 symmetry = link->symmetric_rates ||
524 link->symmetric_channels ||
525 link->symmetric_samplebits;
526
527 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
528 symmetry = symmetry ||
529 cpu_dai->driver->symmetric_rates ||
530 cpu_dai->driver->symmetric_channels ||
531 cpu_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200532
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000533 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200534 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000535 codec_dai->driver->symmetric_rates ||
536 codec_dai->driver->symmetric_channels ||
537 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200538
539 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100540}
541
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200542static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000543{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200544 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100545 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000546
547 if (!bits)
548 return;
549
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100550 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
551 if (ret != 0)
552 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
553 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000554}
555
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200556static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200557{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200558 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800559 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200560 struct snd_soc_dai *codec_dai;
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900561 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
562 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200563 int i;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800564 unsigned int bits = 0, cpu_bits = 0;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200565
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900566 for_each_rtd_codec_dai(rtd, i, codec_dai) {
567 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
568
569 if (pcm_codec->sig_bits == 0) {
570 bits = 0;
571 break;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200572 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900573 bits = max(pcm_codec->sig_bits, bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200574 }
575
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800576 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
577 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
578
579 if (pcm_cpu->sig_bits == 0) {
580 cpu_bits = 0;
581 break;
582 }
583 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
584 }
Kuninori Morimoto57be9202020-02-19 15:56:36 +0900585
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200586 soc_pcm_set_msb(substream, bits);
587 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200588}
589
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200590static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200591{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200592 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100593 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000595 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800596 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200597 struct snd_soc_pcm_stream *codec_stream;
598 struct snd_soc_pcm_stream *cpu_stream;
599 unsigned int chan_min = 0, chan_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800600 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200601 unsigned int rate_min = 0, rate_max = UINT_MAX;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800602 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
603 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200604 u64 formats = ULLONG_MAX;
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900605 int stream = substream->stream;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200606 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100607
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800608 /* first calculate min/max only for CPUs in the DAI link */
609 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +0800610
611 /*
612 * Skip CPUs which don't support the current stream type.
613 * Otherwise, since the rate, channel, and format values will
614 * zero in that case, we would have no usable settings left,
615 * causing the resulting setup to fail.
616 * At least one CPU should match, otherwise we should have
617 * bailed out on a higher level, since there would be no
618 * CPU to support the transfer direction in that case.
619 */
620 if (!snd_soc_dai_stream_valid(cpu_dai,
621 substream->stream))
622 continue;
623
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800624 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100625
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800626 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
627 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
628 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
629 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
630 formats &= cpu_stream->formats;
631 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
632 cpu_rates);
633 }
634
635 /* second calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000636 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200637
638 /*
639 * Skip CODECs which don't support the current stream type.
640 * Otherwise, since the rate, channel, and format values will
641 * zero in that case, we would have no usable settings left,
642 * causing the resulting setup to fail.
643 * At least one CODEC should match, otherwise we should have
644 * bailed out on a higher level, since there would be no
645 * CODEC to support the transfer direction in that case.
646 */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +0900647 if (!snd_soc_dai_stream_valid(codec_dai, stream))
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200648 continue;
649
Kuninori Morimotoacf253c2020-02-19 15:56:30 +0900650 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
651
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200652 chan_min = max(chan_min, codec_stream->channels_min);
653 chan_max = min(chan_max, codec_stream->channels_max);
654 rate_min = max(rate_min, codec_stream->rate_min);
655 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
656 formats &= codec_stream->formats;
657 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
658 }
659
660 /*
661 * chan min/max cannot be enforced if there are multiple CODEC DAIs
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800662 * connected to CPU DAI(s), use CPU DAI's directly and let
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200663 * channel allocation be fixed up later
664 */
665 if (rtd->num_codecs > 1) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800666 chan_min = cpu_chan_min;
667 chan_max = cpu_chan_max;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200668 }
669
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800670 /* finally find a intersection between CODECs and CPUs */
671 hw->channels_min = max(chan_min, cpu_chan_min);
672 hw->channels_max = min(chan_max, cpu_chan_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200673 if (hw->formats)
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800674 hw->formats &= formats;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200675 else
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800676 hw->formats = formats;
677 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100678
679 snd_pcm_limit_hw_rates(runtime);
680
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800681 hw->rate_min = max(hw->rate_min, cpu_rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200682 hw->rate_min = max(hw->rate_min, rate_min);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800683 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200684 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200685}
686
Kuninori Morimotodd039072020-02-10 12:14:37 +0900687static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900688{
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200690 struct snd_soc_component *last = NULL;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900691 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900692 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900693
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900694 for_each_rtd_components(rtd, i, component) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200695 last = component;
696
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900697 ret = snd_soc_component_module_get_when_open(component);
698 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900699 dev_err(component->dev,
700 "ASoC: can't get module %s\n",
701 component->name);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200702 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900703 }
704
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900705 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900706 if (ret < 0) {
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200707 snd_soc_component_module_put_when_close(component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900708 dev_err(component->dev,
709 "ASoC: can't open component %s: %d\n",
710 component->name, ret);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200711 break;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900712 }
713 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900714
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200715 if (ret < 0) {
716 /* rollback on error */
717 for_each_rtd_components(rtd, i, component) {
718 if (component == last)
719 break;
720
721 snd_soc_component_close(component, substream);
722 snd_soc_component_module_put_when_close(component);
723 }
724 }
725
726 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900727}
728
Kuninori Morimotodd039072020-02-10 12:14:37 +0900729static int soc_pcm_components_close(struct snd_pcm_substream *substream)
Charles Keepax244e2932018-06-19 16:22:09 +0100730{
731 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100732 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900733 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100734
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900735 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900736 r = snd_soc_component_close(component, substream);
737 if (r < 0)
738 ret = r; /* use last ret */
739
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900740 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100741 }
742
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900743 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100744}
745
Mark Brown58ba9b22012-01-16 18:38:51 +0000746/*
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900747 * Called by ALSA when a PCM substream is closed. Private data can be
748 * freed here. The cpu DAI, codec DAI, machine and components are also
749 * shutdown.
750 */
751static int soc_pcm_close(struct snd_pcm_substream *substream)
752{
753 struct snd_soc_pcm_runtime *rtd = substream->private_data;
754 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800755 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900756 struct snd_soc_dai *codec_dai;
757 int i;
758
759 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
760
761 snd_soc_runtime_deactivate(rtd, substream->stream);
762
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800763 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
764 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900765
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800766 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
767 snd_soc_dai_shutdown(cpu_dai, substream);
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900768
769 for_each_rtd_codec_dai(rtd, i, codec_dai)
770 snd_soc_dai_shutdown(codec_dai, substream);
771
772 soc_rtd_shutdown(rtd, substream);
773
774 soc_pcm_components_close(substream);
775
776 snd_soc_dapm_stream_stop(rtd, substream->stream);
777
778 mutex_unlock(&rtd->card->pcm_mutex);
779
780 for_each_rtd_components(rtd, i, component) {
781 pm_runtime_mark_last_busy(component->dev);
782 pm_runtime_put_autosuspend(component->dev);
783 }
784
785 for_each_rtd_components(rtd, i, component)
786 if (!component->active)
787 pinctrl_pm_select_sleep_state(component->dev);
788
789 return 0;
790}
791
792/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100793 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
794 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100795 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100796 */
797static int soc_pcm_open(struct snd_pcm_substream *substream)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
800 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000801 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800802 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200803 struct snd_soc_dai *codec_dai;
804 const char *codec_dai_name = "multicodec";
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800805 const char *cpu_dai_name = "multicpu";
Charles Keepax244e2932018-06-19 16:22:09 +0100806 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100807
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900808 for_each_rtd_components(rtd, i, component)
809 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000810
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900811 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000812 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000813
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300814 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900816 ret = soc_pcm_components_open(substream);
817 if (ret < 0)
818 goto component_err;
819
820 ret = soc_rtd_startup(rtd, substream);
821 if (ret < 0) {
822 pr_err("ASoC: %s startup failed: %d\n",
823 rtd->dai_link->name, ret);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200824 goto rtd_startup_err;
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900825 }
826
Liam Girdwoodddee6272011-06-09 14:45:53 +0100827 /* startup the audio subsystem */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800828 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
829 ret = snd_soc_dai_startup(cpu_dai, substream);
830 if (ret < 0) {
831 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
832 cpu_dai->name, ret);
833 goto cpu_dai_err;
834 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100835 }
836
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000837 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900838 ret = snd_soc_dai_startup(codec_dai, substream);
839 if (ret < 0) {
840 dev_err(codec_dai->dev,
841 "ASoC: can't open codec %s: %d\n",
842 codec_dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900843 goto config_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100844 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200845
846 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
847 codec_dai->tx_mask = 0;
848 else
849 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100850 }
851
Liam Girdwood01d75842012-04-25 12:12:49 +0100852 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
853 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
854 goto dynamic;
855
Liam Girdwoodddee6272011-06-09 14:45:53 +0100856 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200857 soc_pcm_init_runtime_hw(substream);
858
859 if (rtd->num_codecs == 1)
860 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100861
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800862 if (rtd->num_cpus == 1)
863 cpu_dai_name = rtd->cpu_dai->name;
864
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100865 if (soc_pcm_has_symmetry(substream))
866 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
867
Liam Girdwoodddee6272011-06-09 14:45:53 +0100868 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100869 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000870 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800871 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100872 goto config_err;
873 }
874 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000875 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800876 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100877 goto config_err;
878 }
879 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
880 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000881 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800882 codec_dai_name, cpu_dai_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100883 goto config_err;
884 }
885
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200886 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000887
Liam Girdwoodddee6272011-06-09 14:45:53 +0100888 /* Symmetry only applies if we've already got an active stream. */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800889 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
890 if (cpu_dai->active) {
891 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
892 if (ret != 0)
893 goto config_err;
894 }
Dong Aisheng17841022011-08-29 17:15:14 +0800895 }
896
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000897 for_each_rtd_codec_dai(rtd, i, codec_dai) {
898 if (codec_dai->active) {
899 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200900 if (ret != 0)
901 goto config_err;
902 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100903 }
904
Liam Girdwood103d84a2012-11-19 14:39:15 +0000905 pr_debug("ASoC: %s <-> %s info:\n",
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800906 codec_dai_name, cpu_dai_name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000907 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
908 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100909 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000910 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100911 runtime->hw.rate_max);
912
Liam Girdwood01d75842012-04-25 12:12:49 +0100913dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100914
915 snd_soc_runtime_activate(rtd, substream->stream);
916
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300917 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100918 return 0;
919
920config_err:
Kuninori Morimotob56be802020-02-10 12:14:33 +0900921 for_each_rtd_codec_dai(rtd, i, codec_dai)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900922 snd_soc_dai_shutdown(codec_dai, substream);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900923cpu_dai_err:
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800924 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
925 snd_soc_dai_shutdown(cpu_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200926
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900927 soc_rtd_shutdown(rtd, substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200928rtd_startup_err:
Kuninori Morimotodd039072020-02-10 12:14:37 +0900929 soc_pcm_components_close(substream);
Kai Vehmanend2aaa8d2020-02-20 11:49:55 +0200930component_err:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300931 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000932
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900933 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000934 pm_runtime_mark_last_busy(component->dev);
935 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530936 }
937
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900938 for_each_rtd_components(rtd, i, component)
939 if (!component->active)
940 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000941
Liam Girdwoodddee6272011-06-09 14:45:53 +0100942 return ret;
943}
944
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800945static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200946{
947 /*
948 * Currently nothing to do for c2c links
949 * Since c2c links are internal nodes in the DAPM graph and
950 * don't interface with the outside world or application layer
951 * we don't have to do any special handling on close.
952 */
953}
954
Liam Girdwoodddee6272011-06-09 14:45:53 +0100955/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100956 * Called by ALSA when the PCM substream is prepared, can set format, sample
957 * rate, etc. This function is non atomic and can be called multiple times,
958 * it can refer to the runtime info.
959 */
960static int soc_pcm_prepare(struct snd_pcm_substream *substream)
961{
962 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000963 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800964 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200965 struct snd_soc_dai *codec_dai;
966 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100967
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300968 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100969
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900970 ret = soc_rtd_prepare(rtd, substream);
971 if (ret < 0) {
972 dev_err(rtd->card->dev,
973 "ASoC: machine prepare error: %d\n", ret);
974 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975 }
976
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900977 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900978 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000979 if (ret < 0) {
980 dev_err(component->dev,
981 "ASoC: platform prepare error: %d\n", ret);
982 goto out;
983 }
984 }
985
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000986 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900987 ret = snd_soc_dai_prepare(codec_dai, substream);
988 if (ret < 0) {
989 dev_err(codec_dai->dev,
990 "ASoC: codec DAI prepare error: %d\n",
991 ret);
992 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 }
994 }
995
Shreyas NC19bdcc7a2020-02-25 21:39:13 +0800996 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
997 ret = snd_soc_dai_prepare(cpu_dai, substream);
998 if (ret < 0) {
999 dev_err(cpu_dai->dev,
1000 "ASoC: cpu DAI prepare error: %d\n", ret);
1001 goto out;
1002 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003 }
1004
1005 /* cancel any delayed stream shutdown that is pending */
1006 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -06001007 rtd->pop_wait) {
1008 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001009 cancel_delayed_work(&rtd->delayed_work);
1010 }
1011
Liam Girdwoodd9b09512012-03-07 16:32:59 +00001012 snd_soc_dapm_stream_event(rtd, substream->stream,
1013 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001014
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001015 for_each_rtd_codec_dai(rtd, i, codec_dai)
1016 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001017 substream->stream);
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001018 for_each_rtd_cpu_dai(rtd, i, cpu_dai)
1019 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001020
1021out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001022 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001023 return ret;
1024}
1025
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001026static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
1027 unsigned int mask)
1028{
1029 struct snd_interval *interval;
1030 int channels = hweight_long(mask);
1031
1032 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1033 interval->min = channels;
1034 interval->max = channels;
1035}
1036
Charles Keepax244e2932018-06-19 16:22:09 +01001037static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
1038 struct snd_soc_component *last)
1039{
1040 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +01001041 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +09001042 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +01001043
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001044 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +01001045 if (component == last)
1046 break;
1047
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +09001048 r = snd_soc_component_hw_free(component, substream);
1049 if (r < 0)
1050 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +01001051 }
1052
Kuninori Morimotoeae71362019-07-26 13:50:24 +09001053 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +01001054}
1055
Liam Girdwoodddee6272011-06-09 14:45:53 +01001056/*
1057 * Called by ALSA when the hardware params are set by application. This
1058 * function can also be called multiple times and can allocate buffers
1059 * (using snd_pcm_lib_* ). It's non-atomic.
1060 */
1061static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1062 struct snd_pcm_hw_params *params)
1063{
1064 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001065 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001066 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001067 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +01001068 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001069
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001070 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +08001071
1072 ret = soc_pcm_params_symmetry(substream, params);
1073 if (ret)
1074 goto out;
1075
Kuninori Morimotode9ad992020-01-22 09:44:48 +09001076 ret = soc_rtd_hw_params(rtd, substream, params);
1077 if (ret < 0) {
1078 dev_err(rtd->card->dev,
1079 "ASoC: machine hw_params failed: %d\n", ret);
1080 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001081 }
1082
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001083 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001084 struct snd_pcm_hw_params codec_params;
1085
Ricard Wanderlofcde79032015-08-24 14:16:51 +02001086 /*
1087 * Skip CODECs which don't support the current stream type,
1088 * the idea being that if a CODEC is not used for the currently
1089 * set up transfer direction, it should not need to be
1090 * configured, especially since the configuration used might
1091 * not even be supported by that CODEC. There may be cases
1092 * however where a CODEC needs to be set up although it is
1093 * actually not being used for the transfer, e.g. if a
1094 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1095 * for the DAI link including a playback-only CODEC.
1096 * If this becomes necessary, we will have to augment the
1097 * machine driver setup with information on how to act, so
1098 * we can do the right thing here.
1099 */
1100 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1101 continue;
1102
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001103 /* copy params for each codec */
1104 codec_params = *params;
1105
1106 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +08001107 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1108 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001109 soc_pcm_codec_params_fixup(&codec_params,
1110 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +08001111
1112 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1113 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001114 soc_pcm_codec_params_fixup(&codec_params,
1115 codec_dai->rx_mask);
1116
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +09001117 ret = snd_soc_dai_hw_params(codec_dai, substream,
1118 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +02001119 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001120 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001121
1122 codec_dai->rate = params_rate(&codec_params);
1123 codec_dai->channels = params_channels(&codec_params);
1124 codec_dai->sample_bits = snd_pcm_format_physical_width(
1125 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +00001126
1127 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001128 }
1129
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001130 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001131 /*
1132 * Skip CPUs which don't support the current stream
1133 * type. See soc_pcm_init_runtime_hw() for more details
1134 */
1135 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1136 continue;
1137
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001138 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1139 if (ret < 0)
1140 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001141
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001142 /* store the parameters for each DAI */
1143 cpu_dai->rate = params_rate(params);
1144 cpu_dai->channels = params_channels(params);
1145 cpu_dai->sample_bits =
1146 snd_pcm_format_physical_width(params_format(params));
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001147
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001148 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1149 }
Kuninori Morimotoca58221d2019-05-13 16:07:43 +09001150
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001151 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +09001152 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +01001153 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +00001154 dev_err(component->dev,
1155 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +01001156 component->name, ret);
1157 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001158 }
1159 }
Charles Keepax244e2932018-06-19 16:22:09 +01001160 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001161
Liam Girdwoodddee6272011-06-09 14:45:53 +01001162out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001163 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001164 return ret;
1165
Kuninori Morimotob8135862017-10-11 01:37:23 +00001166component_err:
Charles Keepax244e2932018-06-19 16:22:09 +01001167 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001168
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001169 i = rtd->num_cpus;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001170
1171interface_err:
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001172 for_each_rtd_cpu_dai_rollback(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001173 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1174 continue;
1175
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001176 snd_soc_dai_hw_free(cpu_dai, substream);
1177 cpu_dai->rate = 0;
1178 }
1179
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001180 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001181
1182codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +00001183 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001184 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1185 continue;
1186
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001187 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001188 codec_dai->rate = 0;
1189 }
1190
Kuninori Morimoto49f020e2020-01-22 09:44:52 +09001191 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001192
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001193 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001194 return ret;
1195}
1196
1197/*
1198 * Frees resources allocated by hw_params, can be called multiple times
1199 */
1200static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1201{
1202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001203 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001204 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001205 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001206
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001207 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001208
Nicolin Chend3383422013-11-20 18:37:09 +08001209 /* clear the corresponding DAIs parameters when going to be inactive */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001210 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1211 if (cpu_dai->active == 1) {
1212 cpu_dai->rate = 0;
1213 cpu_dai->channels = 0;
1214 cpu_dai->sample_bits = 0;
1215 }
Nicolin Chend3383422013-11-20 18:37:09 +08001216 }
1217
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001218 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001219 if (codec_dai->active == 1) {
1220 codec_dai->rate = 0;
1221 codec_dai->channels = 0;
1222 codec_dai->sample_bits = 0;
1223 }
Nicolin Chend3383422013-11-20 18:37:09 +08001224 }
1225
Liam Girdwoodddee6272011-06-09 14:45:53 +01001226 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001227 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto67ad8772020-03-06 10:10:04 +09001228 int active = codec_dai->stream_active[substream->stream];
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +09001229
Kuninori Morimoto67ad8772020-03-06 10:10:04 +09001230 if (active == 1)
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001231 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001232 substream->stream);
1233 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001234
1235 /* free any machine hw params */
Kuninori Morimoto49f020e2020-01-22 09:44:52 +09001236 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001237
Kuninori Morimotob8135862017-10-11 01:37:23 +00001238 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001239 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001240
Liam Girdwoodddee6272011-06-09 14:45:53 +01001241 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001242 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001243 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1244 continue;
1245
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001246 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001247 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001248
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001249 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1250 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1251 continue;
1252
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001253 snd_soc_dai_hw_free(cpu_dai, substream);
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001254 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001255
Peter Ujfalusi72b745e2019-08-13 13:45:32 +03001256 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001257 return 0;
1258}
1259
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001260static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001261{
1262 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001263 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001264 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001265 struct snd_soc_dai *codec_dai;
1266 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001267
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001268 ret = soc_rtd_trigger(rtd, substream, cmd);
1269 if (ret < 0)
1270 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001271
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001272 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001273 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001274 if (ret < 0)
1275 return ret;
1276 }
1277
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001278 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1279 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1280 if (ret < 0)
1281 return ret;
1282 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001283
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001284 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1285 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1286 if (ret < 0)
1287 return ret;
1288 }
1289
1290 return 0;
1291}
1292
1293static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1294{
1295 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1296 struct snd_soc_component *component;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001297 struct snd_soc_dai *cpu_dai;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001298 struct snd_soc_dai *codec_dai;
1299 int i, ret;
1300
1301 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1302 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1303 if (ret < 0)
1304 return ret;
1305 }
1306
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001307 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1308 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1309 if (ret < 0)
1310 return ret;
1311 }
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001312
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001313 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001314 ret = snd_soc_component_trigger(component, substream, cmd);
1315 if (ret < 0)
1316 return ret;
1317 }
1318
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001319 ret = soc_rtd_trigger(rtd, substream, cmd);
1320 if (ret < 0)
1321 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001322
Liam Girdwoodddee6272011-06-09 14:45:53 +01001323 return 0;
1324}
1325
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001326static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1327{
1328 int ret;
1329
1330 switch (cmd) {
1331 case SNDRV_PCM_TRIGGER_START:
1332 case SNDRV_PCM_TRIGGER_RESUME:
1333 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1334 ret = soc_pcm_trigger_start(substream, cmd);
1335 break;
1336 case SNDRV_PCM_TRIGGER_STOP:
1337 case SNDRV_PCM_TRIGGER_SUSPEND:
1338 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1339 ret = soc_pcm_trigger_stop(substream, cmd);
1340 break;
1341 default:
1342 return -EINVAL;
1343 }
1344
1345 return ret;
1346}
1347
Mark Brown45c0a182012-05-09 21:46:27 +01001348static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1349 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001350{
1351 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001352 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001353 struct snd_soc_dai *codec_dai;
1354 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001355
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001356 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001357 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001358 if (ret < 0)
1359 return ret;
1360 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001361
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001362 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1363 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1364 if (ret < 0)
1365 return ret;
1366 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001367
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001368 return 0;
1369}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001370/*
1371 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001372 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001373 * the runtime->delay will be updated accordingly.
1374 */
1375static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1376{
1377 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001378 struct snd_soc_dai *cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001379 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001380 struct snd_pcm_runtime *runtime = substream->runtime;
1381 snd_pcm_uframes_t offset = 0;
1382 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001383 snd_pcm_sframes_t codec_delay = 0;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001384 snd_pcm_sframes_t cpu_delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001385 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001386
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301387 /* clearing the previous total delay */
1388 runtime->delay = 0;
1389
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001390 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001391
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301392 /* base delay if assigned in pointer callback */
1393 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001394
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001395 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1396 cpu_delay = max(cpu_delay,
1397 snd_soc_dai_delay(cpu_dai, substream));
1398 }
1399 delay += cpu_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001400
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001401 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001402 codec_delay = max(codec_delay,
1403 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001404 }
1405 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001406
Liam Girdwoodddee6272011-06-09 14:45:53 +01001407 runtime->delay = delay;
1408
1409 return offset;
1410}
1411
Liam Girdwood01d75842012-04-25 12:12:49 +01001412/* connect a FE and BE */
1413static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1414 struct snd_soc_pcm_runtime *be, int stream)
1415{
1416 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001417 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001418
1419 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001420 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001421 if (dpcm->be == be && dpcm->fe == fe)
1422 return 0;
1423 }
1424
1425 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1426 if (!dpcm)
1427 return -ENOMEM;
1428
1429 dpcm->be = be;
1430 dpcm->fe = fe;
1431 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1432 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001433 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001434 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1435 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001436 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001437
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001438 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001439 stream ? "capture" : "playback", fe->dai_link->name,
1440 stream ? "<-" : "->", be->dai_link->name);
1441
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001442 dpcm_create_debugfs_state(dpcm, stream);
1443
Liam Girdwood01d75842012-04-25 12:12:49 +01001444 return 1;
1445}
1446
1447/* reparent a BE onto another FE */
1448static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1449 struct snd_soc_pcm_runtime *be, int stream)
1450{
1451 struct snd_soc_dpcm *dpcm;
1452 struct snd_pcm_substream *fe_substream, *be_substream;
1453
1454 /* reparent if BE is connected to other FEs */
1455 if (!be->dpcm[stream].users)
1456 return;
1457
1458 be_substream = snd_soc_dpcm_get_substream(be, stream);
1459
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001460 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001461 if (dpcm->fe == fe)
1462 continue;
1463
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001464 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001465 stream ? "capture" : "playback",
1466 dpcm->fe->dai_link->name,
1467 stream ? "<-" : "->", dpcm->be->dai_link->name);
1468
1469 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1470 be_substream->runtime = fe_substream->runtime;
1471 break;
1472 }
1473}
1474
1475/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001476void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001477{
1478 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001479 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001480
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001481 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001482 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001483 stream ? "capture" : "playback",
1484 dpcm->be->dai_link->name);
1485
1486 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1487 continue;
1488
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001489 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001490 stream ? "capture" : "playback", fe->dai_link->name,
1491 stream ? "<-" : "->", dpcm->be->dai_link->name);
1492
1493 /* BEs still alive need new FE */
1494 dpcm_be_reparent(fe, dpcm->be, stream);
1495
Kuninori Morimoto154dae82020-02-19 15:57:06 +09001496 dpcm_remove_debugfs_state(dpcm);
1497
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001498 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001499 list_del(&dpcm->list_be);
1500 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001501 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001502 kfree(dpcm);
1503 }
1504}
1505
1506/* get BE for DAI widget and stream */
1507static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1508 struct snd_soc_dapm_widget *widget, int stream)
1509{
1510 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001511 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001512 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001513 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001514
Liam Girdwood3c146462018-03-14 20:43:51 +00001515 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1516
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001517 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001518
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001519 if (!be->dai_link->no_pcm)
1520 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001521
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001522 for_each_rtd_cpu_dai(be, i, dai) {
1523 w = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001524
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001525 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1526 w ? w->name : "(not set)");
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001527
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001528 if (w == widget)
1529 return be;
1530 }
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001531
1532 for_each_rtd_codec_dai(be, i, dai) {
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09001533 w = snd_soc_dai_get_widget(dai, stream);
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001534
1535 if (w == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001536 return be;
Liam Girdwood01d75842012-04-25 12:12:49 +01001537 }
1538 }
1539
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001540 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001541 return NULL;
1542}
1543
Liam Girdwood01d75842012-04-25 12:12:49 +01001544static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1545 struct snd_soc_dapm_widget *widget)
1546{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001547 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001548 int i;
1549
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001550 for_each_dapm_widgets(list, i, w)
1551 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001552 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001553
1554 return 0;
1555}
1556
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001557static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1558 enum snd_soc_dapm_direction dir)
1559{
1560 struct snd_soc_card *card = widget->dapm->card;
1561 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001562 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001563
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001564 /* adjust dir to stream */
1565 if (dir == SND_SOC_DAPM_DIR_OUT)
1566 stream = SNDRV_PCM_STREAM_PLAYBACK;
1567 else
1568 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001569
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001570 rtd = dpcm_get_be(card, widget, stream);
1571 if (rtd)
1572 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001573
1574 return false;
1575}
1576
Liam Girdwood23607022014-01-17 17:03:55 +00001577int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001578 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001579{
1580 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001581 int paths;
1582
Bard Liao6e1276a2020-02-25 21:39:16 +08001583 if (fe->num_cpus > 1) {
1584 dev_err(fe->dev,
1585 "%s doesn't support Multi CPU yet\n", __func__);
1586 return -EINVAL;
1587 }
1588
Liam Girdwood01d75842012-04-25 12:12:49 +01001589 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001590 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001591 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001592
Liam Girdwood103d84a2012-11-19 14:39:15 +00001593 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001594 stream ? "capture" : "playback");
1595
Liam Girdwood01d75842012-04-25 12:12:49 +01001596 return paths;
1597}
1598
Kuninori Morimoto52645e332020-02-19 15:56:52 +09001599void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1600{
1601 snd_soc_dapm_dai_free_widgets(list);
1602}
1603
Liam Girdwood01d75842012-04-25 12:12:49 +01001604static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1605 struct snd_soc_dapm_widget_list **list_)
1606{
1607 struct snd_soc_dpcm *dpcm;
1608 struct snd_soc_dapm_widget_list *list = *list_;
1609 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001610 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001611 int prune = 0;
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001612 int do_prune;
Liam Girdwood01d75842012-04-25 12:12:49 +01001613
1614 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001615 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001616 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001617
1618 /* is there a valid CPU DAI widget for this BE */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001619 do_prune = 1;
1620 for_each_rtd_cpu_dai(dpcm->be, i, dai) {
1621 widget = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001622
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001623 /*
1624 * The BE is pruned only if none of the cpu_dai
1625 * widgets are in the active list.
1626 */
1627 if (widget && widget_in_list(list, widget))
1628 do_prune = 0;
1629 }
1630 if (!do_prune)
Liam Girdwood01d75842012-04-25 12:12:49 +01001631 continue;
1632
1633 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001634 do_prune = 1;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001635 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Kuninori Morimoto0c01f6c2020-02-19 15:56:41 +09001636 widget = snd_soc_dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001637
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001638 /* prune the BE if it's no longer in our active list */
1639 if (widget && widget_in_list(list, widget))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001640 do_prune = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001641 }
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001642 if (!do_prune)
1643 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001644
Liam Girdwood103d84a2012-11-19 14:39:15 +00001645 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001646 stream ? "capture" : "playback",
1647 dpcm->be->dai_link->name, fe->dai_link->name);
1648 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1649 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1650 prune++;
1651 }
1652
Liam Girdwood103d84a2012-11-19 14:39:15 +00001653 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001654 return prune;
1655}
1656
1657static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1658 struct snd_soc_dapm_widget_list **list_)
1659{
1660 struct snd_soc_card *card = fe->card;
1661 struct snd_soc_dapm_widget_list *list = *list_;
1662 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001663 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001664 int i, new = 0, err;
1665
1666 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001667 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001668
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001669 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001670 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001671 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1672 continue;
1673 break;
Mark Brown46162742013-06-05 19:36:11 +01001674 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001675 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1676 continue;
Mark Brown46162742013-06-05 19:36:11 +01001677 break;
1678 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001679 continue;
Mark Brown46162742013-06-05 19:36:11 +01001680 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001681
1682 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001683 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001684 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001685 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001686 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001687 continue;
1688 }
1689
Liam Girdwood01d75842012-04-25 12:12:49 +01001690 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001691 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001692 continue;
1693
1694 /* newly connected FE and BE */
1695 err = dpcm_be_connect(fe, be, stream);
1696 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001697 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001698 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001699 break;
1700 } else if (err == 0) /* already connected */
1701 continue;
1702
1703 /* new */
1704 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1705 new++;
1706 }
1707
Liam Girdwood103d84a2012-11-19 14:39:15 +00001708 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001709 return new;
1710}
1711
1712/*
1713 * Find the corresponding BE DAIs that source or sink audio to this
1714 * FE substream.
1715 */
Liam Girdwood23607022014-01-17 17:03:55 +00001716int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001717 int stream, struct snd_soc_dapm_widget_list **list, int new)
1718{
1719 if (new)
1720 return dpcm_add_paths(fe, stream, list);
1721 else
1722 return dpcm_prune_paths(fe, stream, list);
1723}
1724
Liam Girdwood23607022014-01-17 17:03:55 +00001725void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001726{
1727 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001728 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001729
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001730 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001731 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001732 dpcm->be->dpcm[stream].runtime_update =
1733 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001734 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001735}
1736
1737static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1738 int stream)
1739{
1740 struct snd_soc_dpcm *dpcm;
1741
1742 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001743 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001744
1745 struct snd_soc_pcm_runtime *be = dpcm->be;
1746 struct snd_pcm_substream *be_substream =
1747 snd_soc_dpcm_get_substream(be, stream);
1748
1749 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001750 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001751 stream ? "capture" : "playback",
1752 be->dpcm[stream].state);
1753
1754 if (--be->dpcm[stream].users != 0)
1755 continue;
1756
1757 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1758 continue;
1759
1760 soc_pcm_close(be_substream);
1761 be_substream->runtime = NULL;
1762 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1763 }
1764}
1765
Liam Girdwood23607022014-01-17 17:03:55 +00001766int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001767{
1768 struct snd_soc_dpcm *dpcm;
1769 int err, count = 0;
1770
1771 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001772 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001773
1774 struct snd_soc_pcm_runtime *be = dpcm->be;
1775 struct snd_pcm_substream *be_substream =
1776 snd_soc_dpcm_get_substream(be, stream);
1777
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001778 if (!be_substream) {
1779 dev_err(be->dev, "ASoC: no backend %s stream\n",
1780 stream ? "capture" : "playback");
1781 continue;
1782 }
1783
Liam Girdwood01d75842012-04-25 12:12:49 +01001784 /* is this op for this BE ? */
1785 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1786 continue;
1787
1788 /* first time the dpcm is open ? */
1789 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001790 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001791 stream ? "capture" : "playback",
1792 be->dpcm[stream].state);
1793
1794 if (be->dpcm[stream].users++ != 0)
1795 continue;
1796
1797 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1798 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1799 continue;
1800
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001801 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1802 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001803
1804 be_substream->runtime = be->dpcm[stream].runtime;
1805 err = soc_pcm_open(be_substream);
1806 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001807 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001808 be->dpcm[stream].users--;
1809 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001810 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001811 stream ? "capture" : "playback",
1812 be->dpcm[stream].state);
1813
1814 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1815 goto unwind;
1816 }
1817
1818 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1819 count++;
1820 }
1821
1822 return count;
1823
1824unwind:
1825 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001826 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001827 struct snd_soc_pcm_runtime *be = dpcm->be;
1828 struct snd_pcm_substream *be_substream =
1829 snd_soc_dpcm_get_substream(be, stream);
1830
1831 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1832 continue;
1833
1834 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 stream ? "capture" : "playback",
1837 be->dpcm[stream].state);
1838
1839 if (--be->dpcm[stream].users != 0)
1840 continue;
1841
1842 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1843 continue;
1844
1845 soc_pcm_close(be_substream);
1846 be_substream->runtime = NULL;
1847 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1848 }
1849
1850 return err;
1851}
1852
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001853static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001854 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001855{
1856 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001857 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001858 runtime->hw.channels_min = stream->channels_min;
1859 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001860 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001861 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001862 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001863 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001864 runtime->hw.rates = stream->rates;
1865}
1866
Jerome Brunet435ffb72018-07-05 12:13:48 +02001867static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1868 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001869{
1870 struct snd_soc_pcm_runtime *fe = substream->private_data;
1871 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001872 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001873 int stream = substream->stream;
1874
1875 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001876 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001877
1878 /*
1879 * It returns merged BE codec format
1880 * if FE want to use it (= dpcm_merged_format)
1881 */
1882
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001883 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001884 struct snd_soc_pcm_runtime *be = dpcm->be;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001885 struct snd_soc_pcm_stream *codec_stream;
1886 int i;
1887
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001888 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001889 /*
1890 * Skip CODECs which don't support the current stream
1891 * type. See soc_pcm_init_runtime_hw() for more details
1892 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001893 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001894 continue;
1895
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001896 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001897
Jerome Brunet435ffb72018-07-05 12:13:48 +02001898 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001899 }
1900 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001901}
1902
Jerome Brunet435ffb72018-07-05 12:13:48 +02001903static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1904 unsigned int *channels_min,
1905 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001906{
1907 struct snd_soc_pcm_runtime *fe = substream->private_data;
1908 struct snd_soc_dpcm *dpcm;
1909 int stream = substream->stream;
1910
1911 if (!fe->dai_link->dpcm_merged_chan)
1912 return;
1913
Jiada Wangf4c277b2018-06-20 18:25:20 +09001914 /*
1915 * It returns merged BE codec channel;
1916 * if FE want to use it (= dpcm_merged_chan)
1917 */
1918
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001919 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001920 struct snd_soc_pcm_runtime *be = dpcm->be;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001921 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001922 struct snd_soc_pcm_stream *cpu_stream;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001923 struct snd_soc_dai *dai;
1924 int i;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001925
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001926 for_each_rtd_cpu_dai(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001927 /*
1928 * Skip CPUs which don't support the current stream
1929 * type. See soc_pcm_init_runtime_hw() for more details
1930 */
1931 if (!snd_soc_dai_stream_valid(dai, stream))
1932 continue;
1933
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001934 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001935
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001936 *channels_min = max(*channels_min,
1937 cpu_stream->channels_min);
1938 *channels_max = min(*channels_max,
1939 cpu_stream->channels_max);
1940 }
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001941
1942 /*
1943 * chan min/max cannot be enforced if there are multiple CODEC
1944 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1945 */
1946 if (be->num_codecs == 1) {
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09001947 codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001948
1949 *channels_min = max(*channels_min,
1950 codec_stream->channels_min);
1951 *channels_max = min(*channels_max,
1952 codec_stream->channels_max);
1953 }
1954 }
1955}
1956
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001957static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1958 unsigned int *rates,
1959 unsigned int *rate_min,
1960 unsigned int *rate_max)
1961{
1962 struct snd_soc_pcm_runtime *fe = substream->private_data;
1963 struct snd_soc_dpcm *dpcm;
1964 int stream = substream->stream;
1965
1966 if (!fe->dai_link->dpcm_merged_rate)
1967 return;
1968
1969 /*
1970 * It returns merged BE codec channel;
1971 * if FE want to use it (= dpcm_merged_chan)
1972 */
1973
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001974 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001975 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001976 struct snd_soc_pcm_stream *codec_stream;
1977 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001978 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001979 int i;
1980
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001981 for_each_rtd_cpu_dai(be, i, dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08001982 /*
1983 * Skip CPUs which don't support the current stream
1984 * type. See soc_pcm_init_runtime_hw() for more details
1985 */
1986 if (!snd_soc_dai_stream_valid(dai, stream))
1987 continue;
1988
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001989 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001990
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08001991 *rate_min = max(*rate_min, cpu_stream->rate_min);
1992 *rate_max = min_not_zero(*rate_max,
1993 cpu_stream->rate_max);
1994 *rates = snd_pcm_rate_mask_intersect(*rates,
1995 cpu_stream->rates);
1996 }
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001997
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001998 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001999 /*
2000 * Skip CODECs which don't support the current stream
2001 * type. See soc_pcm_init_runtime_hw() for more details
2002 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002003 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02002004 continue;
2005
Kuninori Morimotoacf253c2020-02-19 15:56:30 +09002006 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02002007
2008 *rate_min = max(*rate_min, codec_stream->rate_min);
2009 *rate_max = min_not_zero(*rate_max,
2010 codec_stream->rate_max);
2011 *rates = snd_pcm_rate_mask_intersect(*rates,
2012 codec_stream->rates);
2013 }
2014 }
2015}
2016
Mark Brown45c0a182012-05-09 21:46:27 +01002017static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002018{
2019 struct snd_pcm_runtime *runtime = substream->runtime;
2020 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002021 struct snd_soc_dai *cpu_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002022 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002023
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002024 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
Bard Liao0e9cf4c42020-02-25 21:39:17 +08002025 /*
2026 * Skip CPUs which don't support the current stream
2027 * type. See soc_pcm_init_runtime_hw() for more details
2028 */
2029 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
2030 continue;
2031
Kuninori Morimoto0c9ba722020-03-06 10:09:54 +09002032 dpcm_init_runtime_hw(runtime,
2033 snd_soc_dai_get_pcm_stream(cpu_dai,
2034 substream->stream));
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002035 }
Jiada Wangf4c277b2018-06-20 18:25:20 +09002036
Jerome Brunet435ffb72018-07-05 12:13:48 +02002037 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
2038 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
2039 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02002040 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
2041 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01002042}
2043
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002044static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
2045
2046/* Set FE's runtime_update state; the state is protected via PCM stream lock
2047 * for avoiding the race with trigger callback.
2048 * If the state is unset and a trigger is pending while the previous operation,
2049 * process the pending trigger action here.
2050 */
2051static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2052 int stream, enum snd_soc_dpcm_update state)
2053{
2054 struct snd_pcm_substream *substream =
2055 snd_soc_dpcm_get_substream(fe, stream);
2056
2057 snd_pcm_stream_lock_irq(substream);
2058 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2059 dpcm_fe_dai_do_trigger(substream,
2060 fe->dpcm[stream].trigger_pending - 1);
2061 fe->dpcm[stream].trigger_pending = 0;
2062 }
2063 fe->dpcm[stream].runtime_update = state;
2064 snd_pcm_stream_unlock_irq(substream);
2065}
2066
PC Liao906c7d62015-12-11 11:33:51 +08002067static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
2068 int stream)
2069{
2070 struct snd_soc_dpcm *dpcm;
2071 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002072 struct snd_soc_dai *fe_cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08002073 int err;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002074 int i;
PC Liao906c7d62015-12-11 11:33:51 +08002075
2076 /* apply symmetry for FE */
2077 if (soc_pcm_has_symmetry(fe_substream))
2078 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2079
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002080 for_each_rtd_cpu_dai (fe, i, fe_cpu_dai) {
2081 /* Symmetry only applies if we've got an active stream. */
2082 if (fe_cpu_dai->active) {
2083 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
2084 if (err < 0)
2085 return err;
2086 }
PC Liao906c7d62015-12-11 11:33:51 +08002087 }
2088
2089 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002090 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08002091 struct snd_soc_pcm_runtime *be = dpcm->be;
2092 struct snd_pcm_substream *be_substream =
2093 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02002094 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002095 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002096 struct snd_soc_dai *cpu_dai;
PC Liao906c7d62015-12-11 11:33:51 +08002097 int i;
2098
Jerome Brunet6246f282019-04-01 15:03:54 +02002099 /* A backend may not have the requested substream */
2100 if (!be_substream)
2101 continue;
2102
2103 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05302104 if (rtd->dai_link->be_hw_params_fixup)
2105 continue;
2106
PC Liao906c7d62015-12-11 11:33:51 +08002107 if (soc_pcm_has_symmetry(be_substream))
2108 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2109
2110 /* Symmetry only applies if we've got an active stream. */
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08002111 for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
2112 if (cpu_dai->active) {
2113 err = soc_pcm_apply_symmetry(fe_substream,
2114 cpu_dai);
2115 if (err < 0)
2116 return err;
2117 }
PC Liao906c7d62015-12-11 11:33:51 +08002118 }
2119
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002120 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2121 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08002122 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002123 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08002124 if (err < 0)
2125 return err;
2126 }
2127 }
2128 }
2129
2130 return 0;
2131}
2132
Liam Girdwood01d75842012-04-25 12:12:49 +01002133static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
2134{
2135 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2136 struct snd_pcm_runtime *runtime = fe_substream->runtime;
2137 int stream = fe_substream->stream, ret = 0;
2138
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002139 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002140
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002141 ret = dpcm_be_dai_startup(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002142 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002143 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002144 goto be_err;
2145 }
2146
Liam Girdwood103d84a2012-11-19 14:39:15 +00002147 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002148
2149 /* start the DAI frontend */
2150 ret = soc_pcm_open(fe_substream);
2151 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002152 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002153 goto unwind;
2154 }
2155
2156 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2157
2158 dpcm_set_fe_runtime(fe_substream);
2159 snd_pcm_limit_hw_rates(runtime);
2160
PC Liao906c7d62015-12-11 11:33:51 +08002161 ret = dpcm_apply_symmetry(fe_substream, stream);
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09002162 if (ret < 0)
PC Liao906c7d62015-12-11 11:33:51 +08002163 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2164 ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002165
2166unwind:
Kuninori Morimoto8a01fbf2020-03-06 10:09:59 +09002167 if (ret < 0)
2168 dpcm_be_dai_startup_unwind(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002169be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002170 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002171 return ret;
2172}
2173
Liam Girdwood23607022014-01-17 17:03:55 +00002174int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002175{
2176 struct snd_soc_dpcm *dpcm;
2177
2178 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002179 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002180
2181 struct snd_soc_pcm_runtime *be = dpcm->be;
2182 struct snd_pcm_substream *be_substream =
2183 snd_soc_dpcm_get_substream(be, stream);
2184
2185 /* is this op for this BE ? */
2186 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2187 continue;
2188
2189 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002190 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002191 stream ? "capture" : "playback",
2192 be->dpcm[stream].state);
2193
2194 if (--be->dpcm[stream].users != 0)
2195 continue;
2196
2197 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08002198 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2199 soc_pcm_hw_free(be_substream);
2200 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2201 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002202
Liam Girdwood103d84a2012-11-19 14:39:15 +00002203 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002204 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002205
2206 soc_pcm_close(be_substream);
2207 be_substream->runtime = NULL;
2208
2209 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2210 }
2211 return 0;
2212}
2213
2214static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2215{
2216 struct snd_soc_pcm_runtime *fe = substream->private_data;
2217 int stream = substream->stream;
2218
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002219 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002220
2221 /* shutdown the BEs */
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002222 dpcm_be_dai_shutdown(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002223
Liam Girdwood103d84a2012-11-19 14:39:15 +00002224 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002225
2226 /* now shutdown the frontend */
2227 soc_pcm_close(substream);
2228
2229 /* run the stream event for each BE */
Kuninori Morimoto1c531232020-02-21 10:25:18 +09002230 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
Liam Girdwood01d75842012-04-25 12:12:49 +01002231
2232 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002233 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002234 return 0;
2235}
2236
Liam Girdwood23607022014-01-17 17:03:55 +00002237int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002238{
2239 struct snd_soc_dpcm *dpcm;
2240
2241 /* only hw_params backends that are either sinks or sources
2242 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002243 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002244
2245 struct snd_soc_pcm_runtime *be = dpcm->be;
2246 struct snd_pcm_substream *be_substream =
2247 snd_soc_dpcm_get_substream(be, stream);
2248
2249 /* is this op for this BE ? */
2250 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2251 continue;
2252
2253 /* only free hw when no longer used - check all FEs */
2254 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2255 continue;
2256
Qiao Zhou36fba622014-12-03 10:13:43 +08002257 /* do not free hw if this BE is used by other FE */
2258 if (be->dpcm[stream].users > 1)
2259 continue;
2260
Liam Girdwood01d75842012-04-25 12:12:49 +01002261 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2262 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2263 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002264 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302265 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2266 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002267 continue;
2268
Liam Girdwood103d84a2012-11-19 14:39:15 +00002269 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002270 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002271
2272 soc_pcm_hw_free(be_substream);
2273
2274 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2275 }
2276
2277 return 0;
2278}
2279
Mark Brown45c0a182012-05-09 21:46:27 +01002280static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002281{
2282 struct snd_soc_pcm_runtime *fe = substream->private_data;
2283 int err, stream = substream->stream;
2284
2285 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002286 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002287
Liam Girdwood103d84a2012-11-19 14:39:15 +00002288 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002289
2290 /* call hw_free on the frontend */
2291 err = soc_pcm_hw_free(substream);
2292 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002293 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002294 fe->dai_link->name);
2295
2296 /* only hw_params backends that are either sinks or sources
2297 * to this frontend DAI */
2298 err = dpcm_be_dai_hw_free(fe, stream);
2299
2300 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002301 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002302
2303 mutex_unlock(&fe->card->mutex);
2304 return 0;
2305}
2306
Liam Girdwood23607022014-01-17 17:03:55 +00002307int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002308{
2309 struct snd_soc_dpcm *dpcm;
2310 int ret;
2311
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002312 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002313
2314 struct snd_soc_pcm_runtime *be = dpcm->be;
2315 struct snd_pcm_substream *be_substream =
2316 snd_soc_dpcm_get_substream(be, stream);
2317
2318 /* is this op for this BE ? */
2319 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2320 continue;
2321
Liam Girdwood01d75842012-04-25 12:12:49 +01002322 /* copy params for each dpcm */
2323 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2324 sizeof(struct snd_pcm_hw_params));
2325
2326 /* perform any hw_params fixups */
2327 if (be->dai_link->be_hw_params_fixup) {
2328 ret = be->dai_link->be_hw_params_fixup(be,
2329 &dpcm->hw_params);
2330 if (ret < 0) {
2331 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002332 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002333 ret);
2334 goto unwind;
2335 }
2336 }
2337
Libin Yangae061d22019-04-19 09:53:12 +08002338 /* copy the fixed-up hw params for BE dai */
2339 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2340 sizeof(struct snd_pcm_hw_params));
2341
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002342 /* only allow hw_params() if no connected FEs are running */
2343 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2344 continue;
2345
2346 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2347 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2348 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2349 continue;
2350
2351 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002352 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002353
Liam Girdwood01d75842012-04-25 12:12:49 +01002354 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2355 if (ret < 0) {
2356 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002357 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002358 goto unwind;
2359 }
2360
2361 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2362 }
2363 return 0;
2364
2365unwind:
2366 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002367 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002368 struct snd_soc_pcm_runtime *be = dpcm->be;
2369 struct snd_pcm_substream *be_substream =
2370 snd_soc_dpcm_get_substream(be, stream);
2371
2372 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2373 continue;
2374
2375 /* only allow hw_free() if no connected FEs are running */
2376 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2377 continue;
2378
2379 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2380 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2381 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2382 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2383 continue;
2384
2385 soc_pcm_hw_free(be_substream);
2386 }
2387
2388 return ret;
2389}
2390
Mark Brown45c0a182012-05-09 21:46:27 +01002391static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2392 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002393{
2394 struct snd_soc_pcm_runtime *fe = substream->private_data;
2395 int ret, stream = substream->stream;
2396
2397 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002398 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002399
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002400 memcpy(&fe->dpcm[stream].hw_params, params,
Liam Girdwood01d75842012-04-25 12:12:49 +01002401 sizeof(struct snd_pcm_hw_params));
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002402 ret = dpcm_be_dai_hw_params(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002403 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002404 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002405 goto out;
2406 }
2407
Liam Girdwood103d84a2012-11-19 14:39:15 +00002408 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002409 fe->dai_link->name, params_rate(params),
2410 params_channels(params), params_format(params));
2411
2412 /* call hw_params on the frontend */
2413 ret = soc_pcm_hw_params(substream, params);
2414 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002415 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002416 dpcm_be_dai_hw_free(fe, stream);
2417 } else
2418 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2419
2420out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002421 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002422 mutex_unlock(&fe->card->mutex);
2423 return ret;
2424}
2425
2426static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2427 struct snd_pcm_substream *substream, int cmd)
2428{
2429 int ret;
2430
Liam Girdwood103d84a2012-11-19 14:39:15 +00002431 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002432 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002433
2434 ret = soc_pcm_trigger(substream, cmd);
2435 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002436 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002437
2438 return ret;
2439}
2440
Liam Girdwood23607022014-01-17 17:03:55 +00002441int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002442 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002443{
2444 struct snd_soc_dpcm *dpcm;
2445 int ret = 0;
2446
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002447 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002448
2449 struct snd_soc_pcm_runtime *be = dpcm->be;
2450 struct snd_pcm_substream *be_substream =
2451 snd_soc_dpcm_get_substream(be, stream);
2452
2453 /* is this op for this BE ? */
2454 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2455 continue;
2456
2457 switch (cmd) {
2458 case SNDRV_PCM_TRIGGER_START:
2459 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2460 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2461 continue;
2462
2463 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2464 if (ret)
2465 return ret;
2466
2467 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2468 break;
2469 case SNDRV_PCM_TRIGGER_RESUME:
2470 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2471 continue;
2472
2473 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2474 if (ret)
2475 return ret;
2476
2477 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2478 break;
2479 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2480 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2481 continue;
2482
2483 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2484 if (ret)
2485 return ret;
2486
2487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2488 break;
2489 case SNDRV_PCM_TRIGGER_STOP:
2490 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2491 continue;
2492
2493 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2494 continue;
2495
2496 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2497 if (ret)
2498 return ret;
2499
2500 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2501 break;
2502 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002503 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002504 continue;
2505
2506 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2507 continue;
2508
2509 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2510 if (ret)
2511 return ret;
2512
2513 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2514 break;
2515 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2516 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2517 continue;
2518
2519 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2520 continue;
2521
2522 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2523 if (ret)
2524 return ret;
2525
2526 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2527 break;
2528 }
2529 }
2530
2531 return ret;
2532}
2533EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2534
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002535static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2536 int cmd, bool fe_first)
2537{
2538 struct snd_soc_pcm_runtime *fe = substream->private_data;
2539 int ret;
2540
2541 /* call trigger on the frontend before the backend. */
2542 if (fe_first) {
2543 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2544 fe->dai_link->name, cmd);
2545
2546 ret = soc_pcm_trigger(substream, cmd);
2547 if (ret < 0)
2548 return ret;
2549
2550 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2551 return ret;
2552 }
2553
2554 /* call trigger on the frontend after the backend. */
2555 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2556 if (ret < 0)
2557 return ret;
2558
2559 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2560 fe->dai_link->name, cmd);
2561
2562 ret = soc_pcm_trigger(substream, cmd);
2563
2564 return ret;
2565}
2566
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002567static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002568{
2569 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002570 int stream = substream->stream;
2571 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002572 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2573
2574 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2575
2576 switch (trigger) {
2577 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002578 switch (cmd) {
2579 case SNDRV_PCM_TRIGGER_START:
2580 case SNDRV_PCM_TRIGGER_RESUME:
2581 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2582 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2583 break;
2584 case SNDRV_PCM_TRIGGER_STOP:
2585 case SNDRV_PCM_TRIGGER_SUSPEND:
2586 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2587 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2588 break;
2589 default:
2590 ret = -EINVAL;
2591 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002592 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002593 break;
2594 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002595 switch (cmd) {
2596 case SNDRV_PCM_TRIGGER_START:
2597 case SNDRV_PCM_TRIGGER_RESUME:
2598 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2599 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2600 break;
2601 case SNDRV_PCM_TRIGGER_STOP:
2602 case SNDRV_PCM_TRIGGER_SUSPEND:
2603 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2604 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2605 break;
2606 default:
2607 ret = -EINVAL;
2608 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002609 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002610 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002611 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2612 /* bespoke trigger() - handles both FE and BEs */
2613
Liam Girdwood103d84a2012-11-19 14:39:15 +00002614 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002615 fe->dai_link->name, cmd);
2616
2617 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002618 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002619 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002620 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002621 fe->dai_link->name);
2622 ret = -EINVAL;
2623 goto out;
2624 }
2625
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002626 if (ret < 0) {
2627 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2628 cmd, ret);
2629 goto out;
2630 }
2631
Liam Girdwood01d75842012-04-25 12:12:49 +01002632 switch (cmd) {
2633 case SNDRV_PCM_TRIGGER_START:
2634 case SNDRV_PCM_TRIGGER_RESUME:
2635 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2636 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2637 break;
2638 case SNDRV_PCM_TRIGGER_STOP:
2639 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002640 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2641 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002642 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2643 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2644 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002645 }
2646
2647out:
2648 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2649 return ret;
2650}
2651
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002652static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2653{
2654 struct snd_soc_pcm_runtime *fe = substream->private_data;
2655 int stream = substream->stream;
2656
2657 /* if FE's runtime_update is already set, we're in race;
2658 * process this trigger later at exit
2659 */
2660 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2661 fe->dpcm[stream].trigger_pending = cmd + 1;
2662 return 0; /* delayed, assuming it's successful */
2663 }
2664
2665 /* we're alone, let's trigger */
2666 return dpcm_fe_dai_do_trigger(substream, cmd);
2667}
2668
Liam Girdwood23607022014-01-17 17:03:55 +00002669int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002670{
2671 struct snd_soc_dpcm *dpcm;
2672 int ret = 0;
2673
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002674 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002675
2676 struct snd_soc_pcm_runtime *be = dpcm->be;
2677 struct snd_pcm_substream *be_substream =
2678 snd_soc_dpcm_get_substream(be, stream);
2679
2680 /* is this op for this BE ? */
2681 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2682 continue;
2683
2684 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002685 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002686 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2687 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002688 continue;
2689
Liam Girdwood103d84a2012-11-19 14:39:15 +00002690 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002691 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002692
2693 ret = soc_pcm_prepare(be_substream);
2694 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002695 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002696 ret);
2697 break;
2698 }
2699
2700 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2701 }
2702 return ret;
2703}
2704
Mark Brown45c0a182012-05-09 21:46:27 +01002705static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002706{
2707 struct snd_soc_pcm_runtime *fe = substream->private_data;
2708 int stream = substream->stream, ret = 0;
2709
2710 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2711
Liam Girdwood103d84a2012-11-19 14:39:15 +00002712 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002713
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002714 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002715
2716 /* there is no point preparing this FE if there are no BEs */
2717 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002718 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002719 fe->dai_link->name);
2720 ret = -EINVAL;
2721 goto out;
2722 }
2723
Kuninori Morimoto25c2f512020-02-27 10:54:38 +09002724 ret = dpcm_be_dai_prepare(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01002725 if (ret < 0)
2726 goto out;
2727
2728 /* call prepare on the frontend */
2729 ret = soc_pcm_prepare(substream);
2730 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002731 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002732 fe->dai_link->name);
2733 goto out;
2734 }
2735
2736 /* run the stream event for each BE */
2737 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2738 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2739
2740out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002741 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002742 mutex_unlock(&fe->card->mutex);
2743
2744 return ret;
2745}
2746
Liam Girdwood618dae12012-04-25 12:12:51 +01002747static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2748{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002749 struct snd_pcm_substream *substream =
2750 snd_soc_dpcm_get_substream(fe, stream);
2751 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002752 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002753
Liam Girdwood103d84a2012-11-19 14:39:15 +00002754 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002755 stream ? "capture" : "playback", fe->dai_link->name);
2756
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002757 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2758 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002759 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002760 fe->dai_link->name);
2761
2762 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2763 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002764 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002765 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002766 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002767 fe->dai_link->name);
2768
2769 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2770 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002771 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002772 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002773
2774 err = dpcm_be_dai_hw_free(fe, stream);
2775 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002776 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002777
2778 err = dpcm_be_dai_shutdown(fe, stream);
2779 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002780 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002781
2782 /* run the stream event for each BE */
2783 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2784
2785 return 0;
2786}
2787
2788static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2789{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002790 struct snd_pcm_substream *substream =
2791 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002792 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002793 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002794 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002795 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002796
Liam Girdwood103d84a2012-11-19 14:39:15 +00002797 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002798 stream ? "capture" : "playback", fe->dai_link->name);
2799
2800 /* Only start the BE if the FE is ready */
2801 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2802 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2803 return -EINVAL;
2804
2805 /* startup must always be called for new BEs */
2806 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002807 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002808 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002809
2810 /* keep going if FE state is > open */
2811 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2812 return 0;
2813
2814 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002815 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002816 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002817
2818 /* keep going if FE state is > hw_params */
2819 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2820 return 0;
2821
2822
2823 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002824 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002825 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002826
2827 /* run the stream event for each BE */
2828 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2829
2830 /* keep going if FE state is > prepare */
2831 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2832 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2833 return 0;
2834
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002835 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2836 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002837 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002838 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002839
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002840 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2841 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002842 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002843 goto hw_free;
2844 }
2845 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002846 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002847 fe->dai_link->name);
2848
2849 ret = dpcm_be_dai_trigger(fe, stream,
2850 SNDRV_PCM_TRIGGER_START);
2851 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002852 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002853 goto hw_free;
2854 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002855 }
2856
2857 return 0;
2858
2859hw_free:
2860 dpcm_be_dai_hw_free(fe, stream);
2861close:
2862 dpcm_be_dai_shutdown(fe, stream);
2863disconnect:
2864 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002865 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002866 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002867 struct snd_soc_pcm_runtime *be = dpcm->be;
2868 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2869 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2870 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002871 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002872
2873 return ret;
2874}
2875
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002876static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2877{
2878 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002879 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002880 int count, paths;
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002881 int ret;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002882
Bard Liao6e1276a2020-02-25 21:39:16 +08002883 if (fe->num_cpus > 1) {
2884 dev_err(fe->dev,
2885 "%s doesn't support Multi CPU yet\n", __func__);
2886 return -EINVAL;
2887 }
2888
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002889 if (!fe->dai_link->dynamic)
2890 return 0;
2891
2892 /* only check active links */
2893 if (!fe->cpu_dai->active)
2894 return 0;
2895
2896 /* DAPM sync will call this to update DSP paths */
2897 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2898 new ? "new" : "old", fe->dai_link->name);
2899
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002900 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002901
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002902 /* skip if FE doesn't have playback/capture capability */
2903 if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) ||
2904 !snd_soc_dai_stream_valid(fe->codec_dai, stream))
2905 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002906
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002907 /* skip if FE isn't currently playing/capturing */
2908 if (!fe->cpu_dai->stream_active[stream] ||
2909 !fe->codec_dai->stream_active[stream])
2910 continue;
2911
2912 paths = dpcm_path_get(fe, stream, &list);
2913 if (paths < 0) {
2914 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2915 fe->dai_link->name,
2916 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2917 "playback" : "capture");
2918 return paths;
2919 }
2920
2921 /* update any playback/capture paths */
2922 count = dpcm_process_paths(fe, stream, &list, new);
2923 if (count) {
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002924 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002925 if (new)
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002926 ret = dpcm_run_update_startup(fe, stream);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002927 else
Kuninori Morimoto580dff32020-02-19 15:56:46 +09002928 ret = dpcm_run_update_shutdown(fe, stream);
2929 if (ret < 0)
2930 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2931 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002932
2933 dpcm_clear_pending_state(fe, stream);
2934 dpcm_be_disconnect(fe, stream);
2935 }
2936
2937 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002938 }
2939
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002940 return 0;
2941}
2942
Liam Girdwood618dae12012-04-25 12:12:51 +01002943/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2944 * any DAI links.
2945 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002946int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002947{
Mengdong Lin1a497982015-11-18 02:34:11 -05002948 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002949 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002950
Liam Girdwood618dae12012-04-25 12:12:51 +01002951 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002952 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002953 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002954 ret = soc_dpcm_fe_runtime_update(fe, 0);
2955 if (ret)
2956 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002957 }
2958
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002959 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002960 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002961 ret = soc_dpcm_fe_runtime_update(fe, 1);
2962 if (ret)
2963 goto out;
2964 }
2965
2966out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002967 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002968 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002969}
Liam Girdwood01d75842012-04-25 12:12:49 +01002970
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002971static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002972{
2973 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2974 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002975 int stream = fe_substream->stream;
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002976
2977 /* mark FE's links ready to prune */
2978 for_each_dpcm_be(fe, stream, dpcm)
2979 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2980
2981 dpcm_be_disconnect(fe, stream);
2982
2983 fe->dpcm[stream].runtime = NULL;
Kuninori Morimoto265694b2020-03-06 10:09:49 +09002984}
2985
2986static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2987{
2988 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2989 int ret;
2990
2991 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2992 ret = dpcm_fe_dai_shutdown(fe_substream);
2993
2994 dpcm_fe_dai_cleanup(fe_substream);
2995
Kuninori Morimoto30fca262020-03-06 10:09:44 +09002996 mutex_unlock(&fe->card->mutex);
2997 return ret;
2998}
2999
Mark Brown45c0a182012-05-09 21:46:27 +01003000static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01003001{
3002 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
Liam Girdwood01d75842012-04-25 12:12:49 +01003003 struct snd_soc_dapm_widget_list *list;
3004 int ret;
3005 int stream = fe_substream->stream;
3006
3007 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
3008 fe->dpcm[stream].runtime = fe_substream->runtime;
3009
Qiao Zhou8f70e512014-09-10 17:54:07 +08003010 ret = dpcm_path_get(fe, stream, &list);
3011 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09003012 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08003013 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003014 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01003015 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01003016 }
3017
3018 /* calculate valid and active FE <-> BE dpcms */
3019 dpcm_process_paths(fe, stream, &list, 1);
3020
3021 ret = dpcm_fe_dai_startup(fe_substream);
Kuninori Morimoto265694b2020-03-06 10:09:49 +09003022 if (ret < 0)
3023 dpcm_fe_dai_cleanup(fe_substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01003024
3025 dpcm_clear_pending_state(fe, stream);
3026 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09003027open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01003028 mutex_unlock(&fe->card->mutex);
3029 return ret;
3030}
3031
Liam Girdwoodddee6272011-06-09 14:45:53 +01003032/* create a new pcm */
3033int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3034{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003035 struct snd_soc_dai *codec_dai;
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08003036 struct snd_soc_dai *cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09003037 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003038 struct snd_pcm *pcm;
3039 char new_name[64];
3040 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003041 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003042
Liam Girdwood01d75842012-04-25 12:12:49 +01003043 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00003044 playback = rtd->dai_link->dpcm_playback;
3045 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01003046 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02003047 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01003048 int cpu_capture = rtd->dai_link->params ?
3049 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
3050 int cpu_playback = rtd->dai_link->params ?
3051 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02003052
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00003053 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08003054 if (rtd->num_cpus == 1) {
3055 cpu_dai = rtd->cpu_dais[0];
3056 } else if (rtd->num_cpus == rtd->num_codecs) {
3057 cpu_dai = rtd->cpu_dais[i];
3058 } else {
3059 dev_err(rtd->card->dev,
3060 "N cpus to M codecs link is not supported yet\n");
3061 return -EINVAL;
3062 }
3063
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003064 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01003065 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003066 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003067 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01003068 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003069 capture = 1;
3070 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003071 }
Sangsu Parka5002312012-01-02 17:15:10 +09003072
Fabio Estevamd6bead02013-08-29 10:32:13 -03003073 if (rtd->dai_link->playback_only) {
3074 playback = 1;
3075 capture = 0;
3076 }
3077
3078 if (rtd->dai_link->capture_only) {
3079 playback = 0;
3080 capture = 1;
3081 }
3082
Liam Girdwood01d75842012-04-25 12:12:49 +01003083 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02003084 if (rtd->dai_link->params) {
3085 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
3086 rtd->dai_link->stream_name);
3087
3088 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3089 playback, capture, &pcm);
3090 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003091 snprintf(new_name, sizeof(new_name), "(%s)",
3092 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003093
Liam Girdwood01d75842012-04-25 12:12:49 +01003094 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3095 playback, capture, &pcm);
3096 } else {
3097 if (rtd->dai_link->dynamic)
3098 snprintf(new_name, sizeof(new_name), "%s (*)",
3099 rtd->dai_link->stream_name);
3100 else
3101 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003102 rtd->dai_link->stream_name,
3103 (rtd->num_codecs > 1) ?
3104 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003105
Liam Girdwood01d75842012-04-25 12:12:49 +01003106 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3107 capture, &pcm);
3108 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01003109 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003110 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01003111 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003112 return ret;
3113 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00003114 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003115
3116 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02003117 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08003118 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02003119 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09003120 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003121
Vinod Koul48c76992015-02-12 09:59:53 +05303122 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003123 rtd->pcm = pcm;
3124 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003125
Jerome Bruneta3420312019-07-25 18:59:47 +02003126 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003127 if (playback)
3128 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3129 if (capture)
3130 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3131 goto out;
3132 }
3133
3134 /* ASoC PCM operations */
3135 if (rtd->dai_link->dynamic) {
3136 rtd->ops.open = dpcm_fe_dai_open;
3137 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3138 rtd->ops.prepare = dpcm_fe_dai_prepare;
3139 rtd->ops.trigger = dpcm_fe_dai_trigger;
3140 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3141 rtd->ops.close = dpcm_fe_dai_close;
3142 rtd->ops.pointer = soc_pcm_pointer;
3143 } else {
3144 rtd->ops.open = soc_pcm_open;
3145 rtd->ops.hw_params = soc_pcm_hw_params;
3146 rtd->ops.prepare = soc_pcm_prepare;
3147 rtd->ops.trigger = soc_pcm_trigger;
3148 rtd->ops.hw_free = soc_pcm_hw_free;
3149 rtd->ops.close = soc_pcm_close;
3150 rtd->ops.pointer = soc_pcm_pointer;
3151 }
3152
Kuninori Morimoto613fb502020-01-10 11:35:21 +09003153 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09003154 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003155
Takashi Iwai3b1c9522019-11-21 20:07:08 +01003156 if (drv->ioctl)
3157 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01003158 if (drv->sync_stop)
3159 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003160 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09003161 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003162 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09003163 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09003164 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09003165 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003166 }
3167
Liam Girdwoodddee6272011-06-09 14:45:53 +01003168 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003169 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003170
3171 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003172 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003173
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09003174 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09003175 if (ret < 0) {
3176 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3177 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003178 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003179
Takashi Iwai3d21ef02019-01-11 15:58:39 +01003180 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01003181out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003182 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3183 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
Shreyas NC19bdcc7a2020-02-25 21:39:13 +08003184 (rtd->num_cpus > 1) ? "multicpu" : rtd->cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003185 return ret;
3186}
Liam Girdwood01d75842012-04-25 12:12:49 +01003187
3188/* is the current PCM operation for this FE ? */
3189int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3190{
3191 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3192 return 1;
3193 return 0;
3194}
3195EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3196
3197/* is the current PCM operation for this BE ? */
3198int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3199 struct snd_soc_pcm_runtime *be, int stream)
3200{
3201 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3202 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3203 be->dpcm[stream].runtime_update))
3204 return 1;
3205 return 0;
3206}
3207EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3208
3209/* get the substream for this BE */
3210struct snd_pcm_substream *
3211 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3212{
3213 return be->pcm->streams[stream].substream;
3214}
3215EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3216
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003217static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3218 struct snd_soc_pcm_runtime *be,
3219 int stream,
3220 const enum snd_soc_dpcm_state *states,
3221 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01003222{
3223 struct snd_soc_dpcm *dpcm;
3224 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003225 int ret = 1;
3226 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003227 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01003228
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003229 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003230 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003231
3232 if (dpcm->fe == fe)
3233 continue;
3234
3235 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003236 for (i = 0; i < num_states; i++) {
3237 if (state == states[i]) {
3238 ret = 0;
3239 break;
3240 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003241 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003242 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003243 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003244
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003245 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003246 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003247}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003248
3249/*
3250 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3251 * are not running, paused or suspended for the specified stream direction.
3252 */
3253int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3254 struct snd_soc_pcm_runtime *be, int stream)
3255{
3256 const enum snd_soc_dpcm_state state[] = {
3257 SND_SOC_DPCM_STATE_START,
3258 SND_SOC_DPCM_STATE_PAUSED,
3259 SND_SOC_DPCM_STATE_SUSPEND,
3260 };
3261
3262 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3263}
Liam Girdwood01d75842012-04-25 12:12:49 +01003264EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3265
3266/*
3267 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3268 * running, paused or suspended for the specified stream direction.
3269 */
3270int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3271 struct snd_soc_pcm_runtime *be, int stream)
3272{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003273 const enum snd_soc_dpcm_state state[] = {
3274 SND_SOC_DPCM_STATE_START,
3275 SND_SOC_DPCM_STATE_PAUSED,
3276 SND_SOC_DPCM_STATE_SUSPEND,
3277 SND_SOC_DPCM_STATE_PREPARE,
3278 };
Liam Girdwood01d75842012-04-25 12:12:49 +01003279
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003280 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01003281}
3282EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);