blob: aff27c8599ef98950ad563d6f8563f8f845bfb17 [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 Morimotod9303692020-02-17 17:27:39 +090085static inline
86struct snd_soc_dapm_widget *dai_get_widget(struct snd_soc_dai *dai, int stream)
87{
88 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
89 return dai->playback_widget;
90 else
91 return dai->capture_widget;
92}
93
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +090094static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
95 int stream, int action)
96{
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai;
99 int i;
100
101 lockdep_assert_held(&rtd->card->pcm_mutex);
102
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900103 cpu_dai->stream_active[stream] += action;
104 for_each_rtd_codec_dai(rtd, i, codec_dai)
105 codec_dai->stream_active[stream] += action;
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900106
107 cpu_dai->active += action;
108 cpu_dai->component->active += action;
109 for_each_rtd_codec_dai(rtd, i, codec_dai) {
110 codec_dai->active += action;
111 codec_dai->component->active += action;
112 }
113}
114
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200115/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100116 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
117 * @rtd: ASoC PCM runtime that is activated
118 * @stream: Direction of the PCM stream
119 *
120 * Increments the active count for all the DAIs and components attached to a PCM
121 * runtime. Should typically be called when a stream is opened.
122 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300123 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100124 */
125void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
126{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900127 snd_soc_runtime_action(rtd, stream, 1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100128}
129
130/**
131 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
132 * @rtd: ASoC PCM runtime that is deactivated
133 * @stream: Direction of the PCM stream
134 *
135 * Decrements the active count for all the DAIs and components attached to a PCM
136 * runtime. Should typically be called when a stream is closed.
137 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300138 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100139 */
140void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
141{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900142 snd_soc_runtime_action(rtd, stream, -1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100143}
144
145/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100146 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
147 * @rtd: The ASoC PCM runtime that should be checked.
148 *
149 * This function checks whether the power down delay should be ignored for a
150 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
151 * been configured to ignore the delay, or if none of the components benefits
152 * from having the delay.
153 */
154bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
155{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000156 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200157 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900158 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200159
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100160 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
161 return true;
162
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900163 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000164 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000165
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000166 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100167}
168
169/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200170 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
171 * @substream: the pcm substream
172 * @hw: the hardware parameters
173 *
174 * Sets the substream runtime hardware parameters.
175 */
176int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
177 const struct snd_pcm_hardware *hw)
178{
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 runtime->hw.info = hw->info;
181 runtime->hw.formats = hw->formats;
182 runtime->hw.period_bytes_min = hw->period_bytes_min;
183 runtime->hw.period_bytes_max = hw->period_bytes_max;
184 runtime->hw.periods_min = hw->periods_min;
185 runtime->hw.periods_max = hw->periods_max;
186 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
187 runtime->hw.fifo_size = hw->fifo_size;
188 return 0;
189}
190EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
191
Liam Girdwood01d75842012-04-25 12:12:49 +0100192/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000193int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100194 int event)
195{
196 struct snd_soc_dpcm *dpcm;
197
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000198 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100199
200 struct snd_soc_pcm_runtime *be = dpcm->be;
201
Liam Girdwood103d84a2012-11-19 14:39:15 +0000202 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100203 be->dai_link->name, event, dir);
204
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700205 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
206 (be->dpcm[dir].users >= 1))
207 continue;
208
Liam Girdwood01d75842012-04-25 12:12:49 +0100209 snd_soc_dapm_stream_event(be, dir, event);
210 }
211
212 snd_soc_dapm_stream_event(fe, dir, event);
213
214 return 0;
215}
216
Dong Aisheng17841022011-08-29 17:15:14 +0800217static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
218 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100219{
220 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100221 int ret;
222
Nicolin Chen3635bf02013-11-13 18:56:24 +0800223 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
224 rtd->dai_link->symmetric_rates)) {
225 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
226 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100227
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200228 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800229 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200230 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800231 if (ret < 0) {
232 dev_err(soc_dai->dev,
233 "ASoC: Unable to apply rate constraint: %d\n",
234 ret);
235 return ret;
236 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100237 }
238
Nicolin Chen3635bf02013-11-13 18:56:24 +0800239 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
240 rtd->dai_link->symmetric_channels)) {
241 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
242 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200244 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800245 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800246 soc_dai->channels);
247 if (ret < 0) {
248 dev_err(soc_dai->dev,
249 "ASoC: Unable to apply channel symmetry constraint: %d\n",
250 ret);
251 return ret;
252 }
253 }
254
255 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
256 rtd->dai_link->symmetric_samplebits)) {
257 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
258 soc_dai->sample_bits);
259
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200260 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800261 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800262 soc_dai->sample_bits);
263 if (ret < 0) {
264 dev_err(soc_dai->dev,
265 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
266 ret);
267 return ret;
268 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100269 }
270
271 return 0;
272}
273
Nicolin Chen3635bf02013-11-13 18:56:24 +0800274static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
275 struct snd_pcm_hw_params *params)
276{
277 struct snd_soc_pcm_runtime *rtd = substream->private_data;
278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000279 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200280 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800281
282 rate = params_rate(params);
283 channels = params_channels(params);
284 sample_bits = snd_pcm_format_physical_width(params_format(params));
285
286 /* reject unmatched parameters when applying symmetry */
287 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800288 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000290 for_each_rtd_codec_dai(rtd, i, codec_dai)
291 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200292
Nicolin Chen3635bf02013-11-13 18:56:24 +0800293 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
294 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
295 cpu_dai->rate, rate);
296 return -EINVAL;
297 }
298
299 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800300 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200301
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000302 for_each_rtd_codec_dai(rtd, i, codec_dai)
303 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304
Nicolin Chen3635bf02013-11-13 18:56:24 +0800305 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
306 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
307 cpu_dai->channels, channels);
308 return -EINVAL;
309 }
310
311 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800312 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200313
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000314 for_each_rtd_codec_dai(rtd, i, codec_dai)
315 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200316
Nicolin Chen3635bf02013-11-13 18:56:24 +0800317 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
318 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
319 cpu_dai->sample_bits, sample_bits);
320 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100321 }
322
323 return 0;
324}
325
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100326static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
327{
328 struct snd_soc_pcm_runtime *rtd = substream->private_data;
329 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100330 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000331 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200332 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100333
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200334 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
335 cpu_driver->symmetric_channels || link->symmetric_channels ||
336 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
337
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000338 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200339 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000340 codec_dai->driver->symmetric_rates ||
341 codec_dai->driver->symmetric_channels ||
342 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200343
344 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100345}
346
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200347static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000348{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200349 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100350 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000351
352 if (!bits)
353 return;
354
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100355 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
356 if (ret != 0)
357 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
358 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000359}
360
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200361static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200362{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200363 struct snd_soc_pcm_runtime *rtd = substream->private_data;
364 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200365 struct snd_soc_dai *codec_dai;
366 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200367 unsigned int bits = 0, cpu_bits;
368
369 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000370 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200371 if (codec_dai->driver->playback.sig_bits == 0) {
372 bits = 0;
373 break;
374 }
375 bits = max(codec_dai->driver->playback.sig_bits, bits);
376 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200377 cpu_bits = cpu_dai->driver->playback.sig_bits;
378 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000379 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200380 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200381 bits = 0;
382 break;
383 }
384 bits = max(codec_dai->driver->capture.sig_bits, bits);
385 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200386 cpu_bits = cpu_dai->driver->capture.sig_bits;
387 }
388
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200389 soc_pcm_set_msb(substream, bits);
390 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200391}
392
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200393static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200394{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200395 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100396 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200397 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000398 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200399 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
400 struct snd_soc_dai_driver *codec_dai_drv;
401 struct snd_soc_pcm_stream *codec_stream;
402 struct snd_soc_pcm_stream *cpu_stream;
403 unsigned int chan_min = 0, chan_max = UINT_MAX;
404 unsigned int rate_min = 0, rate_max = UINT_MAX;
405 unsigned int rates = UINT_MAX;
406 u64 formats = ULLONG_MAX;
407 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100408
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100411 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200412 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100413
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200414 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000415 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200416
417 /*
418 * Skip CODECs which don't support the current stream type.
419 * Otherwise, since the rate, channel, and format values will
420 * zero in that case, we would have no usable settings left,
421 * causing the resulting setup to fail.
422 * At least one CODEC should match, otherwise we should have
423 * bailed out on a higher level, since there would be no
424 * CODEC to support the transfer direction in that case.
425 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000426 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200427 substream->stream))
428 continue;
429
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000430 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
432 codec_stream = &codec_dai_drv->playback;
433 else
434 codec_stream = &codec_dai_drv->capture;
435 chan_min = max(chan_min, codec_stream->channels_min);
436 chan_max = min(chan_max, codec_stream->channels_max);
437 rate_min = max(rate_min, codec_stream->rate_min);
438 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
439 formats &= codec_stream->formats;
440 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
441 }
442
443 /*
444 * chan min/max cannot be enforced if there are multiple CODEC DAIs
445 * connected to a single CPU DAI, use CPU DAI's directly and let
446 * channel allocation be fixed up later
447 */
448 if (rtd->num_codecs > 1) {
449 chan_min = cpu_stream->channels_min;
450 chan_max = cpu_stream->channels_max;
451 }
452
453 hw->channels_min = max(chan_min, cpu_stream->channels_min);
454 hw->channels_max = min(chan_max, cpu_stream->channels_max);
455 if (hw->formats)
456 hw->formats &= formats & cpu_stream->formats;
457 else
458 hw->formats = formats & cpu_stream->formats;
459 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100460
461 snd_pcm_limit_hw_rates(runtime);
462
463 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200464 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100465 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200466 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200467}
468
Kuninori Morimotodd039072020-02-10 12:14:37 +0900469static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900470{
471 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900472 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900473 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900474
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900475 for_each_rtd_components(rtd, i, component) {
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 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900492
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900493 return 0;
494}
495
Kuninori Morimotodd039072020-02-10 12:14:37 +0900496static int soc_pcm_components_close(struct snd_pcm_substream *substream)
Charles Keepax244e2932018-06-19 16:22:09 +0100497{
498 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100499 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900500 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100501
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900502 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900503 r = snd_soc_component_close(component, substream);
504 if (r < 0)
505 ret = r; /* use last ret */
506
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900507 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100508 }
509
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900510 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100511}
512
Mark Brown58ba9b22012-01-16 18:38:51 +0000513/*
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900514 * Called by ALSA when a PCM substream is closed. Private data can be
515 * freed here. The cpu DAI, codec DAI, machine and components are also
516 * shutdown.
517 */
518static int soc_pcm_close(struct snd_pcm_substream *substream)
519{
520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 struct snd_soc_component *component;
522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
523 struct snd_soc_dai *codec_dai;
524 int i;
525
526 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
527
528 snd_soc_runtime_deactivate(rtd, substream->stream);
529
530 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
531
532 snd_soc_dai_shutdown(cpu_dai, substream);
533
534 for_each_rtd_codec_dai(rtd, i, codec_dai)
535 snd_soc_dai_shutdown(codec_dai, substream);
536
537 soc_rtd_shutdown(rtd, substream);
538
539 soc_pcm_components_close(substream);
540
541 snd_soc_dapm_stream_stop(rtd, substream->stream);
542
543 mutex_unlock(&rtd->card->pcm_mutex);
544
545 for_each_rtd_components(rtd, i, component) {
546 pm_runtime_mark_last_busy(component->dev);
547 pm_runtime_put_autosuspend(component->dev);
548 }
549
550 for_each_rtd_components(rtd, i, component)
551 if (!component->active)
552 pinctrl_pm_select_sleep_state(component->dev);
553
554 return 0;
555}
556
557/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100558 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
559 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100560 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100561 */
562static int soc_pcm_open(struct snd_pcm_substream *substream)
563{
564 struct snd_soc_pcm_runtime *rtd = substream->private_data;
565 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000566 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100567 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200568 struct snd_soc_dai *codec_dai;
569 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100570 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100571
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900572 for_each_rtd_components(rtd, i, component)
573 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000574
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900575 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000576 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000577
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300578 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900580 ret = soc_pcm_components_open(substream);
581 if (ret < 0)
582 goto component_err;
583
584 ret = soc_rtd_startup(rtd, substream);
585 if (ret < 0) {
586 pr_err("ASoC: %s startup failed: %d\n",
587 rtd->dai_link->name, ret);
588 goto component_err;
589 }
590
Liam Girdwoodddee6272011-06-09 14:45:53 +0100591 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900592 ret = snd_soc_dai_startup(cpu_dai, substream);
593 if (ret < 0) {
594 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
595 cpu_dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900596 goto cpu_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100597 }
598
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000599 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900600 ret = snd_soc_dai_startup(codec_dai, substream);
601 if (ret < 0) {
602 dev_err(codec_dai->dev,
603 "ASoC: can't open codec %s: %d\n",
604 codec_dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900605 goto config_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100606 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200607
608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
609 codec_dai->tx_mask = 0;
610 else
611 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 }
613
Liam Girdwood01d75842012-04-25 12:12:49 +0100614 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
615 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
616 goto dynamic;
617
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200619 soc_pcm_init_runtime_hw(substream);
620
621 if (rtd->num_codecs == 1)
622 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100623
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100624 if (soc_pcm_has_symmetry(substream))
625 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
626
Liam Girdwoodddee6272011-06-09 14:45:53 +0100627 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000629 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200630 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100631 goto config_err;
632 }
633 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000634 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200635 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100636 goto config_err;
637 }
638 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
639 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200641 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 goto config_err;
643 }
644
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200645 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000646
Liam Girdwoodddee6272011-06-09 14:45:53 +0100647 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800648 if (cpu_dai->active) {
649 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
650 if (ret != 0)
651 goto config_err;
652 }
653
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000654 for_each_rtd_codec_dai(rtd, i, codec_dai) {
655 if (codec_dai->active) {
656 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200657 if (ret != 0)
658 goto config_err;
659 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100660 }
661
Liam Girdwood103d84a2012-11-19 14:39:15 +0000662 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200663 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000664 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
665 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000667 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668 runtime->hw.rate_max);
669
Liam Girdwood01d75842012-04-25 12:12:49 +0100670dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100671
672 snd_soc_runtime_activate(rtd, substream->stream);
673
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300674 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 return 0;
676
677config_err:
Kuninori Morimotob56be802020-02-10 12:14:33 +0900678 for_each_rtd_codec_dai(rtd, i, codec_dai)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900679 snd_soc_dai_shutdown(codec_dai, substream);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900680cpu_dai_err:
681 snd_soc_dai_shutdown(cpu_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200682
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900683 soc_rtd_shutdown(rtd, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000684component_err:
Kuninori Morimotodd039072020-02-10 12:14:37 +0900685 soc_pcm_components_close(substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900686
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300687 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000688
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900689 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000690 pm_runtime_mark_last_busy(component->dev);
691 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530692 }
693
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900694 for_each_rtd_components(rtd, i, component)
695 if (!component->active)
696 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000697
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698 return ret;
699}
700
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800701static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200702{
703 /*
704 * Currently nothing to do for c2c links
705 * Since c2c links are internal nodes in the DAPM graph and
706 * don't interface with the outside world or application layer
707 * we don't have to do any special handling on close.
708 */
709}
710
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 * Called by ALSA when the PCM substream is prepared, can set format, sample
713 * rate, etc. This function is non atomic and can be called multiple times,
714 * it can refer to the runtime info.
715 */
716static int soc_pcm_prepare(struct snd_pcm_substream *substream)
717{
718 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000719 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200721 struct snd_soc_dai *codec_dai;
722 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100723
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300724 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100725
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900726 ret = soc_rtd_prepare(rtd, substream);
727 if (ret < 0) {
728 dev_err(rtd->card->dev,
729 "ASoC: machine prepare error: %d\n", ret);
730 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100731 }
732
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900733 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900734 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000735 if (ret < 0) {
736 dev_err(component->dev,
737 "ASoC: platform prepare error: %d\n", ret);
738 goto out;
739 }
740 }
741
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000742 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900743 ret = snd_soc_dai_prepare(codec_dai, substream);
744 if (ret < 0) {
745 dev_err(codec_dai->dev,
746 "ASoC: codec DAI prepare error: %d\n",
747 ret);
748 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100749 }
750 }
751
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900752 ret = snd_soc_dai_prepare(cpu_dai, substream);
753 if (ret < 0) {
754 dev_err(cpu_dai->dev,
755 "ASoC: cpu DAI prepare error: %d\n", ret);
756 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 }
758
759 /* cancel any delayed stream shutdown that is pending */
760 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600761 rtd->pop_wait) {
762 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100763 cancel_delayed_work(&rtd->delayed_work);
764 }
765
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000766 snd_soc_dapm_stream_event(rtd, substream->stream,
767 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100768
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000769 for_each_rtd_codec_dai(rtd, i, codec_dai)
770 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200771 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530772 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773
774out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300775 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100776 return ret;
777}
778
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200779static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
780 unsigned int mask)
781{
782 struct snd_interval *interval;
783 int channels = hweight_long(mask);
784
785 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
786 interval->min = channels;
787 interval->max = channels;
788}
789
Charles Keepax244e2932018-06-19 16:22:09 +0100790static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
791 struct snd_soc_component *last)
792{
793 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100794 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900795 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100796
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900797 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100798 if (component == last)
799 break;
800
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900801 r = snd_soc_component_hw_free(component, substream);
802 if (r < 0)
803 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +0100804 }
805
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900806 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100807}
808
Liam Girdwoodddee6272011-06-09 14:45:53 +0100809/*
810 * Called by ALSA when the hardware params are set by application. This
811 * function can also be called multiple times and can allocate buffers
812 * (using snd_pcm_lib_* ). It's non-atomic.
813 */
814static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
815 struct snd_pcm_hw_params *params)
816{
817 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000818 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100819 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000820 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100821 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300823 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800824
825 ret = soc_pcm_params_symmetry(substream, params);
826 if (ret)
827 goto out;
828
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900829 ret = soc_rtd_hw_params(rtd, substream, params);
830 if (ret < 0) {
831 dev_err(rtd->card->dev,
832 "ASoC: machine hw_params failed: %d\n", ret);
833 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100834 }
835
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000836 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200837 struct snd_pcm_hw_params codec_params;
838
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200839 /*
840 * Skip CODECs which don't support the current stream type,
841 * the idea being that if a CODEC is not used for the currently
842 * set up transfer direction, it should not need to be
843 * configured, especially since the configuration used might
844 * not even be supported by that CODEC. There may be cases
845 * however where a CODEC needs to be set up although it is
846 * actually not being used for the transfer, e.g. if a
847 * capture-only CODEC is acting as an LRCLK and/or BCLK master
848 * for the DAI link including a playback-only CODEC.
849 * If this becomes necessary, we will have to augment the
850 * machine driver setup with information on how to act, so
851 * we can do the right thing here.
852 */
853 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
854 continue;
855
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200856 /* copy params for each codec */
857 codec_params = *params;
858
859 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
861 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200862 soc_pcm_codec_params_fixup(&codec_params,
863 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800864
865 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
866 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200867 soc_pcm_codec_params_fixup(&codec_params,
868 codec_dai->rx_mask);
869
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900870 ret = snd_soc_dai_hw_params(codec_dai, substream,
871 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200872 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100873 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200874
875 codec_dai->rate = params_rate(&codec_params);
876 codec_dai->channels = params_channels(&codec_params);
877 codec_dai->sample_bits = snd_pcm_format_physical_width(
878 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000879
880 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100881 }
882
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900883 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200884 if (ret < 0)
885 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100886
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900887 /* store the parameters for each DAIs */
888 cpu_dai->rate = params_rate(params);
889 cpu_dai->channels = params_channels(params);
890 cpu_dai->sample_bits =
891 snd_pcm_format_physical_width(params_format(params));
892
893 snd_soc_dapm_update_dai(substream, params, cpu_dai);
894
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900895 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900896 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100897 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000898 dev_err(component->dev,
899 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100900 component->name, ret);
901 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000902 }
903 }
Charles Keepax244e2932018-06-19 16:22:09 +0100904 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000905
Liam Girdwoodddee6272011-06-09 14:45:53 +0100906out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300907 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100908 return ret;
909
Kuninori Morimotob8135862017-10-11 01:37:23 +0000910component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100911 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000912
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900913 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900914 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100915
916interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200917 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100918
919codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000920 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200921 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
922 continue;
923
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900924 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200925 codec_dai->rate = 0;
926 }
927
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900928 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100929
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300930 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931 return ret;
932}
933
934/*
935 * Frees resources allocated by hw_params, can be called multiple times
936 */
937static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
938{
939 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100940 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200941 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800942 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200943 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100944
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300945 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946
Nicolin Chend3383422013-11-20 18:37:09 +0800947 /* clear the corresponding DAIs parameters when going to be inactive */
948 if (cpu_dai->active == 1) {
949 cpu_dai->rate = 0;
950 cpu_dai->channels = 0;
951 cpu_dai->sample_bits = 0;
952 }
953
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000954 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200955 if (codec_dai->active == 1) {
956 codec_dai->rate = 0;
957 codec_dai->channels = 0;
958 codec_dai->sample_bits = 0;
959 }
Nicolin Chend3383422013-11-20 18:37:09 +0800960 }
961
Liam Girdwoodddee6272011-06-09 14:45:53 +0100962 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000963 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto0f6011f2020-02-17 17:28:15 +0900964 int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK];
965 int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE];
966
967 if ((playback && playback_active == 1) ||
968 (!playback && capture_active == 1))
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000969 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200970 substream->stream);
971 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100972
973 /* free any machine hw params */
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900974 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975
Kuninori Morimotob8135862017-10-11 01:37:23 +0000976 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +0100977 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000978
Liam Girdwoodddee6272011-06-09 14:45:53 +0100979 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000980 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200981 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
982 continue;
983
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900984 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200985 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100986
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900987 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100988
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300989 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100990 return 0;
991}
992
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +0300993static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100994{
995 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000996 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100997 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200998 struct snd_soc_dai *codec_dai;
999 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001000
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001001 ret = soc_rtd_trigger(rtd, substream, cmd);
1002 if (ret < 0)
1003 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001004
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001005 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001006 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001007 if (ret < 0)
1008 return ret;
1009 }
1010
Dan Carpenter901e8222019-09-23 17:22:57 +03001011 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001012 if (ret < 0)
1013 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001014
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001015 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1016 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1017 if (ret < 0)
1018 return ret;
1019 }
1020
1021 return 0;
1022}
1023
1024static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1025{
1026 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1027 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001028 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1029 struct snd_soc_dai *codec_dai;
1030 int i, ret;
1031
1032 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1033 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1034 if (ret < 0)
1035 return ret;
1036 }
1037
1038 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1039 if (ret < 0)
1040 return ret;
1041
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001042 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001043 ret = snd_soc_component_trigger(component, substream, cmd);
1044 if (ret < 0)
1045 return ret;
1046 }
1047
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001048 ret = soc_rtd_trigger(rtd, substream, cmd);
1049 if (ret < 0)
1050 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001051
Liam Girdwoodddee6272011-06-09 14:45:53 +01001052 return 0;
1053}
1054
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001055static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1056{
1057 int ret;
1058
1059 switch (cmd) {
1060 case SNDRV_PCM_TRIGGER_START:
1061 case SNDRV_PCM_TRIGGER_RESUME:
1062 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1063 ret = soc_pcm_trigger_start(substream, cmd);
1064 break;
1065 case SNDRV_PCM_TRIGGER_STOP:
1066 case SNDRV_PCM_TRIGGER_SUSPEND:
1067 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1068 ret = soc_pcm_trigger_stop(substream, cmd);
1069 break;
1070 default:
1071 return -EINVAL;
1072 }
1073
1074 return ret;
1075}
1076
Mark Brown45c0a182012-05-09 21:46:27 +01001077static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1078 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001079{
1080 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001081 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001082 struct snd_soc_dai *codec_dai;
1083 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001084
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001085 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001086 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001087 if (ret < 0)
1088 return ret;
1089 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001090
Dan Carpenter901e8222019-09-23 17:22:57 +03001091 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001092 if (ret < 0)
1093 return ret;
1094
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001095 return 0;
1096}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001097/*
1098 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001099 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001100 * the runtime->delay will be updated accordingly.
1101 */
1102static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1103{
1104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001105 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001106 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001107 struct snd_pcm_runtime *runtime = substream->runtime;
1108 snd_pcm_uframes_t offset = 0;
1109 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001110 snd_pcm_sframes_t codec_delay = 0;
1111 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001112
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301113 /* clearing the previous total delay */
1114 runtime->delay = 0;
1115
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001116 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001117
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301118 /* base delay if assigned in pointer callback */
1119 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001120
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001121 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001122
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001123 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001124 codec_delay = max(codec_delay,
1125 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001126 }
1127 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001128
Liam Girdwoodddee6272011-06-09 14:45:53 +01001129 runtime->delay = delay;
1130
1131 return offset;
1132}
1133
Liam Girdwood01d75842012-04-25 12:12:49 +01001134/* connect a FE and BE */
1135static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1136 struct snd_soc_pcm_runtime *be, int stream)
1137{
1138 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001139 unsigned long flags;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001140#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001141 char *name;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001142#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001143
1144 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001145 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001146 if (dpcm->be == be && dpcm->fe == fe)
1147 return 0;
1148 }
1149
1150 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1151 if (!dpcm)
1152 return -ENOMEM;
1153
1154 dpcm->be = be;
1155 dpcm->fe = fe;
1156 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1157 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001158 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001159 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1160 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001161 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001162
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001163 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001164 stream ? "capture" : "playback", fe->dai_link->name,
1165 stream ? "<-" : "->", be->dai_link->name);
1166
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001167#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001168 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1169 stream ? "capture" : "playback");
1170 if (name) {
1171 dpcm->debugfs_state = debugfs_create_dir(name,
1172 fe->debugfs_dpcm_root);
1173 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1174 &dpcm->state);
1175 kfree(name);
1176 }
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001177#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001178 return 1;
1179}
1180
1181/* reparent a BE onto another FE */
1182static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1183 struct snd_soc_pcm_runtime *be, int stream)
1184{
1185 struct snd_soc_dpcm *dpcm;
1186 struct snd_pcm_substream *fe_substream, *be_substream;
1187
1188 /* reparent if BE is connected to other FEs */
1189 if (!be->dpcm[stream].users)
1190 return;
1191
1192 be_substream = snd_soc_dpcm_get_substream(be, stream);
1193
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001194 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001195 if (dpcm->fe == fe)
1196 continue;
1197
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001198 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001199 stream ? "capture" : "playback",
1200 dpcm->fe->dai_link->name,
1201 stream ? "<-" : "->", dpcm->be->dai_link->name);
1202
1203 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1204 be_substream->runtime = fe_substream->runtime;
1205 break;
1206 }
1207}
1208
1209/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001210void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001211{
1212 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001213 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001214
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001215 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001216 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 stream ? "capture" : "playback",
1218 dpcm->be->dai_link->name);
1219
1220 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1221 continue;
1222
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001223 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001224 stream ? "capture" : "playback", fe->dai_link->name,
1225 stream ? "<-" : "->", dpcm->be->dai_link->name);
1226
1227 /* BEs still alive need new FE */
1228 dpcm_be_reparent(fe, dpcm->be, stream);
1229
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001230#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001231 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001232#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001233 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001234 list_del(&dpcm->list_be);
1235 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001236 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001237 kfree(dpcm);
1238 }
1239}
1240
1241/* get BE for DAI widget and stream */
1242static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1243 struct snd_soc_dapm_widget *widget, int stream)
1244{
1245 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001246 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001247 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001248 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001249
Liam Girdwood3c146462018-03-14 20:43:51 +00001250 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1251
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001252 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001253
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001254 if (!be->dai_link->no_pcm)
1255 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001256
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001257 w = dai_get_widget(be->cpu_dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001258
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001259 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1260 w ? w->name : "(not set)");
1261
1262 if (w == widget)
1263 return be;
1264
1265 for_each_rtd_codec_dai(be, i, dai) {
1266 w = dai_get_widget(dai, stream);
1267
1268 if (w == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001269 return be;
Liam Girdwood01d75842012-04-25 12:12:49 +01001270 }
1271 }
1272
Jerome Brunet9d6ee362020-02-19 12:50:48 +01001273 /* Widget provided is not a BE */
Liam Girdwood01d75842012-04-25 12:12:49 +01001274 return NULL;
1275}
1276
Liam Girdwood01d75842012-04-25 12:12:49 +01001277static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1278 struct snd_soc_dapm_widget *widget)
1279{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001280 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001281 int i;
1282
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001283 for_each_dapm_widgets(list, i, w)
1284 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001285 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001286
1287 return 0;
1288}
1289
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001290static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1291 enum snd_soc_dapm_direction dir)
1292{
1293 struct snd_soc_card *card = widget->dapm->card;
1294 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001295 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001296
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001297 /* adjust dir to stream */
1298 if (dir == SND_SOC_DAPM_DIR_OUT)
1299 stream = SNDRV_PCM_STREAM_PLAYBACK;
1300 else
1301 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001302
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001303 rtd = dpcm_get_be(card, widget, stream);
1304 if (rtd)
1305 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001306
1307 return false;
1308}
1309
Liam Girdwood23607022014-01-17 17:03:55 +00001310int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001311 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001312{
1313 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001314 int paths;
1315
Liam Girdwood01d75842012-04-25 12:12:49 +01001316 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001317 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001318 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001319
Liam Girdwood103d84a2012-11-19 14:39:15 +00001320 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001321 stream ? "capture" : "playback");
1322
Liam Girdwood01d75842012-04-25 12:12:49 +01001323 return paths;
1324}
1325
Liam Girdwood01d75842012-04-25 12:12:49 +01001326static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1327 struct snd_soc_dapm_widget_list **list_)
1328{
1329 struct snd_soc_dpcm *dpcm;
1330 struct snd_soc_dapm_widget_list *list = *list_;
1331 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001332 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001333 int prune = 0;
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001334 int do_prune;
Liam Girdwood01d75842012-04-25 12:12:49 +01001335
1336 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001337 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001338 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001339
1340 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001341 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001342
1343 /* prune the BE if it's no longer in our active list */
1344 if (widget && widget_in_list(list, widget))
1345 continue;
1346
1347 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001348 do_prune = 1;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001349 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001350 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001351
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001352 /* prune the BE if it's no longer in our active list */
1353 if (widget && widget_in_list(list, widget))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001354 do_prune = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001355 }
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001356 if (!do_prune)
1357 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001358
Liam Girdwood103d84a2012-11-19 14:39:15 +00001359 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001360 stream ? "capture" : "playback",
1361 dpcm->be->dai_link->name, fe->dai_link->name);
1362 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1363 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1364 prune++;
1365 }
1366
Liam Girdwood103d84a2012-11-19 14:39:15 +00001367 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001368 return prune;
1369}
1370
1371static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1372 struct snd_soc_dapm_widget_list **list_)
1373{
1374 struct snd_soc_card *card = fe->card;
1375 struct snd_soc_dapm_widget_list *list = *list_;
1376 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001377 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001378 int i, new = 0, err;
1379
1380 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001381 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001382
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001383 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001384 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001385 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1386 continue;
1387 break;
Mark Brown46162742013-06-05 19:36:11 +01001388 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001389 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1390 continue;
Mark Brown46162742013-06-05 19:36:11 +01001391 break;
1392 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 continue;
Mark Brown46162742013-06-05 19:36:11 +01001394 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001395
1396 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001397 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001399 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001400 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001401 continue;
1402 }
1403
1404 /* make sure BE is a real BE */
1405 if (!be->dai_link->no_pcm)
1406 continue;
1407
1408 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001409 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001410 continue;
1411
1412 /* newly connected FE and BE */
1413 err = dpcm_be_connect(fe, be, stream);
1414 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001415 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001416 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001417 break;
1418 } else if (err == 0) /* already connected */
1419 continue;
1420
1421 /* new */
1422 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1423 new++;
1424 }
1425
Liam Girdwood103d84a2012-11-19 14:39:15 +00001426 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001427 return new;
1428}
1429
1430/*
1431 * Find the corresponding BE DAIs that source or sink audio to this
1432 * FE substream.
1433 */
Liam Girdwood23607022014-01-17 17:03:55 +00001434int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001435 int stream, struct snd_soc_dapm_widget_list **list, int new)
1436{
1437 if (new)
1438 return dpcm_add_paths(fe, stream, list);
1439 else
1440 return dpcm_prune_paths(fe, stream, list);
1441}
1442
Liam Girdwood23607022014-01-17 17:03:55 +00001443void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001444{
1445 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001446 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001447
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001448 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001449 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001450 dpcm->be->dpcm[stream].runtime_update =
1451 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001452 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001453}
1454
1455static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1456 int stream)
1457{
1458 struct snd_soc_dpcm *dpcm;
1459
1460 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001461 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001462
1463 struct snd_soc_pcm_runtime *be = dpcm->be;
1464 struct snd_pcm_substream *be_substream =
1465 snd_soc_dpcm_get_substream(be, stream);
1466
1467 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001468 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001469 stream ? "capture" : "playback",
1470 be->dpcm[stream].state);
1471
1472 if (--be->dpcm[stream].users != 0)
1473 continue;
1474
1475 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1476 continue;
1477
1478 soc_pcm_close(be_substream);
1479 be_substream->runtime = NULL;
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1481 }
1482}
1483
Liam Girdwood23607022014-01-17 17:03:55 +00001484int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001485{
1486 struct snd_soc_dpcm *dpcm;
1487 int err, count = 0;
1488
1489 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001490 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001491
1492 struct snd_soc_pcm_runtime *be = dpcm->be;
1493 struct snd_pcm_substream *be_substream =
1494 snd_soc_dpcm_get_substream(be, stream);
1495
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001496 if (!be_substream) {
1497 dev_err(be->dev, "ASoC: no backend %s stream\n",
1498 stream ? "capture" : "playback");
1499 continue;
1500 }
1501
Liam Girdwood01d75842012-04-25 12:12:49 +01001502 /* is this op for this BE ? */
1503 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1504 continue;
1505
1506 /* first time the dpcm is open ? */
1507 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001508 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001509 stream ? "capture" : "playback",
1510 be->dpcm[stream].state);
1511
1512 if (be->dpcm[stream].users++ != 0)
1513 continue;
1514
1515 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1516 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1517 continue;
1518
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001519 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1520 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001521
1522 be_substream->runtime = be->dpcm[stream].runtime;
1523 err = soc_pcm_open(be_substream);
1524 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001525 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001526 be->dpcm[stream].users--;
1527 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001528 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001529 stream ? "capture" : "playback",
1530 be->dpcm[stream].state);
1531
1532 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1533 goto unwind;
1534 }
1535
1536 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1537 count++;
1538 }
1539
1540 return count;
1541
1542unwind:
1543 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001544 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001545 struct snd_soc_pcm_runtime *be = dpcm->be;
1546 struct snd_pcm_substream *be_substream =
1547 snd_soc_dpcm_get_substream(be, stream);
1548
1549 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1550 continue;
1551
1552 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001553 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001554 stream ? "capture" : "playback",
1555 be->dpcm[stream].state);
1556
1557 if (--be->dpcm[stream].users != 0)
1558 continue;
1559
1560 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1561 continue;
1562
1563 soc_pcm_close(be_substream);
1564 be_substream->runtime = NULL;
1565 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1566 }
1567
1568 return err;
1569}
1570
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001571static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001572 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001573{
1574 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001575 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001576 runtime->hw.channels_min = stream->channels_min;
1577 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001578 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001579 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001580 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001581 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001582 runtime->hw.rates = stream->rates;
1583}
1584
Jerome Brunet435ffb72018-07-05 12:13:48 +02001585static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1586 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001587{
1588 struct snd_soc_pcm_runtime *fe = substream->private_data;
1589 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001590 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001591 int stream = substream->stream;
1592
1593 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001594 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001595
1596 /*
1597 * It returns merged BE codec format
1598 * if FE want to use it (= dpcm_merged_format)
1599 */
1600
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001601 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001602 struct snd_soc_pcm_runtime *be = dpcm->be;
1603 struct snd_soc_dai_driver *codec_dai_drv;
1604 struct snd_soc_pcm_stream *codec_stream;
1605 int i;
1606
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001607 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001608 /*
1609 * Skip CODECs which don't support the current stream
1610 * type. See soc_pcm_init_runtime_hw() for more details
1611 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001612 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001613 continue;
1614
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001615 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001616 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1617 codec_stream = &codec_dai_drv->playback;
1618 else
1619 codec_stream = &codec_dai_drv->capture;
1620
Jerome Brunet435ffb72018-07-05 12:13:48 +02001621 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001622 }
1623 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001624}
1625
Jerome Brunet435ffb72018-07-05 12:13:48 +02001626static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1627 unsigned int *channels_min,
1628 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001629{
1630 struct snd_soc_pcm_runtime *fe = substream->private_data;
1631 struct snd_soc_dpcm *dpcm;
1632 int stream = substream->stream;
1633
1634 if (!fe->dai_link->dpcm_merged_chan)
1635 return;
1636
Jiada Wangf4c277b2018-06-20 18:25:20 +09001637 /*
1638 * It returns merged BE codec channel;
1639 * if FE want to use it (= dpcm_merged_chan)
1640 */
1641
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001642 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001643 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001644 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001645 struct snd_soc_dai_driver *codec_dai_drv;
1646 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001647 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001648
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001649 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1650 cpu_stream = &cpu_dai_drv->playback;
1651 else
1652 cpu_stream = &cpu_dai_drv->capture;
1653
1654 *channels_min = max(*channels_min, cpu_stream->channels_min);
1655 *channels_max = min(*channels_max, cpu_stream->channels_max);
1656
1657 /*
1658 * chan min/max cannot be enforced if there are multiple CODEC
1659 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1660 */
1661 if (be->num_codecs == 1) {
1662 codec_dai_drv = be->codec_dais[0]->driver;
1663
Jiada Wangf4c277b2018-06-20 18:25:20 +09001664 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1665 codec_stream = &codec_dai_drv->playback;
1666 else
1667 codec_stream = &codec_dai_drv->capture;
1668
1669 *channels_min = max(*channels_min,
1670 codec_stream->channels_min);
1671 *channels_max = min(*channels_max,
1672 codec_stream->channels_max);
1673 }
1674 }
1675}
1676
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001677static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1678 unsigned int *rates,
1679 unsigned int *rate_min,
1680 unsigned int *rate_max)
1681{
1682 struct snd_soc_pcm_runtime *fe = substream->private_data;
1683 struct snd_soc_dpcm *dpcm;
1684 int stream = substream->stream;
1685
1686 if (!fe->dai_link->dpcm_merged_rate)
1687 return;
1688
1689 /*
1690 * It returns merged BE codec channel;
1691 * if FE want to use it (= dpcm_merged_chan)
1692 */
1693
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001694 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001695 struct snd_soc_pcm_runtime *be = dpcm->be;
1696 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1697 struct snd_soc_dai_driver *codec_dai_drv;
1698 struct snd_soc_pcm_stream *codec_stream;
1699 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001700 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001701 int i;
1702
1703 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1704 cpu_stream = &cpu_dai_drv->playback;
1705 else
1706 cpu_stream = &cpu_dai_drv->capture;
1707
1708 *rate_min = max(*rate_min, cpu_stream->rate_min);
1709 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1710 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1711
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001712 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001713 /*
1714 * Skip CODECs which don't support the current stream
1715 * type. See soc_pcm_init_runtime_hw() for more details
1716 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001717 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001718 continue;
1719
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001720 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001721 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1722 codec_stream = &codec_dai_drv->playback;
1723 else
1724 codec_stream = &codec_dai_drv->capture;
1725
1726 *rate_min = max(*rate_min, codec_stream->rate_min);
1727 *rate_max = min_not_zero(*rate_max,
1728 codec_stream->rate_max);
1729 *rates = snd_pcm_rate_mask_intersect(*rates,
1730 codec_stream->rates);
1731 }
1732 }
1733}
1734
Mark Brown45c0a182012-05-09 21:46:27 +01001735static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001736{
1737 struct snd_pcm_runtime *runtime = substream->runtime;
1738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1740 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1741
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001742 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001743 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001744 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001745 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001746
Jerome Brunet435ffb72018-07-05 12:13:48 +02001747 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1748 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1749 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001750 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1751 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001752}
1753
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001754static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1755
1756/* Set FE's runtime_update state; the state is protected via PCM stream lock
1757 * for avoiding the race with trigger callback.
1758 * If the state is unset and a trigger is pending while the previous operation,
1759 * process the pending trigger action here.
1760 */
1761static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1762 int stream, enum snd_soc_dpcm_update state)
1763{
1764 struct snd_pcm_substream *substream =
1765 snd_soc_dpcm_get_substream(fe, stream);
1766
1767 snd_pcm_stream_lock_irq(substream);
1768 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1769 dpcm_fe_dai_do_trigger(substream,
1770 fe->dpcm[stream].trigger_pending - 1);
1771 fe->dpcm[stream].trigger_pending = 0;
1772 }
1773 fe->dpcm[stream].runtime_update = state;
1774 snd_pcm_stream_unlock_irq(substream);
1775}
1776
PC Liao906c7d62015-12-11 11:33:51 +08001777static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1778 int stream)
1779{
1780 struct snd_soc_dpcm *dpcm;
1781 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1782 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1783 int err;
1784
1785 /* apply symmetry for FE */
1786 if (soc_pcm_has_symmetry(fe_substream))
1787 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1788
1789 /* Symmetry only applies if we've got an active stream. */
1790 if (fe_cpu_dai->active) {
1791 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1792 if (err < 0)
1793 return err;
1794 }
1795
1796 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001797 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001798 struct snd_soc_pcm_runtime *be = dpcm->be;
1799 struct snd_pcm_substream *be_substream =
1800 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001801 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001802 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001803 int i;
1804
Jerome Brunet6246f282019-04-01 15:03:54 +02001805 /* A backend may not have the requested substream */
1806 if (!be_substream)
1807 continue;
1808
1809 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301810 if (rtd->dai_link->be_hw_params_fixup)
1811 continue;
1812
PC Liao906c7d62015-12-11 11:33:51 +08001813 if (soc_pcm_has_symmetry(be_substream))
1814 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1815
1816 /* Symmetry only applies if we've got an active stream. */
1817 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001818 err = soc_pcm_apply_symmetry(fe_substream,
1819 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001820 if (err < 0)
1821 return err;
1822 }
1823
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001824 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1825 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001826 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001827 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001828 if (err < 0)
1829 return err;
1830 }
1831 }
1832 }
1833
1834 return 0;
1835}
1836
Liam Girdwood01d75842012-04-25 12:12:49 +01001837static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1838{
1839 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1840 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1841 int stream = fe_substream->stream, ret = 0;
1842
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001843 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001844
1845 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1846 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001847 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001848 goto be_err;
1849 }
1850
Liam Girdwood103d84a2012-11-19 14:39:15 +00001851 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001852
1853 /* start the DAI frontend */
1854 ret = soc_pcm_open(fe_substream);
1855 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001856 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001857 goto unwind;
1858 }
1859
1860 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1861
1862 dpcm_set_fe_runtime(fe_substream);
1863 snd_pcm_limit_hw_rates(runtime);
1864
PC Liao906c7d62015-12-11 11:33:51 +08001865 ret = dpcm_apply_symmetry(fe_substream, stream);
1866 if (ret < 0) {
1867 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1868 ret);
1869 goto unwind;
1870 }
1871
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001872 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001873 return 0;
1874
1875unwind:
1876 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1877be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001878 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001879 return ret;
1880}
1881
Liam Girdwood23607022014-01-17 17:03:55 +00001882int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001883{
1884 struct snd_soc_dpcm *dpcm;
1885
1886 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001887 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001888
1889 struct snd_soc_pcm_runtime *be = dpcm->be;
1890 struct snd_pcm_substream *be_substream =
1891 snd_soc_dpcm_get_substream(be, stream);
1892
1893 /* is this op for this BE ? */
1894 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1895 continue;
1896
1897 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001899 stream ? "capture" : "playback",
1900 be->dpcm[stream].state);
1901
1902 if (--be->dpcm[stream].users != 0)
1903 continue;
1904
1905 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001906 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1907 soc_pcm_hw_free(be_substream);
1908 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1909 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001910
Liam Girdwood103d84a2012-11-19 14:39:15 +00001911 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001912 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001913
1914 soc_pcm_close(be_substream);
1915 be_substream->runtime = NULL;
1916
1917 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1918 }
1919 return 0;
1920}
1921
1922static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1923{
1924 struct snd_soc_pcm_runtime *fe = substream->private_data;
1925 int stream = substream->stream;
1926
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001927 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001928
1929 /* shutdown the BEs */
1930 dpcm_be_dai_shutdown(fe, substream->stream);
1931
Liam Girdwood103d84a2012-11-19 14:39:15 +00001932 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001933
1934 /* now shutdown the frontend */
1935 soc_pcm_close(substream);
1936
1937 /* run the stream event for each BE */
Kuninori Morimotob0edff42020-01-10 11:36:56 +09001938 snd_soc_dapm_stream_stop(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001939
1940 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001941 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001942 return 0;
1943}
1944
Liam Girdwood23607022014-01-17 17:03:55 +00001945int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001946{
1947 struct snd_soc_dpcm *dpcm;
1948
1949 /* only hw_params backends that are either sinks or sources
1950 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001951 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001952
1953 struct snd_soc_pcm_runtime *be = dpcm->be;
1954 struct snd_pcm_substream *be_substream =
1955 snd_soc_dpcm_get_substream(be, stream);
1956
1957 /* is this op for this BE ? */
1958 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1959 continue;
1960
1961 /* only free hw when no longer used - check all FEs */
1962 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1963 continue;
1964
Qiao Zhou36fba622014-12-03 10:13:43 +08001965 /* do not free hw if this BE is used by other FE */
1966 if (be->dpcm[stream].users > 1)
1967 continue;
1968
Liam Girdwood01d75842012-04-25 12:12:49 +01001969 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1970 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1971 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001972 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301973 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1974 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001975 continue;
1976
Liam Girdwood103d84a2012-11-19 14:39:15 +00001977 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001978 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001979
1980 soc_pcm_hw_free(be_substream);
1981
1982 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1983 }
1984
1985 return 0;
1986}
1987
Mark Brown45c0a182012-05-09 21:46:27 +01001988static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001989{
1990 struct snd_soc_pcm_runtime *fe = substream->private_data;
1991 int err, stream = substream->stream;
1992
1993 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001994 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001995
Liam Girdwood103d84a2012-11-19 14:39:15 +00001996 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001997
1998 /* call hw_free on the frontend */
1999 err = soc_pcm_hw_free(substream);
2000 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002001 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002002 fe->dai_link->name);
2003
2004 /* only hw_params backends that are either sinks or sources
2005 * to this frontend DAI */
2006 err = dpcm_be_dai_hw_free(fe, stream);
2007
2008 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002009 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002010
2011 mutex_unlock(&fe->card->mutex);
2012 return 0;
2013}
2014
Liam Girdwood23607022014-01-17 17:03:55 +00002015int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002016{
2017 struct snd_soc_dpcm *dpcm;
2018 int ret;
2019
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002020 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002021
2022 struct snd_soc_pcm_runtime *be = dpcm->be;
2023 struct snd_pcm_substream *be_substream =
2024 snd_soc_dpcm_get_substream(be, stream);
2025
2026 /* is this op for this BE ? */
2027 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2028 continue;
2029
Liam Girdwood01d75842012-04-25 12:12:49 +01002030 /* copy params for each dpcm */
2031 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2032 sizeof(struct snd_pcm_hw_params));
2033
2034 /* perform any hw_params fixups */
2035 if (be->dai_link->be_hw_params_fixup) {
2036 ret = be->dai_link->be_hw_params_fixup(be,
2037 &dpcm->hw_params);
2038 if (ret < 0) {
2039 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002040 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002041 ret);
2042 goto unwind;
2043 }
2044 }
2045
Libin Yangae061d22019-04-19 09:53:12 +08002046 /* copy the fixed-up hw params for BE dai */
2047 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2048 sizeof(struct snd_pcm_hw_params));
2049
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002050 /* only allow hw_params() if no connected FEs are running */
2051 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2052 continue;
2053
2054 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2055 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2056 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2057 continue;
2058
2059 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002060 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002061
Liam Girdwood01d75842012-04-25 12:12:49 +01002062 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2063 if (ret < 0) {
2064 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002065 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002066 goto unwind;
2067 }
2068
2069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2070 }
2071 return 0;
2072
2073unwind:
2074 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002075 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002076 struct snd_soc_pcm_runtime *be = dpcm->be;
2077 struct snd_pcm_substream *be_substream =
2078 snd_soc_dpcm_get_substream(be, stream);
2079
2080 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2081 continue;
2082
2083 /* only allow hw_free() if no connected FEs are running */
2084 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2085 continue;
2086
2087 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2088 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2089 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2090 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2091 continue;
2092
2093 soc_pcm_hw_free(be_substream);
2094 }
2095
2096 return ret;
2097}
2098
Mark Brown45c0a182012-05-09 21:46:27 +01002099static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2100 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002101{
2102 struct snd_soc_pcm_runtime *fe = substream->private_data;
2103 int ret, stream = substream->stream;
2104
2105 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002106 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002107
2108 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2109 sizeof(struct snd_pcm_hw_params));
2110 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2111 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002112 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002113 goto out;
2114 }
2115
Liam Girdwood103d84a2012-11-19 14:39:15 +00002116 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002117 fe->dai_link->name, params_rate(params),
2118 params_channels(params), params_format(params));
2119
2120 /* call hw_params on the frontend */
2121 ret = soc_pcm_hw_params(substream, params);
2122 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002123 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002124 dpcm_be_dai_hw_free(fe, stream);
2125 } else
2126 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2127
2128out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002129 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002130 mutex_unlock(&fe->card->mutex);
2131 return ret;
2132}
2133
2134static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2135 struct snd_pcm_substream *substream, int cmd)
2136{
2137 int ret;
2138
Liam Girdwood103d84a2012-11-19 14:39:15 +00002139 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002140 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002141
2142 ret = soc_pcm_trigger(substream, cmd);
2143 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002144 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002145
2146 return ret;
2147}
2148
Liam Girdwood23607022014-01-17 17:03:55 +00002149int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002150 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002151{
2152 struct snd_soc_dpcm *dpcm;
2153 int ret = 0;
2154
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002155 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002156
2157 struct snd_soc_pcm_runtime *be = dpcm->be;
2158 struct snd_pcm_substream *be_substream =
2159 snd_soc_dpcm_get_substream(be, stream);
2160
2161 /* is this op for this BE ? */
2162 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2163 continue;
2164
2165 switch (cmd) {
2166 case SNDRV_PCM_TRIGGER_START:
2167 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2168 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2169 continue;
2170
2171 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2172 if (ret)
2173 return ret;
2174
2175 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2176 break;
2177 case SNDRV_PCM_TRIGGER_RESUME:
2178 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2179 continue;
2180
2181 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2182 if (ret)
2183 return ret;
2184
2185 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2186 break;
2187 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2188 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2189 continue;
2190
2191 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2192 if (ret)
2193 return ret;
2194
2195 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2196 break;
2197 case SNDRV_PCM_TRIGGER_STOP:
2198 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2199 continue;
2200
2201 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2202 continue;
2203
2204 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2205 if (ret)
2206 return ret;
2207
2208 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2209 break;
2210 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002211 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 continue;
2213
2214 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2215 continue;
2216
2217 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2218 if (ret)
2219 return ret;
2220
2221 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2222 break;
2223 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2224 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2225 continue;
2226
2227 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2228 continue;
2229
2230 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2231 if (ret)
2232 return ret;
2233
2234 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2235 break;
2236 }
2237 }
2238
2239 return ret;
2240}
2241EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2242
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002243static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2244 int cmd, bool fe_first)
2245{
2246 struct snd_soc_pcm_runtime *fe = substream->private_data;
2247 int ret;
2248
2249 /* call trigger on the frontend before the backend. */
2250 if (fe_first) {
2251 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2252 fe->dai_link->name, cmd);
2253
2254 ret = soc_pcm_trigger(substream, cmd);
2255 if (ret < 0)
2256 return ret;
2257
2258 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2259 return ret;
2260 }
2261
2262 /* call trigger on the frontend after the backend. */
2263 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2264 if (ret < 0)
2265 return ret;
2266
2267 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2268 fe->dai_link->name, cmd);
2269
2270 ret = soc_pcm_trigger(substream, cmd);
2271
2272 return ret;
2273}
2274
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002275static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002276{
2277 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002278 int stream = substream->stream;
2279 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002280 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2281
2282 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2283
2284 switch (trigger) {
2285 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002286 switch (cmd) {
2287 case SNDRV_PCM_TRIGGER_START:
2288 case SNDRV_PCM_TRIGGER_RESUME:
2289 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2290 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2291 break;
2292 case SNDRV_PCM_TRIGGER_STOP:
2293 case SNDRV_PCM_TRIGGER_SUSPEND:
2294 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2295 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2296 break;
2297 default:
2298 ret = -EINVAL;
2299 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002300 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002301 break;
2302 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002303 switch (cmd) {
2304 case SNDRV_PCM_TRIGGER_START:
2305 case SNDRV_PCM_TRIGGER_RESUME:
2306 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2307 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2308 break;
2309 case SNDRV_PCM_TRIGGER_STOP:
2310 case SNDRV_PCM_TRIGGER_SUSPEND:
2311 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2312 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2313 break;
2314 default:
2315 ret = -EINVAL;
2316 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002317 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002318 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002319 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2320 /* bespoke trigger() - handles both FE and BEs */
2321
Liam Girdwood103d84a2012-11-19 14:39:15 +00002322 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002323 fe->dai_link->name, cmd);
2324
2325 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002326 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002327 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002328 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002329 fe->dai_link->name);
2330 ret = -EINVAL;
2331 goto out;
2332 }
2333
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002334 if (ret < 0) {
2335 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2336 cmd, ret);
2337 goto out;
2338 }
2339
Liam Girdwood01d75842012-04-25 12:12:49 +01002340 switch (cmd) {
2341 case SNDRV_PCM_TRIGGER_START:
2342 case SNDRV_PCM_TRIGGER_RESUME:
2343 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2344 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2345 break;
2346 case SNDRV_PCM_TRIGGER_STOP:
2347 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002348 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2349 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002350 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2351 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2352 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002353 }
2354
2355out:
2356 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2357 return ret;
2358}
2359
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002360static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2361{
2362 struct snd_soc_pcm_runtime *fe = substream->private_data;
2363 int stream = substream->stream;
2364
2365 /* if FE's runtime_update is already set, we're in race;
2366 * process this trigger later at exit
2367 */
2368 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2369 fe->dpcm[stream].trigger_pending = cmd + 1;
2370 return 0; /* delayed, assuming it's successful */
2371 }
2372
2373 /* we're alone, let's trigger */
2374 return dpcm_fe_dai_do_trigger(substream, cmd);
2375}
2376
Liam Girdwood23607022014-01-17 17:03:55 +00002377int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002378{
2379 struct snd_soc_dpcm *dpcm;
2380 int ret = 0;
2381
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002382 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002383
2384 struct snd_soc_pcm_runtime *be = dpcm->be;
2385 struct snd_pcm_substream *be_substream =
2386 snd_soc_dpcm_get_substream(be, stream);
2387
2388 /* is this op for this BE ? */
2389 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2390 continue;
2391
2392 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002393 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002394 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2395 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002396 continue;
2397
Liam Girdwood103d84a2012-11-19 14:39:15 +00002398 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002399 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002400
2401 ret = soc_pcm_prepare(be_substream);
2402 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002403 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002404 ret);
2405 break;
2406 }
2407
2408 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2409 }
2410 return ret;
2411}
2412
Mark Brown45c0a182012-05-09 21:46:27 +01002413static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002414{
2415 struct snd_soc_pcm_runtime *fe = substream->private_data;
2416 int stream = substream->stream, ret = 0;
2417
2418 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2419
Liam Girdwood103d84a2012-11-19 14:39:15 +00002420 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002421
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002422 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002423
2424 /* there is no point preparing this FE if there are no BEs */
2425 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002426 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002427 fe->dai_link->name);
2428 ret = -EINVAL;
2429 goto out;
2430 }
2431
2432 ret = dpcm_be_dai_prepare(fe, substream->stream);
2433 if (ret < 0)
2434 goto out;
2435
2436 /* call prepare on the frontend */
2437 ret = soc_pcm_prepare(substream);
2438 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002439 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002440 fe->dai_link->name);
2441 goto out;
2442 }
2443
2444 /* run the stream event for each BE */
2445 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2446 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2447
2448out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002449 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002450 mutex_unlock(&fe->card->mutex);
2451
2452 return ret;
2453}
2454
Liam Girdwood618dae12012-04-25 12:12:51 +01002455static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2456{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002457 struct snd_pcm_substream *substream =
2458 snd_soc_dpcm_get_substream(fe, stream);
2459 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002460 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002461
Liam Girdwood103d84a2012-11-19 14:39:15 +00002462 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002463 stream ? "capture" : "playback", fe->dai_link->name);
2464
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002465 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2466 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002467 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002468 fe->dai_link->name);
2469
2470 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2471 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002472 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002473 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002474 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002475 fe->dai_link->name);
2476
2477 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2478 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002479 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002480 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002481
2482 err = dpcm_be_dai_hw_free(fe, stream);
2483 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002484 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002485
2486 err = dpcm_be_dai_shutdown(fe, stream);
2487 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002488 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002489
2490 /* run the stream event for each BE */
2491 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2492
2493 return 0;
2494}
2495
2496static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2497{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002498 struct snd_pcm_substream *substream =
2499 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002500 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002501 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002502 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002503 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002504
Liam Girdwood103d84a2012-11-19 14:39:15 +00002505 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002506 stream ? "capture" : "playback", fe->dai_link->name);
2507
2508 /* Only start the BE if the FE is ready */
2509 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2510 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2511 return -EINVAL;
2512
2513 /* startup must always be called for new BEs */
2514 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002515 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002516 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002517
2518 /* keep going if FE state is > open */
2519 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2520 return 0;
2521
2522 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002523 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002524 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002525
2526 /* keep going if FE state is > hw_params */
2527 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2528 return 0;
2529
2530
2531 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002532 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002533 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002534
2535 /* run the stream event for each BE */
2536 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2537
2538 /* keep going if FE state is > prepare */
2539 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2540 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2541 return 0;
2542
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002543 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2544 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002545 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002546 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002547
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002548 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2549 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002550 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002551 goto hw_free;
2552 }
2553 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002554 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002555 fe->dai_link->name);
2556
2557 ret = dpcm_be_dai_trigger(fe, stream,
2558 SNDRV_PCM_TRIGGER_START);
2559 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002560 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002561 goto hw_free;
2562 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002563 }
2564
2565 return 0;
2566
2567hw_free:
2568 dpcm_be_dai_hw_free(fe, stream);
2569close:
2570 dpcm_be_dai_shutdown(fe, stream);
2571disconnect:
2572 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002573 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002574 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002575 struct snd_soc_pcm_runtime *be = dpcm->be;
2576 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2577 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2578 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002579 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002580
2581 return ret;
2582}
2583
2584static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2585{
2586 int ret;
2587
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002588 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002589 ret = dpcm_run_update_startup(fe, stream);
2590 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002591 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002592 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002593
2594 return ret;
2595}
2596
2597static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2598{
2599 int ret;
2600
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002601 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002602 ret = dpcm_run_update_shutdown(fe, stream);
2603 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002604 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002605 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002606
2607 return ret;
2608}
2609
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002610static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2611{
2612 struct snd_soc_dapm_widget_list *list;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002613 int stream;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002614 int count, paths;
2615
2616 if (!fe->dai_link->dynamic)
2617 return 0;
2618
2619 /* only check active links */
2620 if (!fe->cpu_dai->active)
2621 return 0;
2622
2623 /* DAPM sync will call this to update DSP paths */
2624 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2625 new ? "new" : "old", fe->dai_link->name);
2626
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002627 for_each_pcm_streams(stream) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002628
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002629 /* skip if FE doesn't have playback/capture capability */
2630 if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) ||
2631 !snd_soc_dai_stream_valid(fe->codec_dai, stream))
2632 continue;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002633
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09002634 /* skip if FE isn't currently playing/capturing */
2635 if (!fe->cpu_dai->stream_active[stream] ||
2636 !fe->codec_dai->stream_active[stream])
2637 continue;
2638
2639 paths = dpcm_path_get(fe, stream, &list);
2640 if (paths < 0) {
2641 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2642 fe->dai_link->name,
2643 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2644 "playback" : "capture");
2645 return paths;
2646 }
2647
2648 /* update any playback/capture paths */
2649 count = dpcm_process_paths(fe, stream, &list, new);
2650 if (count) {
2651 if (new)
2652 dpcm_run_new_update(fe, stream);
2653 else
2654 dpcm_run_old_update(fe, stream);
2655
2656 dpcm_clear_pending_state(fe, stream);
2657 dpcm_be_disconnect(fe, stream);
2658 }
2659
2660 dpcm_path_put(&list);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002661 }
2662
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002663 return 0;
2664}
2665
Liam Girdwood618dae12012-04-25 12:12:51 +01002666/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2667 * any DAI links.
2668 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002669int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002670{
Mengdong Lin1a497982015-11-18 02:34:11 -05002671 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002672 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002673
Liam Girdwood618dae12012-04-25 12:12:51 +01002674 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002675 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002676 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002677 ret = soc_dpcm_fe_runtime_update(fe, 0);
2678 if (ret)
2679 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002680 }
2681
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002682 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002683 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002684 ret = soc_dpcm_fe_runtime_update(fe, 1);
2685 if (ret)
2686 goto out;
2687 }
2688
2689out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002690 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002691 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002692}
Liam Girdwood01d75842012-04-25 12:12:49 +01002693
Mark Brown45c0a182012-05-09 21:46:27 +01002694static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002695{
2696 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2697 struct snd_soc_dpcm *dpcm;
2698 struct snd_soc_dapm_widget_list *list;
2699 int ret;
2700 int stream = fe_substream->stream;
2701
2702 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2703 fe->dpcm[stream].runtime = fe_substream->runtime;
2704
Qiao Zhou8f70e512014-09-10 17:54:07 +08002705 ret = dpcm_path_get(fe, stream, &list);
2706 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002707 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08002708 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002709 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002710 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002711 }
2712
2713 /* calculate valid and active FE <-> BE dpcms */
2714 dpcm_process_paths(fe, stream, &list, 1);
2715
2716 ret = dpcm_fe_dai_startup(fe_substream);
2717 if (ret < 0) {
2718 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002719 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002720 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2721
2722 dpcm_be_disconnect(fe, stream);
2723 fe->dpcm[stream].runtime = NULL;
2724 }
2725
2726 dpcm_clear_pending_state(fe, stream);
2727 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002728open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002729 mutex_unlock(&fe->card->mutex);
2730 return ret;
2731}
2732
Mark Brown45c0a182012-05-09 21:46:27 +01002733static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002734{
2735 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2736 struct snd_soc_dpcm *dpcm;
2737 int stream = fe_substream->stream, ret;
2738
2739 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2740 ret = dpcm_fe_dai_shutdown(fe_substream);
2741
2742 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002743 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002744 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2745
2746 dpcm_be_disconnect(fe, stream);
2747
2748 fe->dpcm[stream].runtime = NULL;
2749 mutex_unlock(&fe->card->mutex);
2750 return ret;
2751}
2752
Liam Girdwoodddee6272011-06-09 14:45:53 +01002753/* create a new pcm */
2754int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2755{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002756 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002757 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002758 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002759 struct snd_pcm *pcm;
2760 char new_name[64];
2761 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002762 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002763
Liam Girdwood01d75842012-04-25 12:12:49 +01002764 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002765 playback = rtd->dai_link->dpcm_playback;
2766 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002767 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002768 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002769 int cpu_capture = rtd->dai_link->params ?
2770 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2771 int cpu_playback = rtd->dai_link->params ?
2772 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002773
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002774 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002775 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002776 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002777 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002778 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002779 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002780 capture = 1;
2781 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002782 }
Sangsu Parka5002312012-01-02 17:15:10 +09002783
Fabio Estevamd6bead02013-08-29 10:32:13 -03002784 if (rtd->dai_link->playback_only) {
2785 playback = 1;
2786 capture = 0;
2787 }
2788
2789 if (rtd->dai_link->capture_only) {
2790 playback = 0;
2791 capture = 1;
2792 }
2793
Liam Girdwood01d75842012-04-25 12:12:49 +01002794 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002795 if (rtd->dai_link->params) {
2796 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2797 rtd->dai_link->stream_name);
2798
2799 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2800 playback, capture, &pcm);
2801 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002802 snprintf(new_name, sizeof(new_name), "(%s)",
2803 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002804
Liam Girdwood01d75842012-04-25 12:12:49 +01002805 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2806 playback, capture, &pcm);
2807 } else {
2808 if (rtd->dai_link->dynamic)
2809 snprintf(new_name, sizeof(new_name), "%s (*)",
2810 rtd->dai_link->stream_name);
2811 else
2812 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002813 rtd->dai_link->stream_name,
2814 (rtd->num_codecs > 1) ?
2815 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002816
Liam Girdwood01d75842012-04-25 12:12:49 +01002817 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2818 capture, &pcm);
2819 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002820 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002821 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002822 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002823 return ret;
2824 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002825 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002826
2827 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002828 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002829 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002830 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002831 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002832
Vinod Koul48c76992015-02-12 09:59:53 +05302833 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002834 rtd->pcm = pcm;
2835 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002836
Jerome Bruneta3420312019-07-25 18:59:47 +02002837 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002838 if (playback)
2839 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2840 if (capture)
2841 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2842 goto out;
2843 }
2844
2845 /* ASoC PCM operations */
2846 if (rtd->dai_link->dynamic) {
2847 rtd->ops.open = dpcm_fe_dai_open;
2848 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2849 rtd->ops.prepare = dpcm_fe_dai_prepare;
2850 rtd->ops.trigger = dpcm_fe_dai_trigger;
2851 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2852 rtd->ops.close = dpcm_fe_dai_close;
2853 rtd->ops.pointer = soc_pcm_pointer;
2854 } else {
2855 rtd->ops.open = soc_pcm_open;
2856 rtd->ops.hw_params = soc_pcm_hw_params;
2857 rtd->ops.prepare = soc_pcm_prepare;
2858 rtd->ops.trigger = soc_pcm_trigger;
2859 rtd->ops.hw_free = soc_pcm_hw_free;
2860 rtd->ops.close = soc_pcm_close;
2861 rtd->ops.pointer = soc_pcm_pointer;
2862 }
2863
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002864 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002865 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002866
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002867 if (drv->ioctl)
2868 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002869 if (drv->sync_stop)
2870 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002871 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002872 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002873 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002874 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002875 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002876 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002877 }
2878
Liam Girdwoodddee6272011-06-09 14:45:53 +01002879 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002880 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002881
2882 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002883 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002884
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002885 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002886 if (ret < 0) {
2887 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2888 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002889 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002890
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002891 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002892out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002893 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2894 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2895 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002896 return ret;
2897}
Liam Girdwood01d75842012-04-25 12:12:49 +01002898
2899/* is the current PCM operation for this FE ? */
2900int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2901{
2902 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2903 return 1;
2904 return 0;
2905}
2906EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2907
2908/* is the current PCM operation for this BE ? */
2909int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2910 struct snd_soc_pcm_runtime *be, int stream)
2911{
2912 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2913 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2914 be->dpcm[stream].runtime_update))
2915 return 1;
2916 return 0;
2917}
2918EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2919
2920/* get the substream for this BE */
2921struct snd_pcm_substream *
2922 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2923{
2924 return be->pcm->streams[stream].substream;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2927
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002928static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2929 struct snd_soc_pcm_runtime *be,
2930 int stream,
2931 const enum snd_soc_dpcm_state *states,
2932 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01002933{
2934 struct snd_soc_dpcm *dpcm;
2935 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002936 int ret = 1;
2937 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002938 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002939
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002940 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00002941 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002942
2943 if (dpcm->fe == fe)
2944 continue;
2945
2946 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002947 for (i = 0; i < num_states; i++) {
2948 if (state == states[i]) {
2949 ret = 0;
2950 break;
2951 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002952 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002953 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002954 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01002955
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002956 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002957 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01002958}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002959
2960/*
2961 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2962 * are not running, paused or suspended for the specified stream direction.
2963 */
2964int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2965 struct snd_soc_pcm_runtime *be, int stream)
2966{
2967 const enum snd_soc_dpcm_state state[] = {
2968 SND_SOC_DPCM_STATE_START,
2969 SND_SOC_DPCM_STATE_PAUSED,
2970 SND_SOC_DPCM_STATE_SUSPEND,
2971 };
2972
2973 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2974}
Liam Girdwood01d75842012-04-25 12:12:49 +01002975EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2976
2977/*
2978 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2979 * running, paused or suspended for the specified stream direction.
2980 */
2981int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2982 struct snd_soc_pcm_runtime *be, int stream)
2983{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002984 const enum snd_soc_dpcm_state state[] = {
2985 SND_SOC_DPCM_STATE_START,
2986 SND_SOC_DPCM_STATE_PAUSED,
2987 SND_SOC_DPCM_STATE_SUSPEND,
2988 SND_SOC_DPCM_STATE_PREPARE,
2989 };
Liam Girdwood01d75842012-04-25 12:12:49 +01002990
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002991 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01002992}
2993EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002994
2995#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01002996static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002997{
2998 switch (state) {
2999 case SND_SOC_DPCM_STATE_NEW:
3000 return "new";
3001 case SND_SOC_DPCM_STATE_OPEN:
3002 return "open";
3003 case SND_SOC_DPCM_STATE_HW_PARAMS:
3004 return "hw_params";
3005 case SND_SOC_DPCM_STATE_PREPARE:
3006 return "prepare";
3007 case SND_SOC_DPCM_STATE_START:
3008 return "start";
3009 case SND_SOC_DPCM_STATE_STOP:
3010 return "stop";
3011 case SND_SOC_DPCM_STATE_SUSPEND:
3012 return "suspend";
3013 case SND_SOC_DPCM_STATE_PAUSED:
3014 return "paused";
3015 case SND_SOC_DPCM_STATE_HW_FREE:
3016 return "hw_free";
3017 case SND_SOC_DPCM_STATE_CLOSE:
3018 return "close";
3019 }
3020
3021 return "unknown";
3022}
3023
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003024static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3025 int stream, char *buf, size_t size)
3026{
3027 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3028 struct snd_soc_dpcm *dpcm;
3029 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003030 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003031
3032 /* FE state */
3033 offset += snprintf(buf + offset, size - offset,
3034 "[%s - %s]\n", fe->dai_link->name,
3035 stream ? "Capture" : "Playback");
3036
3037 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3038 dpcm_state_string(fe->dpcm[stream].state));
3039
3040 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3041 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3042 offset += snprintf(buf + offset, size - offset,
3043 "Hardware Params: "
3044 "Format = %s, Channels = %d, Rate = %d\n",
3045 snd_pcm_format_name(params_format(params)),
3046 params_channels(params),
3047 params_rate(params));
3048
3049 /* BEs state */
3050 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3051
3052 if (list_empty(&fe->dpcm[stream].be_clients)) {
3053 offset += snprintf(buf + offset, size - offset,
3054 " No active DSP links\n");
3055 goto out;
3056 }
3057
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003058 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003059 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003060 struct snd_soc_pcm_runtime *be = dpcm->be;
3061 params = &dpcm->hw_params;
3062
3063 offset += snprintf(buf + offset, size - offset,
3064 "- %s\n", be->dai_link->name);
3065
3066 offset += snprintf(buf + offset, size - offset,
3067 " State: %s\n",
3068 dpcm_state_string(be->dpcm[stream].state));
3069
3070 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3071 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3072 offset += snprintf(buf + offset, size - offset,
3073 " Hardware Params: "
3074 "Format = %s, Channels = %d, Rate = %d\n",
3075 snd_pcm_format_name(params_format(params)),
3076 params_channels(params),
3077 params_rate(params));
3078 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003079 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003080out:
3081 return offset;
3082}
3083
3084static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3085 size_t count, loff_t *ppos)
3086{
3087 struct snd_soc_pcm_runtime *fe = file->private_data;
3088 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09003089 int stream;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003090 char *buf;
3091
3092 buf = kmalloc(out_count, GFP_KERNEL);
3093 if (!buf)
3094 return -ENOMEM;
3095
Kuninori Morimoto7083f8772020-02-17 17:28:28 +09003096 for_each_pcm_streams(stream)
3097 if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
3098 offset += dpcm_show_state(fe, stream,
3099 buf + offset,
3100 out_count - offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003101
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003102 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003103
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003104 kfree(buf);
3105 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003106}
3107
3108static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003109 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003110 .read = dpcm_state_read_file,
3111 .llseek = default_llseek,
3112};
3113
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003114void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003115{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003116 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003117 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003118
Kuninori Morimoto596becd2019-08-07 10:31:36 +09003119 if (!rtd->dai_link->dynamic)
3120 return;
3121
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003122 if (!rtd->card->debugfs_card_root)
3123 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003124
3125 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3126 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003127
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003128 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3129 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003130}
3131#endif