blob: ad908e008b2fb75d9f6633419c93ea396a5d35d0 [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 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030039 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010040 */
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
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030047 lockdep_assert_held(&rtd->card->pcm_mutex);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010048
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 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030075 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010076 */
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
Peter Ujfalusi72b745e2019-08-13 13:45:32 +030083 lockdep_assert_held(&rtd->card->pcm_mutex);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010084
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_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200115 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900116 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200117
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100118 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
119 return true;
120
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900121 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000122 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000123
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000124 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100125}
126
127/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200128 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
129 * @substream: the pcm substream
130 * @hw: the hardware parameters
131 *
132 * Sets the substream runtime hardware parameters.
133 */
134int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
135 const struct snd_pcm_hardware *hw)
136{
137 struct snd_pcm_runtime *runtime = substream->runtime;
138 runtime->hw.info = hw->info;
139 runtime->hw.formats = hw->formats;
140 runtime->hw.period_bytes_min = hw->period_bytes_min;
141 runtime->hw.period_bytes_max = hw->period_bytes_max;
142 runtime->hw.periods_min = hw->periods_min;
143 runtime->hw.periods_max = hw->periods_max;
144 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
145 runtime->hw.fifo_size = hw->fifo_size;
146 return 0;
147}
148EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
149
Liam Girdwood01d75842012-04-25 12:12:49 +0100150/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000151int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100152 int event)
153{
154 struct snd_soc_dpcm *dpcm;
155
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000156 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100157
158 struct snd_soc_pcm_runtime *be = dpcm->be;
159
Liam Girdwood103d84a2012-11-19 14:39:15 +0000160 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100161 be->dai_link->name, event, dir);
162
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700163 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
164 (be->dpcm[dir].users >= 1))
165 continue;
166
Liam Girdwood01d75842012-04-25 12:12:49 +0100167 snd_soc_dapm_stream_event(be, dir, event);
168 }
169
170 snd_soc_dapm_stream_event(fe, dir, event);
171
172 return 0;
173}
174
Dong Aisheng17841022011-08-29 17:15:14 +0800175static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
176 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100177{
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100179 int ret;
180
Nicolin Chen3635bf02013-11-13 18:56:24 +0800181 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
182 rtd->dai_link->symmetric_rates)) {
183 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
184 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100185
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200186 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800187 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200188 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800189 if (ret < 0) {
190 dev_err(soc_dai->dev,
191 "ASoC: Unable to apply rate constraint: %d\n",
192 ret);
193 return ret;
194 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100195 }
196
Nicolin Chen3635bf02013-11-13 18:56:24 +0800197 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
198 rtd->dai_link->symmetric_channels)) {
199 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
200 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100201
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200202 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800203 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800204 soc_dai->channels);
205 if (ret < 0) {
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
208 ret);
209 return ret;
210 }
211 }
212
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
217
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200218 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800220 soc_dai->sample_bits);
221 if (ret < 0) {
222 dev_err(soc_dai->dev,
223 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
224 ret);
225 return ret;
226 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100227 }
228
229 return 0;
230}
231
Nicolin Chen3635bf02013-11-13 18:56:24 +0800232static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *params)
234{
235 struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000237 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200238 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800239
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
243
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800246 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200247
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000248 for_each_rtd_codec_dai(rtd, i, codec_dai)
249 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200250
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
254 return -EINVAL;
255 }
256
257 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800258 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200259
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000260 for_each_rtd_codec_dai(rtd, i, codec_dai)
261 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200262
Nicolin Chen3635bf02013-11-13 18:56:24 +0800263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
266 return -EINVAL;
267 }
268
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800270 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200271
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000272 for_each_rtd_codec_dai(rtd, i, codec_dai)
273 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200274
Nicolin Chen3635bf02013-11-13 18:56:24 +0800275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
278 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 }
280
281 return 0;
282}
283
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100284static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
285{
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100288 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000289 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200290 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100291
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200292 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
293 cpu_driver->symmetric_channels || link->symmetric_channels ||
294 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
295
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000296 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200297 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000298 codec_dai->driver->symmetric_rates ||
299 codec_dai->driver->symmetric_channels ||
300 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200301
302 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100303}
304
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200305static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000306{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200307 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100308 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000309
310 if (!bits)
311 return;
312
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100313 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
314 if (ret != 0)
315 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
316 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000317}
318
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200319static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200320{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200323 struct snd_soc_dai *codec_dai;
324 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200325 unsigned int bits = 0, cpu_bits;
326
327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000328 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200329 if (codec_dai->driver->playback.sig_bits == 0) {
330 bits = 0;
331 break;
332 }
333 bits = max(codec_dai->driver->playback.sig_bits, bits);
334 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200335 cpu_bits = cpu_dai->driver->playback.sig_bits;
336 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000337 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200338 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200339 bits = 0;
340 break;
341 }
342 bits = max(codec_dai->driver->capture.sig_bits, bits);
343 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200344 cpu_bits = cpu_dai->driver->capture.sig_bits;
345 }
346
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200347 soc_pcm_set_msb(substream, bits);
348 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200349}
350
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200351static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200352{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200353 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100354 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000356 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200357 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv;
359 struct snd_soc_pcm_stream *codec_stream;
360 struct snd_soc_pcm_stream *cpu_stream;
361 unsigned int chan_min = 0, chan_max = UINT_MAX;
362 unsigned int rate_min = 0, rate_max = UINT_MAX;
363 unsigned int rates = UINT_MAX;
364 u64 formats = ULLONG_MAX;
365 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100366
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100369 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000373 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200374
375 /*
376 * Skip CODECs which don't support the current stream type.
377 * Otherwise, since the rate, channel, and format values will
378 * zero in that case, we would have no usable settings left,
379 * causing the resulting setup to fail.
380 * At least one CODEC should match, otherwise we should have
381 * bailed out on a higher level, since there would be no
382 * CODEC to support the transfer direction in that case.
383 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000384 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200385 substream->stream))
386 continue;
387
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000388 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 codec_stream = &codec_dai_drv->playback;
391 else
392 codec_stream = &codec_dai_drv->capture;
393 chan_min = max(chan_min, codec_stream->channels_min);
394 chan_max = min(chan_max, codec_stream->channels_max);
395 rate_min = max(rate_min, codec_stream->rate_min);
396 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
397 formats &= codec_stream->formats;
398 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
399 }
400
401 /*
402 * chan min/max cannot be enforced if there are multiple CODEC DAIs
403 * connected to a single CPU DAI, use CPU DAI's directly and let
404 * channel allocation be fixed up later
405 */
406 if (rtd->num_codecs > 1) {
407 chan_min = cpu_stream->channels_min;
408 chan_max = cpu_stream->channels_max;
409 }
410
411 hw->channels_min = max(chan_min, cpu_stream->channels_min);
412 hw->channels_max = min(chan_max, cpu_stream->channels_max);
413 if (hw->formats)
414 hw->formats &= formats & cpu_stream->formats;
415 else
416 hw->formats = formats & cpu_stream->formats;
417 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100418
419 snd_pcm_limit_hw_rates(runtime);
420
421 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200422 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100423 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200424 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200425}
426
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900427static int soc_pcm_components_open(struct snd_pcm_substream *substream,
428 struct snd_soc_component **last)
429{
430 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900431 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900432 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900433
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900434 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900435 *last = component;
436
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900437 ret = snd_soc_component_module_get_when_open(component);
438 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900439 dev_err(component->dev,
440 "ASoC: can't get module %s\n",
441 component->name);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900442 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900443 }
444
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900445 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900446 if (ret < 0) {
447 dev_err(component->dev,
448 "ASoC: can't open component %s: %d\n",
449 component->name, ret);
450 return ret;
451 }
452 }
453 *last = NULL;
454 return 0;
455}
456
Charles Keepax244e2932018-06-19 16:22:09 +0100457static int soc_pcm_components_close(struct snd_pcm_substream *substream,
458 struct snd_soc_component *last)
459{
460 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100461 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900462 int i, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100463
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900464 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100465 if (component == last)
466 break;
467
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900468 ret |= snd_soc_component_close(component, substream);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900469 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100470 }
471
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900472 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100473}
474
Mark Brown58ba9b22012-01-16 18:38:51 +0000475/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100478 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100479 */
480static int soc_pcm_open(struct snd_pcm_substream *substream)
481{
482 struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000484 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200486 struct snd_soc_dai *codec_dai;
487 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100488 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100489
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900490 for_each_rtd_components(rtd, i, component)
491 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000492
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900493 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000494 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000495
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300496 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100497
498 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900499 ret = snd_soc_dai_startup(cpu_dai, substream);
500 if (ret < 0) {
501 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
502 cpu_dai->name, ret);
503 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100504 }
505
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900506 ret = soc_pcm_components_open(substream, &component);
507 if (ret < 0)
508 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000509
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000510 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900511 ret = snd_soc_dai_startup(codec_dai, substream);
512 if (ret < 0) {
513 dev_err(codec_dai->dev,
514 "ASoC: can't open codec %s: %d\n",
515 codec_dai->name, ret);
516 goto codec_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100517 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200518
519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
520 codec_dai->tx_mask = 0;
521 else
522 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100523 }
524
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000525 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100526 ret = rtd->dai_link->ops->startup(substream);
527 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000528 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000529 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530 goto machine_err;
531 }
532 }
533
Liam Girdwood01d75842012-04-25 12:12:49 +0100534 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
535 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
536 goto dynamic;
537
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200539 soc_pcm_init_runtime_hw(substream);
540
541 if (rtd->num_codecs == 1)
542 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100544 if (soc_pcm_has_symmetry(substream))
545 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
546
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100548 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000549 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200550 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 goto config_err;
552 }
553 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000554 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200555 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556 goto config_err;
557 }
558 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
559 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000560 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200561 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100562 goto config_err;
563 }
564
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200565 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000566
Liam Girdwoodddee6272011-06-09 14:45:53 +0100567 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800568 if (cpu_dai->active) {
569 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
570 if (ret != 0)
571 goto config_err;
572 }
573
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000574 for_each_rtd_codec_dai(rtd, i, codec_dai) {
575 if (codec_dai->active) {
576 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200577 if (ret != 0)
578 goto config_err;
579 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100580 }
581
Liam Girdwood103d84a2012-11-19 14:39:15 +0000582 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200583 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000584 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
585 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100586 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000587 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100588 runtime->hw.rate_max);
589
Liam Girdwood01d75842012-04-25 12:12:49 +0100590dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100591
592 snd_soc_runtime_activate(rtd, substream->stream);
593
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300594 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100595 return 0;
596
597config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000598 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100599 rtd->dai_link->ops->shutdown(substream);
600
601machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200602 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100603
604codec_dai_err:
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900605 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
606 snd_soc_dai_shutdown(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200607
Kuninori Morimotob8135862017-10-11 01:37:23 +0000608component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100609 soc_pcm_components_close(substream, component);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900610
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900611 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300613 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000614
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900615 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000616 pm_runtime_mark_last_busy(component->dev);
617 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530618 }
619
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900620 for_each_rtd_components(rtd, i, component)
621 if (!component->active)
622 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000623
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 return ret;
625}
626
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800627static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200628{
629 /*
630 * Currently nothing to do for c2c links
631 * Since c2c links are internal nodes in the DAPM graph and
632 * don't interface with the outside world or application layer
633 * we don't have to do any special handling on close.
634 */
635}
636
Liam Girdwoodddee6272011-06-09 14:45:53 +0100637/*
638 * Called by ALSA when a PCM substream is closed. Private data can be
Charles Keepaxef050be2018-04-24 16:39:02 +0100639 * freed here. The cpu DAI, codec DAI, machine and components are also
Liam Girdwoodddee6272011-06-09 14:45:53 +0100640 * shutdown.
641 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100642static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100643{
644 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000645 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200647 struct snd_soc_dai *codec_dai;
648 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100649
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300650 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100652 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100653
Dong Aisheng17841022011-08-29 17:15:14 +0800654 /* clear the corresponding DAIs rate when inactive */
655 if (!cpu_dai->active)
656 cpu_dai->rate = 0;
657
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000658 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200659 if (!codec_dai->active)
660 codec_dai->rate = 0;
661 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200662
Ramesh Babuae116012014-10-15 12:34:59 +0530663 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
664
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900665 snd_soc_dai_shutdown(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900667 for_each_rtd_codec_dai(rtd, i, codec_dai)
668 snd_soc_dai_shutdown(codec_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000670 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671 rtd->dai_link->ops->shutdown(substream);
672
Charles Keepax244e2932018-06-19 16:22:09 +0100673 soc_pcm_components_close(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000674
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100676 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300677 /* powered down playback stream now */
678 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800679 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800680 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300681 } else {
682 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600683 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100684 queue_delayed_work(system_power_efficient_wq,
685 &rtd->delayed_work,
686 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300687 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688 } else {
689 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800690 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000691 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100692 }
693
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300694 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000695
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900696 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000697 pm_runtime_mark_last_busy(component->dev);
698 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530699 }
700
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900701 for_each_rtd_components(rtd, i, component)
702 if (!component->active)
703 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000704
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705 return 0;
706}
707
708/*
709 * Called by ALSA when the PCM substream is prepared, can set format, sample
710 * rate, etc. This function is non atomic and can be called multiple times,
711 * it can refer to the runtime info.
712 */
713static int soc_pcm_prepare(struct snd_pcm_substream *substream)
714{
715 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000716 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200718 struct snd_soc_dai *codec_dai;
719 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300721 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000723 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100724 ret = rtd->dai_link->ops->prepare(substream);
725 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000726 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
727 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100728 goto out;
729 }
730 }
731
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900732 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900733 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000734 if (ret < 0) {
735 dev_err(component->dev,
736 "ASoC: platform prepare error: %d\n", ret);
737 goto out;
738 }
739 }
740
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000741 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900742 ret = snd_soc_dai_prepare(codec_dai, substream);
743 if (ret < 0) {
744 dev_err(codec_dai->dev,
745 "ASoC: codec DAI prepare error: %d\n",
746 ret);
747 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100748 }
749 }
750
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900751 ret = snd_soc_dai_prepare(cpu_dai, substream);
752 if (ret < 0) {
753 dev_err(cpu_dai->dev,
754 "ASoC: cpu DAI prepare error: %d\n", ret);
755 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756 }
757
758 /* cancel any delayed stream shutdown that is pending */
759 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600760 rtd->pop_wait) {
761 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100762 cancel_delayed_work(&rtd->delayed_work);
763 }
764
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000765 snd_soc_dapm_stream_event(rtd, substream->stream,
766 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100767
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000768 for_each_rtd_codec_dai(rtd, i, codec_dai)
769 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200770 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530771 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100772
773out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300774 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100775 return ret;
776}
777
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200778static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
779 unsigned int mask)
780{
781 struct snd_interval *interval;
782 int channels = hweight_long(mask);
783
784 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
785 interval->min = channels;
786 interval->max = channels;
787}
788
Charles Keepax244e2932018-06-19 16:22:09 +0100789static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
790 struct snd_soc_component *last)
791{
792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100793 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900794 int i, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100795
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900796 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100797 if (component == last)
798 break;
799
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900800 ret |= snd_soc_component_hw_free(component, substream);
Charles Keepax244e2932018-06-19 16:22:09 +0100801 }
802
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900803 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100804}
805
Liam Girdwoodddee6272011-06-09 14:45:53 +0100806/*
807 * Called by ALSA when the hardware params are set by application. This
808 * function can also be called multiple times and can allocate buffers
809 * (using snd_pcm_lib_* ). It's non-atomic.
810 */
811static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
812 struct snd_pcm_hw_params *params)
813{
814 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000815 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100816 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000817 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100818 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100819
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300820 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800821
822 ret = soc_pcm_params_symmetry(substream, params);
823 if (ret)
824 goto out;
825
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000826 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100827 ret = rtd->dai_link->ops->hw_params(substream, params);
828 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000829 dev_err(rtd->card->dev, "ASoC: machine hw_params"
830 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100831 goto out;
832 }
833 }
834
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000835 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200836 struct snd_pcm_hw_params codec_params;
837
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200838 /*
839 * Skip CODECs which don't support the current stream type,
840 * the idea being that if a CODEC is not used for the currently
841 * set up transfer direction, it should not need to be
842 * configured, especially since the configuration used might
843 * not even be supported by that CODEC. There may be cases
844 * however where a CODEC needs to be set up although it is
845 * actually not being used for the transfer, e.g. if a
846 * capture-only CODEC is acting as an LRCLK and/or BCLK master
847 * for the DAI link including a playback-only CODEC.
848 * If this becomes necessary, we will have to augment the
849 * machine driver setup with information on how to act, so
850 * we can do the right thing here.
851 */
852 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
853 continue;
854
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200855 /* copy params for each codec */
856 codec_params = *params;
857
858 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800859 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
860 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200861 soc_pcm_codec_params_fixup(&codec_params,
862 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800863
864 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
865 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200866 soc_pcm_codec_params_fixup(&codec_params,
867 codec_dai->rx_mask);
868
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900869 ret = snd_soc_dai_hw_params(codec_dai, substream,
870 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200871 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100872 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200873
874 codec_dai->rate = params_rate(&codec_params);
875 codec_dai->channels = params_channels(&codec_params);
876 codec_dai->sample_bits = snd_pcm_format_physical_width(
877 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000878
879 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100880 }
881
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900882 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200883 if (ret < 0)
884 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100885
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900886 /* store the parameters for each DAIs */
887 cpu_dai->rate = params_rate(params);
888 cpu_dai->channels = params_channels(params);
889 cpu_dai->sample_bits =
890 snd_pcm_format_physical_width(params_format(params));
891
892 snd_soc_dapm_update_dai(substream, params, cpu_dai);
893
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900894 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900895 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100896 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000897 dev_err(component->dev,
898 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100899 component->name, ret);
900 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000901 }
902 }
Charles Keepax244e2932018-06-19 16:22:09 +0100903 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000904
Liam Girdwoodddee6272011-06-09 14:45:53 +0100905out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300906 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100907 return ret;
908
Kuninori Morimotob8135862017-10-11 01:37:23 +0000909component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100910 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000911
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900912 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900913 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100914
915interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200916 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100917
918codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000919 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200920 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
921 continue;
922
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900923 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200924 codec_dai->rate = 0;
925 }
926
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000927 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100928 rtd->dai_link->ops->hw_free(substream);
929
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300930 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931 return ret;
932}
933
934/*
935 * Frees resources allocated by hw_params, can be called multiple times
936 */
937static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
938{
939 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100940 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200941 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800942 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200943 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100944
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300945 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946
Nicolin Chend3383422013-11-20 18:37:09 +0800947 /* clear the corresponding DAIs parameters when going to be inactive */
948 if (cpu_dai->active == 1) {
949 cpu_dai->rate = 0;
950 cpu_dai->channels = 0;
951 cpu_dai->sample_bits = 0;
952 }
953
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000954 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200955 if (codec_dai->active == 1) {
956 codec_dai->rate = 0;
957 codec_dai->channels = 0;
958 codec_dai->sample_bits = 0;
959 }
Nicolin Chend3383422013-11-20 18:37:09 +0800960 }
961
Liam Girdwoodddee6272011-06-09 14:45:53 +0100962 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000963 for_each_rtd_codec_dai(rtd, i, codec_dai) {
964 if ((playback && codec_dai->playback_active == 1) ||
965 (!playback && codec_dai->capture_active == 1))
966 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200967 substream->stream);
968 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100969
970 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000971 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100972 rtd->dai_link->ops->hw_free(substream);
973
Kuninori Morimotob8135862017-10-11 01:37:23 +0000974 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +0100975 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000976
Liam Girdwoodddee6272011-06-09 14:45:53 +0100977 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000978 for_each_rtd_codec_dai(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 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100984
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900985 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100986
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300987 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100988 return 0;
989}
990
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +0300991static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100992{
993 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000994 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100995 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200996 struct snd_soc_dai *codec_dai;
997 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100998
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +0300999 if (rtd->dai_link->ops->trigger) {
1000 ret = rtd->dai_link->ops->trigger(substream, cmd);
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001001 if (ret < 0)
1002 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003 }
1004
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001005 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001006 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001007 if (ret < 0)
1008 return ret;
1009 }
1010
Dan Carpenter901e8222019-09-23 17:22:57 +03001011 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001012 if (ret < 0)
1013 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001014
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001015 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1016 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1017 if (ret < 0)
1018 return ret;
1019 }
1020
1021 return 0;
1022}
1023
1024static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1025{
1026 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1027 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001028 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1029 struct snd_soc_dai *codec_dai;
1030 int i, ret;
1031
1032 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1033 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1034 if (ret < 0)
1035 return ret;
1036 }
1037
1038 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1039 if (ret < 0)
1040 return ret;
1041
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001042 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001043 ret = snd_soc_component_trigger(component, substream, cmd);
1044 if (ret < 0)
1045 return ret;
1046 }
1047
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001048 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001049 ret = rtd->dai_link->ops->trigger(substream, cmd);
1050 if (ret < 0)
1051 return ret;
1052 }
1053
Liam Girdwoodddee6272011-06-09 14:45:53 +01001054 return 0;
1055}
1056
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001057static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1058{
1059 int ret;
1060
1061 switch (cmd) {
1062 case SNDRV_PCM_TRIGGER_START:
1063 case SNDRV_PCM_TRIGGER_RESUME:
1064 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1065 ret = soc_pcm_trigger_start(substream, cmd);
1066 break;
1067 case SNDRV_PCM_TRIGGER_STOP:
1068 case SNDRV_PCM_TRIGGER_SUSPEND:
1069 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1070 ret = soc_pcm_trigger_stop(substream, cmd);
1071 break;
1072 default:
1073 return -EINVAL;
1074 }
1075
1076 return ret;
1077}
1078
Mark Brown45c0a182012-05-09 21:46:27 +01001079static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1080 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001081{
1082 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001083 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001084 struct snd_soc_dai *codec_dai;
1085 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001086
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001087 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001088 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001089 if (ret < 0)
1090 return ret;
1091 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001092
Dan Carpenter901e8222019-09-23 17:22:57 +03001093 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001094 if (ret < 0)
1095 return ret;
1096
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001097 return 0;
1098}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001099/*
1100 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001101 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001102 * the runtime->delay will be updated accordingly.
1103 */
1104static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1105{
1106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001107 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001108 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 snd_pcm_uframes_t offset = 0;
1111 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001112 snd_pcm_sframes_t codec_delay = 0;
1113 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001114
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301115 /* clearing the previous total delay */
1116 runtime->delay = 0;
1117
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001118 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001119
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301120 /* base delay if assigned in pointer callback */
1121 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001122
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001123 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001124
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001125 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001126 codec_delay = max(codec_delay,
1127 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001128 }
1129 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001130
Liam Girdwoodddee6272011-06-09 14:45:53 +01001131 runtime->delay = delay;
1132
1133 return offset;
1134}
1135
Liam Girdwood01d75842012-04-25 12:12:49 +01001136/* connect a FE and BE */
1137static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1138 struct snd_soc_pcm_runtime *be, int stream)
1139{
1140 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001141 unsigned long flags;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001142#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001143 char *name;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001144#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001145
1146 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001147 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001148 if (dpcm->be == be && dpcm->fe == fe)
1149 return 0;
1150 }
1151
1152 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1153 if (!dpcm)
1154 return -ENOMEM;
1155
1156 dpcm->be = be;
1157 dpcm->fe = fe;
1158 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1159 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001160 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001161 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1162 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001163 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001164
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001165 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001166 stream ? "capture" : "playback", fe->dai_link->name,
1167 stream ? "<-" : "->", be->dai_link->name);
1168
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001169#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001170 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1171 stream ? "capture" : "playback");
1172 if (name) {
1173 dpcm->debugfs_state = debugfs_create_dir(name,
1174 fe->debugfs_dpcm_root);
1175 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1176 &dpcm->state);
1177 kfree(name);
1178 }
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001179#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001180 return 1;
1181}
1182
1183/* reparent a BE onto another FE */
1184static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1185 struct snd_soc_pcm_runtime *be, int stream)
1186{
1187 struct snd_soc_dpcm *dpcm;
1188 struct snd_pcm_substream *fe_substream, *be_substream;
1189
1190 /* reparent if BE is connected to other FEs */
1191 if (!be->dpcm[stream].users)
1192 return;
1193
1194 be_substream = snd_soc_dpcm_get_substream(be, stream);
1195
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001196 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001197 if (dpcm->fe == fe)
1198 continue;
1199
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001200 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001201 stream ? "capture" : "playback",
1202 dpcm->fe->dai_link->name,
1203 stream ? "<-" : "->", dpcm->be->dai_link->name);
1204
1205 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1206 be_substream->runtime = fe_substream->runtime;
1207 break;
1208 }
1209}
1210
1211/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001212void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001213{
1214 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001215 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001216
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001217 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001218 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001219 stream ? "capture" : "playback",
1220 dpcm->be->dai_link->name);
1221
1222 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1223 continue;
1224
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001225 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001226 stream ? "capture" : "playback", fe->dai_link->name,
1227 stream ? "<-" : "->", dpcm->be->dai_link->name);
1228
1229 /* BEs still alive need new FE */
1230 dpcm_be_reparent(fe, dpcm->be, stream);
1231
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001232#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001233 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001234#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001235 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001236 list_del(&dpcm->list_be);
1237 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001238 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001239 kfree(dpcm);
1240 }
1241}
1242
1243/* get BE for DAI widget and stream */
1244static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1245 struct snd_soc_dapm_widget *widget, int stream)
1246{
1247 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001248 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001249 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001250
Liam Girdwood3c146462018-03-14 20:43:51 +00001251 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1252
Liam Girdwood01d75842012-04-25 12:12:49 +01001253 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001254 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001255
Liam Girdwood35ea0652012-06-05 19:26:59 +01001256 if (!be->dai_link->no_pcm)
1257 continue;
1258
Liam Girdwood3c146462018-03-14 20:43:51 +00001259 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1260 be->cpu_dai->playback_widget ?
1261 be->cpu_dai->playback_widget->name : "(not set)");
1262
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001263 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001264 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001265
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001266 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001267 if (dai->playback_widget == widget)
1268 return be;
1269 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001270 }
1271 } else {
1272
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001273 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001274
Liam Girdwood35ea0652012-06-05 19:26:59 +01001275 if (!be->dai_link->no_pcm)
1276 continue;
1277
Liam Girdwood3c146462018-03-14 20:43:51 +00001278 dev_dbg(card->dev, "ASoC: try BE %s\n",
1279 be->cpu_dai->capture_widget ?
1280 be->cpu_dai->capture_widget->name : "(not set)");
1281
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001282 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001283 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001284
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001285 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001286 if (dai->capture_widget == widget)
1287 return be;
1288 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001289 }
1290 }
1291
Liam Girdwood3c146462018-03-14 20:43:51 +00001292 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001293 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001294 stream ? "capture" : "playback", widget->name);
1295 return NULL;
1296}
1297
1298static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001299 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001300{
1301 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001302 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001303 else
Benoit Cousson37018612014-04-24 14:01:45 +02001304 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001305}
1306
1307static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1308 struct snd_soc_dapm_widget *widget)
1309{
1310 int i;
1311
1312 for (i = 0; i < list->num_widgets; i++) {
1313 if (widget == list->widgets[i])
1314 return 1;
1315 }
1316
1317 return 0;
1318}
1319
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001320static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1321 enum snd_soc_dapm_direction dir)
1322{
1323 struct snd_soc_card *card = widget->dapm->card;
1324 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001325 struct snd_soc_dai *dai;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001326 int i;
1327
1328 if (dir == SND_SOC_DAPM_DIR_OUT) {
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001329 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001330 if (!rtd->dai_link->no_pcm)
1331 continue;
1332
1333 if (rtd->cpu_dai->playback_widget == widget)
1334 return true;
1335
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001336 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001337 if (dai->playback_widget == widget)
1338 return true;
1339 }
1340 }
1341 } else { /* SND_SOC_DAPM_DIR_IN */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001342 for_each_card_rtds(card, rtd) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001343 if (!rtd->dai_link->no_pcm)
1344 continue;
1345
1346 if (rtd->cpu_dai->capture_widget == widget)
1347 return true;
1348
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001349 for_each_rtd_codec_dai(rtd, i, dai) {
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001350 if (dai->capture_widget == widget)
1351 return true;
1352 }
1353 }
1354 }
1355
1356 return false;
1357}
1358
Liam Girdwood23607022014-01-17 17:03:55 +00001359int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001360 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001361{
1362 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001363 int paths;
1364
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001366 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001367 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001368
Liam Girdwood103d84a2012-11-19 14:39:15 +00001369 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001370 stream ? "capture" : "playback");
1371
Liam Girdwood01d75842012-04-25 12:12:49 +01001372 return paths;
1373}
1374
Liam Girdwood01d75842012-04-25 12:12:49 +01001375static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1376 struct snd_soc_dapm_widget_list **list_)
1377{
1378 struct snd_soc_dpcm *dpcm;
1379 struct snd_soc_dapm_widget_list *list = *list_;
1380 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001381 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001382 int prune = 0;
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001383 int do_prune;
Liam Girdwood01d75842012-04-25 12:12:49 +01001384
1385 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001386 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001387 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001388
1389 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001390 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001391
1392 /* prune the BE if it's no longer in our active list */
1393 if (widget && widget_in_list(list, widget))
1394 continue;
1395
1396 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001397 do_prune = 1;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001398 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001399 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001400
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001401 /* prune the BE if it's no longer in our active list */
1402 if (widget && widget_in_list(list, widget))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001403 do_prune = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001404 }
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001405 if (!do_prune)
1406 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001407
Liam Girdwood103d84a2012-11-19 14:39:15 +00001408 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001409 stream ? "capture" : "playback",
1410 dpcm->be->dai_link->name, fe->dai_link->name);
1411 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1412 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1413 prune++;
1414 }
1415
Liam Girdwood103d84a2012-11-19 14:39:15 +00001416 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001417 return prune;
1418}
1419
1420static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1421 struct snd_soc_dapm_widget_list **list_)
1422{
1423 struct snd_soc_card *card = fe->card;
1424 struct snd_soc_dapm_widget_list *list = *list_;
1425 struct snd_soc_pcm_runtime *be;
1426 int i, new = 0, err;
1427
1428 /* Create any new FE <--> BE connections */
1429 for (i = 0; i < list->num_widgets; i++) {
1430
Mark Brown46162742013-06-05 19:36:11 +01001431 switch (list->widgets[i]->id) {
1432 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001433 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1434 continue;
1435 break;
Mark Brown46162742013-06-05 19:36:11 +01001436 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001437 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1438 continue;
Mark Brown46162742013-06-05 19:36:11 +01001439 break;
1440 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001441 continue;
Mark Brown46162742013-06-05 19:36:11 +01001442 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001443
1444 /* is there a valid BE rtd for this widget */
1445 be = dpcm_get_be(card, list->widgets[i], stream);
1446 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001447 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001448 list->widgets[i]->name);
1449 continue;
1450 }
1451
1452 /* make sure BE is a real BE */
1453 if (!be->dai_link->no_pcm)
1454 continue;
1455
1456 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001457 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001458 continue;
1459
1460 /* newly connected FE and BE */
1461 err = dpcm_be_connect(fe, be, stream);
1462 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001463 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001464 list->widgets[i]->name);
1465 break;
1466 } else if (err == 0) /* already connected */
1467 continue;
1468
1469 /* new */
1470 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1471 new++;
1472 }
1473
Liam Girdwood103d84a2012-11-19 14:39:15 +00001474 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001475 return new;
1476}
1477
1478/*
1479 * Find the corresponding BE DAIs that source or sink audio to this
1480 * FE substream.
1481 */
Liam Girdwood23607022014-01-17 17:03:55 +00001482int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001483 int stream, struct snd_soc_dapm_widget_list **list, int new)
1484{
1485 if (new)
1486 return dpcm_add_paths(fe, stream, list);
1487 else
1488 return dpcm_prune_paths(fe, stream, list);
1489}
1490
Liam Girdwood23607022014-01-17 17:03:55 +00001491void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001492{
1493 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001494 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001495
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001496 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001497 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001498 dpcm->be->dpcm[stream].runtime_update =
1499 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001500 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001501}
1502
1503static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1504 int stream)
1505{
1506 struct snd_soc_dpcm *dpcm;
1507
1508 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001509 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001510
1511 struct snd_soc_pcm_runtime *be = dpcm->be;
1512 struct snd_pcm_substream *be_substream =
1513 snd_soc_dpcm_get_substream(be, stream);
1514
1515 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001516 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001517 stream ? "capture" : "playback",
1518 be->dpcm[stream].state);
1519
1520 if (--be->dpcm[stream].users != 0)
1521 continue;
1522
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1524 continue;
1525
1526 soc_pcm_close(be_substream);
1527 be_substream->runtime = NULL;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1529 }
1530}
1531
Liam Girdwood23607022014-01-17 17:03:55 +00001532int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001533{
1534 struct snd_soc_dpcm *dpcm;
1535 int err, count = 0;
1536
1537 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001538 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001539
1540 struct snd_soc_pcm_runtime *be = dpcm->be;
1541 struct snd_pcm_substream *be_substream =
1542 snd_soc_dpcm_get_substream(be, stream);
1543
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001544 if (!be_substream) {
1545 dev_err(be->dev, "ASoC: no backend %s stream\n",
1546 stream ? "capture" : "playback");
1547 continue;
1548 }
1549
Liam Girdwood01d75842012-04-25 12:12:49 +01001550 /* is this op for this BE ? */
1551 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1552 continue;
1553
1554 /* first time the dpcm is open ? */
1555 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001556 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001557 stream ? "capture" : "playback",
1558 be->dpcm[stream].state);
1559
1560 if (be->dpcm[stream].users++ != 0)
1561 continue;
1562
1563 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1564 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1565 continue;
1566
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001567 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1568 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001569
1570 be_substream->runtime = be->dpcm[stream].runtime;
1571 err = soc_pcm_open(be_substream);
1572 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001573 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001574 be->dpcm[stream].users--;
1575 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001576 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001577 stream ? "capture" : "playback",
1578 be->dpcm[stream].state);
1579
1580 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1581 goto unwind;
1582 }
1583
1584 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1585 count++;
1586 }
1587
1588 return count;
1589
1590unwind:
1591 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001592 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001593 struct snd_soc_pcm_runtime *be = dpcm->be;
1594 struct snd_pcm_substream *be_substream =
1595 snd_soc_dpcm_get_substream(be, stream);
1596
1597 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1598 continue;
1599
1600 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001601 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001602 stream ? "capture" : "playback",
1603 be->dpcm[stream].state);
1604
1605 if (--be->dpcm[stream].users != 0)
1606 continue;
1607
1608 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1609 continue;
1610
1611 soc_pcm_close(be_substream);
1612 be_substream->runtime = NULL;
1613 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1614 }
1615
1616 return err;
1617}
1618
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001619static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001620 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001621{
1622 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001623 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001624 runtime->hw.channels_min = stream->channels_min;
1625 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001626 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001627 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001628 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001629 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001630 runtime->hw.rates = stream->rates;
1631}
1632
Jerome Brunet435ffb72018-07-05 12:13:48 +02001633static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1634 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001635{
1636 struct snd_soc_pcm_runtime *fe = substream->private_data;
1637 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001638 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001639 int stream = substream->stream;
1640
1641 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001642 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001643
1644 /*
1645 * It returns merged BE codec format
1646 * if FE want to use it (= dpcm_merged_format)
1647 */
1648
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001649 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001650 struct snd_soc_pcm_runtime *be = dpcm->be;
1651 struct snd_soc_dai_driver *codec_dai_drv;
1652 struct snd_soc_pcm_stream *codec_stream;
1653 int i;
1654
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001655 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001656 /*
1657 * Skip CODECs which don't support the current stream
1658 * type. See soc_pcm_init_runtime_hw() for more details
1659 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001660 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001661 continue;
1662
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001663 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001664 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1665 codec_stream = &codec_dai_drv->playback;
1666 else
1667 codec_stream = &codec_dai_drv->capture;
1668
Jerome Brunet435ffb72018-07-05 12:13:48 +02001669 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001670 }
1671 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001672}
1673
Jerome Brunet435ffb72018-07-05 12:13:48 +02001674static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1675 unsigned int *channels_min,
1676 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001677{
1678 struct snd_soc_pcm_runtime *fe = substream->private_data;
1679 struct snd_soc_dpcm *dpcm;
1680 int stream = substream->stream;
1681
1682 if (!fe->dai_link->dpcm_merged_chan)
1683 return;
1684
Jiada Wangf4c277b2018-06-20 18:25:20 +09001685 /*
1686 * It returns merged BE codec channel;
1687 * if FE want to use it (= dpcm_merged_chan)
1688 */
1689
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001690 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001691 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001692 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001693 struct snd_soc_dai_driver *codec_dai_drv;
1694 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001695 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001696
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001697 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1698 cpu_stream = &cpu_dai_drv->playback;
1699 else
1700 cpu_stream = &cpu_dai_drv->capture;
1701
1702 *channels_min = max(*channels_min, cpu_stream->channels_min);
1703 *channels_max = min(*channels_max, cpu_stream->channels_max);
1704
1705 /*
1706 * chan min/max cannot be enforced if there are multiple CODEC
1707 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1708 */
1709 if (be->num_codecs == 1) {
1710 codec_dai_drv = be->codec_dais[0]->driver;
1711
Jiada Wangf4c277b2018-06-20 18:25:20 +09001712 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1713 codec_stream = &codec_dai_drv->playback;
1714 else
1715 codec_stream = &codec_dai_drv->capture;
1716
1717 *channels_min = max(*channels_min,
1718 codec_stream->channels_min);
1719 *channels_max = min(*channels_max,
1720 codec_stream->channels_max);
1721 }
1722 }
1723}
1724
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001725static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1726 unsigned int *rates,
1727 unsigned int *rate_min,
1728 unsigned int *rate_max)
1729{
1730 struct snd_soc_pcm_runtime *fe = substream->private_data;
1731 struct snd_soc_dpcm *dpcm;
1732 int stream = substream->stream;
1733
1734 if (!fe->dai_link->dpcm_merged_rate)
1735 return;
1736
1737 /*
1738 * It returns merged BE codec channel;
1739 * if FE want to use it (= dpcm_merged_chan)
1740 */
1741
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001742 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001743 struct snd_soc_pcm_runtime *be = dpcm->be;
1744 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1745 struct snd_soc_dai_driver *codec_dai_drv;
1746 struct snd_soc_pcm_stream *codec_stream;
1747 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001748 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001749 int i;
1750
1751 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1752 cpu_stream = &cpu_dai_drv->playback;
1753 else
1754 cpu_stream = &cpu_dai_drv->capture;
1755
1756 *rate_min = max(*rate_min, cpu_stream->rate_min);
1757 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1758 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1759
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001760 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001761 /*
1762 * Skip CODECs which don't support the current stream
1763 * type. See soc_pcm_init_runtime_hw() for more details
1764 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001765 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001766 continue;
1767
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001768 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001769 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1770 codec_stream = &codec_dai_drv->playback;
1771 else
1772 codec_stream = &codec_dai_drv->capture;
1773
1774 *rate_min = max(*rate_min, codec_stream->rate_min);
1775 *rate_max = min_not_zero(*rate_max,
1776 codec_stream->rate_max);
1777 *rates = snd_pcm_rate_mask_intersect(*rates,
1778 codec_stream->rates);
1779 }
1780 }
1781}
1782
Mark Brown45c0a182012-05-09 21:46:27 +01001783static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001784{
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1786 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1787 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1788 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1789
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001790 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001791 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001792 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001793 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001794
Jerome Brunet435ffb72018-07-05 12:13:48 +02001795 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1796 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1797 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001798 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1799 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001800}
1801
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001802static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1803
1804/* Set FE's runtime_update state; the state is protected via PCM stream lock
1805 * for avoiding the race with trigger callback.
1806 * If the state is unset and a trigger is pending while the previous operation,
1807 * process the pending trigger action here.
1808 */
1809static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1810 int stream, enum snd_soc_dpcm_update state)
1811{
1812 struct snd_pcm_substream *substream =
1813 snd_soc_dpcm_get_substream(fe, stream);
1814
1815 snd_pcm_stream_lock_irq(substream);
1816 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1817 dpcm_fe_dai_do_trigger(substream,
1818 fe->dpcm[stream].trigger_pending - 1);
1819 fe->dpcm[stream].trigger_pending = 0;
1820 }
1821 fe->dpcm[stream].runtime_update = state;
1822 snd_pcm_stream_unlock_irq(substream);
1823}
1824
PC Liao906c7d62015-12-11 11:33:51 +08001825static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1826 int stream)
1827{
1828 struct snd_soc_dpcm *dpcm;
1829 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1830 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1831 int err;
1832
1833 /* apply symmetry for FE */
1834 if (soc_pcm_has_symmetry(fe_substream))
1835 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1836
1837 /* Symmetry only applies if we've got an active stream. */
1838 if (fe_cpu_dai->active) {
1839 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1840 if (err < 0)
1841 return err;
1842 }
1843
1844 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001845 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001846 struct snd_soc_pcm_runtime *be = dpcm->be;
1847 struct snd_pcm_substream *be_substream =
1848 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001849 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001850 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001851 int i;
1852
Jerome Brunet6246f282019-04-01 15:03:54 +02001853 /* A backend may not have the requested substream */
1854 if (!be_substream)
1855 continue;
1856
1857 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301858 if (rtd->dai_link->be_hw_params_fixup)
1859 continue;
1860
PC Liao906c7d62015-12-11 11:33:51 +08001861 if (soc_pcm_has_symmetry(be_substream))
1862 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1863
1864 /* Symmetry only applies if we've got an active stream. */
1865 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001866 err = soc_pcm_apply_symmetry(fe_substream,
1867 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001868 if (err < 0)
1869 return err;
1870 }
1871
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001872 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1873 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001874 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001875 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001876 if (err < 0)
1877 return err;
1878 }
1879 }
1880 }
1881
1882 return 0;
1883}
1884
Liam Girdwood01d75842012-04-25 12:12:49 +01001885static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1886{
1887 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1888 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1889 int stream = fe_substream->stream, ret = 0;
1890
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001891 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001892
1893 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1894 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001895 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001896 goto be_err;
1897 }
1898
Liam Girdwood103d84a2012-11-19 14:39:15 +00001899 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001900
1901 /* start the DAI frontend */
1902 ret = soc_pcm_open(fe_substream);
1903 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001904 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001905 goto unwind;
1906 }
1907
1908 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1909
1910 dpcm_set_fe_runtime(fe_substream);
1911 snd_pcm_limit_hw_rates(runtime);
1912
PC Liao906c7d62015-12-11 11:33:51 +08001913 ret = dpcm_apply_symmetry(fe_substream, stream);
1914 if (ret < 0) {
1915 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1916 ret);
1917 goto unwind;
1918 }
1919
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001920 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001921 return 0;
1922
1923unwind:
1924 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1925be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001926 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001927 return ret;
1928}
1929
Liam Girdwood23607022014-01-17 17:03:55 +00001930int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001931{
1932 struct snd_soc_dpcm *dpcm;
1933
1934 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001935 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001936
1937 struct snd_soc_pcm_runtime *be = dpcm->be;
1938 struct snd_pcm_substream *be_substream =
1939 snd_soc_dpcm_get_substream(be, stream);
1940
1941 /* is this op for this BE ? */
1942 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1943 continue;
1944
1945 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001946 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001947 stream ? "capture" : "playback",
1948 be->dpcm[stream].state);
1949
1950 if (--be->dpcm[stream].users != 0)
1951 continue;
1952
1953 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001954 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1955 soc_pcm_hw_free(be_substream);
1956 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1957 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001958
Liam Girdwood103d84a2012-11-19 14:39:15 +00001959 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001960 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001961
1962 soc_pcm_close(be_substream);
1963 be_substream->runtime = NULL;
1964
1965 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1966 }
1967 return 0;
1968}
1969
1970static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1971{
1972 struct snd_soc_pcm_runtime *fe = substream->private_data;
1973 int stream = substream->stream;
1974
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001975 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001976
1977 /* shutdown the BEs */
1978 dpcm_be_dai_shutdown(fe, substream->stream);
1979
Liam Girdwood103d84a2012-11-19 14:39:15 +00001980 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001981
1982 /* now shutdown the frontend */
1983 soc_pcm_close(substream);
1984
1985 /* run the stream event for each BE */
1986 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1987
1988 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001989 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001990 return 0;
1991}
1992
Liam Girdwood23607022014-01-17 17:03:55 +00001993int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001994{
1995 struct snd_soc_dpcm *dpcm;
1996
1997 /* only hw_params backends that are either sinks or sources
1998 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001999 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002000
2001 struct snd_soc_pcm_runtime *be = dpcm->be;
2002 struct snd_pcm_substream *be_substream =
2003 snd_soc_dpcm_get_substream(be, stream);
2004
2005 /* is this op for this BE ? */
2006 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2007 continue;
2008
2009 /* only free hw when no longer used - check all FEs */
2010 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2011 continue;
2012
Qiao Zhou36fba622014-12-03 10:13:43 +08002013 /* do not free hw if this BE is used by other FE */
2014 if (be->dpcm[stream].users > 1)
2015 continue;
2016
Liam Girdwood01d75842012-04-25 12:12:49 +01002017 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2018 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2019 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302021 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2022 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002023 continue;
2024
Liam Girdwood103d84a2012-11-19 14:39:15 +00002025 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002026 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002027
2028 soc_pcm_hw_free(be_substream);
2029
2030 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2031 }
2032
2033 return 0;
2034}
2035
Mark Brown45c0a182012-05-09 21:46:27 +01002036static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002037{
2038 struct snd_soc_pcm_runtime *fe = substream->private_data;
2039 int err, stream = substream->stream;
2040
2041 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002042 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002043
Liam Girdwood103d84a2012-11-19 14:39:15 +00002044 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002045
2046 /* call hw_free on the frontend */
2047 err = soc_pcm_hw_free(substream);
2048 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002049 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002050 fe->dai_link->name);
2051
2052 /* only hw_params backends that are either sinks or sources
2053 * to this frontend DAI */
2054 err = dpcm_be_dai_hw_free(fe, stream);
2055
2056 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002057 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002058
2059 mutex_unlock(&fe->card->mutex);
2060 return 0;
2061}
2062
Liam Girdwood23607022014-01-17 17:03:55 +00002063int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002064{
2065 struct snd_soc_dpcm *dpcm;
2066 int ret;
2067
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002068 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002069
2070 struct snd_soc_pcm_runtime *be = dpcm->be;
2071 struct snd_pcm_substream *be_substream =
2072 snd_soc_dpcm_get_substream(be, stream);
2073
2074 /* is this op for this BE ? */
2075 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2076 continue;
2077
Liam Girdwood01d75842012-04-25 12:12:49 +01002078 /* copy params for each dpcm */
2079 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2080 sizeof(struct snd_pcm_hw_params));
2081
2082 /* perform any hw_params fixups */
2083 if (be->dai_link->be_hw_params_fixup) {
2084 ret = be->dai_link->be_hw_params_fixup(be,
2085 &dpcm->hw_params);
2086 if (ret < 0) {
2087 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002088 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002089 ret);
2090 goto unwind;
2091 }
2092 }
2093
Libin Yangae061d22019-04-19 09:53:12 +08002094 /* copy the fixed-up hw params for BE dai */
2095 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2096 sizeof(struct snd_pcm_hw_params));
2097
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002098 /* only allow hw_params() if no connected FEs are running */
2099 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2100 continue;
2101
2102 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2103 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2104 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2105 continue;
2106
2107 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002108 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002109
Liam Girdwood01d75842012-04-25 12:12:49 +01002110 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2111 if (ret < 0) {
2112 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002113 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002114 goto unwind;
2115 }
2116
2117 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2118 }
2119 return 0;
2120
2121unwind:
2122 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002123 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002124 struct snd_soc_pcm_runtime *be = dpcm->be;
2125 struct snd_pcm_substream *be_substream =
2126 snd_soc_dpcm_get_substream(be, stream);
2127
2128 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2129 continue;
2130
2131 /* only allow hw_free() if no connected FEs are running */
2132 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2133 continue;
2134
2135 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2136 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2137 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2138 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2139 continue;
2140
2141 soc_pcm_hw_free(be_substream);
2142 }
2143
2144 return ret;
2145}
2146
Mark Brown45c0a182012-05-09 21:46:27 +01002147static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2148 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002149{
2150 struct snd_soc_pcm_runtime *fe = substream->private_data;
2151 int ret, stream = substream->stream;
2152
2153 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002154 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002155
2156 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2157 sizeof(struct snd_pcm_hw_params));
2158 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2159 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002160 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002161 goto out;
2162 }
2163
Liam Girdwood103d84a2012-11-19 14:39:15 +00002164 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002165 fe->dai_link->name, params_rate(params),
2166 params_channels(params), params_format(params));
2167
2168 /* call hw_params on the frontend */
2169 ret = soc_pcm_hw_params(substream, params);
2170 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002171 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002172 dpcm_be_dai_hw_free(fe, stream);
2173 } else
2174 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2175
2176out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002177 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002178 mutex_unlock(&fe->card->mutex);
2179 return ret;
2180}
2181
2182static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2183 struct snd_pcm_substream *substream, int cmd)
2184{
2185 int ret;
2186
Liam Girdwood103d84a2012-11-19 14:39:15 +00002187 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002188 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002189
2190 ret = soc_pcm_trigger(substream, cmd);
2191 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002192 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002193
2194 return ret;
2195}
2196
Liam Girdwood23607022014-01-17 17:03:55 +00002197int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002198 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002199{
2200 struct snd_soc_dpcm *dpcm;
2201 int ret = 0;
2202
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002203 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002204
2205 struct snd_soc_pcm_runtime *be = dpcm->be;
2206 struct snd_pcm_substream *be_substream =
2207 snd_soc_dpcm_get_substream(be, stream);
2208
2209 /* is this op for this BE ? */
2210 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2211 continue;
2212
2213 switch (cmd) {
2214 case SNDRV_PCM_TRIGGER_START:
2215 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2216 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2217 continue;
2218
2219 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2220 if (ret)
2221 return ret;
2222
2223 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2224 break;
2225 case SNDRV_PCM_TRIGGER_RESUME:
2226 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2227 continue;
2228
2229 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2230 if (ret)
2231 return ret;
2232
2233 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2234 break;
2235 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2236 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2237 continue;
2238
2239 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2240 if (ret)
2241 return ret;
2242
2243 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2244 break;
2245 case SNDRV_PCM_TRIGGER_STOP:
2246 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2247 continue;
2248
2249 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2250 continue;
2251
2252 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2253 if (ret)
2254 return ret;
2255
2256 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2257 break;
2258 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002259 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002260 continue;
2261
2262 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2263 continue;
2264
2265 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2266 if (ret)
2267 return ret;
2268
2269 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2270 break;
2271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2272 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2273 continue;
2274
2275 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2276 continue;
2277
2278 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2279 if (ret)
2280 return ret;
2281
2282 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2283 break;
2284 }
2285 }
2286
2287 return ret;
2288}
2289EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2290
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002291static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2292 int cmd, bool fe_first)
2293{
2294 struct snd_soc_pcm_runtime *fe = substream->private_data;
2295 int ret;
2296
2297 /* call trigger on the frontend before the backend. */
2298 if (fe_first) {
2299 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2300 fe->dai_link->name, cmd);
2301
2302 ret = soc_pcm_trigger(substream, cmd);
2303 if (ret < 0)
2304 return ret;
2305
2306 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2307 return ret;
2308 }
2309
2310 /* call trigger on the frontend after the backend. */
2311 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2312 if (ret < 0)
2313 return ret;
2314
2315 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2316 fe->dai_link->name, cmd);
2317
2318 ret = soc_pcm_trigger(substream, cmd);
2319
2320 return ret;
2321}
2322
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002323static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002324{
2325 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002326 int stream = substream->stream;
2327 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002328 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2329
2330 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2331
2332 switch (trigger) {
2333 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002334 switch (cmd) {
2335 case SNDRV_PCM_TRIGGER_START:
2336 case SNDRV_PCM_TRIGGER_RESUME:
2337 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2338 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2339 break;
2340 case SNDRV_PCM_TRIGGER_STOP:
2341 case SNDRV_PCM_TRIGGER_SUSPEND:
2342 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2343 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2344 break;
2345 default:
2346 ret = -EINVAL;
2347 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002348 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002349 break;
2350 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002351 switch (cmd) {
2352 case SNDRV_PCM_TRIGGER_START:
2353 case SNDRV_PCM_TRIGGER_RESUME:
2354 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2355 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2356 break;
2357 case SNDRV_PCM_TRIGGER_STOP:
2358 case SNDRV_PCM_TRIGGER_SUSPEND:
2359 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2360 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2361 break;
2362 default:
2363 ret = -EINVAL;
2364 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002365 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002366 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002367 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2368 /* bespoke trigger() - handles both FE and BEs */
2369
Liam Girdwood103d84a2012-11-19 14:39:15 +00002370 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002371 fe->dai_link->name, cmd);
2372
2373 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002374 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002375 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002376 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002377 fe->dai_link->name);
2378 ret = -EINVAL;
2379 goto out;
2380 }
2381
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002382 if (ret < 0) {
2383 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2384 cmd, ret);
2385 goto out;
2386 }
2387
Liam Girdwood01d75842012-04-25 12:12:49 +01002388 switch (cmd) {
2389 case SNDRV_PCM_TRIGGER_START:
2390 case SNDRV_PCM_TRIGGER_RESUME:
2391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2393 break;
2394 case SNDRV_PCM_TRIGGER_STOP:
2395 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002396 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2397 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002398 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2399 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2400 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002401 }
2402
2403out:
2404 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2405 return ret;
2406}
2407
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002408static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2409{
2410 struct snd_soc_pcm_runtime *fe = substream->private_data;
2411 int stream = substream->stream;
2412
2413 /* if FE's runtime_update is already set, we're in race;
2414 * process this trigger later at exit
2415 */
2416 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2417 fe->dpcm[stream].trigger_pending = cmd + 1;
2418 return 0; /* delayed, assuming it's successful */
2419 }
2420
2421 /* we're alone, let's trigger */
2422 return dpcm_fe_dai_do_trigger(substream, cmd);
2423}
2424
Liam Girdwood23607022014-01-17 17:03:55 +00002425int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002426{
2427 struct snd_soc_dpcm *dpcm;
2428 int ret = 0;
2429
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002430 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002431
2432 struct snd_soc_pcm_runtime *be = dpcm->be;
2433 struct snd_pcm_substream *be_substream =
2434 snd_soc_dpcm_get_substream(be, stream);
2435
2436 /* is this op for this BE ? */
2437 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2438 continue;
2439
2440 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002441 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2443 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002444 continue;
2445
Liam Girdwood103d84a2012-11-19 14:39:15 +00002446 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002447 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002448
2449 ret = soc_pcm_prepare(be_substream);
2450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002451 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002452 ret);
2453 break;
2454 }
2455
2456 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2457 }
2458 return ret;
2459}
2460
Mark Brown45c0a182012-05-09 21:46:27 +01002461static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002462{
2463 struct snd_soc_pcm_runtime *fe = substream->private_data;
2464 int stream = substream->stream, ret = 0;
2465
2466 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2467
Liam Girdwood103d84a2012-11-19 14:39:15 +00002468 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002469
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002470 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002471
2472 /* there is no point preparing this FE if there are no BEs */
2473 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002474 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002475 fe->dai_link->name);
2476 ret = -EINVAL;
2477 goto out;
2478 }
2479
2480 ret = dpcm_be_dai_prepare(fe, substream->stream);
2481 if (ret < 0)
2482 goto out;
2483
2484 /* call prepare on the frontend */
2485 ret = soc_pcm_prepare(substream);
2486 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002487 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002488 fe->dai_link->name);
2489 goto out;
2490 }
2491
2492 /* run the stream event for each BE */
2493 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2494 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2495
2496out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002497 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002498 mutex_unlock(&fe->card->mutex);
2499
2500 return ret;
2501}
2502
Liam Girdwood618dae12012-04-25 12:12:51 +01002503static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2504{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002505 struct snd_pcm_substream *substream =
2506 snd_soc_dpcm_get_substream(fe, stream);
2507 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002508 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002509
Liam Girdwood103d84a2012-11-19 14:39:15 +00002510 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002511 stream ? "capture" : "playback", fe->dai_link->name);
2512
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002513 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2514 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002515 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002516 fe->dai_link->name);
2517
2518 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2519 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002520 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002521 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002522 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002523 fe->dai_link->name);
2524
2525 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2526 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002527 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002528 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002529
2530 err = dpcm_be_dai_hw_free(fe, stream);
2531 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002532 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002533
2534 err = dpcm_be_dai_shutdown(fe, stream);
2535 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002536 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002537
2538 /* run the stream event for each BE */
2539 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2540
2541 return 0;
2542}
2543
2544static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2545{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002546 struct snd_pcm_substream *substream =
2547 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002548 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002549 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002550 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002551 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002552
Liam Girdwood103d84a2012-11-19 14:39:15 +00002553 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002554 stream ? "capture" : "playback", fe->dai_link->name);
2555
2556 /* Only start the BE if the FE is ready */
2557 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2558 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2559 return -EINVAL;
2560
2561 /* startup must always be called for new BEs */
2562 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002563 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002564 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002565
2566 /* keep going if FE state is > open */
2567 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2568 return 0;
2569
2570 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002571 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002572 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002573
2574 /* keep going if FE state is > hw_params */
2575 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2576 return 0;
2577
2578
2579 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002580 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002581 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002582
2583 /* run the stream event for each BE */
2584 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2585
2586 /* keep going if FE state is > prepare */
2587 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2588 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2589 return 0;
2590
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002591 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2592 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002593 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002594 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002595
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002596 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2597 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002598 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002599 goto hw_free;
2600 }
2601 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002602 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002603 fe->dai_link->name);
2604
2605 ret = dpcm_be_dai_trigger(fe, stream,
2606 SNDRV_PCM_TRIGGER_START);
2607 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002608 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002609 goto hw_free;
2610 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002611 }
2612
2613 return 0;
2614
2615hw_free:
2616 dpcm_be_dai_hw_free(fe, stream);
2617close:
2618 dpcm_be_dai_shutdown(fe, stream);
2619disconnect:
2620 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002621 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002622 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002623 struct snd_soc_pcm_runtime *be = dpcm->be;
2624 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2625 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2626 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002627 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002628
2629 return ret;
2630}
2631
2632static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2633{
2634 int ret;
2635
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002636 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002637 ret = dpcm_run_update_startup(fe, stream);
2638 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002639 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002640 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002641
2642 return ret;
2643}
2644
2645static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2646{
2647 int ret;
2648
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002650 ret = dpcm_run_update_shutdown(fe, stream);
2651 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002652 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002654
2655 return ret;
2656}
2657
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002658static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2659{
2660 struct snd_soc_dapm_widget_list *list;
2661 int count, paths;
2662
2663 if (!fe->dai_link->dynamic)
2664 return 0;
2665
2666 /* only check active links */
2667 if (!fe->cpu_dai->active)
2668 return 0;
2669
2670 /* DAPM sync will call this to update DSP paths */
2671 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2672 new ? "new" : "old", fe->dai_link->name);
2673
2674 /* skip if FE doesn't have playback capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002675 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2676 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002677 goto capture;
2678
2679 /* skip if FE isn't currently playing */
2680 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2681 goto capture;
2682
2683 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2684 if (paths < 0) {
2685 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2686 fe->dai_link->name, "playback");
2687 return paths;
2688 }
2689
2690 /* update any playback paths */
2691 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2692 if (count) {
2693 if (new)
2694 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2695 else
2696 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2697
2698 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2699 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2700 }
2701
2702 dpcm_path_put(&list);
2703
2704capture:
2705 /* skip if FE doesn't have capture capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002706 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2707 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002708 return 0;
2709
2710 /* skip if FE isn't currently capturing */
2711 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2712 return 0;
2713
2714 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2715 if (paths < 0) {
2716 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2717 fe->dai_link->name, "capture");
2718 return paths;
2719 }
2720
2721 /* update any old capture paths */
2722 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2723 if (count) {
2724 if (new)
2725 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2726 else
2727 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2728
2729 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2730 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2731 }
2732
2733 dpcm_path_put(&list);
2734
2735 return 0;
2736}
2737
Liam Girdwood618dae12012-04-25 12:12:51 +01002738/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2739 * any DAI links.
2740 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002741int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002742{
Mengdong Lin1a497982015-11-18 02:34:11 -05002743 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002744 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002745
Liam Girdwood618dae12012-04-25 12:12:51 +01002746 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002747 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002748 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002749 ret = soc_dpcm_fe_runtime_update(fe, 0);
2750 if (ret)
2751 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002752 }
2753
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002754 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002755 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002756 ret = soc_dpcm_fe_runtime_update(fe, 1);
2757 if (ret)
2758 goto out;
2759 }
2760
2761out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002762 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002763 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002764}
Liam Girdwood01d75842012-04-25 12:12:49 +01002765int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2766{
2767 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002768 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01002769
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002770 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002771
2772 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002773 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002774
2775 if (be->dai_link->ignore_suspend)
2776 continue;
2777
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00002778 for_each_rtd_codec_dai(be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002779 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002780
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002781 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2782 be->dai_link->name);
2783
2784 if (drv->ops && drv->ops->digital_mute &&
2785 dai->playback_active)
2786 drv->ops->digital_mute(dai, mute);
2787 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002788 }
2789
2790 return 0;
2791}
2792
Mark Brown45c0a182012-05-09 21:46:27 +01002793static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002794{
2795 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2796 struct snd_soc_dpcm *dpcm;
2797 struct snd_soc_dapm_widget_list *list;
2798 int ret;
2799 int stream = fe_substream->stream;
2800
2801 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2802 fe->dpcm[stream].runtime = fe_substream->runtime;
2803
Qiao Zhou8f70e512014-09-10 17:54:07 +08002804 ret = dpcm_path_get(fe, stream, &list);
2805 if (ret < 0) {
2806 mutex_unlock(&fe->card->mutex);
2807 return ret;
2808 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002809 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002810 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002811 }
2812
2813 /* calculate valid and active FE <-> BE dpcms */
2814 dpcm_process_paths(fe, stream, &list, 1);
2815
2816 ret = dpcm_fe_dai_startup(fe_substream);
2817 if (ret < 0) {
2818 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002819 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002820 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2821
2822 dpcm_be_disconnect(fe, stream);
2823 fe->dpcm[stream].runtime = NULL;
2824 }
2825
2826 dpcm_clear_pending_state(fe, stream);
2827 dpcm_path_put(&list);
2828 mutex_unlock(&fe->card->mutex);
2829 return ret;
2830}
2831
Mark Brown45c0a182012-05-09 21:46:27 +01002832static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002833{
2834 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2835 struct snd_soc_dpcm *dpcm;
2836 int stream = fe_substream->stream, ret;
2837
2838 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2839 ret = dpcm_fe_dai_shutdown(fe_substream);
2840
2841 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002842 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002843 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2844
2845 dpcm_be_disconnect(fe, stream);
2846
2847 fe->dpcm[stream].runtime = NULL;
2848 mutex_unlock(&fe->card->mutex);
2849 return ret;
2850}
2851
Liam Girdwoodddee6272011-06-09 14:45:53 +01002852/* create a new pcm */
2853int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2854{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002855 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002856 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002857 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002858 struct snd_pcm *pcm;
2859 char new_name[64];
2860 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002861 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002862
Liam Girdwood01d75842012-04-25 12:12:49 +01002863 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002864 playback = rtd->dai_link->dpcm_playback;
2865 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002866 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002867 /* Adapt stream for codec2codec links */
2868 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2869 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2870 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2871 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2872
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002873 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002874 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2875 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002876 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002877 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2878 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002879 capture = 1;
2880 }
Jerome Bruneta3420312019-07-25 18:59:47 +02002881
2882 capture = capture && cpu_capture->channels_min;
2883 playback = playback && cpu_playback->channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002884 }
Sangsu Parka5002312012-01-02 17:15:10 +09002885
Fabio Estevamd6bead02013-08-29 10:32:13 -03002886 if (rtd->dai_link->playback_only) {
2887 playback = 1;
2888 capture = 0;
2889 }
2890
2891 if (rtd->dai_link->capture_only) {
2892 playback = 0;
2893 capture = 1;
2894 }
2895
Liam Girdwood01d75842012-04-25 12:12:49 +01002896 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002897 if (rtd->dai_link->params) {
2898 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2899 rtd->dai_link->stream_name);
2900
2901 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2902 playback, capture, &pcm);
2903 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002904 snprintf(new_name, sizeof(new_name), "(%s)",
2905 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002906
Liam Girdwood01d75842012-04-25 12:12:49 +01002907 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2908 playback, capture, &pcm);
2909 } else {
2910 if (rtd->dai_link->dynamic)
2911 snprintf(new_name, sizeof(new_name), "%s (*)",
2912 rtd->dai_link->stream_name);
2913 else
2914 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002915 rtd->dai_link->stream_name,
2916 (rtd->num_codecs > 1) ?
2917 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002918
Liam Girdwood01d75842012-04-25 12:12:49 +01002919 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2920 capture, &pcm);
2921 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002922 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002923 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002924 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002925 return ret;
2926 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002927 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002928
2929 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002930 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002931 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002932 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002933 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002934
Vinod Koul48c76992015-02-12 09:59:53 +05302935 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002936 rtd->pcm = pcm;
2937 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002938
Jerome Bruneta3420312019-07-25 18:59:47 +02002939 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002940 if (playback)
2941 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2942 if (capture)
2943 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2944 goto out;
2945 }
2946
2947 /* ASoC PCM operations */
2948 if (rtd->dai_link->dynamic) {
2949 rtd->ops.open = dpcm_fe_dai_open;
2950 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2951 rtd->ops.prepare = dpcm_fe_dai_prepare;
2952 rtd->ops.trigger = dpcm_fe_dai_trigger;
2953 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2954 rtd->ops.close = dpcm_fe_dai_close;
2955 rtd->ops.pointer = soc_pcm_pointer;
2956 } else {
2957 rtd->ops.open = soc_pcm_open;
2958 rtd->ops.hw_params = soc_pcm_hw_params;
2959 rtd->ops.prepare = soc_pcm_prepare;
2960 rtd->ops.trigger = soc_pcm_trigger;
2961 rtd->ops.hw_free = soc_pcm_hw_free;
2962 rtd->ops.close = soc_pcm_close;
2963 rtd->ops.pointer = soc_pcm_pointer;
2964 }
2965
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002966 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002967 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002968
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002969 if (drv->ioctl)
2970 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002971 if (drv->sync_stop)
2972 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002973 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002974 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002975 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002976 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002977 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002978 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002979 }
2980
Liam Girdwoodddee6272011-06-09 14:45:53 +01002981 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002982 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002983
2984 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002985 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002986
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002987 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002988 if (ret < 0) {
2989 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2990 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002991 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002992
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002993 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002994out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002995 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2996 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2997 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002998 return ret;
2999}
Liam Girdwood01d75842012-04-25 12:12:49 +01003000
3001/* is the current PCM operation for this FE ? */
3002int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3003{
3004 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3005 return 1;
3006 return 0;
3007}
3008EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3009
3010/* is the current PCM operation for this BE ? */
3011int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3012 struct snd_soc_pcm_runtime *be, int stream)
3013{
3014 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3015 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3016 be->dpcm[stream].runtime_update))
3017 return 1;
3018 return 0;
3019}
3020EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3021
3022/* get the substream for this BE */
3023struct snd_pcm_substream *
3024 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3025{
3026 return be->pcm->streams[stream].substream;
3027}
3028EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3029
3030/* get the BE runtime state */
3031enum snd_soc_dpcm_state
3032 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3033{
3034 return be->dpcm[stream].state;
3035}
3036EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3037
3038/* set the BE runtime state */
3039void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3040 int stream, enum snd_soc_dpcm_state state)
3041{
3042 be->dpcm[stream].state = state;
3043}
3044EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3045
3046/*
3047 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3048 * are not running, paused or suspended for the specified stream direction.
3049 */
3050int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3051 struct snd_soc_pcm_runtime *be, int stream)
3052{
3053 struct snd_soc_dpcm *dpcm;
3054 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003055 int ret = 1;
3056 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003057
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003058 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003059 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003060
3061 if (dpcm->fe == fe)
3062 continue;
3063
3064 state = dpcm->fe->dpcm[stream].state;
3065 if (state == SND_SOC_DPCM_STATE_START ||
3066 state == SND_SOC_DPCM_STATE_PAUSED ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003067 state == SND_SOC_DPCM_STATE_SUSPEND) {
3068 ret = 0;
3069 break;
3070 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003071 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003072 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003073
3074 /* it's safe to free/stop this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003075 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003076}
3077EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3078
3079/*
3080 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3081 * running, paused or suspended for the specified stream direction.
3082 */
3083int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3084 struct snd_soc_pcm_runtime *be, int stream)
3085{
3086 struct snd_soc_dpcm *dpcm;
3087 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003088 int ret = 1;
3089 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01003090
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003091 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00003092 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003093
3094 if (dpcm->fe == fe)
3095 continue;
3096
3097 state = dpcm->fe->dpcm[stream].state;
3098 if (state == SND_SOC_DPCM_STATE_START ||
3099 state == SND_SOC_DPCM_STATE_PAUSED ||
3100 state == SND_SOC_DPCM_STATE_SUSPEND ||
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003101 state == SND_SOC_DPCM_STATE_PREPARE) {
3102 ret = 0;
3103 break;
3104 }
Liam Girdwood01d75842012-04-25 12:12:49 +01003105 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003106 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01003107
3108 /* it's safe to change hw_params */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003109 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01003110}
3111EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003112
3113#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003114static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003115{
3116 switch (state) {
3117 case SND_SOC_DPCM_STATE_NEW:
3118 return "new";
3119 case SND_SOC_DPCM_STATE_OPEN:
3120 return "open";
3121 case SND_SOC_DPCM_STATE_HW_PARAMS:
3122 return "hw_params";
3123 case SND_SOC_DPCM_STATE_PREPARE:
3124 return "prepare";
3125 case SND_SOC_DPCM_STATE_START:
3126 return "start";
3127 case SND_SOC_DPCM_STATE_STOP:
3128 return "stop";
3129 case SND_SOC_DPCM_STATE_SUSPEND:
3130 return "suspend";
3131 case SND_SOC_DPCM_STATE_PAUSED:
3132 return "paused";
3133 case SND_SOC_DPCM_STATE_HW_FREE:
3134 return "hw_free";
3135 case SND_SOC_DPCM_STATE_CLOSE:
3136 return "close";
3137 }
3138
3139 return "unknown";
3140}
3141
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003142static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3143 int stream, char *buf, size_t size)
3144{
3145 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3146 struct snd_soc_dpcm *dpcm;
3147 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003148 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003149
3150 /* FE state */
3151 offset += snprintf(buf + offset, size - offset,
3152 "[%s - %s]\n", fe->dai_link->name,
3153 stream ? "Capture" : "Playback");
3154
3155 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3156 dpcm_state_string(fe->dpcm[stream].state));
3157
3158 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3159 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3160 offset += snprintf(buf + offset, size - offset,
3161 "Hardware Params: "
3162 "Format = %s, Channels = %d, Rate = %d\n",
3163 snd_pcm_format_name(params_format(params)),
3164 params_channels(params),
3165 params_rate(params));
3166
3167 /* BEs state */
3168 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3169
3170 if (list_empty(&fe->dpcm[stream].be_clients)) {
3171 offset += snprintf(buf + offset, size - offset,
3172 " No active DSP links\n");
3173 goto out;
3174 }
3175
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003176 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003177 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003178 struct snd_soc_pcm_runtime *be = dpcm->be;
3179 params = &dpcm->hw_params;
3180
3181 offset += snprintf(buf + offset, size - offset,
3182 "- %s\n", be->dai_link->name);
3183
3184 offset += snprintf(buf + offset, size - offset,
3185 " State: %s\n",
3186 dpcm_state_string(be->dpcm[stream].state));
3187
3188 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3189 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3190 offset += snprintf(buf + offset, size - offset,
3191 " Hardware Params: "
3192 "Format = %s, Channels = %d, Rate = %d\n",
3193 snd_pcm_format_name(params_format(params)),
3194 params_channels(params),
3195 params_rate(params));
3196 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003197 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003198out:
3199 return offset;
3200}
3201
3202static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3203 size_t count, loff_t *ppos)
3204{
3205 struct snd_soc_pcm_runtime *fe = file->private_data;
3206 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3207 char *buf;
3208
3209 buf = kmalloc(out_count, GFP_KERNEL);
3210 if (!buf)
3211 return -ENOMEM;
3212
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003213 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003214 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3215 buf + offset, out_count - offset);
3216
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003217 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003218 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3219 buf + offset, out_count - offset);
3220
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003221 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003222
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003223 kfree(buf);
3224 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003225}
3226
3227static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003228 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003229 .read = dpcm_state_read_file,
3230 .llseek = default_llseek,
3231};
3232
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003233void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003234{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003235 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003236 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003237
Kuninori Morimoto596becd2019-08-07 10:31:36 +09003238 if (!rtd->dai_link->dynamic)
3239 return;
3240
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003241 if (!rtd->card->debugfs_card_root)
3242 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003243
3244 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3245 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003246
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003247 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3248 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003249}
3250#endif