blob: cd49c2d688c3d669fe69db357deb81c3ab9a943d [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
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020031/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010032 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
33 * @rtd: ASoC PCM runtime that is activated
34 * @stream: Direction of the PCM stream
35 *
36 * Increments the active count for all the DAIs and components attached to a PCM
37 * runtime. Should typically be called when a stream is opened.
38 *
39 * Must be called with the rtd->pcm_mutex being held
40 */
41void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
42{
43 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000044 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020045 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010046
47 lockdep_assert_held(&rtd->pcm_mutex);
48
49 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
50 cpu_dai->playback_active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000051 for_each_rtd_codec_dai(rtd, i, codec_dai)
52 codec_dai->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010053 } else {
54 cpu_dai->capture_active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000055 for_each_rtd_codec_dai(rtd, i, codec_dai)
56 codec_dai->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010057 }
58
59 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010060 cpu_dai->component->active++;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000061 for_each_rtd_codec_dai(rtd, i, codec_dai) {
62 codec_dai->active++;
63 codec_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020064 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010065}
66
67/**
68 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
69 * @rtd: ASoC PCM runtime that is deactivated
70 * @stream: Direction of the PCM stream
71 *
72 * Decrements the active count for all the DAIs and components attached to a PCM
73 * runtime. Should typically be called when a stream is closed.
74 *
75 * Must be called with the rtd->pcm_mutex being held
76 */
77void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
78{
79 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000080 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020081 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010082
83 lockdep_assert_held(&rtd->pcm_mutex);
84
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000087 for_each_rtd_codec_dai(rtd, i, codec_dai)
88 codec_dai->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010089 } else {
90 cpu_dai->capture_active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000091 for_each_rtd_codec_dai(rtd, i, codec_dai)
92 codec_dai->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010093 }
94
95 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010096 cpu_dai->component->active--;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000097 for_each_rtd_codec_dai(rtd, i, codec_dai) {
98 codec_dai->component->active--;
99 codec_dai->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200100 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100101}
102
103/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100104 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
105 * @rtd: The ASoC PCM runtime that should be checked.
106 *
107 * This function checks whether the power down delay should be ignored for a
108 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
109 * been configured to ignore the delay, or if none of the components benefits
110 * from having the delay.
111 */
112bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
113{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000114 struct snd_soc_rtdcom_list *rtdcom;
115 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200116 bool ignore = true;
117
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100118 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
119 return true;
120
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000121 for_each_rtdcom(rtd, rtdcom) {
122 component = rtdcom->component;
123
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000124 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000125 }
126
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000127 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100128}
129
130/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
134 *
135 * Sets the substream runtime hardware parameters.
136 */
137int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
152
Liam Girdwood01d75842012-04-25 12:12:49 +0100153/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000154int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 int event)
156{
157 struct snd_soc_dpcm *dpcm;
158
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000159 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100160
161 struct snd_soc_pcm_runtime *be = dpcm->be;
162
Liam Girdwood103d84a2012-11-19 14:39:15 +0000163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100164 be->dai_link->name, event, dir);
165
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700166 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
167 (be->dpcm[dir].users >= 1))
168 continue;
169
Liam Girdwood01d75842012-04-25 12:12:49 +0100170 snd_soc_dapm_stream_event(be, dir, event);
171 }
172
173 snd_soc_dapm_stream_event(fe, dir, event);
174
175 return 0;
176}
177
Dong Aisheng17841022011-08-29 17:15:14 +0800178static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
179 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100180{
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100182 int ret;
183
Nicolin Chen3635bf02013-11-13 18:56:24 +0800184 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
185 rtd->dai_link->symmetric_rates)) {
186 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
187 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100188
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200189 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800190 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200191 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800192 if (ret < 0) {
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply rate constraint: %d\n",
195 ret);
196 return ret;
197 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 }
199
Nicolin Chen3635bf02013-11-13 18:56:24 +0800200 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
201 rtd->dai_link->symmetric_channels)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
203 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100204
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200205 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800206 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800207 soc_dai->channels);
208 if (ret < 0) {
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply channel symmetry constraint: %d\n",
211 ret);
212 return ret;
213 }
214 }
215
216 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
217 rtd->dai_link->symmetric_samplebits)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
219 soc_dai->sample_bits);
220
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200221 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800222 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800223 soc_dai->sample_bits);
224 if (ret < 0) {
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
227 ret);
228 return ret;
229 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100230 }
231
232 return 0;
233}
234
Nicolin Chen3635bf02013-11-13 18:56:24 +0800235static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *params)
237{
238 struct snd_soc_pcm_runtime *rtd = substream->private_data;
239 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000240 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200241 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800242
243 rate = params_rate(params);
244 channels = params_channels(params);
245 sample_bits = snd_pcm_format_physical_width(params_format(params));
246
247 /* reject unmatched parameters when applying symmetry */
248 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800249 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200250
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000251 for_each_rtd_codec_dai(rtd, i, codec_dai)
252 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200253
Nicolin Chen3635bf02013-11-13 18:56:24 +0800254 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
255 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
256 cpu_dai->rate, rate);
257 return -EINVAL;
258 }
259
260 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800261 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200262
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000263 for_each_rtd_codec_dai(rtd, i, codec_dai)
264 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200265
Nicolin Chen3635bf02013-11-13 18:56:24 +0800266 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
267 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
268 cpu_dai->channels, channels);
269 return -EINVAL;
270 }
271
272 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800273 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200274
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000275 for_each_rtd_codec_dai(rtd, i, codec_dai)
276 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200277
Nicolin Chen3635bf02013-11-13 18:56:24 +0800278 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
279 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
280 cpu_dai->sample_bits, sample_bits);
281 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282 }
283
284 return 0;
285}
286
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100287static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
288{
289 struct snd_soc_pcm_runtime *rtd = substream->private_data;
290 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100291 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000292 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200293 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100294
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200295 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
296 cpu_driver->symmetric_channels || link->symmetric_channels ||
297 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
298
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000299 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200300 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000301 codec_dai->driver->symmetric_rates ||
302 codec_dai->driver->symmetric_channels ||
303 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304
305 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100306}
307
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200308static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000309{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100311 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000312
313 if (!bits)
314 return;
315
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100316 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
317 if (ret != 0)
318 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
319 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000320}
321
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200322static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200323{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
325 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200326 struct snd_soc_dai *codec_dai;
327 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200328 unsigned int bits = 0, cpu_bits;
329
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000331 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200332 if (codec_dai->driver->playback.sig_bits == 0) {
333 bits = 0;
334 break;
335 }
336 bits = max(codec_dai->driver->playback.sig_bits, bits);
337 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338 cpu_bits = cpu_dai->driver->playback.sig_bits;
339 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000340 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200341 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200342 bits = 0;
343 break;
344 }
345 bits = max(codec_dai->driver->capture.sig_bits, bits);
346 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200347 cpu_bits = cpu_dai->driver->capture.sig_bits;
348 }
349
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200350 soc_pcm_set_msb(substream, bits);
351 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200352}
353
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200354static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200355{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200356 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100357 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200358 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000359 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200360 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
361 struct snd_soc_dai_driver *codec_dai_drv;
362 struct snd_soc_pcm_stream *codec_stream;
363 struct snd_soc_pcm_stream *cpu_stream;
364 unsigned int chan_min = 0, chan_max = UINT_MAX;
365 unsigned int rate_min = 0, rate_max = UINT_MAX;
366 unsigned int rates = UINT_MAX;
367 u64 formats = ULLONG_MAX;
368 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100369
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
371 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100372 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200373 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100374
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200375 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000376 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200377
378 /*
379 * Skip CODECs which don't support the current stream type.
380 * Otherwise, since the rate, channel, and format values will
381 * zero in that case, we would have no usable settings left,
382 * causing the resulting setup to fail.
383 * At least one CODEC should match, otherwise we should have
384 * bailed out on a higher level, since there would be no
385 * CODEC to support the transfer direction in that case.
386 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000387 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200388 substream->stream))
389 continue;
390
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000391 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
393 codec_stream = &codec_dai_drv->playback;
394 else
395 codec_stream = &codec_dai_drv->capture;
396 chan_min = max(chan_min, codec_stream->channels_min);
397 chan_max = min(chan_max, codec_stream->channels_max);
398 rate_min = max(rate_min, codec_stream->rate_min);
399 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
400 formats &= codec_stream->formats;
401 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
402 }
403
404 /*
405 * chan min/max cannot be enforced if there are multiple CODEC DAIs
406 * connected to a single CPU DAI, use CPU DAI's directly and let
407 * channel allocation be fixed up later
408 */
409 if (rtd->num_codecs > 1) {
410 chan_min = cpu_stream->channels_min;
411 chan_max = cpu_stream->channels_max;
412 }
413
414 hw->channels_min = max(chan_min, cpu_stream->channels_min);
415 hw->channels_max = min(chan_max, cpu_stream->channels_max);
416 if (hw->formats)
417 hw->formats &= formats & cpu_stream->formats;
418 else
419 hw->formats = formats & cpu_stream->formats;
420 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100421
422 snd_pcm_limit_hw_rates(runtime);
423
424 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200425 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100426 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200427 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200428}
429
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900430static int soc_pcm_components_open(struct snd_pcm_substream *substream,
431 struct snd_soc_component **last)
432{
433 struct snd_soc_pcm_runtime *rtd = substream->private_data;
434 struct snd_soc_rtdcom_list *rtdcom;
435 struct snd_soc_component *component;
436 int ret = 0;
437
438 for_each_rtdcom(rtd, rtdcom) {
439 component = rtdcom->component;
440 *last = component;
441
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900442 ret = snd_soc_component_module_get_when_open(component);
443 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900444 dev_err(component->dev,
445 "ASoC: can't get module %s\n",
446 component->name);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900447 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900448 }
449
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900450 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900451 if (ret < 0) {
452 dev_err(component->dev,
453 "ASoC: can't open component %s: %d\n",
454 component->name, ret);
455 return ret;
456 }
457 }
458 *last = NULL;
459 return 0;
460}
461
Charles Keepax244e2932018-06-19 16:22:09 +0100462static int soc_pcm_components_close(struct snd_pcm_substream *substream,
463 struct snd_soc_component *last)
464{
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_soc_rtdcom_list *rtdcom;
467 struct snd_soc_component *component;
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900468 int ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100469
470 for_each_rtdcom(rtd, rtdcom) {
471 component = rtdcom->component;
472
473 if (component == last)
474 break;
475
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900476 ret |= snd_soc_component_close(component, substream);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900477 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100478 }
479
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900480 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100481}
482
Mark Brown58ba9b22012-01-16 18:38:51 +0000483/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100484 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
485 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100486 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 */
488static int soc_pcm_open(struct snd_pcm_substream *substream)
489{
490 struct snd_soc_pcm_runtime *rtd = substream->private_data;
491 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000492 struct snd_soc_component *component;
493 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200495 struct snd_soc_dai *codec_dai;
496 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100497 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800499 pinctrl_pm_select_default_state(cpu_dai->dev);
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000500 for_each_rtd_codec_dai(rtd, i, codec_dai)
501 pinctrl_pm_select_default_state(codec_dai->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000502
503 for_each_rtdcom(rtd, rtdcom) {
504 component = rtdcom->component;
505
506 pm_runtime_get_sync(component->dev);
507 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000508
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100509 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100510
511 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900512 ret = snd_soc_dai_startup(cpu_dai, substream);
513 if (ret < 0) {
514 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
515 cpu_dai->name, ret);
516 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100517 }
518
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900519 ret = soc_pcm_components_open(substream, &component);
520 if (ret < 0)
521 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000522
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000523 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900524 ret = snd_soc_dai_startup(codec_dai, substream);
525 if (ret < 0) {
526 dev_err(codec_dai->dev,
527 "ASoC: can't open codec %s: %d\n",
528 codec_dai->name, ret);
529 goto codec_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200531
532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
533 codec_dai->tx_mask = 0;
534 else
535 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 }
537
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000538 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539 ret = rtd->dai_link->ops->startup(substream);
540 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000541 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000542 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543 goto machine_err;
544 }
545 }
546
Liam Girdwood01d75842012-04-25 12:12:49 +0100547 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
548 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
549 goto dynamic;
550
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200552 soc_pcm_init_runtime_hw(substream);
553
554 if (rtd->num_codecs == 1)
555 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100557 if (soc_pcm_has_symmetry(substream))
558 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
559
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100561 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000562 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200563 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 goto config_err;
565 }
566 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000567 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200568 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100569 goto config_err;
570 }
571 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
572 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000573 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200574 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100575 goto config_err;
576 }
577
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200578 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000579
Liam Girdwoodddee6272011-06-09 14:45:53 +0100580 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800581 if (cpu_dai->active) {
582 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
583 if (ret != 0)
584 goto config_err;
585 }
586
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000587 for_each_rtd_codec_dai(rtd, i, codec_dai) {
588 if (codec_dai->active) {
589 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200590 if (ret != 0)
591 goto config_err;
592 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 }
594
Liam Girdwood103d84a2012-11-19 14:39:15 +0000595 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200596 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100599 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601 runtime->hw.rate_max);
602
Liam Girdwood01d75842012-04-25 12:12:49 +0100603dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100604
605 snd_soc_runtime_activate(rtd, substream->stream);
606
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100607 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100608 return 0;
609
610config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000611 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 rtd->dai_link->ops->shutdown(substream);
613
614machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200615 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616
617codec_dai_err:
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900618 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
619 snd_soc_dai_shutdown(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200620
Kuninori Morimotob8135862017-10-11 01:37:23 +0000621component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100622 soc_pcm_components_close(substream, component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900623
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900624 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100625out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100626 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000627
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000628 for_each_rtdcom(rtd, rtdcom) {
629 component = rtdcom->component;
630
631 pm_runtime_mark_last_busy(component->dev);
632 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530633 }
634
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000635 for_each_rtd_codec_dai(rtd, i, codec_dai) {
636 if (!codec_dai->active)
637 pinctrl_pm_select_sleep_state(codec_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200638 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800639 if (!cpu_dai->active)
640 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000641
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 return ret;
643}
644
645/*
646 * Power down the audio subsystem pmdown_time msecs after close is called.
647 * This is to ensure there are no pops or clicks in between any music tracks
648 * due to DAPM power cycling.
649 */
650static void close_delayed_work(struct work_struct *work)
651{
652 struct snd_soc_pcm_runtime *rtd =
653 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200654 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100655
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100656 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657
Liam Girdwood103d84a2012-11-19 14:39:15 +0000658 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100659 codec_dai->driver->playback.stream_name,
660 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600661 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662
663 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600664 if (rtd->pop_wait == 1) {
665 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800666 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000667 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668 }
669
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100670 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671}
672
Jerome Bruneta3420312019-07-25 18:59:47 +0200673static void codec2codec_close_delayed_work(struct work_struct *work)
674{
675 /*
676 * Currently nothing to do for c2c links
677 * Since c2c links are internal nodes in the DAPM graph and
678 * don't interface with the outside world or application layer
679 * we don't have to do any special handling on close.
680 */
681}
682
Liam Girdwoodddee6272011-06-09 14:45:53 +0100683/*
684 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100685 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100686 * shutdown.
687 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100688static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689{
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000691 struct snd_soc_component *component;
692 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200694 struct snd_soc_dai *codec_dai;
695 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100696
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100697 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100699 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100700
Dong Aisheng17841022011-08-29 17:15:14 +0800701 /* clear the corresponding DAIs rate when inactive */
702 if (!cpu_dai->active)
703 cpu_dai->rate = 0;
704
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000705 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200706 if (!codec_dai->active)
707 codec_dai->rate = 0;
708 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200709
Ramesh Babuae116012014-10-15 12:34:59 +0530710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
711
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900712 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100713
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900714 for_each_rtd_codec_dai(rtd, i, codec_dai)
715 snd_soc_dai_shutdown(codec_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100716
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000717 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100718 rtd->dai_link->ops->shutdown(substream);
719
Charles Keepax244e2932018-06-19 16:22:09 +0100720 soc_pcm_components_close(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000721
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100723 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300724 /* powered down playback stream now */
725 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800726 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800727 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300728 } else {
729 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600730 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100731 queue_delayed_work(system_power_efficient_wq,
732 &rtd->delayed_work,
733 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300734 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100735 } else {
736 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800737 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000738 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100739 }
740
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100741 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000742
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000743 for_each_rtdcom(rtd, rtdcom) {
744 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530745
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000746 pm_runtime_mark_last_busy(component->dev);
747 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530748 }
749
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000750 for_each_rtd_codec_dai(rtd, i, codec_dai) {
751 if (!codec_dai->active)
752 pinctrl_pm_select_sleep_state(codec_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200753 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800754 if (!cpu_dai->active)
755 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000756
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 return 0;
758}
759
760/*
761 * Called by ALSA when the PCM substream is prepared, can set format, sample
762 * rate, etc. This function is non atomic and can be called multiple times,
763 * it can refer to the runtime info.
764 */
765static int soc_pcm_prepare(struct snd_pcm_substream *substream)
766{
767 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000768 struct snd_soc_component *component;
769 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100770 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200771 struct snd_soc_dai *codec_dai;
772 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100773
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100774 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100775
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000776 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777 ret = rtd->dai_link->ops->prepare(substream);
778 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000779 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
780 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781 goto out;
782 }
783 }
784
Kuninori Morimotob8135862017-10-11 01:37:23 +0000785 for_each_rtdcom(rtd, rtdcom) {
786 component = rtdcom->component;
787
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900788 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000789 if (ret < 0) {
790 dev_err(component->dev,
791 "ASoC: platform prepare error: %d\n", ret);
792 goto out;
793 }
794 }
795
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000796 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900797 ret = snd_soc_dai_prepare(codec_dai, substream);
798 if (ret < 0) {
799 dev_err(codec_dai->dev,
800 "ASoC: codec DAI prepare error: %d\n",
801 ret);
802 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803 }
804 }
805
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900806 ret = snd_soc_dai_prepare(cpu_dai, substream);
807 if (ret < 0) {
808 dev_err(cpu_dai->dev,
809 "ASoC: cpu DAI prepare error: %d\n", ret);
810 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100811 }
812
813 /* cancel any delayed stream shutdown that is pending */
814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600815 rtd->pop_wait) {
816 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100817 cancel_delayed_work(&rtd->delayed_work);
818 }
819
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000820 snd_soc_dapm_stream_event(rtd, substream->stream,
821 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000823 for_each_rtd_codec_dai(rtd, i, codec_dai)
824 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200825 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530826 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100827
828out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100829 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100830 return ret;
831}
832
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200833static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
834 unsigned int mask)
835{
836 struct snd_interval *interval;
837 int channels = hweight_long(mask);
838
839 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
840 interval->min = channels;
841 interval->max = channels;
842}
843
Charles Keepax244e2932018-06-19 16:22:09 +0100844static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
845 struct snd_soc_component *last)
846{
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_rtdcom_list *rtdcom;
849 struct snd_soc_component *component;
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900850 int ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100851
852 for_each_rtdcom(rtd, rtdcom) {
853 component = rtdcom->component;
854
855 if (component == last)
856 break;
857
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900858 ret |= snd_soc_component_hw_free(component, substream);
Charles Keepax244e2932018-06-19 16:22:09 +0100859 }
860
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900861 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100862}
863
Liam Girdwoodddee6272011-06-09 14:45:53 +0100864/*
865 * Called by ALSA when the hardware params are set by application. This
866 * function can also be called multiple times and can allocate buffers
867 * (using snd_pcm_lib_* ). It's non-atomic.
868 */
869static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
870 struct snd_pcm_hw_params *params)
871{
872 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000873 struct snd_soc_component *component;
874 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100875 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000876 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100877 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100878
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100879 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000880 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100881 ret = rtd->dai_link->ops->hw_params(substream, params);
882 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000883 dev_err(rtd->card->dev, "ASoC: machine hw_params"
884 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100885 goto out;
886 }
887 }
888
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000889 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200890 struct snd_pcm_hw_params codec_params;
891
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200892 /*
893 * Skip CODECs which don't support the current stream type,
894 * the idea being that if a CODEC is not used for the currently
895 * set up transfer direction, it should not need to be
896 * configured, especially since the configuration used might
897 * not even be supported by that CODEC. There may be cases
898 * however where a CODEC needs to be set up although it is
899 * actually not being used for the transfer, e.g. if a
900 * capture-only CODEC is acting as an LRCLK and/or BCLK master
901 * for the DAI link including a playback-only CODEC.
902 * If this becomes necessary, we will have to augment the
903 * machine driver setup with information on how to act, so
904 * we can do the right thing here.
905 */
906 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
907 continue;
908
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200909 /* copy params for each codec */
910 codec_params = *params;
911
912 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800913 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
914 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200915 soc_pcm_codec_params_fixup(&codec_params,
916 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800917
918 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
919 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200920 soc_pcm_codec_params_fixup(&codec_params,
921 codec_dai->rx_mask);
922
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900923 ret = snd_soc_dai_hw_params(codec_dai, substream,
924 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200925 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100926 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200927
928 codec_dai->rate = params_rate(&codec_params);
929 codec_dai->channels = params_channels(&codec_params);
930 codec_dai->sample_bits = snd_pcm_format_physical_width(
931 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000932
933 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100934 }
935
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900936 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200937 if (ret < 0)
938 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100939
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900940 /* store the parameters for each DAIs */
941 cpu_dai->rate = params_rate(params);
942 cpu_dai->channels = params_channels(params);
943 cpu_dai->sample_bits =
944 snd_pcm_format_physical_width(params_format(params));
945
946 snd_soc_dapm_update_dai(substream, params, cpu_dai);
947
Kuninori Morimotob8135862017-10-11 01:37:23 +0000948 for_each_rtdcom(rtd, rtdcom) {
949 component = rtdcom->component;
950
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900951 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100952 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000953 dev_err(component->dev,
954 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100955 component->name, ret);
956 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000957 }
958 }
Charles Keepax244e2932018-06-19 16:22:09 +0100959 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000960
jiada wang957ce0c2017-09-20 15:25:30 +0900961 ret = soc_pcm_params_symmetry(substream, params);
962 if (ret)
Kuninori Morimotob8135862017-10-11 01:37:23 +0000963 goto component_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100964out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100965 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100966 return ret;
967
Kuninori Morimotob8135862017-10-11 01:37:23 +0000968component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100969 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000970
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900971 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900972 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100973
974interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200975 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100976
977codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000978 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200979 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
980 continue;
981
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900982 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200983 codec_dai->rate = 0;
984 }
985
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000986 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100987 rtd->dai_link->ops->hw_free(substream);
988
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100989 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100990 return ret;
991}
992
993/*
994 * Frees resources allocated by hw_params, can be called multiple times
995 */
996static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
997{
998 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100999 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001000 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001001 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001002 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001004 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001005
Nicolin Chend3383422013-11-20 18:37:09 +08001006 /* clear the corresponding DAIs parameters when going to be inactive */
1007 if (cpu_dai->active == 1) {
1008 cpu_dai->rate = 0;
1009 cpu_dai->channels = 0;
1010 cpu_dai->sample_bits = 0;
1011 }
1012
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001013 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001014 if (codec_dai->active == 1) {
1015 codec_dai->rate = 0;
1016 codec_dai->channels = 0;
1017 codec_dai->sample_bits = 0;
1018 }
Nicolin Chend3383422013-11-20 18:37:09 +08001019 }
1020
Liam Girdwoodddee6272011-06-09 14:45:53 +01001021 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001022 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1023 if ((playback && codec_dai->playback_active == 1) ||
1024 (!playback && codec_dai->capture_active == 1))
1025 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001026 substream->stream);
1027 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001028
1029 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001030 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001031 rtd->dai_link->ops->hw_free(substream);
1032
Kuninori Morimotob8135862017-10-11 01:37:23 +00001033 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +01001034 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001035
Liam Girdwoodddee6272011-06-09 14:45:53 +01001036 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001037 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +02001038 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1039 continue;
1040
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001041 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001042 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001043
Kuninori Morimoto846faae2019-07-22 10:33:19 +09001044 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001045
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001046 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001047 return 0;
1048}
1049
1050static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1051{
1052 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001053 struct snd_soc_component *component;
1054 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001055 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001056 struct snd_soc_dai *codec_dai;
1057 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001058
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001059 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001060 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1061 if (ret < 0)
1062 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001063 }
1064
Kuninori Morimotob8135862017-10-11 01:37:23 +00001065 for_each_rtdcom(rtd, rtdcom) {
1066 component = rtdcom->component;
1067
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001068 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001069 if (ret < 0)
1070 return ret;
1071 }
1072
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001073 snd_soc_dai_trigger(cpu_dai, substream, cmd);
1074 if (ret < 0)
1075 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001076
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001077 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001078 ret = rtd->dai_link->ops->trigger(substream, cmd);
1079 if (ret < 0)
1080 return ret;
1081 }
1082
Liam Girdwoodddee6272011-06-09 14:45:53 +01001083 return 0;
1084}
1085
Mark Brown45c0a182012-05-09 21:46:27 +01001086static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1087 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001088{
1089 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001090 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001091 struct snd_soc_dai *codec_dai;
1092 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001093
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001094 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001095 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001096 if (ret < 0)
1097 return ret;
1098 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001099
1100 snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1101 if (ret < 0)
1102 return ret;
1103
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001104 return 0;
1105}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001106/*
1107 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001108 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001109 * the runtime->delay will be updated accordingly.
1110 */
1111static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1112{
1113 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001114 struct snd_soc_component *component;
1115 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001116 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001117 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 snd_pcm_uframes_t offset = 0;
1120 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001121 snd_pcm_sframes_t codec_delay = 0;
1122 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001123
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301124 /* clearing the previous total delay */
1125 runtime->delay = 0;
1126
Kuninori Morimotob8135862017-10-11 01:37:23 +00001127 for_each_rtdcom(rtd, rtdcom) {
1128 component = rtdcom->component;
1129
Kuninori Morimotob8135862017-10-11 01:37:23 +00001130 if (!component->driver->ops ||
1131 !component->driver->ops->pointer)
1132 continue;
1133
1134 /* FIXME: use 1st pointer */
1135 offset = component->driver->ops->pointer(substream);
1136 break;
1137 }
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301138 /* base delay if assigned in pointer callback */
1139 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001140
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001141 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001142
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001143 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001144 codec_delay = max(codec_delay,
1145 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001146 }
1147 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001148
Liam Girdwoodddee6272011-06-09 14:45:53 +01001149 runtime->delay = delay;
1150
1151 return offset;
1152}
1153
Liam Girdwood01d75842012-04-25 12:12:49 +01001154/* connect a FE and BE */
1155static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1156 struct snd_soc_pcm_runtime *be, int stream)
1157{
1158 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001159 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001160
1161 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001162 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001163 if (dpcm->be == be && dpcm->fe == fe)
1164 return 0;
1165 }
1166
1167 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1168 if (!dpcm)
1169 return -ENOMEM;
1170
1171 dpcm->be = be;
1172 dpcm->fe = fe;
1173 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1174 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001175 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001176 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1177 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001178 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001179
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001180 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001181 stream ? "capture" : "playback", fe->dai_link->name,
1182 stream ? "<-" : "->", be->dai_link->name);
1183
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001184#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001185 dpcm->debugfs_state = debugfs_create_dir(be->dai_link->name,
1186 fe->debugfs_dpcm_root);
1187 debugfs_create_u32("state", 0644, dpcm->debugfs_state, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001188#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001189 return 1;
1190}
1191
1192/* reparent a BE onto another FE */
1193static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1194 struct snd_soc_pcm_runtime *be, int stream)
1195{
1196 struct snd_soc_dpcm *dpcm;
1197 struct snd_pcm_substream *fe_substream, *be_substream;
1198
1199 /* reparent if BE is connected to other FEs */
1200 if (!be->dpcm[stream].users)
1201 return;
1202
1203 be_substream = snd_soc_dpcm_get_substream(be, stream);
1204
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001205 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001206 if (dpcm->fe == fe)
1207 continue;
1208
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001209 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001210 stream ? "capture" : "playback",
1211 dpcm->fe->dai_link->name,
1212 stream ? "<-" : "->", dpcm->be->dai_link->name);
1213
1214 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1215 be_substream->runtime = fe_substream->runtime;
1216 break;
1217 }
1218}
1219
1220/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001221void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001222{
1223 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001224 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001225
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001226 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001227 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001228 stream ? "capture" : "playback",
1229 dpcm->be->dai_link->name);
1230
1231 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1232 continue;
1233
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001234 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001235 stream ? "capture" : "playback", fe->dai_link->name,
1236 stream ? "<-" : "->", dpcm->be->dai_link->name);
1237
1238 /* BEs still alive need new FE */
1239 dpcm_be_reparent(fe, dpcm->be, stream);
1240
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001241#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001242 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001243#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001244 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001245 list_del(&dpcm->list_be);
1246 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001247 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001248 kfree(dpcm);
1249 }
1250}
1251
1252/* get BE for DAI widget and stream */
1253static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1254 struct snd_soc_dapm_widget *widget, int stream)
1255{
1256 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001257 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001258 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001259
Liam Girdwood3c146462018-03-14 20:43:51 +00001260 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1261
Liam Girdwood01d75842012-04-25 12:12:49 +01001262 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001263 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001264
Liam Girdwood35ea0652012-06-05 19:26:59 +01001265 if (!be->dai_link->no_pcm)
1266 continue;
1267
Liam Girdwood3c146462018-03-14 20:43:51 +00001268 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1269 be->cpu_dai->playback_widget ?
1270 be->cpu_dai->playback_widget->name : "(not set)");
1271
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001272 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001273 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001274
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001275 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001276 if (dai->playback_widget == widget)
1277 return be;
1278 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001279 }
1280 } else {
1281
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001282 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001283
Liam Girdwood35ea0652012-06-05 19:26:59 +01001284 if (!be->dai_link->no_pcm)
1285 continue;
1286
Liam Girdwood3c146462018-03-14 20:43:51 +00001287 dev_dbg(card->dev, "ASoC: try BE %s\n",
1288 be->cpu_dai->capture_widget ?
1289 be->cpu_dai->capture_widget->name : "(not set)");
1290
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001291 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001292 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001293
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001294 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001295 if (dai->capture_widget == widget)
1296 return be;
1297 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001298 }
1299 }
1300
Liam Girdwood3c146462018-03-14 20:43:51 +00001301 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001302 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001303 stream ? "capture" : "playback", widget->name);
1304 return NULL;
1305}
1306
1307static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001308 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001309{
1310 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001311 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001312 else
Benoit Cousson37018612014-04-24 14:01:45 +02001313 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001314}
1315
1316static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1317 struct snd_soc_dapm_widget *widget)
1318{
1319 int i;
1320
1321 for (i = 0; i < list->num_widgets; i++) {
1322 if (widget == list->widgets[i])
1323 return 1;
1324 }
1325
1326 return 0;
1327}
1328
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001329static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1330 enum snd_soc_dapm_direction dir)
1331{
1332 struct snd_soc_card *card = widget->dapm->card;
1333 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001334 struct snd_soc_dai *dai;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001335 int i;
1336
1337 if (dir == SND_SOC_DAPM_DIR_OUT) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001338 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001339 if (!rtd->dai_link->no_pcm)
1340 continue;
1341
1342 if (rtd->cpu_dai->playback_widget == widget)
1343 return true;
1344
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001345 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001346 if (dai->playback_widget == widget)
1347 return true;
1348 }
1349 }
1350 } else { /* SND_SOC_DAPM_DIR_IN */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001351 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001352 if (!rtd->dai_link->no_pcm)
1353 continue;
1354
1355 if (rtd->cpu_dai->capture_widget == widget)
1356 return true;
1357
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001358 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001359 if (dai->capture_widget == widget)
1360 return true;
1361 }
1362 }
1363 }
1364
1365 return false;
1366}
1367
Liam Girdwood23607022014-01-17 17:03:55 +00001368int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001369 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001370{
1371 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001372 int paths;
1373
Liam Girdwood01d75842012-04-25 12:12:49 +01001374 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001375 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001376 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001377
Liam Girdwood103d84a2012-11-19 14:39:15 +00001378 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001379 stream ? "capture" : "playback");
1380
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 return paths;
1382}
1383
Liam Girdwood01d75842012-04-25 12:12:49 +01001384static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1385 struct snd_soc_dapm_widget_list **list_)
1386{
1387 struct snd_soc_dpcm *dpcm;
1388 struct snd_soc_dapm_widget_list *list = *list_;
1389 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001390 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001391 int prune = 0;
1392
1393 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001394 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001395 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001396
1397 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001398 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001399
1400 /* prune the BE if it's no longer in our active list */
1401 if (widget && widget_in_list(list, widget))
1402 continue;
1403
1404 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001405 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001406 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001407
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001408 /* prune the BE if it's no longer in our active list */
1409 if (widget && widget_in_list(list, widget))
1410 continue;
1411 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001412
Liam Girdwood103d84a2012-11-19 14:39:15 +00001413 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001414 stream ? "capture" : "playback",
1415 dpcm->be->dai_link->name, fe->dai_link->name);
1416 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1417 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1418 prune++;
1419 }
1420
Liam Girdwood103d84a2012-11-19 14:39:15 +00001421 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001422 return prune;
1423}
1424
1425static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1426 struct snd_soc_dapm_widget_list **list_)
1427{
1428 struct snd_soc_card *card = fe->card;
1429 struct snd_soc_dapm_widget_list *list = *list_;
1430 struct snd_soc_pcm_runtime *be;
1431 int i, new = 0, err;
1432
1433 /* Create any new FE <--> BE connections */
1434 for (i = 0; i < list->num_widgets; i++) {
1435
Mark Brown46162742013-06-05 19:36:11 +01001436 switch (list->widgets[i]->id) {
1437 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001438 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1439 continue;
1440 break;
Mark Brown46162742013-06-05 19:36:11 +01001441 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001442 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1443 continue;
Mark Brown46162742013-06-05 19:36:11 +01001444 break;
1445 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001446 continue;
Mark Brown46162742013-06-05 19:36:11 +01001447 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001448
1449 /* is there a valid BE rtd for this widget */
1450 be = dpcm_get_be(card, list->widgets[i], stream);
1451 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001452 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001453 list->widgets[i]->name);
1454 continue;
1455 }
1456
1457 /* make sure BE is a real BE */
1458 if (!be->dai_link->no_pcm)
1459 continue;
1460
1461 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001462 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001463 continue;
1464
1465 /* newly connected FE and BE */
1466 err = dpcm_be_connect(fe, be, stream);
1467 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001468 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001469 list->widgets[i]->name);
1470 break;
1471 } else if (err == 0) /* already connected */
1472 continue;
1473
1474 /* new */
1475 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1476 new++;
1477 }
1478
Liam Girdwood103d84a2012-11-19 14:39:15 +00001479 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001480 return new;
1481}
1482
1483/*
1484 * Find the corresponding BE DAIs that source or sink audio to this
1485 * FE substream.
1486 */
Liam Girdwood23607022014-01-17 17:03:55 +00001487int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001488 int stream, struct snd_soc_dapm_widget_list **list, int new)
1489{
1490 if (new)
1491 return dpcm_add_paths(fe, stream, list);
1492 else
1493 return dpcm_prune_paths(fe, stream, list);
1494}
1495
Liam Girdwood23607022014-01-17 17:03:55 +00001496void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001497{
1498 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001499 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001500
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001501 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001502 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001503 dpcm->be->dpcm[stream].runtime_update =
1504 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001505 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001506}
1507
1508static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1509 int stream)
1510{
1511 struct snd_soc_dpcm *dpcm;
1512
1513 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001514 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001515
1516 struct snd_soc_pcm_runtime *be = dpcm->be;
1517 struct snd_pcm_substream *be_substream =
1518 snd_soc_dpcm_get_substream(be, stream);
1519
1520 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001521 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001522 stream ? "capture" : "playback",
1523 be->dpcm[stream].state);
1524
1525 if (--be->dpcm[stream].users != 0)
1526 continue;
1527
1528 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1529 continue;
1530
1531 soc_pcm_close(be_substream);
1532 be_substream->runtime = NULL;
1533 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1534 }
1535}
1536
Liam Girdwood23607022014-01-17 17:03:55 +00001537int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001538{
1539 struct snd_soc_dpcm *dpcm;
1540 int err, count = 0;
1541
1542 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001543 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001544
1545 struct snd_soc_pcm_runtime *be = dpcm->be;
1546 struct snd_pcm_substream *be_substream =
1547 snd_soc_dpcm_get_substream(be, stream);
1548
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001549 if (!be_substream) {
1550 dev_err(be->dev, "ASoC: no backend %s stream\n",
1551 stream ? "capture" : "playback");
1552 continue;
1553 }
1554
Liam Girdwood01d75842012-04-25 12:12:49 +01001555 /* is this op for this BE ? */
1556 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1557 continue;
1558
1559 /* first time the dpcm is open ? */
1560 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001561 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001562 stream ? "capture" : "playback",
1563 be->dpcm[stream].state);
1564
1565 if (be->dpcm[stream].users++ != 0)
1566 continue;
1567
1568 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1569 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1570 continue;
1571
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001572 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1573 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001574
1575 be_substream->runtime = be->dpcm[stream].runtime;
1576 err = soc_pcm_open(be_substream);
1577 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001578 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001579 be->dpcm[stream].users--;
1580 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001581 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001582 stream ? "capture" : "playback",
1583 be->dpcm[stream].state);
1584
1585 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1586 goto unwind;
1587 }
1588
1589 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1590 count++;
1591 }
1592
1593 return count;
1594
1595unwind:
1596 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001597 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001598 struct snd_soc_pcm_runtime *be = dpcm->be;
1599 struct snd_pcm_substream *be_substream =
1600 snd_soc_dpcm_get_substream(be, stream);
1601
1602 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1603 continue;
1604
1605 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001606 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001607 stream ? "capture" : "playback",
1608 be->dpcm[stream].state);
1609
1610 if (--be->dpcm[stream].users != 0)
1611 continue;
1612
1613 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1614 continue;
1615
1616 soc_pcm_close(be_substream);
1617 be_substream->runtime = NULL;
1618 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1619 }
1620
1621 return err;
1622}
1623
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001624static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001625 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001626{
1627 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001628 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001629 runtime->hw.channels_min = stream->channels_min;
1630 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001631 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001632 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001633 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001634 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001635 runtime->hw.rates = stream->rates;
1636}
1637
Jerome Brunet435ffb72018-07-05 12:13:48 +02001638static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1639 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001640{
1641 struct snd_soc_pcm_runtime *fe = substream->private_data;
1642 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001643 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001644 int stream = substream->stream;
1645
1646 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001647 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001648
1649 /*
1650 * It returns merged BE codec format
1651 * if FE want to use it (= dpcm_merged_format)
1652 */
1653
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001654 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001655 struct snd_soc_pcm_runtime *be = dpcm->be;
1656 struct snd_soc_dai_driver *codec_dai_drv;
1657 struct snd_soc_pcm_stream *codec_stream;
1658 int i;
1659
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001660 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001661 /*
1662 * Skip CODECs which don't support the current stream
1663 * type. See soc_pcm_init_runtime_hw() for more details
1664 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001665 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001666 continue;
1667
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001668 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001669 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1670 codec_stream = &codec_dai_drv->playback;
1671 else
1672 codec_stream = &codec_dai_drv->capture;
1673
Jerome Brunet435ffb72018-07-05 12:13:48 +02001674 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001675 }
1676 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001677}
1678
Jerome Brunet435ffb72018-07-05 12:13:48 +02001679static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1680 unsigned int *channels_min,
1681 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001682{
1683 struct snd_soc_pcm_runtime *fe = substream->private_data;
1684 struct snd_soc_dpcm *dpcm;
1685 int stream = substream->stream;
1686
1687 if (!fe->dai_link->dpcm_merged_chan)
1688 return;
1689
Jiada Wangf4c277b2018-06-20 18:25:20 +09001690 /*
1691 * It returns merged BE codec channel;
1692 * if FE want to use it (= dpcm_merged_chan)
1693 */
1694
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001695 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001696 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001697 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001698 struct snd_soc_dai_driver *codec_dai_drv;
1699 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001700 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001701
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001702 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1703 cpu_stream = &cpu_dai_drv->playback;
1704 else
1705 cpu_stream = &cpu_dai_drv->capture;
1706
1707 *channels_min = max(*channels_min, cpu_stream->channels_min);
1708 *channels_max = min(*channels_max, cpu_stream->channels_max);
1709
1710 /*
1711 * chan min/max cannot be enforced if there are multiple CODEC
1712 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1713 */
1714 if (be->num_codecs == 1) {
1715 codec_dai_drv = be->codec_dais[0]->driver;
1716
Jiada Wangf4c277b2018-06-20 18:25:20 +09001717 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1718 codec_stream = &codec_dai_drv->playback;
1719 else
1720 codec_stream = &codec_dai_drv->capture;
1721
1722 *channels_min = max(*channels_min,
1723 codec_stream->channels_min);
1724 *channels_max = min(*channels_max,
1725 codec_stream->channels_max);
1726 }
1727 }
1728}
1729
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001730static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1731 unsigned int *rates,
1732 unsigned int *rate_min,
1733 unsigned int *rate_max)
1734{
1735 struct snd_soc_pcm_runtime *fe = substream->private_data;
1736 struct snd_soc_dpcm *dpcm;
1737 int stream = substream->stream;
1738
1739 if (!fe->dai_link->dpcm_merged_rate)
1740 return;
1741
1742 /*
1743 * It returns merged BE codec channel;
1744 * if FE want to use it (= dpcm_merged_chan)
1745 */
1746
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001747 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001748 struct snd_soc_pcm_runtime *be = dpcm->be;
1749 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1750 struct snd_soc_dai_driver *codec_dai_drv;
1751 struct snd_soc_pcm_stream *codec_stream;
1752 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001753 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001754 int i;
1755
1756 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1757 cpu_stream = &cpu_dai_drv->playback;
1758 else
1759 cpu_stream = &cpu_dai_drv->capture;
1760
1761 *rate_min = max(*rate_min, cpu_stream->rate_min);
1762 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1763 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1764
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001765 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001766 /*
1767 * Skip CODECs which don't support the current stream
1768 * type. See soc_pcm_init_runtime_hw() for more details
1769 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001770 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001771 continue;
1772
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001773 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001774 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1775 codec_stream = &codec_dai_drv->playback;
1776 else
1777 codec_stream = &codec_dai_drv->capture;
1778
1779 *rate_min = max(*rate_min, codec_stream->rate_min);
1780 *rate_max = min_not_zero(*rate_max,
1781 codec_stream->rate_max);
1782 *rates = snd_pcm_rate_mask_intersect(*rates,
1783 codec_stream->rates);
1784 }
1785 }
1786}
1787
Mark Brown45c0a182012-05-09 21:46:27 +01001788static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001789{
1790 struct snd_pcm_runtime *runtime = substream->runtime;
1791 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1792 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1793 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1794
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001795 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001796 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001797 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001798 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001799
Jerome Brunet435ffb72018-07-05 12:13:48 +02001800 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1801 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1802 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001803 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1804 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001805}
1806
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001807static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1808
1809/* Set FE's runtime_update state; the state is protected via PCM stream lock
1810 * for avoiding the race with trigger callback.
1811 * If the state is unset and a trigger is pending while the previous operation,
1812 * process the pending trigger action here.
1813 */
1814static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1815 int stream, enum snd_soc_dpcm_update state)
1816{
1817 struct snd_pcm_substream *substream =
1818 snd_soc_dpcm_get_substream(fe, stream);
1819
1820 snd_pcm_stream_lock_irq(substream);
1821 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1822 dpcm_fe_dai_do_trigger(substream,
1823 fe->dpcm[stream].trigger_pending - 1);
1824 fe->dpcm[stream].trigger_pending = 0;
1825 }
1826 fe->dpcm[stream].runtime_update = state;
1827 snd_pcm_stream_unlock_irq(substream);
1828}
1829
PC Liao906c7d62015-12-11 11:33:51 +08001830static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1831 int stream)
1832{
1833 struct snd_soc_dpcm *dpcm;
1834 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1835 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1836 int err;
1837
1838 /* apply symmetry for FE */
1839 if (soc_pcm_has_symmetry(fe_substream))
1840 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1841
1842 /* Symmetry only applies if we've got an active stream. */
1843 if (fe_cpu_dai->active) {
1844 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1845 if (err < 0)
1846 return err;
1847 }
1848
1849 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001850 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001851 struct snd_soc_pcm_runtime *be = dpcm->be;
1852 struct snd_pcm_substream *be_substream =
1853 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001854 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001855 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001856 int i;
1857
Jerome Brunet6246f282019-04-01 15:03:54 +02001858 /* A backend may not have the requested substream */
1859 if (!be_substream)
1860 continue;
1861
1862 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301863 if (rtd->dai_link->be_hw_params_fixup)
1864 continue;
1865
PC Liao906c7d62015-12-11 11:33:51 +08001866 if (soc_pcm_has_symmetry(be_substream))
1867 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1868
1869 /* Symmetry only applies if we've got an active stream. */
1870 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001871 err = soc_pcm_apply_symmetry(fe_substream,
1872 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001873 if (err < 0)
1874 return err;
1875 }
1876
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001877 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1878 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001879 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001880 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001881 if (err < 0)
1882 return err;
1883 }
1884 }
1885 }
1886
1887 return 0;
1888}
1889
Liam Girdwood01d75842012-04-25 12:12:49 +01001890static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1891{
1892 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1893 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1894 int stream = fe_substream->stream, ret = 0;
1895
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001896 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001897
1898 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1899 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001900 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001901 goto be_err;
1902 }
1903
Liam Girdwood103d84a2012-11-19 14:39:15 +00001904 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001905
1906 /* start the DAI frontend */
1907 ret = soc_pcm_open(fe_substream);
1908 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001909 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001910 goto unwind;
1911 }
1912
1913 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1914
1915 dpcm_set_fe_runtime(fe_substream);
1916 snd_pcm_limit_hw_rates(runtime);
1917
PC Liao906c7d62015-12-11 11:33:51 +08001918 ret = dpcm_apply_symmetry(fe_substream, stream);
1919 if (ret < 0) {
1920 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1921 ret);
1922 goto unwind;
1923 }
1924
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001925 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001926 return 0;
1927
1928unwind:
1929 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1930be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001931 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001932 return ret;
1933}
1934
Liam Girdwood23607022014-01-17 17:03:55 +00001935int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001936{
1937 struct snd_soc_dpcm *dpcm;
1938
1939 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001940 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001941
1942 struct snd_soc_pcm_runtime *be = dpcm->be;
1943 struct snd_pcm_substream *be_substream =
1944 snd_soc_dpcm_get_substream(be, stream);
1945
1946 /* is this op for this BE ? */
1947 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1948 continue;
1949
1950 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001951 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001952 stream ? "capture" : "playback",
1953 be->dpcm[stream].state);
1954
1955 if (--be->dpcm[stream].users != 0)
1956 continue;
1957
1958 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001959 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1960 soc_pcm_hw_free(be_substream);
1961 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1962 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001963
Liam Girdwood103d84a2012-11-19 14:39:15 +00001964 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001965 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001966
1967 soc_pcm_close(be_substream);
1968 be_substream->runtime = NULL;
1969
1970 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1971 }
1972 return 0;
1973}
1974
1975static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1976{
1977 struct snd_soc_pcm_runtime *fe = substream->private_data;
1978 int stream = substream->stream;
1979
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001980 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001981
1982 /* shutdown the BEs */
1983 dpcm_be_dai_shutdown(fe, substream->stream);
1984
Liam Girdwood103d84a2012-11-19 14:39:15 +00001985 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001986
1987 /* now shutdown the frontend */
1988 soc_pcm_close(substream);
1989
1990 /* run the stream event for each BE */
1991 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1992
1993 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001994 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001995 return 0;
1996}
1997
Liam Girdwood23607022014-01-17 17:03:55 +00001998int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001999{
2000 struct snd_soc_dpcm *dpcm;
2001
2002 /* only hw_params backends that are either sinks or sources
2003 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002004 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002005
2006 struct snd_soc_pcm_runtime *be = dpcm->be;
2007 struct snd_pcm_substream *be_substream =
2008 snd_soc_dpcm_get_substream(be, stream);
2009
2010 /* is this op for this BE ? */
2011 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2012 continue;
2013
2014 /* only free hw when no longer used - check all FEs */
2015 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2016 continue;
2017
Qiao Zhou36fba622014-12-03 10:13:43 +08002018 /* do not free hw if this BE is used by other FE */
2019 if (be->dpcm[stream].users > 1)
2020 continue;
2021
Liam Girdwood01d75842012-04-25 12:12:49 +01002022 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2023 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2024 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002025 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302026 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2027 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002028 continue;
2029
Liam Girdwood103d84a2012-11-19 14:39:15 +00002030 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002031 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002032
2033 soc_pcm_hw_free(be_substream);
2034
2035 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2036 }
2037
2038 return 0;
2039}
2040
Mark Brown45c0a182012-05-09 21:46:27 +01002041static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002042{
2043 struct snd_soc_pcm_runtime *fe = substream->private_data;
2044 int err, stream = substream->stream;
2045
2046 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002047 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002048
Liam Girdwood103d84a2012-11-19 14:39:15 +00002049 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002050
2051 /* call hw_free on the frontend */
2052 err = soc_pcm_hw_free(substream);
2053 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002054 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002055 fe->dai_link->name);
2056
2057 /* only hw_params backends that are either sinks or sources
2058 * to this frontend DAI */
2059 err = dpcm_be_dai_hw_free(fe, stream);
2060
2061 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002062 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002063
2064 mutex_unlock(&fe->card->mutex);
2065 return 0;
2066}
2067
Liam Girdwood23607022014-01-17 17:03:55 +00002068int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002069{
2070 struct snd_soc_dpcm *dpcm;
2071 int ret;
2072
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002073 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002074
2075 struct snd_soc_pcm_runtime *be = dpcm->be;
2076 struct snd_pcm_substream *be_substream =
2077 snd_soc_dpcm_get_substream(be, stream);
2078
2079 /* is this op for this BE ? */
2080 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2081 continue;
2082
Liam Girdwood01d75842012-04-25 12:12:49 +01002083 /* copy params for each dpcm */
2084 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2085 sizeof(struct snd_pcm_hw_params));
2086
2087 /* perform any hw_params fixups */
2088 if (be->dai_link->be_hw_params_fixup) {
2089 ret = be->dai_link->be_hw_params_fixup(be,
2090 &dpcm->hw_params);
2091 if (ret < 0) {
2092 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002093 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002094 ret);
2095 goto unwind;
2096 }
2097 }
2098
Libin Yangae061d22019-04-19 09:53:12 +08002099 /* copy the fixed-up hw params for BE dai */
2100 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2101 sizeof(struct snd_pcm_hw_params));
2102
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002103 /* only allow hw_params() if no connected FEs are running */
2104 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2105 continue;
2106
2107 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2108 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2109 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2110 continue;
2111
2112 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002113 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002114
Liam Girdwood01d75842012-04-25 12:12:49 +01002115 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2116 if (ret < 0) {
2117 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002118 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002119 goto unwind;
2120 }
2121
2122 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2123 }
2124 return 0;
2125
2126unwind:
2127 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002128 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002129 struct snd_soc_pcm_runtime *be = dpcm->be;
2130 struct snd_pcm_substream *be_substream =
2131 snd_soc_dpcm_get_substream(be, stream);
2132
2133 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2134 continue;
2135
2136 /* only allow hw_free() if no connected FEs are running */
2137 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2138 continue;
2139
2140 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2141 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2142 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2143 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2144 continue;
2145
2146 soc_pcm_hw_free(be_substream);
2147 }
2148
2149 return ret;
2150}
2151
Mark Brown45c0a182012-05-09 21:46:27 +01002152static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2153 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002154{
2155 struct snd_soc_pcm_runtime *fe = substream->private_data;
2156 int ret, stream = substream->stream;
2157
2158 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002159 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002160
2161 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2162 sizeof(struct snd_pcm_hw_params));
2163 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2164 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002165 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002166 goto out;
2167 }
2168
Liam Girdwood103d84a2012-11-19 14:39:15 +00002169 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002170 fe->dai_link->name, params_rate(params),
2171 params_channels(params), params_format(params));
2172
2173 /* call hw_params on the frontend */
2174 ret = soc_pcm_hw_params(substream, params);
2175 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002176 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002177 dpcm_be_dai_hw_free(fe, stream);
2178 } else
2179 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2180
2181out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002182 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002183 mutex_unlock(&fe->card->mutex);
2184 return ret;
2185}
2186
2187static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2188 struct snd_pcm_substream *substream, int cmd)
2189{
2190 int ret;
2191
Liam Girdwood103d84a2012-11-19 14:39:15 +00002192 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002193 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002194
2195 ret = soc_pcm_trigger(substream, cmd);
2196 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002197 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002198
2199 return ret;
2200}
2201
Liam Girdwood23607022014-01-17 17:03:55 +00002202int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002203 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002204{
2205 struct snd_soc_dpcm *dpcm;
2206 int ret = 0;
2207
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002208 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002209
2210 struct snd_soc_pcm_runtime *be = dpcm->be;
2211 struct snd_pcm_substream *be_substream =
2212 snd_soc_dpcm_get_substream(be, stream);
2213
2214 /* is this op for this BE ? */
2215 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2216 continue;
2217
2218 switch (cmd) {
2219 case SNDRV_PCM_TRIGGER_START:
2220 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2221 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2222 continue;
2223
2224 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2225 if (ret)
2226 return ret;
2227
2228 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2229 break;
2230 case SNDRV_PCM_TRIGGER_RESUME:
2231 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2232 continue;
2233
2234 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2235 if (ret)
2236 return ret;
2237
2238 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2239 break;
2240 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2241 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2242 continue;
2243
2244 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2245 if (ret)
2246 return ret;
2247
2248 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2249 break;
2250 case SNDRV_PCM_TRIGGER_STOP:
2251 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2252 continue;
2253
2254 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2255 continue;
2256
2257 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2258 if (ret)
2259 return ret;
2260
2261 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2262 break;
2263 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002264 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002265 continue;
2266
2267 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2268 continue;
2269
2270 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2271 if (ret)
2272 return ret;
2273
2274 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2275 break;
2276 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2277 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2278 continue;
2279
2280 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2281 continue;
2282
2283 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2284 if (ret)
2285 return ret;
2286
2287 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2288 break;
2289 }
2290 }
2291
2292 return ret;
2293}
2294EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2295
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002296static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002297{
2298 struct snd_soc_pcm_runtime *fe = substream->private_data;
2299 int stream = substream->stream, ret;
2300 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2301
2302 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2303
2304 switch (trigger) {
2305 case SND_SOC_DPCM_TRIGGER_PRE:
2306 /* call trigger on the frontend before the backend. */
2307
Liam Girdwood103d84a2012-11-19 14:39:15 +00002308 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002309 fe->dai_link->name, cmd);
2310
2311 ret = soc_pcm_trigger(substream, cmd);
2312 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002313 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002314 goto out;
2315 }
2316
2317 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2318 break;
2319 case SND_SOC_DPCM_TRIGGER_POST:
2320 /* call trigger on the frontend after the backend. */
2321
2322 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2323 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002324 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002325 goto out;
2326 }
2327
Liam Girdwood103d84a2012-11-19 14:39:15 +00002328 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002329 fe->dai_link->name, cmd);
2330
2331 ret = soc_pcm_trigger(substream, cmd);
2332 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002333 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2334 /* bespoke trigger() - handles both FE and BEs */
2335
Liam Girdwood103d84a2012-11-19 14:39:15 +00002336 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002337 fe->dai_link->name, cmd);
2338
2339 ret = soc_pcm_bespoke_trigger(substream, cmd);
2340 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002341 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002342 goto out;
2343 }
2344 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002345 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002346 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002347 fe->dai_link->name);
2348 ret = -EINVAL;
2349 goto out;
2350 }
2351
2352 switch (cmd) {
2353 case SNDRV_PCM_TRIGGER_START:
2354 case SNDRV_PCM_TRIGGER_RESUME:
2355 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2356 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2357 break;
2358 case SNDRV_PCM_TRIGGER_STOP:
2359 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002360 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2361 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002362 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2363 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2364 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002365 }
2366
2367out:
2368 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2369 return ret;
2370}
2371
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002372static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2373{
2374 struct snd_soc_pcm_runtime *fe = substream->private_data;
2375 int stream = substream->stream;
2376
2377 /* if FE's runtime_update is already set, we're in race;
2378 * process this trigger later at exit
2379 */
2380 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2381 fe->dpcm[stream].trigger_pending = cmd + 1;
2382 return 0; /* delayed, assuming it's successful */
2383 }
2384
2385 /* we're alone, let's trigger */
2386 return dpcm_fe_dai_do_trigger(substream, cmd);
2387}
2388
Liam Girdwood23607022014-01-17 17:03:55 +00002389int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002390{
2391 struct snd_soc_dpcm *dpcm;
2392 int ret = 0;
2393
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002394 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002395
2396 struct snd_soc_pcm_runtime *be = dpcm->be;
2397 struct snd_pcm_substream *be_substream =
2398 snd_soc_dpcm_get_substream(be, stream);
2399
2400 /* is this op for this BE ? */
2401 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2402 continue;
2403
2404 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002405 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002406 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2407 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002408 continue;
2409
Liam Girdwood103d84a2012-11-19 14:39:15 +00002410 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002411 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002412
2413 ret = soc_pcm_prepare(be_substream);
2414 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002415 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002416 ret);
2417 break;
2418 }
2419
2420 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2421 }
2422 return ret;
2423}
2424
Mark Brown45c0a182012-05-09 21:46:27 +01002425static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002426{
2427 struct snd_soc_pcm_runtime *fe = substream->private_data;
2428 int stream = substream->stream, ret = 0;
2429
2430 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2431
Liam Girdwood103d84a2012-11-19 14:39:15 +00002432 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002433
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002434 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002435
2436 /* there is no point preparing this FE if there are no BEs */
2437 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002438 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002439 fe->dai_link->name);
2440 ret = -EINVAL;
2441 goto out;
2442 }
2443
2444 ret = dpcm_be_dai_prepare(fe, substream->stream);
2445 if (ret < 0)
2446 goto out;
2447
2448 /* call prepare on the frontend */
2449 ret = soc_pcm_prepare(substream);
2450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002451 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002452 fe->dai_link->name);
2453 goto out;
2454 }
2455
2456 /* run the stream event for each BE */
2457 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2458 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2459
2460out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002461 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002462 mutex_unlock(&fe->card->mutex);
2463
2464 return ret;
2465}
2466
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002467static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2468 unsigned int cmd, void *arg)
2469{
2470 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002471 struct snd_soc_component *component;
2472 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002473
Kuninori Morimotob8135862017-10-11 01:37:23 +00002474 for_each_rtdcom(rtd, rtdcom) {
2475 component = rtdcom->component;
2476
Kuninori Morimotob8135862017-10-11 01:37:23 +00002477 if (!component->driver->ops ||
2478 !component->driver->ops->ioctl)
2479 continue;
2480
2481 /* FIXME: use 1st ioctl */
2482 return component->driver->ops->ioctl(substream, cmd, arg);
2483 }
2484
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002485 return snd_pcm_lib_ioctl(substream, cmd, arg);
2486}
2487
Liam Girdwood618dae12012-04-25 12:12:51 +01002488static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2489{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002490 struct snd_pcm_substream *substream =
2491 snd_soc_dpcm_get_substream(fe, stream);
2492 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002493 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002494
Liam Girdwood103d84a2012-11-19 14:39:15 +00002495 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002496 stream ? "capture" : "playback", fe->dai_link->name);
2497
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002498 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2499 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002500 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002501 fe->dai_link->name);
2502
2503 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2504 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002505 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002506 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002507 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002508 fe->dai_link->name);
2509
2510 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2511 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002512 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002513 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002514
2515 err = dpcm_be_dai_hw_free(fe, stream);
2516 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002517 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002518
2519 err = dpcm_be_dai_shutdown(fe, stream);
2520 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002521 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002522
2523 /* run the stream event for each BE */
2524 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2525
2526 return 0;
2527}
2528
2529static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2530{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002531 struct snd_pcm_substream *substream =
2532 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002533 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002534 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002535 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002536 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002537
Liam Girdwood103d84a2012-11-19 14:39:15 +00002538 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002539 stream ? "capture" : "playback", fe->dai_link->name);
2540
2541 /* Only start the BE if the FE is ready */
2542 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2543 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2544 return -EINVAL;
2545
2546 /* startup must always be called for new BEs */
2547 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002548 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002549 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002550
2551 /* keep going if FE state is > open */
2552 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2553 return 0;
2554
2555 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002556 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002557 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002558
2559 /* keep going if FE state is > hw_params */
2560 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2561 return 0;
2562
2563
2564 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002565 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002566 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002567
2568 /* run the stream event for each BE */
2569 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2570
2571 /* keep going if FE state is > prepare */
2572 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2573 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2574 return 0;
2575
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002576 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2577 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002578 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002579 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002580
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002581 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2582 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002583 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002584 goto hw_free;
2585 }
2586 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002587 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002588 fe->dai_link->name);
2589
2590 ret = dpcm_be_dai_trigger(fe, stream,
2591 SNDRV_PCM_TRIGGER_START);
2592 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002593 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002594 goto hw_free;
2595 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002596 }
2597
2598 return 0;
2599
2600hw_free:
2601 dpcm_be_dai_hw_free(fe, stream);
2602close:
2603 dpcm_be_dai_shutdown(fe, stream);
2604disconnect:
2605 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002606 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002607 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002608 struct snd_soc_pcm_runtime *be = dpcm->be;
2609 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2610 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2611 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002612 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002613
2614 return ret;
2615}
2616
2617static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2618{
2619 int ret;
2620
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002621 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002622 ret = dpcm_run_update_startup(fe, stream);
2623 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002624 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002625 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002626
2627 return ret;
2628}
2629
2630static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2631{
2632 int ret;
2633
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002634 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002635 ret = dpcm_run_update_shutdown(fe, stream);
2636 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002637 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002638 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002639
2640 return ret;
2641}
2642
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002643static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2644{
2645 struct snd_soc_dapm_widget_list *list;
2646 int count, paths;
2647
2648 if (!fe->dai_link->dynamic)
2649 return 0;
2650
2651 /* only check active links */
2652 if (!fe->cpu_dai->active)
2653 return 0;
2654
2655 /* DAPM sync will call this to update DSP paths */
2656 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2657 new ? "new" : "old", fe->dai_link->name);
2658
2659 /* skip if FE doesn't have playback capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002660 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2661 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002662 goto capture;
2663
2664 /* skip if FE isn't currently playing */
2665 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2666 goto capture;
2667
2668 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2669 if (paths < 0) {
2670 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2671 fe->dai_link->name, "playback");
2672 return paths;
2673 }
2674
2675 /* update any playback paths */
2676 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2677 if (count) {
2678 if (new)
2679 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2680 else
2681 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2682
2683 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2684 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2685 }
2686
2687 dpcm_path_put(&list);
2688
2689capture:
2690 /* skip if FE doesn't have capture capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002691 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2692 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002693 return 0;
2694
2695 /* skip if FE isn't currently capturing */
2696 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2697 return 0;
2698
2699 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2700 if (paths < 0) {
2701 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2702 fe->dai_link->name, "capture");
2703 return paths;
2704 }
2705
2706 /* update any old capture paths */
2707 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2708 if (count) {
2709 if (new)
2710 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2711 else
2712 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2713
2714 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2715 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2716 }
2717
2718 dpcm_path_put(&list);
2719
2720 return 0;
2721}
2722
Liam Girdwood618dae12012-04-25 12:12:51 +01002723/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2724 * any DAI links.
2725 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002726int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002727{
Mengdong Lin1a497982015-11-18 02:34:11 -05002728 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002729 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002730
Liam Girdwood618dae12012-04-25 12:12:51 +01002731 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002732 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002733 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002734 ret = soc_dpcm_fe_runtime_update(fe, 0);
2735 if (ret)
2736 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002737 }
2738
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002739 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002740 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002741 ret = soc_dpcm_fe_runtime_update(fe, 1);
2742 if (ret)
2743 goto out;
2744 }
2745
2746out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002747 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002748 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002749}
Liam Girdwood01d75842012-04-25 12:12:49 +01002750int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2751{
2752 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002753 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01002754
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002755 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002756
2757 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002758 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002759
2760 if (be->dai_link->ignore_suspend)
2761 continue;
2762
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002763 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002764 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002765
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002766 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2767 be->dai_link->name);
2768
2769 if (drv->ops && drv->ops->digital_mute &&
2770 dai->playback_active)
2771 drv->ops->digital_mute(dai, mute);
2772 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002773 }
2774
2775 return 0;
2776}
2777
Mark Brown45c0a182012-05-09 21:46:27 +01002778static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002779{
2780 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2781 struct snd_soc_dpcm *dpcm;
2782 struct snd_soc_dapm_widget_list *list;
2783 int ret;
2784 int stream = fe_substream->stream;
2785
2786 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2787 fe->dpcm[stream].runtime = fe_substream->runtime;
2788
Qiao Zhou8f70e512014-09-10 17:54:07 +08002789 ret = dpcm_path_get(fe, stream, &list);
2790 if (ret < 0) {
2791 mutex_unlock(&fe->card->mutex);
2792 return ret;
2793 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002794 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002795 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002796 }
2797
2798 /* calculate valid and active FE <-> BE dpcms */
2799 dpcm_process_paths(fe, stream, &list, 1);
2800
2801 ret = dpcm_fe_dai_startup(fe_substream);
2802 if (ret < 0) {
2803 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002804 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002805 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2806
2807 dpcm_be_disconnect(fe, stream);
2808 fe->dpcm[stream].runtime = NULL;
2809 }
2810
2811 dpcm_clear_pending_state(fe, stream);
2812 dpcm_path_put(&list);
2813 mutex_unlock(&fe->card->mutex);
2814 return ret;
2815}
2816
Mark Brown45c0a182012-05-09 21:46:27 +01002817static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002818{
2819 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2820 struct snd_soc_dpcm *dpcm;
2821 int stream = fe_substream->stream, ret;
2822
2823 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2824 ret = dpcm_fe_dai_shutdown(fe_substream);
2825
2826 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002827 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002828 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2829
2830 dpcm_be_disconnect(fe, stream);
2831
2832 fe->dpcm[stream].runtime = NULL;
2833 mutex_unlock(&fe->card->mutex);
2834 return ret;
2835}
2836
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002837static void soc_pcm_private_free(struct snd_pcm *pcm)
2838{
2839 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002840 struct snd_soc_rtdcom_list *rtdcom;
2841 struct snd_soc_component *component;
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002842
Kuninori Morimotof30a4c32018-01-24 05:18:46 +00002843 /* need to sync the delayed work before releasing resources */
2844 flush_delayed_work(&rtd->delayed_work);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002845 for_each_rtdcom(rtd, rtdcom) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002846 component = rtdcom->component;
2847
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00002848 if (component->driver->pcm_free)
2849 component->driver->pcm_free(pcm);
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002850 }
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002851}
2852
Kuninori Morimotob8135862017-10-11 01:37:23 +00002853static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2854 unsigned long pos, void __user *buf,
2855 unsigned long bytes)
2856{
2857 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2858 struct snd_soc_rtdcom_list *rtdcom;
2859 struct snd_soc_component *component;
2860
2861 for_each_rtdcom(rtd, rtdcom) {
2862 component = rtdcom->component;
2863
2864 if (!component->driver->ops ||
2865 !component->driver->ops->copy_user)
2866 continue;
2867
2868 /* FIXME. it returns 1st copy now */
2869 return component->driver->ops->copy_user(substream, channel,
2870 pos, buf, bytes);
2871 }
2872
2873 return -EINVAL;
2874}
2875
Kuninori Morimotob8135862017-10-11 01:37:23 +00002876static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2877 unsigned long offset)
2878{
2879 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2880 struct snd_soc_rtdcom_list *rtdcom;
2881 struct snd_soc_component *component;
2882 struct page *page;
2883
2884 for_each_rtdcom(rtd, rtdcom) {
2885 component = rtdcom->component;
2886
2887 if (!component->driver->ops ||
2888 !component->driver->ops->page)
2889 continue;
2890
2891 /* FIXME. it returns 1st page now */
2892 page = component->driver->ops->page(substream, offset);
2893 if (page)
2894 return page;
2895 }
2896
2897 return NULL;
2898}
2899
2900static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2901 struct vm_area_struct *vma)
2902{
2903 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2904 struct snd_soc_rtdcom_list *rtdcom;
2905 struct snd_soc_component *component;
2906
2907 for_each_rtdcom(rtd, rtdcom) {
2908 component = rtdcom->component;
2909
2910 if (!component->driver->ops ||
2911 !component->driver->ops->mmap)
2912 continue;
2913
2914 /* FIXME. it returns 1st mmap now */
2915 return component->driver->ops->mmap(substream, vma);
2916 }
2917
2918 return -EINVAL;
2919}
2920
Liam Girdwoodddee6272011-06-09 14:45:53 +01002921/* create a new pcm */
2922int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2923{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002924 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002925 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002926 struct snd_soc_component *component;
2927 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002928 struct snd_pcm *pcm;
2929 char new_name[64];
2930 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002931 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002932
Liam Girdwood01d75842012-04-25 12:12:49 +01002933 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002934 playback = rtd->dai_link->dpcm_playback;
2935 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002936 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002937 /* Adapt stream for codec2codec links */
2938 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2939 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2940 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2941 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2942
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002943 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002944 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2945 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002946 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002947 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2948 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002949 capture = 1;
2950 }
Jerome Bruneta3420312019-07-25 18:59:47 +02002951
2952 capture = capture && cpu_capture->channels_min;
2953 playback = playback && cpu_playback->channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002954 }
Sangsu Parka5002312012-01-02 17:15:10 +09002955
Fabio Estevamd6bead02013-08-29 10:32:13 -03002956 if (rtd->dai_link->playback_only) {
2957 playback = 1;
2958 capture = 0;
2959 }
2960
2961 if (rtd->dai_link->capture_only) {
2962 playback = 0;
2963 capture = 1;
2964 }
2965
Liam Girdwood01d75842012-04-25 12:12:49 +01002966 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002967 if (rtd->dai_link->params) {
2968 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2969 rtd->dai_link->stream_name);
2970
2971 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2972 playback, capture, &pcm);
2973 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002974 snprintf(new_name, sizeof(new_name), "(%s)",
2975 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002976
Liam Girdwood01d75842012-04-25 12:12:49 +01002977 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2978 playback, capture, &pcm);
2979 } else {
2980 if (rtd->dai_link->dynamic)
2981 snprintf(new_name, sizeof(new_name), "%s (*)",
2982 rtd->dai_link->stream_name);
2983 else
2984 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002985 rtd->dai_link->stream_name,
2986 (rtd->num_codecs > 1) ?
2987 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002988
Liam Girdwood01d75842012-04-25 12:12:49 +01002989 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2990 capture, &pcm);
2991 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002992 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002993 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002994 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002995 return ret;
2996 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002997 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002998
2999 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02003000 if (rtd->dai_link->params)
3001 INIT_DELAYED_WORK(&rtd->delayed_work,
3002 codec2codec_close_delayed_work);
3003 else
3004 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003005
Vinod Koul48c76992015-02-12 09:59:53 +05303006 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003007 rtd->pcm = pcm;
3008 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003009
Jerome Bruneta3420312019-07-25 18:59:47 +02003010 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003011 if (playback)
3012 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3013 if (capture)
3014 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3015 goto out;
3016 }
3017
3018 /* ASoC PCM operations */
3019 if (rtd->dai_link->dynamic) {
3020 rtd->ops.open = dpcm_fe_dai_open;
3021 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3022 rtd->ops.prepare = dpcm_fe_dai_prepare;
3023 rtd->ops.trigger = dpcm_fe_dai_trigger;
3024 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3025 rtd->ops.close = dpcm_fe_dai_close;
3026 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003027 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003028 } else {
3029 rtd->ops.open = soc_pcm_open;
3030 rtd->ops.hw_params = soc_pcm_hw_params;
3031 rtd->ops.prepare = soc_pcm_prepare;
3032 rtd->ops.trigger = soc_pcm_trigger;
3033 rtd->ops.hw_free = soc_pcm_hw_free;
3034 rtd->ops.close = soc_pcm_close;
3035 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003036 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003037 }
3038
Kuninori Morimotob8135862017-10-11 01:37:23 +00003039 for_each_rtdcom(rtd, rtdcom) {
3040 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3041
3042 if (!ops)
3043 continue;
3044
Kuninori Morimotob8135862017-10-11 01:37:23 +00003045 if (ops->copy_user)
3046 rtd->ops.copy_user = soc_rtdcom_copy_user;
Kuninori Morimotob8135862017-10-11 01:37:23 +00003047 if (ops->page)
3048 rtd->ops.page = soc_rtdcom_page;
3049 if (ops->mmap)
3050 rtd->ops.mmap = soc_rtdcom_mmap;
3051 }
3052
Liam Girdwoodddee6272011-06-09 14:45:53 +01003053 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003054 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003055
3056 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003057 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003058
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003059 for_each_rtdcom(rtd, rtdcom) {
3060 component = rtdcom->component;
3061
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003062 if (!component->driver->pcm_new)
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003063 continue;
3064
Kuninori Morimoto11fb14f2018-05-08 03:19:49 +00003065 ret = component->driver->pcm_new(rtd);
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003066 if (ret < 0) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003067 dev_err(component->dev,
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003068 "ASoC: pcm constructor failed: %d\n",
3069 ret);
3070 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003071 }
3072 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003073
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02003074 pcm->private_free = soc_pcm_private_free;
Takashi Iwai3d21ef02019-01-11 15:58:39 +01003075 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01003076out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003077 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3078 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3079 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003080 return ret;
3081}
Liam Girdwood01d75842012-04-25 12:12:49 +01003082
3083/* is the current PCM operation for this FE ? */
3084int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3085{
3086 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3087 return 1;
3088 return 0;
3089}
3090EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3091
3092/* is the current PCM operation for this BE ? */
3093int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3094 struct snd_soc_pcm_runtime *be, int stream)
3095{
3096 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3097 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3098 be->dpcm[stream].runtime_update))
3099 return 1;
3100 return 0;
3101}
3102EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3103
3104/* get the substream for this BE */
3105struct snd_pcm_substream *
3106 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3107{
3108 return be->pcm->streams[stream].substream;
3109}
3110EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3111
3112/* get the BE runtime state */
3113enum snd_soc_dpcm_state
3114 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3115{
3116 return be->dpcm[stream].state;
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3119
3120/* set the BE runtime state */
3121void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3122 int stream, enum snd_soc_dpcm_state state)
3123{
3124 be->dpcm[stream].state = state;
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3127
3128/*
3129 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3130 * are not running, paused or suspended for the specified stream direction.
3131 */
3132int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3133 struct snd_soc_pcm_runtime *be, int stream)
3134{
3135 struct snd_soc_dpcm *dpcm;
3136 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003137 int ret = 1;
3138 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003139
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003140 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003141 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003142
3143 if (dpcm->fe == fe)
3144 continue;
3145
3146 state = dpcm->fe->dpcm[stream].state;
3147 if (state == SND_SOC_DPCM_STATE_START ||
3148 state == SND_SOC_DPCM_STATE_PAUSED ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003149 state == SND_SOC_DPCM_STATE_SUSPEND) {
3150 ret = 0;
3151 break;
3152 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003153 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003154 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003155
3156 /* it's safe to free/stop this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003157 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003158}
3159EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3160
3161/*
3162 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3163 * running, paused or suspended for the specified stream direction.
3164 */
3165int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3166 struct snd_soc_pcm_runtime *be, int stream)
3167{
3168 struct snd_soc_dpcm *dpcm;
3169 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003170 int ret = 1;
3171 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003172
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003173 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003174 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003175
3176 if (dpcm->fe == fe)
3177 continue;
3178
3179 state = dpcm->fe->dpcm[stream].state;
3180 if (state == SND_SOC_DPCM_STATE_START ||
3181 state == SND_SOC_DPCM_STATE_PAUSED ||
3182 state == SND_SOC_DPCM_STATE_SUSPEND ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003183 state == SND_SOC_DPCM_STATE_PREPARE) {
3184 ret = 0;
3185 break;
3186 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003187 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003188 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003189
3190 /* it's safe to change hw_params */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003191 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003192}
3193EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003194
3195#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003196static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003197{
3198 switch (state) {
3199 case SND_SOC_DPCM_STATE_NEW:
3200 return "new";
3201 case SND_SOC_DPCM_STATE_OPEN:
3202 return "open";
3203 case SND_SOC_DPCM_STATE_HW_PARAMS:
3204 return "hw_params";
3205 case SND_SOC_DPCM_STATE_PREPARE:
3206 return "prepare";
3207 case SND_SOC_DPCM_STATE_START:
3208 return "start";
3209 case SND_SOC_DPCM_STATE_STOP:
3210 return "stop";
3211 case SND_SOC_DPCM_STATE_SUSPEND:
3212 return "suspend";
3213 case SND_SOC_DPCM_STATE_PAUSED:
3214 return "paused";
3215 case SND_SOC_DPCM_STATE_HW_FREE:
3216 return "hw_free";
3217 case SND_SOC_DPCM_STATE_CLOSE:
3218 return "close";
3219 }
3220
3221 return "unknown";
3222}
3223
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003224static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3225 int stream, char *buf, size_t size)
3226{
3227 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3228 struct snd_soc_dpcm *dpcm;
3229 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003230 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003231
3232 /* FE state */
3233 offset += snprintf(buf + offset, size - offset,
3234 "[%s - %s]\n", fe->dai_link->name,
3235 stream ? "Capture" : "Playback");
3236
3237 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3238 dpcm_state_string(fe->dpcm[stream].state));
3239
3240 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3241 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3242 offset += snprintf(buf + offset, size - offset,
3243 "Hardware Params: "
3244 "Format = %s, Channels = %d, Rate = %d\n",
3245 snd_pcm_format_name(params_format(params)),
3246 params_channels(params),
3247 params_rate(params));
3248
3249 /* BEs state */
3250 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3251
3252 if (list_empty(&fe->dpcm[stream].be_clients)) {
3253 offset += snprintf(buf + offset, size - offset,
3254 " No active DSP links\n");
3255 goto out;
3256 }
3257
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003258 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003259 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003260 struct snd_soc_pcm_runtime *be = dpcm->be;
3261 params = &dpcm->hw_params;
3262
3263 offset += snprintf(buf + offset, size - offset,
3264 "- %s\n", be->dai_link->name);
3265
3266 offset += snprintf(buf + offset, size - offset,
3267 " State: %s\n",
3268 dpcm_state_string(be->dpcm[stream].state));
3269
3270 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3271 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3272 offset += snprintf(buf + offset, size - offset,
3273 " Hardware Params: "
3274 "Format = %s, Channels = %d, Rate = %d\n",
3275 snd_pcm_format_name(params_format(params)),
3276 params_channels(params),
3277 params_rate(params));
3278 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003279 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003280out:
3281 return offset;
3282}
3283
3284static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3285 size_t count, loff_t *ppos)
3286{
3287 struct snd_soc_pcm_runtime *fe = file->private_data;
3288 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3289 char *buf;
3290
3291 buf = kmalloc(out_count, GFP_KERNEL);
3292 if (!buf)
3293 return -ENOMEM;
3294
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003295 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003296 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3297 buf + offset, out_count - offset);
3298
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003299 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003300 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3301 buf + offset, out_count - offset);
3302
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003303 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003304
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003305 kfree(buf);
3306 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003307}
3308
3309static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003310 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003311 .read = dpcm_state_read_file,
3312 .llseek = default_llseek,
3313};
3314
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003315void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003316{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003317 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003318 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003319
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003320 if (!rtd->card->debugfs_card_root)
3321 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003322
3323 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3324 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003325
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003326 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3327 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003328}
3329#endif