blob: d53afb96b05bb7c5c467fb69a016c47e37c505d2 [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 Morimotof183f922020-01-22 09:44:35 +090031static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
32 struct snd_pcm_substream *substream)
33{
34 if (rtd->dai_link->ops &&
35 rtd->dai_link->ops->startup)
36 return rtd->dai_link->ops->startup(substream);
37 return 0;
38}
39
Kuninori Morimoto0be429f2020-01-22 09:44:40 +090040static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
41 struct snd_pcm_substream *substream)
42{
43 if (rtd->dai_link->ops &&
44 rtd->dai_link->ops->shutdown)
45 rtd->dai_link->ops->shutdown(substream);
46}
47
Kuninori Morimoto44c1a752020-01-22 09:44:44 +090048static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
49 struct snd_pcm_substream *substream)
50{
51 if (rtd->dai_link->ops &&
52 rtd->dai_link->ops->prepare)
53 return rtd->dai_link->ops->prepare(substream);
54 return 0;
55}
56
Kuninori Morimotode9ad992020-01-22 09:44:48 +090057static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
58 struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
60{
61 if (rtd->dai_link->ops &&
62 rtd->dai_link->ops->hw_params)
63 return rtd->dai_link->ops->hw_params(substream, params);
64 return 0;
65}
66
Kuninori Morimoto49f020e2020-01-22 09:44:52 +090067static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
68 struct snd_pcm_substream *substream)
69{
70 if (rtd->dai_link->ops &&
71 rtd->dai_link->ops->hw_free)
72 rtd->dai_link->ops->hw_free(substream);
73}
74
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +090075static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
76 struct snd_pcm_substream *substream,
77 int cmd)
78{
79 if (rtd->dai_link->ops &&
80 rtd->dai_link->ops->trigger)
81 return rtd->dai_link->ops->trigger(substream, cmd);
82 return 0;
83}
84
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +090085static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
86 int stream, int action)
87{
88 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
89 struct snd_soc_dai *codec_dai;
90 int i;
91
92 lockdep_assert_held(&rtd->card->pcm_mutex);
93
94 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
95 cpu_dai->playback_active += action;
96 for_each_rtd_codec_dai(rtd, i, codec_dai)
97 codec_dai->playback_active += action;
98 } else {
99 cpu_dai->capture_active += action;
100 for_each_rtd_codec_dai(rtd, i, codec_dai)
101 codec_dai->capture_active += action;
102 }
103
104 cpu_dai->active += action;
105 cpu_dai->component->active += action;
106 for_each_rtd_codec_dai(rtd, i, codec_dai) {
107 codec_dai->active += action;
108 codec_dai->component->active += action;
109 }
110}
111
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200112/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100113 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
114 * @rtd: ASoC PCM runtime that is activated
115 * @stream: Direction of the PCM stream
116 *
117 * Increments the active count for all the DAIs and components attached to a PCM
118 * runtime. Should typically be called when a stream is opened.
119 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300120 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100121 */
122void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
123{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900124 snd_soc_runtime_action(rtd, stream, 1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100125}
126
127/**
128 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
129 * @rtd: ASoC PCM runtime that is deactivated
130 * @stream: Direction of the PCM stream
131 *
132 * Decrements the active count for all the DAIs and components attached to a PCM
133 * runtime. Should typically be called when a stream is closed.
134 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300135 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100136 */
137void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
138{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900139 snd_soc_runtime_action(rtd, stream, -1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100140}
141
142/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100143 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
144 * @rtd: The ASoC PCM runtime that should be checked.
145 *
146 * This function checks whether the power down delay should be ignored for a
147 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
148 * been configured to ignore the delay, or if none of the components benefits
149 * from having the delay.
150 */
151bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
152{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000153 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200154 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900155 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200156
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100157 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
158 return true;
159
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900160 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000161 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000162
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000163 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100164}
165
166/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200167 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
168 * @substream: the pcm substream
169 * @hw: the hardware parameters
170 *
171 * Sets the substream runtime hardware parameters.
172 */
173int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
174 const struct snd_pcm_hardware *hw)
175{
176 struct snd_pcm_runtime *runtime = substream->runtime;
177 runtime->hw.info = hw->info;
178 runtime->hw.formats = hw->formats;
179 runtime->hw.period_bytes_min = hw->period_bytes_min;
180 runtime->hw.period_bytes_max = hw->period_bytes_max;
181 runtime->hw.periods_min = hw->periods_min;
182 runtime->hw.periods_max = hw->periods_max;
183 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
184 runtime->hw.fifo_size = hw->fifo_size;
185 return 0;
186}
187EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
188
Liam Girdwood01d75842012-04-25 12:12:49 +0100189/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000190int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100191 int event)
192{
193 struct snd_soc_dpcm *dpcm;
194
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000195 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100196
197 struct snd_soc_pcm_runtime *be = dpcm->be;
198
Liam Girdwood103d84a2012-11-19 14:39:15 +0000199 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100200 be->dai_link->name, event, dir);
201
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700202 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
203 (be->dpcm[dir].users >= 1))
204 continue;
205
Liam Girdwood01d75842012-04-25 12:12:49 +0100206 snd_soc_dapm_stream_event(be, dir, event);
207 }
208
209 snd_soc_dapm_stream_event(fe, dir, event);
210
211 return 0;
212}
213
Dong Aisheng17841022011-08-29 17:15:14 +0800214static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
215 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100216{
217 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100218 int ret;
219
Nicolin Chen3635bf02013-11-13 18:56:24 +0800220 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
221 rtd->dai_link->symmetric_rates)) {
222 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
223 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100224
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200225 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800226 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200227 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800228 if (ret < 0) {
229 dev_err(soc_dai->dev,
230 "ASoC: Unable to apply rate constraint: %d\n",
231 ret);
232 return ret;
233 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100234 }
235
Nicolin Chen3635bf02013-11-13 18:56:24 +0800236 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
237 rtd->dai_link->symmetric_channels)) {
238 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
239 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100240
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200241 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800242 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800243 soc_dai->channels);
244 if (ret < 0) {
245 dev_err(soc_dai->dev,
246 "ASoC: Unable to apply channel symmetry constraint: %d\n",
247 ret);
248 return ret;
249 }
250 }
251
252 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
253 rtd->dai_link->symmetric_samplebits)) {
254 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
255 soc_dai->sample_bits);
256
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200257 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800258 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800259 soc_dai->sample_bits);
260 if (ret < 0) {
261 dev_err(soc_dai->dev,
262 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
263 ret);
264 return ret;
265 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100266 }
267
268 return 0;
269}
270
Nicolin Chen3635bf02013-11-13 18:56:24 +0800271static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
272 struct snd_pcm_hw_params *params)
273{
274 struct snd_soc_pcm_runtime *rtd = substream->private_data;
275 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000276 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200277 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800278
279 rate = params_rate(params);
280 channels = params_channels(params);
281 sample_bits = snd_pcm_format_physical_width(params_format(params));
282
283 /* reject unmatched parameters when applying symmetry */
284 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800285 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200286
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000287 for_each_rtd_codec_dai(rtd, i, codec_dai)
288 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289
Nicolin Chen3635bf02013-11-13 18:56:24 +0800290 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
291 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
292 cpu_dai->rate, rate);
293 return -EINVAL;
294 }
295
296 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800297 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200298
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000299 for_each_rtd_codec_dai(rtd, i, codec_dai)
300 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200301
Nicolin Chen3635bf02013-11-13 18:56:24 +0800302 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
303 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
304 cpu_dai->channels, channels);
305 return -EINVAL;
306 }
307
308 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800309 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200310
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000311 for_each_rtd_codec_dai(rtd, i, codec_dai)
312 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200313
Nicolin Chen3635bf02013-11-13 18:56:24 +0800314 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
315 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
316 cpu_dai->sample_bits, sample_bits);
317 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100318 }
319
320 return 0;
321}
322
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100323static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
324{
325 struct snd_soc_pcm_runtime *rtd = substream->private_data;
326 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100327 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000328 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200329 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100330
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200331 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
332 cpu_driver->symmetric_channels || link->symmetric_channels ||
333 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
334
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000335 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200336 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000337 codec_dai->driver->symmetric_rates ||
338 codec_dai->driver->symmetric_channels ||
339 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340
341 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100342}
343
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200344static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000345{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200346 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100347 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000348
349 if (!bits)
350 return;
351
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100352 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
353 if (ret != 0)
354 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
355 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000356}
357
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200358static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200359{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200360 struct snd_soc_pcm_runtime *rtd = substream->private_data;
361 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200362 struct snd_soc_dai *codec_dai;
363 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200364 unsigned int bits = 0, cpu_bits;
365
366 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000367 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200368 if (codec_dai->driver->playback.sig_bits == 0) {
369 bits = 0;
370 break;
371 }
372 bits = max(codec_dai->driver->playback.sig_bits, bits);
373 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200374 cpu_bits = cpu_dai->driver->playback.sig_bits;
375 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000376 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200377 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200378 bits = 0;
379 break;
380 }
381 bits = max(codec_dai->driver->capture.sig_bits, bits);
382 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200383 cpu_bits = cpu_dai->driver->capture.sig_bits;
384 }
385
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200386 soc_pcm_set_msb(substream, bits);
387 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200388}
389
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200390static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200391{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100393 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200394 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000395 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200396 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
397 struct snd_soc_dai_driver *codec_dai_drv;
398 struct snd_soc_pcm_stream *codec_stream;
399 struct snd_soc_pcm_stream *cpu_stream;
400 unsigned int chan_min = 0, chan_max = UINT_MAX;
401 unsigned int rate_min = 0, rate_max = UINT_MAX;
402 unsigned int rates = UINT_MAX;
403 u64 formats = ULLONG_MAX;
404 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100405
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200406 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
407 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100408 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200409 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100410
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200411 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000412 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200413
414 /*
415 * Skip CODECs which don't support the current stream type.
416 * Otherwise, since the rate, channel, and format values will
417 * zero in that case, we would have no usable settings left,
418 * causing the resulting setup to fail.
419 * At least one CODEC should match, otherwise we should have
420 * bailed out on a higher level, since there would be no
421 * CODEC to support the transfer direction in that case.
422 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000423 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200424 substream->stream))
425 continue;
426
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000427 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
429 codec_stream = &codec_dai_drv->playback;
430 else
431 codec_stream = &codec_dai_drv->capture;
432 chan_min = max(chan_min, codec_stream->channels_min);
433 chan_max = min(chan_max, codec_stream->channels_max);
434 rate_min = max(rate_min, codec_stream->rate_min);
435 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
436 formats &= codec_stream->formats;
437 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
438 }
439
440 /*
441 * chan min/max cannot be enforced if there are multiple CODEC DAIs
442 * connected to a single CPU DAI, use CPU DAI's directly and let
443 * channel allocation be fixed up later
444 */
445 if (rtd->num_codecs > 1) {
446 chan_min = cpu_stream->channels_min;
447 chan_max = cpu_stream->channels_max;
448 }
449
450 hw->channels_min = max(chan_min, cpu_stream->channels_min);
451 hw->channels_max = min(chan_max, cpu_stream->channels_max);
452 if (hw->formats)
453 hw->formats &= formats & cpu_stream->formats;
454 else
455 hw->formats = formats & cpu_stream->formats;
456 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100457
458 snd_pcm_limit_hw_rates(runtime);
459
460 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200461 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100462 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200463 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200464}
465
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900466static int soc_pcm_components_open(struct snd_pcm_substream *substream,
467 struct snd_soc_component **last)
468{
469 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900470 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900471 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900472
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900473 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900474 *last = component;
475
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900476 ret = snd_soc_component_module_get_when_open(component);
477 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900478 dev_err(component->dev,
479 "ASoC: can't get module %s\n",
480 component->name);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900481 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900482 }
483
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900484 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900485 if (ret < 0) {
486 dev_err(component->dev,
487 "ASoC: can't open component %s: %d\n",
488 component->name, ret);
489 return ret;
490 }
491 }
492 *last = NULL;
493 return 0;
494}
495
Charles Keepax244e2932018-06-19 16:22:09 +0100496static int soc_pcm_components_close(struct snd_pcm_substream *substream,
497 struct snd_soc_component *last)
498{
499 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100500 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900501 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100502
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900503 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100504 if (component == last)
505 break;
506
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900507 r = snd_soc_component_close(component, substream);
508 if (r < 0)
509 ret = r; /* use last ret */
510
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900511 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100512 }
513
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900514 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100515}
516
Mark Brown58ba9b22012-01-16 18:38:51 +0000517/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100518 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
519 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100520 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100521 */
522static int soc_pcm_open(struct snd_pcm_substream *substream)
523{
524 struct snd_soc_pcm_runtime *rtd = substream->private_data;
525 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000526 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100527 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200528 struct snd_soc_dai *codec_dai;
529 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100530 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100531
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900532 for_each_rtd_components(rtd, i, component)
533 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000534
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900535 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000536 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000537
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300538 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
540 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900541 ret = snd_soc_dai_startup(cpu_dai, substream);
542 if (ret < 0) {
543 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
544 cpu_dai->name, ret);
545 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100546 }
547
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900548 ret = soc_pcm_components_open(substream, &component);
549 if (ret < 0)
550 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000551
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000552 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900553 ret = snd_soc_dai_startup(codec_dai, substream);
554 if (ret < 0) {
555 dev_err(codec_dai->dev,
556 "ASoC: can't open codec %s: %d\n",
557 codec_dai->name, ret);
558 goto codec_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200560
561 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
562 codec_dai->tx_mask = 0;
563 else
564 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100565 }
566
Kuninori Morimotof183f922020-01-22 09:44:35 +0900567 ret = soc_rtd_startup(rtd, substream);
568 if (ret < 0) {
569 pr_err("ASoC: %s startup failed: %d\n",
570 rtd->dai_link->name, ret);
Kuninori Morimotob56be802020-02-10 12:14:33 +0900571 goto codec_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100572 }
573
Liam Girdwood01d75842012-04-25 12:12:49 +0100574 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
575 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
576 goto dynamic;
577
Liam Girdwoodddee6272011-06-09 14:45:53 +0100578 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200579 soc_pcm_init_runtime_hw(substream);
580
581 if (rtd->num_codecs == 1)
582 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100583
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100584 if (soc_pcm_has_symmetry(substream))
585 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
586
Liam Girdwoodddee6272011-06-09 14:45:53 +0100587 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100588 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000589 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200590 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100591 goto config_err;
592 }
593 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000594 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200595 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596 goto config_err;
597 }
598 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
599 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200601 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100602 goto config_err;
603 }
604
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200605 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000606
Liam Girdwoodddee6272011-06-09 14:45:53 +0100607 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800608 if (cpu_dai->active) {
609 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
610 if (ret != 0)
611 goto config_err;
612 }
613
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000614 for_each_rtd_codec_dai(rtd, i, codec_dai) {
615 if (codec_dai->active) {
616 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200617 if (ret != 0)
618 goto config_err;
619 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100620 }
621
Liam Girdwood103d84a2012-11-19 14:39:15 +0000622 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200623 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000624 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
625 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100626 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000627 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628 runtime->hw.rate_max);
629
Liam Girdwood01d75842012-04-25 12:12:49 +0100630dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100631
632 snd_soc_runtime_activate(rtd, substream->stream);
633
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300634 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100635 return 0;
636
637config_err:
Kuninori Morimoto0be429f2020-01-22 09:44:40 +0900638 soc_rtd_shutdown(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639
Liam Girdwoodddee6272011-06-09 14:45:53 +0100640codec_dai_err:
Kuninori Morimotob56be802020-02-10 12:14:33 +0900641 for_each_rtd_codec_dai(rtd, i, codec_dai)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900642 snd_soc_dai_shutdown(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200643
Kuninori Morimotob8135862017-10-11 01:37:23 +0000644component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100645 soc_pcm_components_close(substream, component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900646
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900647 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100648out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300649 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000650
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900651 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000652 pm_runtime_mark_last_busy(component->dev);
653 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530654 }
655
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900656 for_each_rtd_components(rtd, i, component)
657 if (!component->active)
658 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000659
Liam Girdwoodddee6272011-06-09 14:45:53 +0100660 return ret;
661}
662
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800663static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200664{
665 /*
666 * Currently nothing to do for c2c links
667 * Since c2c links are internal nodes in the DAPM graph and
668 * don't interface with the outside world or application layer
669 * we don't have to do any special handling on close.
670 */
671}
672
Liam Girdwoodddee6272011-06-09 14:45:53 +0100673/*
674 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100675 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100676 * shutdown.
677 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100678static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100679{
680 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000681 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100682 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200683 struct snd_soc_dai *codec_dai;
684 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100685
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300686 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100687
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100688 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689
Ramesh Babuae116012014-10-15 12:34:59 +0530690 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
691
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900692 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900694 for_each_rtd_codec_dai(rtd, i, codec_dai)
695 snd_soc_dai_shutdown(codec_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100696
Kuninori Morimoto0be429f2020-01-22 09:44:40 +0900697 soc_rtd_shutdown(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698
Charles Keepax244e2932018-06-19 16:22:09 +0100699 soc_pcm_components_close(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000700
Kuninori Morimoto3f4cf792020-01-10 11:36:23 +0900701 snd_soc_dapm_stream_stop(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100702
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300703 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000704
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900705 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000706 pm_runtime_mark_last_busy(component->dev);
707 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530708 }
709
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900710 for_each_rtd_components(rtd, i, component)
711 if (!component->active)
712 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000713
Liam Girdwoodddee6272011-06-09 14:45:53 +0100714 return 0;
715}
716
717/*
718 * Called by ALSA when the PCM substream is prepared, can set format, sample
719 * rate, etc. This function is non atomic and can be called multiple times,
720 * it can refer to the runtime info.
721 */
722static int soc_pcm_prepare(struct snd_pcm_substream *substream)
723{
724 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000725 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200727 struct snd_soc_dai *codec_dai;
728 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300730 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100731
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900732 ret = soc_rtd_prepare(rtd, substream);
733 if (ret < 0) {
734 dev_err(rtd->card->dev,
735 "ASoC: machine prepare error: %d\n", ret);
736 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 }
738
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900739 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900740 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000741 if (ret < 0) {
742 dev_err(component->dev,
743 "ASoC: platform prepare error: %d\n", ret);
744 goto out;
745 }
746 }
747
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000748 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900749 ret = snd_soc_dai_prepare(codec_dai, substream);
750 if (ret < 0) {
751 dev_err(codec_dai->dev,
752 "ASoC: codec DAI prepare error: %d\n",
753 ret);
754 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100755 }
756 }
757
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900758 ret = snd_soc_dai_prepare(cpu_dai, substream);
759 if (ret < 0) {
760 dev_err(cpu_dai->dev,
761 "ASoC: cpu DAI prepare error: %d\n", ret);
762 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100763 }
764
765 /* cancel any delayed stream shutdown that is pending */
766 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600767 rtd->pop_wait) {
768 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100769 cancel_delayed_work(&rtd->delayed_work);
770 }
771
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000772 snd_soc_dapm_stream_event(rtd, substream->stream,
773 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100774
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000775 for_each_rtd_codec_dai(rtd, i, codec_dai)
776 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200777 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530778 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100779
780out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300781 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100782 return ret;
783}
784
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200785static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
786 unsigned int mask)
787{
788 struct snd_interval *interval;
789 int channels = hweight_long(mask);
790
791 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
792 interval->min = channels;
793 interval->max = channels;
794}
795
Charles Keepax244e2932018-06-19 16:22:09 +0100796static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
797 struct snd_soc_component *last)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100800 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900801 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100802
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900803 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100804 if (component == last)
805 break;
806
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900807 r = snd_soc_component_hw_free(component, substream);
808 if (r < 0)
809 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +0100810 }
811
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900812 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100813}
814
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815/*
816 * Called by ALSA when the hardware params are set by application. This
817 * function can also be called multiple times and can allocate buffers
818 * (using snd_pcm_lib_* ). It's non-atomic.
819 */
820static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
821 struct snd_pcm_hw_params *params)
822{
823 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000824 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100825 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000826 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100827 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100828
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300829 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800830
831 ret = soc_pcm_params_symmetry(substream, params);
832 if (ret)
833 goto out;
834
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900835 ret = soc_rtd_hw_params(rtd, substream, params);
836 if (ret < 0) {
837 dev_err(rtd->card->dev,
838 "ASoC: machine hw_params failed: %d\n", ret);
839 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100840 }
841
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000842 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200843 struct snd_pcm_hw_params codec_params;
844
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200845 /*
846 * Skip CODECs which don't support the current stream type,
847 * the idea being that if a CODEC is not used for the currently
848 * set up transfer direction, it should not need to be
849 * configured, especially since the configuration used might
850 * not even be supported by that CODEC. There may be cases
851 * however where a CODEC needs to be set up although it is
852 * actually not being used for the transfer, e.g. if a
853 * capture-only CODEC is acting as an LRCLK and/or BCLK master
854 * for the DAI link including a playback-only CODEC.
855 * If this becomes necessary, we will have to augment the
856 * machine driver setup with information on how to act, so
857 * we can do the right thing here.
858 */
859 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
860 continue;
861
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200862 /* copy params for each codec */
863 codec_params = *params;
864
865 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200868 soc_pcm_codec_params_fixup(&codec_params,
869 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800870
871 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
872 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200873 soc_pcm_codec_params_fixup(&codec_params,
874 codec_dai->rx_mask);
875
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900876 ret = snd_soc_dai_hw_params(codec_dai, substream,
877 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200878 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100879 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200880
881 codec_dai->rate = params_rate(&codec_params);
882 codec_dai->channels = params_channels(&codec_params);
883 codec_dai->sample_bits = snd_pcm_format_physical_width(
884 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000885
886 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100887 }
888
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900889 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200890 if (ret < 0)
891 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100892
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900893 /* store the parameters for each DAIs */
894 cpu_dai->rate = params_rate(params);
895 cpu_dai->channels = params_channels(params);
896 cpu_dai->sample_bits =
897 snd_pcm_format_physical_width(params_format(params));
898
899 snd_soc_dapm_update_dai(substream, params, cpu_dai);
900
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900901 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900902 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100903 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000904 dev_err(component->dev,
905 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100906 component->name, ret);
907 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000908 }
909 }
Charles Keepax244e2932018-06-19 16:22:09 +0100910 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000911
Liam Girdwoodddee6272011-06-09 14:45:53 +0100912out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300913 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100914 return ret;
915
Kuninori Morimotob8135862017-10-11 01:37:23 +0000916component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100917 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000918
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900919 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900920 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100921
922interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200923 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100924
925codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000926 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200927 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
928 continue;
929
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900930 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200931 codec_dai->rate = 0;
932 }
933
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900934 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100935
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300936 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100937 return ret;
938}
939
940/*
941 * Frees resources allocated by hw_params, can be called multiple times
942 */
943static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
944{
945 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200947 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800948 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200949 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100950
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300951 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100952
Nicolin Chend3383422013-11-20 18:37:09 +0800953 /* clear the corresponding DAIs parameters when going to be inactive */
954 if (cpu_dai->active == 1) {
955 cpu_dai->rate = 0;
956 cpu_dai->channels = 0;
957 cpu_dai->sample_bits = 0;
958 }
959
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000960 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200961 if (codec_dai->active == 1) {
962 codec_dai->rate = 0;
963 codec_dai->channels = 0;
964 codec_dai->sample_bits = 0;
965 }
Nicolin Chend3383422013-11-20 18:37:09 +0800966 }
967
Liam Girdwoodddee6272011-06-09 14:45:53 +0100968 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000969 for_each_rtd_codec_dai(rtd, i, codec_dai) {
970 if ((playback && codec_dai->playback_active == 1) ||
971 (!playback && codec_dai->capture_active == 1))
972 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200973 substream->stream);
974 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975
976 /* free any machine hw params */
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900977 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100978
Kuninori Morimotob8135862017-10-11 01:37:23 +0000979 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +0100980 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000981
Liam Girdwoodddee6272011-06-09 14:45:53 +0100982 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000983 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200984 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
985 continue;
986
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900987 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200988 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100989
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900990 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100991
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300992 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 return 0;
994}
995
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +0300996static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100997{
998 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000999 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001000 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001001 struct snd_soc_dai *codec_dai;
1002 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001004 ret = soc_rtd_trigger(rtd, substream, cmd);
1005 if (ret < 0)
1006 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001007
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001008 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001009 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001010 if (ret < 0)
1011 return ret;
1012 }
1013
Dan Carpenter901e8222019-09-23 17:22:57 +03001014 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001015 if (ret < 0)
1016 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001017
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001018 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1019 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1020 if (ret < 0)
1021 return ret;
1022 }
1023
1024 return 0;
1025}
1026
1027static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1028{
1029 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1030 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001031 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1032 struct snd_soc_dai *codec_dai;
1033 int i, ret;
1034
1035 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1036 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1037 if (ret < 0)
1038 return ret;
1039 }
1040
1041 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1042 if (ret < 0)
1043 return ret;
1044
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001045 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001046 ret = snd_soc_component_trigger(component, substream, cmd);
1047 if (ret < 0)
1048 return ret;
1049 }
1050
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001051 ret = soc_rtd_trigger(rtd, substream, cmd);
1052 if (ret < 0)
1053 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001054
Liam Girdwoodddee6272011-06-09 14:45:53 +01001055 return 0;
1056}
1057
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001058static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1059{
1060 int ret;
1061
1062 switch (cmd) {
1063 case SNDRV_PCM_TRIGGER_START:
1064 case SNDRV_PCM_TRIGGER_RESUME:
1065 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1066 ret = soc_pcm_trigger_start(substream, cmd);
1067 break;
1068 case SNDRV_PCM_TRIGGER_STOP:
1069 case SNDRV_PCM_TRIGGER_SUSPEND:
1070 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1071 ret = soc_pcm_trigger_stop(substream, cmd);
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
1076
1077 return ret;
1078}
1079
Mark Brown45c0a182012-05-09 21:46:27 +01001080static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1081 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001082{
1083 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001084 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001085 struct snd_soc_dai *codec_dai;
1086 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001087
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001088 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001089 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001090 if (ret < 0)
1091 return ret;
1092 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001093
Dan Carpenter901e8222019-09-23 17:22:57 +03001094 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001095 if (ret < 0)
1096 return ret;
1097
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001098 return 0;
1099}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001100/*
1101 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001102 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001103 * the runtime->delay will be updated accordingly.
1104 */
1105static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1106{
1107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001108 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001109 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 snd_pcm_uframes_t offset = 0;
1112 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001113 snd_pcm_sframes_t codec_delay = 0;
1114 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301116 /* clearing the previous total delay */
1117 runtime->delay = 0;
1118
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001119 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001120
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301121 /* base delay if assigned in pointer callback */
1122 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001123
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001124 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001125
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001126 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001127 codec_delay = max(codec_delay,
1128 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001129 }
1130 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001131
Liam Girdwoodddee6272011-06-09 14:45:53 +01001132 runtime->delay = delay;
1133
1134 return offset;
1135}
1136
Liam Girdwood01d75842012-04-25 12:12:49 +01001137/* connect a FE and BE */
1138static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1139 struct snd_soc_pcm_runtime *be, int stream)
1140{
1141 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001142 unsigned long flags;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001143#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001144 char *name;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001145#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001146
1147 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001148 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001149 if (dpcm->be == be && dpcm->fe == fe)
1150 return 0;
1151 }
1152
1153 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1154 if (!dpcm)
1155 return -ENOMEM;
1156
1157 dpcm->be = be;
1158 dpcm->fe = fe;
1159 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1160 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001161 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001162 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1163 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001164 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001165
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001166 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001167 stream ? "capture" : "playback", fe->dai_link->name,
1168 stream ? "<-" : "->", be->dai_link->name);
1169
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001170#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001171 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1172 stream ? "capture" : "playback");
1173 if (name) {
1174 dpcm->debugfs_state = debugfs_create_dir(name,
1175 fe->debugfs_dpcm_root);
1176 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1177 &dpcm->state);
1178 kfree(name);
1179 }
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001180#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001181 return 1;
1182}
1183
1184/* reparent a BE onto another FE */
1185static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1186 struct snd_soc_pcm_runtime *be, int stream)
1187{
1188 struct snd_soc_dpcm *dpcm;
1189 struct snd_pcm_substream *fe_substream, *be_substream;
1190
1191 /* reparent if BE is connected to other FEs */
1192 if (!be->dpcm[stream].users)
1193 return;
1194
1195 be_substream = snd_soc_dpcm_get_substream(be, stream);
1196
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001197 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001198 if (dpcm->fe == fe)
1199 continue;
1200
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001201 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001202 stream ? "capture" : "playback",
1203 dpcm->fe->dai_link->name,
1204 stream ? "<-" : "->", dpcm->be->dai_link->name);
1205
1206 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1207 be_substream->runtime = fe_substream->runtime;
1208 break;
1209 }
1210}
1211
1212/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001213void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001214{
1215 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001216 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001217
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001218 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001219 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001220 stream ? "capture" : "playback",
1221 dpcm->be->dai_link->name);
1222
1223 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1224 continue;
1225
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001226 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001227 stream ? "capture" : "playback", fe->dai_link->name,
1228 stream ? "<-" : "->", dpcm->be->dai_link->name);
1229
1230 /* BEs still alive need new FE */
1231 dpcm_be_reparent(fe, dpcm->be, stream);
1232
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001233#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001234 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001235#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001236 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001237 list_del(&dpcm->list_be);
1238 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001239 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001240 kfree(dpcm);
1241 }
1242}
1243
1244/* get BE for DAI widget and stream */
1245static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1246 struct snd_soc_dapm_widget *widget, int stream)
1247{
1248 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001249 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001250 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001251
Liam Girdwood3c146462018-03-14 20:43:51 +00001252 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1253
Liam Girdwood01d75842012-04-25 12:12:49 +01001254 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001255 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001256
Liam Girdwood35ea0652012-06-05 19:26:59 +01001257 if (!be->dai_link->no_pcm)
1258 continue;
1259
Liam Girdwood3c146462018-03-14 20:43:51 +00001260 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1261 be->cpu_dai->playback_widget ?
1262 be->cpu_dai->playback_widget->name : "(not set)");
1263
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001264 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001265 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001266
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001267 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001268 if (dai->playback_widget == widget)
1269 return be;
1270 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001271 }
1272 } else {
1273
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001274 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001275
Liam Girdwood35ea0652012-06-05 19:26:59 +01001276 if (!be->dai_link->no_pcm)
1277 continue;
1278
Liam Girdwood3c146462018-03-14 20:43:51 +00001279 dev_dbg(card->dev, "ASoC: try BE %s\n",
1280 be->cpu_dai->capture_widget ?
1281 be->cpu_dai->capture_widget->name : "(not set)");
1282
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001283 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001284 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001285
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001286 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001287 if (dai->capture_widget == widget)
1288 return be;
1289 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001290 }
1291 }
1292
Liam Girdwood3c146462018-03-14 20:43:51 +00001293 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001294 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001295 stream ? "capture" : "playback", widget->name);
1296 return NULL;
1297}
1298
1299static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001300 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001301{
1302 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001303 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001304 else
Benoit Cousson37018612014-04-24 14:01:45 +02001305 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001306}
1307
1308static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1309 struct snd_soc_dapm_widget *widget)
1310{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001311 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001312 int i;
1313
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001314 for_each_dapm_widgets(list, i, w)
1315 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001316 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001317
1318 return 0;
1319}
1320
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001321static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1322 enum snd_soc_dapm_direction dir)
1323{
1324 struct snd_soc_card *card = widget->dapm->card;
1325 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001326 struct snd_soc_dai *dai;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001327 int i;
1328
1329 if (dir == SND_SOC_DAPM_DIR_OUT) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001330 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001331 if (!rtd->dai_link->no_pcm)
1332 continue;
1333
1334 if (rtd->cpu_dai->playback_widget == widget)
1335 return true;
1336
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001337 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001338 if (dai->playback_widget == widget)
1339 return true;
1340 }
1341 }
1342 } else { /* SND_SOC_DAPM_DIR_IN */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001343 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001344 if (!rtd->dai_link->no_pcm)
1345 continue;
1346
1347 if (rtd->cpu_dai->capture_widget == widget)
1348 return true;
1349
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001350 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001351 if (dai->capture_widget == widget)
1352 return true;
1353 }
1354 }
1355 }
1356
1357 return false;
1358}
1359
Liam Girdwood23607022014-01-17 17:03:55 +00001360int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001361 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001362{
1363 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001364 int paths;
1365
Liam Girdwood01d75842012-04-25 12:12:49 +01001366 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001367 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001368 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001369
Liam Girdwood103d84a2012-11-19 14:39:15 +00001370 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001371 stream ? "capture" : "playback");
1372
Liam Girdwood01d75842012-04-25 12:12:49 +01001373 return paths;
1374}
1375
Liam Girdwood01d75842012-04-25 12:12:49 +01001376static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1377 struct snd_soc_dapm_widget_list **list_)
1378{
1379 struct snd_soc_dpcm *dpcm;
1380 struct snd_soc_dapm_widget_list *list = *list_;
1381 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001382 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001383 int prune = 0;
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001384 int do_prune;
Liam Girdwood01d75842012-04-25 12:12:49 +01001385
1386 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001387 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001388 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001389
1390 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001391 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001392
1393 /* prune the BE if it's no longer in our active list */
1394 if (widget && widget_in_list(list, widget))
1395 continue;
1396
1397 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001398 do_prune = 1;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001399 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001400 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001401
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001402 /* prune the BE if it's no longer in our active list */
1403 if (widget && widget_in_list(list, widget))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001404 do_prune = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001405 }
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001406 if (!do_prune)
1407 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001408
Liam Girdwood103d84a2012-11-19 14:39:15 +00001409 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001410 stream ? "capture" : "playback",
1411 dpcm->be->dai_link->name, fe->dai_link->name);
1412 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1413 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1414 prune++;
1415 }
1416
Liam Girdwood103d84a2012-11-19 14:39:15 +00001417 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001418 return prune;
1419}
1420
1421static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1422 struct snd_soc_dapm_widget_list **list_)
1423{
1424 struct snd_soc_card *card = fe->card;
1425 struct snd_soc_dapm_widget_list *list = *list_;
1426 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001427 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001428 int i, new = 0, err;
1429
1430 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001431 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001432
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001433 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001434 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001435 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1436 continue;
1437 break;
Mark Brown46162742013-06-05 19:36:11 +01001438 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001439 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1440 continue;
Mark Brown46162742013-06-05 19:36:11 +01001441 break;
1442 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001443 continue;
Mark Brown46162742013-06-05 19:36:11 +01001444 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001445
1446 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001447 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001448 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001449 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001450 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001451 continue;
1452 }
1453
1454 /* make sure BE is a real BE */
1455 if (!be->dai_link->no_pcm)
1456 continue;
1457
1458 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001459 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001460 continue;
1461
1462 /* newly connected FE and BE */
1463 err = dpcm_be_connect(fe, be, stream);
1464 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001465 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001466 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001467 break;
1468 } else if (err == 0) /* already connected */
1469 continue;
1470
1471 /* new */
1472 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1473 new++;
1474 }
1475
Liam Girdwood103d84a2012-11-19 14:39:15 +00001476 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001477 return new;
1478}
1479
1480/*
1481 * Find the corresponding BE DAIs that source or sink audio to this
1482 * FE substream.
1483 */
Liam Girdwood23607022014-01-17 17:03:55 +00001484int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001485 int stream, struct snd_soc_dapm_widget_list **list, int new)
1486{
1487 if (new)
1488 return dpcm_add_paths(fe, stream, list);
1489 else
1490 return dpcm_prune_paths(fe, stream, list);
1491}
1492
Liam Girdwood23607022014-01-17 17:03:55 +00001493void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001494{
1495 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001496 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001497
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001498 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001499 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001500 dpcm->be->dpcm[stream].runtime_update =
1501 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001502 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001503}
1504
1505static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1506 int stream)
1507{
1508 struct snd_soc_dpcm *dpcm;
1509
1510 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001511 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001512
1513 struct snd_soc_pcm_runtime *be = dpcm->be;
1514 struct snd_pcm_substream *be_substream =
1515 snd_soc_dpcm_get_substream(be, stream);
1516
1517 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001518 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001519 stream ? "capture" : "playback",
1520 be->dpcm[stream].state);
1521
1522 if (--be->dpcm[stream].users != 0)
1523 continue;
1524
1525 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1526 continue;
1527
1528 soc_pcm_close(be_substream);
1529 be_substream->runtime = NULL;
1530 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1531 }
1532}
1533
Liam Girdwood23607022014-01-17 17:03:55 +00001534int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001535{
1536 struct snd_soc_dpcm *dpcm;
1537 int err, count = 0;
1538
1539 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001540 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001541
1542 struct snd_soc_pcm_runtime *be = dpcm->be;
1543 struct snd_pcm_substream *be_substream =
1544 snd_soc_dpcm_get_substream(be, stream);
1545
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001546 if (!be_substream) {
1547 dev_err(be->dev, "ASoC: no backend %s stream\n",
1548 stream ? "capture" : "playback");
1549 continue;
1550 }
1551
Liam Girdwood01d75842012-04-25 12:12:49 +01001552 /* is this op for this BE ? */
1553 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1554 continue;
1555
1556 /* first time the dpcm is open ? */
1557 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001558 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001559 stream ? "capture" : "playback",
1560 be->dpcm[stream].state);
1561
1562 if (be->dpcm[stream].users++ != 0)
1563 continue;
1564
1565 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1566 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1567 continue;
1568
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001569 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1570 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001571
1572 be_substream->runtime = be->dpcm[stream].runtime;
1573 err = soc_pcm_open(be_substream);
1574 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001575 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001576 be->dpcm[stream].users--;
1577 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001578 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001579 stream ? "capture" : "playback",
1580 be->dpcm[stream].state);
1581
1582 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1583 goto unwind;
1584 }
1585
1586 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1587 count++;
1588 }
1589
1590 return count;
1591
1592unwind:
1593 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001594 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001595 struct snd_soc_pcm_runtime *be = dpcm->be;
1596 struct snd_pcm_substream *be_substream =
1597 snd_soc_dpcm_get_substream(be, stream);
1598
1599 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1600 continue;
1601
1602 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001603 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001604 stream ? "capture" : "playback",
1605 be->dpcm[stream].state);
1606
1607 if (--be->dpcm[stream].users != 0)
1608 continue;
1609
1610 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1611 continue;
1612
1613 soc_pcm_close(be_substream);
1614 be_substream->runtime = NULL;
1615 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1616 }
1617
1618 return err;
1619}
1620
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001621static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001622 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001623{
1624 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001625 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001626 runtime->hw.channels_min = stream->channels_min;
1627 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001628 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001629 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001630 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001631 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001632 runtime->hw.rates = stream->rates;
1633}
1634
Jerome Brunet435ffb72018-07-05 12:13:48 +02001635static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1636 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001637{
1638 struct snd_soc_pcm_runtime *fe = substream->private_data;
1639 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001640 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001641 int stream = substream->stream;
1642
1643 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001644 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001645
1646 /*
1647 * It returns merged BE codec format
1648 * if FE want to use it (= dpcm_merged_format)
1649 */
1650
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001651 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001652 struct snd_soc_pcm_runtime *be = dpcm->be;
1653 struct snd_soc_dai_driver *codec_dai_drv;
1654 struct snd_soc_pcm_stream *codec_stream;
1655 int i;
1656
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001657 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001658 /*
1659 * Skip CODECs which don't support the current stream
1660 * type. See soc_pcm_init_runtime_hw() for more details
1661 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001662 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001663 continue;
1664
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001665 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001666 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1667 codec_stream = &codec_dai_drv->playback;
1668 else
1669 codec_stream = &codec_dai_drv->capture;
1670
Jerome Brunet435ffb72018-07-05 12:13:48 +02001671 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001672 }
1673 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001674}
1675
Jerome Brunet435ffb72018-07-05 12:13:48 +02001676static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1677 unsigned int *channels_min,
1678 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001679{
1680 struct snd_soc_pcm_runtime *fe = substream->private_data;
1681 struct snd_soc_dpcm *dpcm;
1682 int stream = substream->stream;
1683
1684 if (!fe->dai_link->dpcm_merged_chan)
1685 return;
1686
Jiada Wangf4c277b2018-06-20 18:25:20 +09001687 /*
1688 * It returns merged BE codec channel;
1689 * if FE want to use it (= dpcm_merged_chan)
1690 */
1691
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001692 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001693 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001694 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001695 struct snd_soc_dai_driver *codec_dai_drv;
1696 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001697 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001698
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001699 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1700 cpu_stream = &cpu_dai_drv->playback;
1701 else
1702 cpu_stream = &cpu_dai_drv->capture;
1703
1704 *channels_min = max(*channels_min, cpu_stream->channels_min);
1705 *channels_max = min(*channels_max, cpu_stream->channels_max);
1706
1707 /*
1708 * chan min/max cannot be enforced if there are multiple CODEC
1709 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1710 */
1711 if (be->num_codecs == 1) {
1712 codec_dai_drv = be->codec_dais[0]->driver;
1713
Jiada Wangf4c277b2018-06-20 18:25:20 +09001714 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1715 codec_stream = &codec_dai_drv->playback;
1716 else
1717 codec_stream = &codec_dai_drv->capture;
1718
1719 *channels_min = max(*channels_min,
1720 codec_stream->channels_min);
1721 *channels_max = min(*channels_max,
1722 codec_stream->channels_max);
1723 }
1724 }
1725}
1726
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001727static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1728 unsigned int *rates,
1729 unsigned int *rate_min,
1730 unsigned int *rate_max)
1731{
1732 struct snd_soc_pcm_runtime *fe = substream->private_data;
1733 struct snd_soc_dpcm *dpcm;
1734 int stream = substream->stream;
1735
1736 if (!fe->dai_link->dpcm_merged_rate)
1737 return;
1738
1739 /*
1740 * It returns merged BE codec channel;
1741 * if FE want to use it (= dpcm_merged_chan)
1742 */
1743
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001744 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001745 struct snd_soc_pcm_runtime *be = dpcm->be;
1746 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1747 struct snd_soc_dai_driver *codec_dai_drv;
1748 struct snd_soc_pcm_stream *codec_stream;
1749 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001750 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001751 int i;
1752
1753 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1754 cpu_stream = &cpu_dai_drv->playback;
1755 else
1756 cpu_stream = &cpu_dai_drv->capture;
1757
1758 *rate_min = max(*rate_min, cpu_stream->rate_min);
1759 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1760 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1761
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001762 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001763 /*
1764 * Skip CODECs which don't support the current stream
1765 * type. See soc_pcm_init_runtime_hw() for more details
1766 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001767 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001768 continue;
1769
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001770 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001771 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1772 codec_stream = &codec_dai_drv->playback;
1773 else
1774 codec_stream = &codec_dai_drv->capture;
1775
1776 *rate_min = max(*rate_min, codec_stream->rate_min);
1777 *rate_max = min_not_zero(*rate_max,
1778 codec_stream->rate_max);
1779 *rates = snd_pcm_rate_mask_intersect(*rates,
1780 codec_stream->rates);
1781 }
1782 }
1783}
1784
Mark Brown45c0a182012-05-09 21:46:27 +01001785static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001786{
1787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1789 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1790 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1791
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001792 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001793 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001794 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001795 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001796
Jerome Brunet435ffb72018-07-05 12:13:48 +02001797 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1798 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1799 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001800 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1801 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001802}
1803
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001804static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1805
1806/* Set FE's runtime_update state; the state is protected via PCM stream lock
1807 * for avoiding the race with trigger callback.
1808 * If the state is unset and a trigger is pending while the previous operation,
1809 * process the pending trigger action here.
1810 */
1811static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1812 int stream, enum snd_soc_dpcm_update state)
1813{
1814 struct snd_pcm_substream *substream =
1815 snd_soc_dpcm_get_substream(fe, stream);
1816
1817 snd_pcm_stream_lock_irq(substream);
1818 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1819 dpcm_fe_dai_do_trigger(substream,
1820 fe->dpcm[stream].trigger_pending - 1);
1821 fe->dpcm[stream].trigger_pending = 0;
1822 }
1823 fe->dpcm[stream].runtime_update = state;
1824 snd_pcm_stream_unlock_irq(substream);
1825}
1826
PC Liao906c7d62015-12-11 11:33:51 +08001827static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1828 int stream)
1829{
1830 struct snd_soc_dpcm *dpcm;
1831 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1832 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1833 int err;
1834
1835 /* apply symmetry for FE */
1836 if (soc_pcm_has_symmetry(fe_substream))
1837 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1838
1839 /* Symmetry only applies if we've got an active stream. */
1840 if (fe_cpu_dai->active) {
1841 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1842 if (err < 0)
1843 return err;
1844 }
1845
1846 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001847 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001848 struct snd_soc_pcm_runtime *be = dpcm->be;
1849 struct snd_pcm_substream *be_substream =
1850 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001851 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001852 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001853 int i;
1854
Jerome Brunet6246f282019-04-01 15:03:54 +02001855 /* A backend may not have the requested substream */
1856 if (!be_substream)
1857 continue;
1858
1859 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301860 if (rtd->dai_link->be_hw_params_fixup)
1861 continue;
1862
PC Liao906c7d62015-12-11 11:33:51 +08001863 if (soc_pcm_has_symmetry(be_substream))
1864 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1865
1866 /* Symmetry only applies if we've got an active stream. */
1867 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001868 err = soc_pcm_apply_symmetry(fe_substream,
1869 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001870 if (err < 0)
1871 return err;
1872 }
1873
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001874 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1875 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001876 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001877 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001878 if (err < 0)
1879 return err;
1880 }
1881 }
1882 }
1883
1884 return 0;
1885}
1886
Liam Girdwood01d75842012-04-25 12:12:49 +01001887static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1888{
1889 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1890 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1891 int stream = fe_substream->stream, ret = 0;
1892
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001893 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001894
1895 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1896 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001897 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001898 goto be_err;
1899 }
1900
Liam Girdwood103d84a2012-11-19 14:39:15 +00001901 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001902
1903 /* start the DAI frontend */
1904 ret = soc_pcm_open(fe_substream);
1905 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001906 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001907 goto unwind;
1908 }
1909
1910 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1911
1912 dpcm_set_fe_runtime(fe_substream);
1913 snd_pcm_limit_hw_rates(runtime);
1914
PC Liao906c7d62015-12-11 11:33:51 +08001915 ret = dpcm_apply_symmetry(fe_substream, stream);
1916 if (ret < 0) {
1917 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1918 ret);
1919 goto unwind;
1920 }
1921
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001922 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001923 return 0;
1924
1925unwind:
1926 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1927be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001928 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001929 return ret;
1930}
1931
Liam Girdwood23607022014-01-17 17:03:55 +00001932int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001933{
1934 struct snd_soc_dpcm *dpcm;
1935
1936 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001937 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001938
1939 struct snd_soc_pcm_runtime *be = dpcm->be;
1940 struct snd_pcm_substream *be_substream =
1941 snd_soc_dpcm_get_substream(be, stream);
1942
1943 /* is this op for this BE ? */
1944 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1945 continue;
1946
1947 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001948 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001949 stream ? "capture" : "playback",
1950 be->dpcm[stream].state);
1951
1952 if (--be->dpcm[stream].users != 0)
1953 continue;
1954
1955 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001956 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1957 soc_pcm_hw_free(be_substream);
1958 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1959 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001960
Liam Girdwood103d84a2012-11-19 14:39:15 +00001961 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001962 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001963
1964 soc_pcm_close(be_substream);
1965 be_substream->runtime = NULL;
1966
1967 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1968 }
1969 return 0;
1970}
1971
1972static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1973{
1974 struct snd_soc_pcm_runtime *fe = substream->private_data;
1975 int stream = substream->stream;
1976
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001977 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001978
1979 /* shutdown the BEs */
1980 dpcm_be_dai_shutdown(fe, substream->stream);
1981
Liam Girdwood103d84a2012-11-19 14:39:15 +00001982 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001983
1984 /* now shutdown the frontend */
1985 soc_pcm_close(substream);
1986
1987 /* run the stream event for each BE */
Kuninori Morimotob0edff42020-01-10 11:36:56 +09001988 snd_soc_dapm_stream_stop(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001989
1990 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001991 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001992 return 0;
1993}
1994
Liam Girdwood23607022014-01-17 17:03:55 +00001995int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001996{
1997 struct snd_soc_dpcm *dpcm;
1998
1999 /* only hw_params backends that are either sinks or sources
2000 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002001 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
2003 struct snd_soc_pcm_runtime *be = dpcm->be;
2004 struct snd_pcm_substream *be_substream =
2005 snd_soc_dpcm_get_substream(be, stream);
2006
2007 /* is this op for this BE ? */
2008 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2009 continue;
2010
2011 /* only free hw when no longer used - check all FEs */
2012 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2013 continue;
2014
Qiao Zhou36fba622014-12-03 10:13:43 +08002015 /* do not free hw if this BE is used by other FE */
2016 if (be->dpcm[stream].users > 1)
2017 continue;
2018
Liam Girdwood01d75842012-04-25 12:12:49 +01002019 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2021 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002022 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302023 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2024 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002025 continue;
2026
Liam Girdwood103d84a2012-11-19 14:39:15 +00002027 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002028 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002029
2030 soc_pcm_hw_free(be_substream);
2031
2032 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2033 }
2034
2035 return 0;
2036}
2037
Mark Brown45c0a182012-05-09 21:46:27 +01002038static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002039{
2040 struct snd_soc_pcm_runtime *fe = substream->private_data;
2041 int err, stream = substream->stream;
2042
2043 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002044 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002045
Liam Girdwood103d84a2012-11-19 14:39:15 +00002046 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002047
2048 /* call hw_free on the frontend */
2049 err = soc_pcm_hw_free(substream);
2050 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002051 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002052 fe->dai_link->name);
2053
2054 /* only hw_params backends that are either sinks or sources
2055 * to this frontend DAI */
2056 err = dpcm_be_dai_hw_free(fe, stream);
2057
2058 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002059 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002060
2061 mutex_unlock(&fe->card->mutex);
2062 return 0;
2063}
2064
Liam Girdwood23607022014-01-17 17:03:55 +00002065int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002066{
2067 struct snd_soc_dpcm *dpcm;
2068 int ret;
2069
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002070 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002071
2072 struct snd_soc_pcm_runtime *be = dpcm->be;
2073 struct snd_pcm_substream *be_substream =
2074 snd_soc_dpcm_get_substream(be, stream);
2075
2076 /* is this op for this BE ? */
2077 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2078 continue;
2079
Liam Girdwood01d75842012-04-25 12:12:49 +01002080 /* copy params for each dpcm */
2081 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2082 sizeof(struct snd_pcm_hw_params));
2083
2084 /* perform any hw_params fixups */
2085 if (be->dai_link->be_hw_params_fixup) {
2086 ret = be->dai_link->be_hw_params_fixup(be,
2087 &dpcm->hw_params);
2088 if (ret < 0) {
2089 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002090 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002091 ret);
2092 goto unwind;
2093 }
2094 }
2095
Libin Yangae061d22019-04-19 09:53:12 +08002096 /* copy the fixed-up hw params for BE dai */
2097 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2098 sizeof(struct snd_pcm_hw_params));
2099
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002100 /* only allow hw_params() if no connected FEs are running */
2101 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2102 continue;
2103
2104 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2105 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2106 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2107 continue;
2108
2109 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002110 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002111
Liam Girdwood01d75842012-04-25 12:12:49 +01002112 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2113 if (ret < 0) {
2114 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002115 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002116 goto unwind;
2117 }
2118
2119 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2120 }
2121 return 0;
2122
2123unwind:
2124 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002125 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002126 struct snd_soc_pcm_runtime *be = dpcm->be;
2127 struct snd_pcm_substream *be_substream =
2128 snd_soc_dpcm_get_substream(be, stream);
2129
2130 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2131 continue;
2132
2133 /* only allow hw_free() if no connected FEs are running */
2134 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2135 continue;
2136
2137 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2138 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2139 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2140 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2141 continue;
2142
2143 soc_pcm_hw_free(be_substream);
2144 }
2145
2146 return ret;
2147}
2148
Mark Brown45c0a182012-05-09 21:46:27 +01002149static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2150 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002151{
2152 struct snd_soc_pcm_runtime *fe = substream->private_data;
2153 int ret, stream = substream->stream;
2154
2155 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002156 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002157
2158 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2159 sizeof(struct snd_pcm_hw_params));
2160 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2161 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002162 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002163 goto out;
2164 }
2165
Liam Girdwood103d84a2012-11-19 14:39:15 +00002166 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002167 fe->dai_link->name, params_rate(params),
2168 params_channels(params), params_format(params));
2169
2170 /* call hw_params on the frontend */
2171 ret = soc_pcm_hw_params(substream, params);
2172 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002173 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002174 dpcm_be_dai_hw_free(fe, stream);
2175 } else
2176 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2177
2178out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002179 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002180 mutex_unlock(&fe->card->mutex);
2181 return ret;
2182}
2183
2184static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2185 struct snd_pcm_substream *substream, int cmd)
2186{
2187 int ret;
2188
Liam Girdwood103d84a2012-11-19 14:39:15 +00002189 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002190 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002191
2192 ret = soc_pcm_trigger(substream, cmd);
2193 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002194 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002195
2196 return ret;
2197}
2198
Liam Girdwood23607022014-01-17 17:03:55 +00002199int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002200 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002201{
2202 struct snd_soc_dpcm *dpcm;
2203 int ret = 0;
2204
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002205 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002206
2207 struct snd_soc_pcm_runtime *be = dpcm->be;
2208 struct snd_pcm_substream *be_substream =
2209 snd_soc_dpcm_get_substream(be, stream);
2210
2211 /* is this op for this BE ? */
2212 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2213 continue;
2214
2215 switch (cmd) {
2216 case SNDRV_PCM_TRIGGER_START:
2217 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2218 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2219 continue;
2220
2221 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2222 if (ret)
2223 return ret;
2224
2225 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2226 break;
2227 case SNDRV_PCM_TRIGGER_RESUME:
2228 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2229 continue;
2230
2231 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2232 if (ret)
2233 return ret;
2234
2235 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2236 break;
2237 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2238 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2239 continue;
2240
2241 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2242 if (ret)
2243 return ret;
2244
2245 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2246 break;
2247 case SNDRV_PCM_TRIGGER_STOP:
2248 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2249 continue;
2250
2251 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2252 continue;
2253
2254 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2255 if (ret)
2256 return ret;
2257
2258 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2259 break;
2260 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002261 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002262 continue;
2263
2264 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2265 continue;
2266
2267 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2268 if (ret)
2269 return ret;
2270
2271 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2272 break;
2273 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2274 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2275 continue;
2276
2277 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2278 continue;
2279
2280 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2281 if (ret)
2282 return ret;
2283
2284 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2285 break;
2286 }
2287 }
2288
2289 return ret;
2290}
2291EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2292
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002293static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2294 int cmd, bool fe_first)
2295{
2296 struct snd_soc_pcm_runtime *fe = substream->private_data;
2297 int ret;
2298
2299 /* call trigger on the frontend before the backend. */
2300 if (fe_first) {
2301 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2302 fe->dai_link->name, cmd);
2303
2304 ret = soc_pcm_trigger(substream, cmd);
2305 if (ret < 0)
2306 return ret;
2307
2308 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2309 return ret;
2310 }
2311
2312 /* call trigger on the frontend after the backend. */
2313 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2314 if (ret < 0)
2315 return ret;
2316
2317 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2318 fe->dai_link->name, cmd);
2319
2320 ret = soc_pcm_trigger(substream, cmd);
2321
2322 return ret;
2323}
2324
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002325static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002326{
2327 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002328 int stream = substream->stream;
2329 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002330 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2331
2332 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2333
2334 switch (trigger) {
2335 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002336 switch (cmd) {
2337 case SNDRV_PCM_TRIGGER_START:
2338 case SNDRV_PCM_TRIGGER_RESUME:
2339 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2340 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2341 break;
2342 case SNDRV_PCM_TRIGGER_STOP:
2343 case SNDRV_PCM_TRIGGER_SUSPEND:
2344 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2345 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2346 break;
2347 default:
2348 ret = -EINVAL;
2349 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002350 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002351 break;
2352 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002353 switch (cmd) {
2354 case SNDRV_PCM_TRIGGER_START:
2355 case SNDRV_PCM_TRIGGER_RESUME:
2356 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2357 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2358 break;
2359 case SNDRV_PCM_TRIGGER_STOP:
2360 case SNDRV_PCM_TRIGGER_SUSPEND:
2361 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2362 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2363 break;
2364 default:
2365 ret = -EINVAL;
2366 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002367 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002368 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002369 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2370 /* bespoke trigger() - handles both FE and BEs */
2371
Liam Girdwood103d84a2012-11-19 14:39:15 +00002372 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002373 fe->dai_link->name, cmd);
2374
2375 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002376 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002377 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002378 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002379 fe->dai_link->name);
2380 ret = -EINVAL;
2381 goto out;
2382 }
2383
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002384 if (ret < 0) {
2385 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2386 cmd, ret);
2387 goto out;
2388 }
2389
Liam Girdwood01d75842012-04-25 12:12:49 +01002390 switch (cmd) {
2391 case SNDRV_PCM_TRIGGER_START:
2392 case SNDRV_PCM_TRIGGER_RESUME:
2393 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2394 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2395 break;
2396 case SNDRV_PCM_TRIGGER_STOP:
2397 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002398 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2399 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002400 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2401 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2402 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002403 }
2404
2405out:
2406 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2407 return ret;
2408}
2409
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002410static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2411{
2412 struct snd_soc_pcm_runtime *fe = substream->private_data;
2413 int stream = substream->stream;
2414
2415 /* if FE's runtime_update is already set, we're in race;
2416 * process this trigger later at exit
2417 */
2418 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2419 fe->dpcm[stream].trigger_pending = cmd + 1;
2420 return 0; /* delayed, assuming it's successful */
2421 }
2422
2423 /* we're alone, let's trigger */
2424 return dpcm_fe_dai_do_trigger(substream, cmd);
2425}
2426
Liam Girdwood23607022014-01-17 17:03:55 +00002427int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002428{
2429 struct snd_soc_dpcm *dpcm;
2430 int ret = 0;
2431
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002432 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002433
2434 struct snd_soc_pcm_runtime *be = dpcm->be;
2435 struct snd_pcm_substream *be_substream =
2436 snd_soc_dpcm_get_substream(be, stream);
2437
2438 /* is this op for this BE ? */
2439 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2440 continue;
2441
2442 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002443 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002444 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002446 continue;
2447
Liam Girdwood103d84a2012-11-19 14:39:15 +00002448 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002449 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002450
2451 ret = soc_pcm_prepare(be_substream);
2452 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002453 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002454 ret);
2455 break;
2456 }
2457
2458 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2459 }
2460 return ret;
2461}
2462
Mark Brown45c0a182012-05-09 21:46:27 +01002463static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002464{
2465 struct snd_soc_pcm_runtime *fe = substream->private_data;
2466 int stream = substream->stream, ret = 0;
2467
2468 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2469
Liam Girdwood103d84a2012-11-19 14:39:15 +00002470 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002471
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002472 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002473
2474 /* there is no point preparing this FE if there are no BEs */
2475 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002476 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002477 fe->dai_link->name);
2478 ret = -EINVAL;
2479 goto out;
2480 }
2481
2482 ret = dpcm_be_dai_prepare(fe, substream->stream);
2483 if (ret < 0)
2484 goto out;
2485
2486 /* call prepare on the frontend */
2487 ret = soc_pcm_prepare(substream);
2488 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002489 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002490 fe->dai_link->name);
2491 goto out;
2492 }
2493
2494 /* run the stream event for each BE */
2495 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2496 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2497
2498out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002499 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002500 mutex_unlock(&fe->card->mutex);
2501
2502 return ret;
2503}
2504
Liam Girdwood618dae12012-04-25 12:12:51 +01002505static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2506{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002507 struct snd_pcm_substream *substream =
2508 snd_soc_dpcm_get_substream(fe, stream);
2509 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002510 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002511
Liam Girdwood103d84a2012-11-19 14:39:15 +00002512 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002513 stream ? "capture" : "playback", fe->dai_link->name);
2514
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002515 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2516 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002517 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002518 fe->dai_link->name);
2519
2520 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2521 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002522 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002523 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002524 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002525 fe->dai_link->name);
2526
2527 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2528 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002529 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002530 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002531
2532 err = dpcm_be_dai_hw_free(fe, stream);
2533 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002534 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002535
2536 err = dpcm_be_dai_shutdown(fe, stream);
2537 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002538 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002539
2540 /* run the stream event for each BE */
2541 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2542
2543 return 0;
2544}
2545
2546static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2547{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002548 struct snd_pcm_substream *substream =
2549 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002550 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002551 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002552 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002553 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002554
Liam Girdwood103d84a2012-11-19 14:39:15 +00002555 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002556 stream ? "capture" : "playback", fe->dai_link->name);
2557
2558 /* Only start the BE if the FE is ready */
2559 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2560 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2561 return -EINVAL;
2562
2563 /* startup must always be called for new BEs */
2564 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002565 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002566 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002567
2568 /* keep going if FE state is > open */
2569 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2570 return 0;
2571
2572 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002573 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002574 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002575
2576 /* keep going if FE state is > hw_params */
2577 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2578 return 0;
2579
2580
2581 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002582 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002583 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002584
2585 /* run the stream event for each BE */
2586 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2587
2588 /* keep going if FE state is > prepare */
2589 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2590 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2591 return 0;
2592
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002593 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2594 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002595 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002596 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002597
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002598 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2599 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002600 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002601 goto hw_free;
2602 }
2603 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002604 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002605 fe->dai_link->name);
2606
2607 ret = dpcm_be_dai_trigger(fe, stream,
2608 SNDRV_PCM_TRIGGER_START);
2609 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002610 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002611 goto hw_free;
2612 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002613 }
2614
2615 return 0;
2616
2617hw_free:
2618 dpcm_be_dai_hw_free(fe, stream);
2619close:
2620 dpcm_be_dai_shutdown(fe, stream);
2621disconnect:
2622 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002623 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002624 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002625 struct snd_soc_pcm_runtime *be = dpcm->be;
2626 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2627 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2628 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002629 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002630
2631 return ret;
2632}
2633
2634static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2635{
2636 int ret;
2637
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002638 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002639 ret = dpcm_run_update_startup(fe, stream);
2640 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002641 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002642 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002643
2644 return ret;
2645}
2646
2647static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2648{
2649 int ret;
2650
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002651 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002652 ret = dpcm_run_update_shutdown(fe, stream);
2653 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002654 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002655 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002656
2657 return ret;
2658}
2659
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002660static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2661{
2662 struct snd_soc_dapm_widget_list *list;
2663 int count, paths;
2664
2665 if (!fe->dai_link->dynamic)
2666 return 0;
2667
2668 /* only check active links */
2669 if (!fe->cpu_dai->active)
2670 return 0;
2671
2672 /* DAPM sync will call this to update DSP paths */
2673 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2674 new ? "new" : "old", fe->dai_link->name);
2675
2676 /* skip if FE doesn't have playback capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002677 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2678 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002679 goto capture;
2680
2681 /* skip if FE isn't currently playing */
2682 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2683 goto capture;
2684
2685 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2686 if (paths < 0) {
2687 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2688 fe->dai_link->name, "playback");
2689 return paths;
2690 }
2691
2692 /* update any playback paths */
2693 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2694 if (count) {
2695 if (new)
2696 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2697 else
2698 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2699
2700 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2701 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2702 }
2703
2704 dpcm_path_put(&list);
2705
2706capture:
2707 /* skip if FE doesn't have capture capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002708 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2709 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002710 return 0;
2711
2712 /* skip if FE isn't currently capturing */
2713 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2714 return 0;
2715
2716 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2717 if (paths < 0) {
2718 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2719 fe->dai_link->name, "capture");
2720 return paths;
2721 }
2722
2723 /* update any old capture paths */
2724 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2725 if (count) {
2726 if (new)
2727 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2728 else
2729 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2730
2731 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2732 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2733 }
2734
2735 dpcm_path_put(&list);
2736
2737 return 0;
2738}
2739
Liam Girdwood618dae12012-04-25 12:12:51 +01002740/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2741 * any DAI links.
2742 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002743int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002744{
Mengdong Lin1a497982015-11-18 02:34:11 -05002745 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002746 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002747
Liam Girdwood618dae12012-04-25 12:12:51 +01002748 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002749 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002750 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002751 ret = soc_dpcm_fe_runtime_update(fe, 0);
2752 if (ret)
2753 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002754 }
2755
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002756 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002757 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002758 ret = soc_dpcm_fe_runtime_update(fe, 1);
2759 if (ret)
2760 goto out;
2761 }
2762
2763out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002764 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002765 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002766}
Liam Girdwood01d75842012-04-25 12:12:49 +01002767int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2768{
2769 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002770 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01002771
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002772 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002773
2774 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002775 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002776
2777 if (be->dai_link->ignore_suspend)
2778 continue;
2779
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002780 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002781 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002782
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002783 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2784 be->dai_link->name);
2785
2786 if (drv->ops && drv->ops->digital_mute &&
2787 dai->playback_active)
2788 drv->ops->digital_mute(dai, mute);
2789 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002790 }
2791
2792 return 0;
2793}
2794
Mark Brown45c0a182012-05-09 21:46:27 +01002795static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002796{
2797 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2798 struct snd_soc_dpcm *dpcm;
2799 struct snd_soc_dapm_widget_list *list;
2800 int ret;
2801 int stream = fe_substream->stream;
2802
2803 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2804 fe->dpcm[stream].runtime = fe_substream->runtime;
2805
Qiao Zhou8f70e512014-09-10 17:54:07 +08002806 ret = dpcm_path_get(fe, stream, &list);
2807 if (ret < 0) {
2808 mutex_unlock(&fe->card->mutex);
2809 return ret;
2810 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002811 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002812 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002813 }
2814
2815 /* calculate valid and active FE <-> BE dpcms */
2816 dpcm_process_paths(fe, stream, &list, 1);
2817
2818 ret = dpcm_fe_dai_startup(fe_substream);
2819 if (ret < 0) {
2820 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002821 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002822 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2823
2824 dpcm_be_disconnect(fe, stream);
2825 fe->dpcm[stream].runtime = NULL;
2826 }
2827
2828 dpcm_clear_pending_state(fe, stream);
2829 dpcm_path_put(&list);
2830 mutex_unlock(&fe->card->mutex);
2831 return ret;
2832}
2833
Mark Brown45c0a182012-05-09 21:46:27 +01002834static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002835{
2836 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2837 struct snd_soc_dpcm *dpcm;
2838 int stream = fe_substream->stream, ret;
2839
2840 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2841 ret = dpcm_fe_dai_shutdown(fe_substream);
2842
2843 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002844 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002845 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2846
2847 dpcm_be_disconnect(fe, stream);
2848
2849 fe->dpcm[stream].runtime = NULL;
2850 mutex_unlock(&fe->card->mutex);
2851 return ret;
2852}
2853
Liam Girdwoodddee6272011-06-09 14:45:53 +01002854/* create a new pcm */
2855int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2856{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002857 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002858 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002859 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002860 struct snd_pcm *pcm;
2861 char new_name[64];
2862 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002863 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002864
Liam Girdwood01d75842012-04-25 12:12:49 +01002865 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002866 playback = rtd->dai_link->dpcm_playback;
2867 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002868 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002869 /* Adapt stream for codec2codec links */
2870 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2871 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2872 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2873 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2874
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002875 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002876 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Sameer Pujaraf4bac12020-01-19 19:49:23 +05302877 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002878 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002879 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Sameer Pujaraf4bac12020-01-19 19:49:23 +05302880 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002881 capture = 1;
2882 }
Jerome Bruneta3420312019-07-25 18:59:47 +02002883
2884 capture = capture && cpu_capture->channels_min;
2885 playback = playback && cpu_playback->channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002886 }
Sangsu Parka5002312012-01-02 17:15:10 +09002887
Fabio Estevamd6bead02013-08-29 10:32:13 -03002888 if (rtd->dai_link->playback_only) {
2889 playback = 1;
2890 capture = 0;
2891 }
2892
2893 if (rtd->dai_link->capture_only) {
2894 playback = 0;
2895 capture = 1;
2896 }
2897
Liam Girdwood01d75842012-04-25 12:12:49 +01002898 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002899 if (rtd->dai_link->params) {
2900 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2901 rtd->dai_link->stream_name);
2902
2903 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2904 playback, capture, &pcm);
2905 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002906 snprintf(new_name, sizeof(new_name), "(%s)",
2907 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002908
Liam Girdwood01d75842012-04-25 12:12:49 +01002909 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2910 playback, capture, &pcm);
2911 } else {
2912 if (rtd->dai_link->dynamic)
2913 snprintf(new_name, sizeof(new_name), "%s (*)",
2914 rtd->dai_link->stream_name);
2915 else
2916 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002917 rtd->dai_link->stream_name,
2918 (rtd->num_codecs > 1) ?
2919 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002920
Liam Girdwood01d75842012-04-25 12:12:49 +01002921 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2922 capture, &pcm);
2923 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002924 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002925 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002926 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002927 return ret;
2928 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002929 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002930
2931 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002932 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002933 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002934 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002935 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002936
Vinod Koul48c76992015-02-12 09:59:53 +05302937 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002938 rtd->pcm = pcm;
2939 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002940
Jerome Bruneta3420312019-07-25 18:59:47 +02002941 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002942 if (playback)
2943 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2944 if (capture)
2945 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2946 goto out;
2947 }
2948
2949 /* ASoC PCM operations */
2950 if (rtd->dai_link->dynamic) {
2951 rtd->ops.open = dpcm_fe_dai_open;
2952 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2953 rtd->ops.prepare = dpcm_fe_dai_prepare;
2954 rtd->ops.trigger = dpcm_fe_dai_trigger;
2955 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2956 rtd->ops.close = dpcm_fe_dai_close;
2957 rtd->ops.pointer = soc_pcm_pointer;
2958 } else {
2959 rtd->ops.open = soc_pcm_open;
2960 rtd->ops.hw_params = soc_pcm_hw_params;
2961 rtd->ops.prepare = soc_pcm_prepare;
2962 rtd->ops.trigger = soc_pcm_trigger;
2963 rtd->ops.hw_free = soc_pcm_hw_free;
2964 rtd->ops.close = soc_pcm_close;
2965 rtd->ops.pointer = soc_pcm_pointer;
2966 }
2967
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002968 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002969 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002970
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002971 if (drv->ioctl)
2972 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002973 if (drv->sync_stop)
2974 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002975 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002976 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002977 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002978 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002979 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002980 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002981 }
2982
Liam Girdwoodddee6272011-06-09 14:45:53 +01002983 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002984 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002985
2986 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002987 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002988
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002989 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002990 if (ret < 0) {
2991 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2992 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002993 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002994
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002995 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002996out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002997 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2998 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2999 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003000 return ret;
3001}
Liam Girdwood01d75842012-04-25 12:12:49 +01003002
3003/* is the current PCM operation for this FE ? */
3004int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3005{
3006 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3007 return 1;
3008 return 0;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3011
3012/* is the current PCM operation for this BE ? */
3013int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3014 struct snd_soc_pcm_runtime *be, int stream)
3015{
3016 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3017 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3018 be->dpcm[stream].runtime_update))
3019 return 1;
3020 return 0;
3021}
3022EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3023
3024/* get the substream for this BE */
3025struct snd_pcm_substream *
3026 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3027{
3028 return be->pcm->streams[stream].substream;
3029}
3030EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3031
3032/* get the BE runtime state */
3033enum snd_soc_dpcm_state
3034 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3035{
3036 return be->dpcm[stream].state;
3037}
3038EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3039
3040/* set the BE runtime state */
3041void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3042 int stream, enum snd_soc_dpcm_state state)
3043{
3044 be->dpcm[stream].state = state;
3045}
3046EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3047
3048/*
3049 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3050 * are not running, paused or suspended for the specified stream direction.
3051 */
3052int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3053 struct snd_soc_pcm_runtime *be, int stream)
3054{
3055 struct snd_soc_dpcm *dpcm;
3056 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003057 int ret = 1;
3058 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003059
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003060 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003061 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003062
3063 if (dpcm->fe == fe)
3064 continue;
3065
3066 state = dpcm->fe->dpcm[stream].state;
3067 if (state == SND_SOC_DPCM_STATE_START ||
3068 state == SND_SOC_DPCM_STATE_PAUSED ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003069 state == SND_SOC_DPCM_STATE_SUSPEND) {
3070 ret = 0;
3071 break;
3072 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003073 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003074 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003075
3076 /* it's safe to free/stop this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003077 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003078}
3079EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3080
3081/*
3082 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3083 * running, paused or suspended for the specified stream direction.
3084 */
3085int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3086 struct snd_soc_pcm_runtime *be, int stream)
3087{
3088 struct snd_soc_dpcm *dpcm;
3089 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003090 int ret = 1;
3091 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003092
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003093 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003094 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003095
3096 if (dpcm->fe == fe)
3097 continue;
3098
3099 state = dpcm->fe->dpcm[stream].state;
3100 if (state == SND_SOC_DPCM_STATE_START ||
3101 state == SND_SOC_DPCM_STATE_PAUSED ||
3102 state == SND_SOC_DPCM_STATE_SUSPEND ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003103 state == SND_SOC_DPCM_STATE_PREPARE) {
3104 ret = 0;
3105 break;
3106 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003107 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003108 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003109
3110 /* it's safe to change hw_params */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003111 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003112}
3113EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003114
3115#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003116static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003117{
3118 switch (state) {
3119 case SND_SOC_DPCM_STATE_NEW:
3120 return "new";
3121 case SND_SOC_DPCM_STATE_OPEN:
3122 return "open";
3123 case SND_SOC_DPCM_STATE_HW_PARAMS:
3124 return "hw_params";
3125 case SND_SOC_DPCM_STATE_PREPARE:
3126 return "prepare";
3127 case SND_SOC_DPCM_STATE_START:
3128 return "start";
3129 case SND_SOC_DPCM_STATE_STOP:
3130 return "stop";
3131 case SND_SOC_DPCM_STATE_SUSPEND:
3132 return "suspend";
3133 case SND_SOC_DPCM_STATE_PAUSED:
3134 return "paused";
3135 case SND_SOC_DPCM_STATE_HW_FREE:
3136 return "hw_free";
3137 case SND_SOC_DPCM_STATE_CLOSE:
3138 return "close";
3139 }
3140
3141 return "unknown";
3142}
3143
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003144static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3145 int stream, char *buf, size_t size)
3146{
3147 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3148 struct snd_soc_dpcm *dpcm;
3149 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003150 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003151
3152 /* FE state */
3153 offset += snprintf(buf + offset, size - offset,
3154 "[%s - %s]\n", fe->dai_link->name,
3155 stream ? "Capture" : "Playback");
3156
3157 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3158 dpcm_state_string(fe->dpcm[stream].state));
3159
3160 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3161 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3162 offset += snprintf(buf + offset, size - offset,
3163 "Hardware Params: "
3164 "Format = %s, Channels = %d, Rate = %d\n",
3165 snd_pcm_format_name(params_format(params)),
3166 params_channels(params),
3167 params_rate(params));
3168
3169 /* BEs state */
3170 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3171
3172 if (list_empty(&fe->dpcm[stream].be_clients)) {
3173 offset += snprintf(buf + offset, size - offset,
3174 " No active DSP links\n");
3175 goto out;
3176 }
3177
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003178 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003179 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003180 struct snd_soc_pcm_runtime *be = dpcm->be;
3181 params = &dpcm->hw_params;
3182
3183 offset += snprintf(buf + offset, size - offset,
3184 "- %s\n", be->dai_link->name);
3185
3186 offset += snprintf(buf + offset, size - offset,
3187 " State: %s\n",
3188 dpcm_state_string(be->dpcm[stream].state));
3189
3190 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3191 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3192 offset += snprintf(buf + offset, size - offset,
3193 " Hardware Params: "
3194 "Format = %s, Channels = %d, Rate = %d\n",
3195 snd_pcm_format_name(params_format(params)),
3196 params_channels(params),
3197 params_rate(params));
3198 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003199 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003200out:
3201 return offset;
3202}
3203
3204static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3205 size_t count, loff_t *ppos)
3206{
3207 struct snd_soc_pcm_runtime *fe = file->private_data;
3208 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3209 char *buf;
3210
3211 buf = kmalloc(out_count, GFP_KERNEL);
3212 if (!buf)
3213 return -ENOMEM;
3214
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003215 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003216 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3217 buf + offset, out_count - offset);
3218
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003219 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003220 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3221 buf + offset, out_count - offset);
3222
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003223 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003224
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003225 kfree(buf);
3226 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003227}
3228
3229static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003230 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003231 .read = dpcm_state_read_file,
3232 .llseek = default_llseek,
3233};
3234
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003235void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003236{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003237 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003238 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003239
Kuninori Morimoto596becd2019-08-07 10:31:36 +09003240 if (!rtd->dai_link->dynamic)
3241 return;
3242
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003243 if (!rtd->card->debugfs_card_root)
3244 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003245
3246 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3247 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003248
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003249 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3250 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003251}
3252#endif