blob: 5a79a830ee18e811cd9c6315cd2182abcbc56b46 [file] [log] [blame]
Kuninori Morimotoed517582018-07-02 06:22:44 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-pcm.c -- ALSA SoC PCM
4//
5// Copyright 2005 Wolfson Microelectronics PLC.
6// Copyright 2005 Openedhand Ltd.
7// Copyright (C) 2010 Slimlogic Ltd.
8// Copyright (C) 2010 Texas Instruments Inc.
9//
10// Authors: Liam Girdwood <lrg@ti.com>
11// Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010012
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080016#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000017#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010018#include <linux/slab.h>
19#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010020#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010021#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/initval.h>
28
Liam Girdwood01d75842012-04-25 12:12:49 +010029#define DPCM_MAX_BE_USERS 8
30
Kuninori Morimotof183f922020-01-22 09:44:35 +090031static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
32 struct snd_pcm_substream *substream)
33{
34 if (rtd->dai_link->ops &&
35 rtd->dai_link->ops->startup)
36 return rtd->dai_link->ops->startup(substream);
37 return 0;
38}
39
Kuninori Morimoto0be429f2020-01-22 09:44:40 +090040static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
41 struct snd_pcm_substream *substream)
42{
43 if (rtd->dai_link->ops &&
44 rtd->dai_link->ops->shutdown)
45 rtd->dai_link->ops->shutdown(substream);
46}
47
Kuninori Morimoto44c1a752020-01-22 09:44:44 +090048static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
49 struct snd_pcm_substream *substream)
50{
51 if (rtd->dai_link->ops &&
52 rtd->dai_link->ops->prepare)
53 return rtd->dai_link->ops->prepare(substream);
54 return 0;
55}
56
Kuninori Morimotode9ad992020-01-22 09:44:48 +090057static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
58 struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
60{
61 if (rtd->dai_link->ops &&
62 rtd->dai_link->ops->hw_params)
63 return rtd->dai_link->ops->hw_params(substream, params);
64 return 0;
65}
66
Kuninori Morimoto49f020e2020-01-22 09:44:52 +090067static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
68 struct snd_pcm_substream *substream)
69{
70 if (rtd->dai_link->ops &&
71 rtd->dai_link->ops->hw_free)
72 rtd->dai_link->ops->hw_free(substream);
73}
74
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +090075static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
76 struct snd_pcm_substream *substream,
77 int cmd)
78{
79 if (rtd->dai_link->ops &&
80 rtd->dai_link->ops->trigger)
81 return rtd->dai_link->ops->trigger(substream, cmd);
82 return 0;
83}
84
Kuninori Morimotod9303692020-02-17 17:27:39 +090085static inline
86struct snd_soc_dapm_widget *dai_get_widget(struct snd_soc_dai *dai, int stream)
87{
88 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
89 return dai->playback_widget;
90 else
91 return dai->capture_widget;
92}
93
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +090094static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
95 int stream, int action)
96{
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai;
99 int i;
100
101 lockdep_assert_held(&rtd->card->pcm_mutex);
102
103 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
104 cpu_dai->playback_active += action;
105 for_each_rtd_codec_dai(rtd, i, codec_dai)
106 codec_dai->playback_active += action;
107 } else {
108 cpu_dai->capture_active += action;
109 for_each_rtd_codec_dai(rtd, i, codec_dai)
110 codec_dai->capture_active += action;
111 }
112
113 cpu_dai->active += action;
114 cpu_dai->component->active += action;
115 for_each_rtd_codec_dai(rtd, i, codec_dai) {
116 codec_dai->active += action;
117 codec_dai->component->active += action;
118 }
119}
120
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200121/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100122 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
123 * @rtd: ASoC PCM runtime that is activated
124 * @stream: Direction of the PCM stream
125 *
126 * Increments the active count for all the DAIs and components attached to a PCM
127 * runtime. Should typically be called when a stream is opened.
128 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300129 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100130 */
131void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
132{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900133 snd_soc_runtime_action(rtd, stream, 1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100134}
135
136/**
137 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
138 * @rtd: ASoC PCM runtime that is deactivated
139 * @stream: Direction of the PCM stream
140 *
141 * Decrements the active count for all the DAIs and components attached to a PCM
142 * runtime. Should typically be called when a stream is closed.
143 *
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300144 * Must be called with the rtd->card->pcm_mutex being held
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100145 */
146void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
147{
Kuninori Morimoto7a5aaba2020-02-10 12:14:12 +0900148 snd_soc_runtime_action(rtd, stream, -1);
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100149}
150
151/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100152 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
153 * @rtd: The ASoC PCM runtime that should be checked.
154 *
155 * This function checks whether the power down delay should be ignored for a
156 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
157 * been configured to ignore the delay, or if none of the components benefits
158 * from having the delay.
159 */
160bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
161{
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000162 struct snd_soc_component *component;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200163 bool ignore = true;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900164 int i;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200165
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100166 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
167 return true;
168
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900169 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto72c38182018-01-19 05:21:19 +0000170 ignore &= !component->driver->use_pmdown_time;
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000171
Kuninori Morimotofbb16562017-10-11 01:38:08 +0000172 return ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100173}
174
175/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200176 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
177 * @substream: the pcm substream
178 * @hw: the hardware parameters
179 *
180 * Sets the substream runtime hardware parameters.
181 */
182int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
183 const struct snd_pcm_hardware *hw)
184{
185 struct snd_pcm_runtime *runtime = substream->runtime;
186 runtime->hw.info = hw->info;
187 runtime->hw.formats = hw->formats;
188 runtime->hw.period_bytes_min = hw->period_bytes_min;
189 runtime->hw.period_bytes_max = hw->period_bytes_max;
190 runtime->hw.periods_min = hw->periods_min;
191 runtime->hw.periods_max = hw->periods_max;
192 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
193 runtime->hw.fifo_size = hw->fifo_size;
194 return 0;
195}
196EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
197
Liam Girdwood01d75842012-04-25 12:12:49 +0100198/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000199int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100200 int event)
201{
202 struct snd_soc_dpcm *dpcm;
203
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +0000204 for_each_dpcm_be(fe, dir, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +0100205
206 struct snd_soc_pcm_runtime *be = dpcm->be;
207
Liam Girdwood103d84a2012-11-19 14:39:15 +0000208 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100209 be->dai_link->name, event, dir);
210
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700211 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
212 (be->dpcm[dir].users >= 1))
213 continue;
214
Liam Girdwood01d75842012-04-25 12:12:49 +0100215 snd_soc_dapm_stream_event(be, dir, event);
216 }
217
218 snd_soc_dapm_stream_event(fe, dir, event);
219
220 return 0;
221}
222
Dong Aisheng17841022011-08-29 17:15:14 +0800223static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
224 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100225{
226 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100227 int ret;
228
Nicolin Chen3635bf02013-11-13 18:56:24 +0800229 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
230 rtd->dai_link->symmetric_rates)) {
231 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
232 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100233
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200234 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800235 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200236 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800237 if (ret < 0) {
238 dev_err(soc_dai->dev,
239 "ASoC: Unable to apply rate constraint: %d\n",
240 ret);
241 return ret;
242 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243 }
244
Nicolin Chen3635bf02013-11-13 18:56:24 +0800245 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
246 rtd->dai_link->symmetric_channels)) {
247 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
248 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100249
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200250 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800252 soc_dai->channels);
253 if (ret < 0) {
254 dev_err(soc_dai->dev,
255 "ASoC: Unable to apply channel symmetry constraint: %d\n",
256 ret);
257 return ret;
258 }
259 }
260
261 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
262 rtd->dai_link->symmetric_samplebits)) {
263 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
264 soc_dai->sample_bits);
265
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200266 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800267 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800268 soc_dai->sample_bits);
269 if (ret < 0) {
270 dev_err(soc_dai->dev,
271 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
272 ret);
273 return ret;
274 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100275 }
276
277 return 0;
278}
279
Nicolin Chen3635bf02013-11-13 18:56:24 +0800280static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params)
282{
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
284 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000285 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200286 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800287
288 rate = params_rate(params);
289 channels = params_channels(params);
290 sample_bits = snd_pcm_format_physical_width(params_format(params));
291
292 /* reject unmatched parameters when applying symmetry */
293 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800294 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200295
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000296 for_each_rtd_codec_dai(rtd, i, codec_dai)
297 symmetry |= codec_dai->driver->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200298
Nicolin Chen3635bf02013-11-13 18:56:24 +0800299 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
300 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
301 cpu_dai->rate, rate);
302 return -EINVAL;
303 }
304
305 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800306 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200307
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000308 for_each_rtd_codec_dai(rtd, i, codec_dai)
309 symmetry |= codec_dai->driver->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200310
Nicolin Chen3635bf02013-11-13 18:56:24 +0800311 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
312 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
313 cpu_dai->channels, channels);
314 return -EINVAL;
315 }
316
317 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800318 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200319
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000320 for_each_rtd_codec_dai(rtd, i, codec_dai)
321 symmetry |= codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200322
Nicolin Chen3635bf02013-11-13 18:56:24 +0800323 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
324 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
325 cpu_dai->sample_bits, sample_bits);
326 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100327 }
328
329 return 0;
330}
331
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100332static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
333{
334 struct snd_soc_pcm_runtime *rtd = substream->private_data;
335 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100336 struct snd_soc_dai_link *link = rtd->dai_link;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000337 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200338 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100339
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
341 cpu_driver->symmetric_channels || link->symmetric_channels ||
342 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
343
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000344 for_each_rtd_codec_dai(rtd, i, codec_dai)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200345 symmetry = symmetry ||
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000346 codec_dai->driver->symmetric_rates ||
347 codec_dai->driver->symmetric_channels ||
348 codec_dai->driver->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200349
350 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100351}
352
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200353static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000354{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100356 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000357
358 if (!bits)
359 return;
360
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100361 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
362 if (ret != 0)
363 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
364 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000365}
366
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200367static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200368{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200369 struct snd_soc_pcm_runtime *rtd = substream->private_data;
370 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200371 struct snd_soc_dai *codec_dai;
372 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200373 unsigned int bits = 0, cpu_bits;
374
375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000376 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200377 if (codec_dai->driver->playback.sig_bits == 0) {
378 bits = 0;
379 break;
380 }
381 bits = max(codec_dai->driver->playback.sig_bits, bits);
382 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200383 cpu_bits = cpu_dai->driver->playback.sig_bits;
384 } else {
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000385 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200386 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200387 bits = 0;
388 break;
389 }
390 bits = max(codec_dai->driver->capture.sig_bits, bits);
391 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200392 cpu_bits = cpu_dai->driver->capture.sig_bits;
393 }
394
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200395 soc_pcm_set_msb(substream, bits);
396 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200397}
398
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200399static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200400{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200401 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100402 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200403 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000404 struct snd_soc_dai *codec_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200405 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
406 struct snd_soc_dai_driver *codec_dai_drv;
407 struct snd_soc_pcm_stream *codec_stream;
408 struct snd_soc_pcm_stream *cpu_stream;
409 unsigned int chan_min = 0, chan_max = UINT_MAX;
410 unsigned int rate_min = 0, rate_max = UINT_MAX;
411 unsigned int rates = UINT_MAX;
412 u64 formats = ULLONG_MAX;
413 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100414
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200415 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
416 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100417 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200418 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100419
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200420 /* first calculate min/max only for CODECs in the DAI link */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000421 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200422
423 /*
424 * Skip CODECs which don't support the current stream type.
425 * Otherwise, since the rate, channel, and format values will
426 * zero in that case, we would have no usable settings left,
427 * causing the resulting setup to fail.
428 * At least one CODEC should match, otherwise we should have
429 * bailed out on a higher level, since there would be no
430 * CODEC to support the transfer direction in that case.
431 */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000432 if (!snd_soc_dai_stream_valid(codec_dai,
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200433 substream->stream))
434 continue;
435
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000436 codec_dai_drv = codec_dai->driver;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200437 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
438 codec_stream = &codec_dai_drv->playback;
439 else
440 codec_stream = &codec_dai_drv->capture;
441 chan_min = max(chan_min, codec_stream->channels_min);
442 chan_max = min(chan_max, codec_stream->channels_max);
443 rate_min = max(rate_min, codec_stream->rate_min);
444 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
445 formats &= codec_stream->formats;
446 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
447 }
448
449 /*
450 * chan min/max cannot be enforced if there are multiple CODEC DAIs
451 * connected to a single CPU DAI, use CPU DAI's directly and let
452 * channel allocation be fixed up later
453 */
454 if (rtd->num_codecs > 1) {
455 chan_min = cpu_stream->channels_min;
456 chan_max = cpu_stream->channels_max;
457 }
458
459 hw->channels_min = max(chan_min, cpu_stream->channels_min);
460 hw->channels_max = min(chan_max, cpu_stream->channels_max);
461 if (hw->formats)
462 hw->formats &= formats & cpu_stream->formats;
463 else
464 hw->formats = formats & cpu_stream->formats;
465 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100466
467 snd_pcm_limit_hw_rates(runtime);
468
469 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200470 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100471 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200472 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200473}
474
Kuninori Morimotodd039072020-02-10 12:14:37 +0900475static int soc_pcm_components_open(struct snd_pcm_substream *substream)
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900476{
477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900478 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900479 int i, ret = 0;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900480
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900481 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900482 ret = snd_soc_component_module_get_when_open(component);
483 if (ret < 0) {
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900484 dev_err(component->dev,
485 "ASoC: can't get module %s\n",
486 component->name);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900487 return ret;
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900488 }
489
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900490 ret = snd_soc_component_open(component, substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900491 if (ret < 0) {
492 dev_err(component->dev,
493 "ASoC: can't open component %s: %d\n",
494 component->name, ret);
495 return ret;
496 }
497 }
Kuninori Morimotodd039072020-02-10 12:14:37 +0900498
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900499 return 0;
500}
501
Kuninori Morimotodd039072020-02-10 12:14:37 +0900502static int soc_pcm_components_close(struct snd_pcm_substream *substream)
Charles Keepax244e2932018-06-19 16:22:09 +0100503{
504 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100505 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900506 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100507
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900508 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900509 r = snd_soc_component_close(component, substream);
510 if (r < 0)
511 ret = r; /* use last ret */
512
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900513 snd_soc_component_module_put_when_close(component);
Charles Keepax244e2932018-06-19 16:22:09 +0100514 }
515
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900516 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100517}
518
Mark Brown58ba9b22012-01-16 18:38:51 +0000519/*
Kuninori Morimoto62c86d12020-02-10 12:14:41 +0900520 * Called by ALSA when a PCM substream is closed. Private data can be
521 * freed here. The cpu DAI, codec DAI, machine and components are also
522 * shutdown.
523 */
524static int soc_pcm_close(struct snd_pcm_substream *substream)
525{
526 struct snd_soc_pcm_runtime *rtd = substream->private_data;
527 struct snd_soc_component *component;
528 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
529 struct snd_soc_dai *codec_dai;
530 int i;
531
532 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
533
534 snd_soc_runtime_deactivate(rtd, substream->stream);
535
536 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
537
538 snd_soc_dai_shutdown(cpu_dai, substream);
539
540 for_each_rtd_codec_dai(rtd, i, codec_dai)
541 snd_soc_dai_shutdown(codec_dai, substream);
542
543 soc_rtd_shutdown(rtd, substream);
544
545 soc_pcm_components_close(substream);
546
547 snd_soc_dapm_stream_stop(rtd, substream->stream);
548
549 mutex_unlock(&rtd->card->pcm_mutex);
550
551 for_each_rtd_components(rtd, i, component) {
552 pm_runtime_mark_last_busy(component->dev);
553 pm_runtime_put_autosuspend(component->dev);
554 }
555
556 for_each_rtd_components(rtd, i, component)
557 if (!component->active)
558 pinctrl_pm_select_sleep_state(component->dev);
559
560 return 0;
561}
562
563/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
565 * then initialized and any private data can be allocated. This also calls
Charles Keepaxef050be2018-04-24 16:39:02 +0100566 * startup for the cpu DAI, component, machine and codec DAI.
Liam Girdwoodddee6272011-06-09 14:45:53 +0100567 */
568static int soc_pcm_open(struct snd_pcm_substream *substream)
569{
570 struct snd_soc_pcm_runtime *rtd = substream->private_data;
571 struct snd_pcm_runtime *runtime = substream->runtime;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000572 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200574 struct snd_soc_dai *codec_dai;
575 const char *codec_dai_name = "multicodec";
Charles Keepax244e2932018-06-19 16:22:09 +0100576 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900578 for_each_rtd_components(rtd, i, component)
579 pinctrl_pm_select_default_state(component->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000580
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900581 for_each_rtd_components(rtd, i, component)
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000582 pm_runtime_get_sync(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000583
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300584 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100585
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900586 ret = soc_pcm_components_open(substream);
587 if (ret < 0)
588 goto component_err;
589
590 ret = soc_rtd_startup(rtd, substream);
591 if (ret < 0) {
592 pr_err("ASoC: %s startup failed: %d\n",
593 rtd->dai_link->name, ret);
594 goto component_err;
595 }
596
Liam Girdwoodddee6272011-06-09 14:45:53 +0100597 /* startup the audio subsystem */
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900598 ret = snd_soc_dai_startup(cpu_dai, substream);
599 if (ret < 0) {
600 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
601 cpu_dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900602 goto cpu_dai_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100603 }
604
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000605 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5a52a042019-07-22 10:33:32 +0900606 ret = snd_soc_dai_startup(codec_dai, substream);
607 if (ret < 0) {
608 dev_err(codec_dai->dev,
609 "ASoC: can't open codec %s: %d\n",
610 codec_dai->name, ret);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900611 goto config_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200613
614 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
615 codec_dai->tx_mask = 0;
616 else
617 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618 }
619
Liam Girdwood01d75842012-04-25 12:12:49 +0100620 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
621 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
622 goto dynamic;
623
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200625 soc_pcm_init_runtime_hw(substream);
626
627 if (rtd->num_codecs == 1)
628 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100629
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100630 if (soc_pcm_has_symmetry(substream))
631 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
632
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100634 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000635 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200636 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100637 goto config_err;
638 }
639 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200641 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 goto config_err;
643 }
644 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
645 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000646 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200647 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100648 goto config_err;
649 }
650
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200651 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000652
Liam Girdwoodddee6272011-06-09 14:45:53 +0100653 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800654 if (cpu_dai->active) {
655 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
656 if (ret != 0)
657 goto config_err;
658 }
659
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000660 for_each_rtd_codec_dai(rtd, i, codec_dai) {
661 if (codec_dai->active) {
662 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200663 if (ret != 0)
664 goto config_err;
665 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666 }
667
Liam Girdwood103d84a2012-11-19 14:39:15 +0000668 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200669 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000670 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
671 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100672 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000673 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100674 runtime->hw.rate_max);
675
Liam Girdwood01d75842012-04-25 12:12:49 +0100676dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100677
678 snd_soc_runtime_activate(rtd, substream->stream);
679
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300680 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100681 return 0;
682
683config_err:
Kuninori Morimotob56be802020-02-10 12:14:33 +0900684 for_each_rtd_codec_dai(rtd, i, codec_dai)
Kuninori Morimoto330fcb52019-07-22 10:33:39 +0900685 snd_soc_dai_shutdown(codec_dai, substream);
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900686cpu_dai_err:
687 snd_soc_dai_shutdown(cpu_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200688
Kuninori Morimoto5d9fa032020-02-10 12:14:45 +0900689 soc_rtd_shutdown(rtd, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000690component_err:
Kuninori Morimotodd039072020-02-10 12:14:37 +0900691 soc_pcm_components_close(substream);
Kuninori Morimotoe7ecfdb2019-05-13 16:08:33 +0900692
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300693 mutex_unlock(&rtd->card->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000694
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900695 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000696 pm_runtime_mark_last_busy(component->dev);
697 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530698 }
699
Kuninori Morimoto76c39e82020-01-10 11:36:13 +0900700 for_each_rtd_components(rtd, i, component)
701 if (!component->active)
702 pinctrl_pm_select_sleep_state(component->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000703
Liam Girdwoodddee6272011-06-09 14:45:53 +0100704 return ret;
705}
706
Curtis Malainey4bf2e382019-12-03 09:30:07 -0800707static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
Jerome Bruneta3420312019-07-25 18:59:47 +0200708{
709 /*
710 * Currently nothing to do for c2c links
711 * Since c2c links are internal nodes in the DAPM graph and
712 * don't interface with the outside world or application layer
713 * we don't have to do any special handling on close.
714 */
715}
716
Liam Girdwoodddee6272011-06-09 14:45:53 +0100717/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100718 * Called by ALSA when the PCM substream is prepared, can set format, sample
719 * rate, etc. This function is non atomic and can be called multiple times,
720 * it can refer to the runtime info.
721 */
722static int soc_pcm_prepare(struct snd_pcm_substream *substream)
723{
724 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000725 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200727 struct snd_soc_dai *codec_dai;
728 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300730 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100731
Kuninori Morimoto44c1a752020-01-22 09:44:44 +0900732 ret = soc_rtd_prepare(rtd, substream);
733 if (ret < 0) {
734 dev_err(rtd->card->dev,
735 "ASoC: machine prepare error: %d\n", ret);
736 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 }
738
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900739 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900740 ret = snd_soc_component_prepare(component, substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000741 if (ret < 0) {
742 dev_err(component->dev,
743 "ASoC: platform prepare error: %d\n", ret);
744 goto out;
745 }
746 }
747
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000748 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900749 ret = snd_soc_dai_prepare(codec_dai, substream);
750 if (ret < 0) {
751 dev_err(codec_dai->dev,
752 "ASoC: codec DAI prepare error: %d\n",
753 ret);
754 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100755 }
756 }
757
Kuninori Morimoto4beb8e12019-07-22 10:33:45 +0900758 ret = snd_soc_dai_prepare(cpu_dai, substream);
759 if (ret < 0) {
760 dev_err(cpu_dai->dev,
761 "ASoC: cpu DAI prepare error: %d\n", ret);
762 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100763 }
764
765 /* cancel any delayed stream shutdown that is pending */
766 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600767 rtd->pop_wait) {
768 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100769 cancel_delayed_work(&rtd->delayed_work);
770 }
771
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000772 snd_soc_dapm_stream_event(rtd, substream->stream,
773 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100774
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000775 for_each_rtd_codec_dai(rtd, i, codec_dai)
776 snd_soc_dai_digital_mute(codec_dai, 0,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200777 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530778 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100779
780out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300781 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100782 return ret;
783}
784
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200785static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
786 unsigned int mask)
787{
788 struct snd_interval *interval;
789 int channels = hweight_long(mask);
790
791 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
792 interval->min = channels;
793 interval->max = channels;
794}
795
Charles Keepax244e2932018-06-19 16:22:09 +0100796static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
797 struct snd_soc_component *last)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Charles Keepax244e2932018-06-19 16:22:09 +0100800 struct snd_soc_component *component;
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900801 int i, r, ret = 0;
Charles Keepax244e2932018-06-19 16:22:09 +0100802
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900803 for_each_rtd_components(rtd, i, component) {
Charles Keepax244e2932018-06-19 16:22:09 +0100804 if (component == last)
805 break;
806
Kuninori Morimotoe82ebff2020-02-10 12:14:26 +0900807 r = snd_soc_component_hw_free(component, substream);
808 if (r < 0)
809 ret = r; /* use last ret */
Charles Keepax244e2932018-06-19 16:22:09 +0100810 }
811
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900812 return ret;
Charles Keepax244e2932018-06-19 16:22:09 +0100813}
814
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815/*
816 * Called by ALSA when the hardware params are set by application. This
817 * function can also be called multiple times and can allocate buffers
818 * (using snd_pcm_lib_* ). It's non-atomic.
819 */
820static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
821 struct snd_pcm_hw_params *params)
822{
823 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000824 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100825 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000826 struct snd_soc_dai *codec_dai;
Charles Keepax244e2932018-06-19 16:22:09 +0100827 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100828
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300829 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Shengjiu Wang5cca5952019-11-12 18:46:42 +0800830
831 ret = soc_pcm_params_symmetry(substream, params);
832 if (ret)
833 goto out;
834
Kuninori Morimotode9ad992020-01-22 09:44:48 +0900835 ret = soc_rtd_hw_params(rtd, substream, params);
836 if (ret < 0) {
837 dev_err(rtd->card->dev,
838 "ASoC: machine hw_params failed: %d\n", ret);
839 goto out;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100840 }
841
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000842 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200843 struct snd_pcm_hw_params codec_params;
844
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200845 /*
846 * Skip CODECs which don't support the current stream type,
847 * the idea being that if a CODEC is not used for the currently
848 * set up transfer direction, it should not need to be
849 * configured, especially since the configuration used might
850 * not even be supported by that CODEC. There may be cases
851 * however where a CODEC needs to be set up although it is
852 * actually not being used for the transfer, e.g. if a
853 * capture-only CODEC is acting as an LRCLK and/or BCLK master
854 * for the DAI link including a playback-only CODEC.
855 * If this becomes necessary, we will have to augment the
856 * machine driver setup with information on how to act, so
857 * we can do the right thing here.
858 */
859 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
860 continue;
861
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200862 /* copy params for each codec */
863 codec_params = *params;
864
865 /* fixup params based on TDM slot masks */
Rander Wang570f18b2019-03-08 16:38:57 +0800866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 codec_dai->tx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200868 soc_pcm_codec_params_fixup(&codec_params,
869 codec_dai->tx_mask);
Rander Wang570f18b2019-03-08 16:38:57 +0800870
871 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
872 codec_dai->rx_mask)
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200873 soc_pcm_codec_params_fixup(&codec_params,
874 codec_dai->rx_mask);
875
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900876 ret = snd_soc_dai_hw_params(codec_dai, substream,
877 &codec_params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200878 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100879 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200880
881 codec_dai->rate = params_rate(&codec_params);
882 codec_dai->channels = params_channels(&codec_params);
883 codec_dai->sample_bits = snd_pcm_format_physical_width(
884 params_format(&codec_params));
Charles Keepax078a85f2019-01-31 13:30:18 +0000885
886 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100887 }
888
Kuninori Morimotoaa6166c2019-07-22 10:33:04 +0900889 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
Benoit Cousson93e69582014-07-08 23:19:38 +0200890 if (ret < 0)
891 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100892
Kuninori Morimotoca58221d2019-05-13 16:07:43 +0900893 /* store the parameters for each DAIs */
894 cpu_dai->rate = params_rate(params);
895 cpu_dai->channels = params_channels(params);
896 cpu_dai->sample_bits =
897 snd_pcm_format_physical_width(params_format(params));
898
899 snd_soc_dapm_update_dai(substream, params, cpu_dai);
900
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900901 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto245c5392019-07-26 13:50:19 +0900902 ret = snd_soc_component_hw_params(component, substream, params);
Charles Keepax244e2932018-06-19 16:22:09 +0100903 if (ret < 0) {
Kuninori Morimotob8135862017-10-11 01:37:23 +0000904 dev_err(component->dev,
905 "ASoC: %s hw params failed: %d\n",
Charles Keepax244e2932018-06-19 16:22:09 +0100906 component->name, ret);
907 goto component_err;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000908 }
909 }
Charles Keepax244e2932018-06-19 16:22:09 +0100910 component = NULL;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000911
Liam Girdwoodddee6272011-06-09 14:45:53 +0100912out:
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300913 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100914 return ret;
915
Kuninori Morimotob8135862017-10-11 01:37:23 +0000916component_err:
Charles Keepax244e2932018-06-19 16:22:09 +0100917 soc_pcm_components_hw_free(substream, component);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000918
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900919 snd_soc_dai_hw_free(cpu_dai, substream);
Kuninori Morimoto2371abd2019-05-13 16:07:52 +0900920 cpu_dai->rate = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100921
922interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200923 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100924
925codec_err:
Kuninori Morimoto6d11b122018-09-18 01:28:30 +0000926 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200927 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
928 continue;
929
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900930 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200931 codec_dai->rate = 0;
932 }
933
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900934 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100935
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300936 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100937 return ret;
938}
939
940/*
941 * Frees resources allocated by hw_params, can be called multiple times
942 */
943static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
944{
945 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100946 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200947 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800948 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200949 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100950
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300951 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100952
Nicolin Chend3383422013-11-20 18:37:09 +0800953 /* clear the corresponding DAIs parameters when going to be inactive */
954 if (cpu_dai->active == 1) {
955 cpu_dai->rate = 0;
956 cpu_dai->channels = 0;
957 cpu_dai->sample_bits = 0;
958 }
959
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000960 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200961 if (codec_dai->active == 1) {
962 codec_dai->rate = 0;
963 codec_dai->channels = 0;
964 codec_dai->sample_bits = 0;
965 }
Nicolin Chend3383422013-11-20 18:37:09 +0800966 }
967
Liam Girdwoodddee6272011-06-09 14:45:53 +0100968 /* apply codec digital mute */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000969 for_each_rtd_codec_dai(rtd, i, codec_dai) {
970 if ((playback && codec_dai->playback_active == 1) ||
971 (!playback && codec_dai->capture_active == 1))
972 snd_soc_dai_digital_mute(codec_dai, 1,
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200973 substream->stream);
974 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100975
976 /* free any machine hw params */
Kuninori Morimoto49f020e2020-01-22 09:44:52 +0900977 soc_rtd_hw_free(rtd, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100978
Kuninori Morimotob8135862017-10-11 01:37:23 +0000979 /* free any component resources */
Charles Keepax244e2932018-06-19 16:22:09 +0100980 soc_pcm_components_hw_free(substream, NULL);
Kuninori Morimotob8135862017-10-11 01:37:23 +0000981
Liam Girdwoodddee6272011-06-09 14:45:53 +0100982 /* now free hw params for the DAIs */
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +0000983 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Jerome Brunetf47b9ad2019-04-29 11:47:50 +0200984 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
985 continue;
986
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900987 snd_soc_dai_hw_free(codec_dai, substream);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200988 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100989
Kuninori Morimoto846faae2019-07-22 10:33:19 +0900990 snd_soc_dai_hw_free(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100991
Peter Ujfalusi72b745e2019-08-13 13:45:32 +0300992 mutex_unlock(&rtd->card->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 return 0;
994}
995
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +0300996static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100997{
998 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000999 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001000 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001001 struct snd_soc_dai *codec_dai;
1002 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001003
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001004 ret = soc_rtd_trigger(rtd, substream, cmd);
1005 if (ret < 0)
1006 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001007
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001008 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto5693d502019-07-26 13:50:29 +09001009 ret = snd_soc_component_trigger(component, substream, cmd);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001010 if (ret < 0)
1011 return ret;
1012 }
1013
Dan Carpenter901e8222019-09-23 17:22:57 +03001014 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto95aef352019-07-22 10:33:51 +09001015 if (ret < 0)
1016 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001017
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001018 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1019 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1020 if (ret < 0)
1021 return ret;
1022 }
1023
1024 return 0;
1025}
1026
1027static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1028{
1029 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1030 struct snd_soc_component *component;
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001031 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1032 struct snd_soc_dai *codec_dai;
1033 int i, ret;
1034
1035 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1036 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1037 if (ret < 0)
1038 return ret;
1039 }
1040
1041 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1042 if (ret < 0)
1043 return ret;
1044
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001045 for_each_rtd_components(rtd, i, component) {
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001046 ret = snd_soc_component_trigger(component, substream, cmd);
1047 if (ret < 0)
1048 return ret;
1049 }
1050
Kuninori Morimotoad2bf9f2020-01-22 09:44:56 +09001051 ret = soc_rtd_trigger(rtd, substream, cmd);
1052 if (ret < 0)
1053 return ret;
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001054
Liam Girdwoodddee6272011-06-09 14:45:53 +01001055 return 0;
1056}
1057
Peter Ujfalusi4378f1f2019-09-27 10:16:46 +03001058static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1059{
1060 int ret;
1061
1062 switch (cmd) {
1063 case SNDRV_PCM_TRIGGER_START:
1064 case SNDRV_PCM_TRIGGER_RESUME:
1065 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1066 ret = soc_pcm_trigger_start(substream, cmd);
1067 break;
1068 case SNDRV_PCM_TRIGGER_STOP:
1069 case SNDRV_PCM_TRIGGER_SUSPEND:
1070 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1071 ret = soc_pcm_trigger_stop(substream, cmd);
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
1076
1077 return ret;
1078}
1079
Mark Brown45c0a182012-05-09 21:46:27 +01001080static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1081 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001082{
1083 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001084 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001085 struct snd_soc_dai *codec_dai;
1086 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001087
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001088 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001089 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001090 if (ret < 0)
1091 return ret;
1092 }
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001093
Dan Carpenter901e8222019-09-23 17:22:57 +03001094 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
Kuninori Morimoto5c0769af2019-07-22 10:33:56 +09001095 if (ret < 0)
1096 return ret;
1097
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001098 return 0;
1099}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001100/*
1101 * soc level wrapper for pointer callback
Charles Keepaxef050be2018-04-24 16:39:02 +01001102 * If cpu_dai, codec_dai, component driver has the delay callback, then
Liam Girdwoodddee6272011-06-09 14:45:53 +01001103 * the runtime->delay will be updated accordingly.
1104 */
1105static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1106{
1107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001108 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001109 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 snd_pcm_uframes_t offset = 0;
1112 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001113 snd_pcm_sframes_t codec_delay = 0;
1114 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301116 /* clearing the previous total delay */
1117 runtime->delay = 0;
1118
Kuninori Morimoto0035e252019-07-26 13:51:47 +09001119 offset = snd_soc_pcm_component_pointer(substream);
Kuninori Morimotob8135862017-10-11 01:37:23 +00001120
Akshu Agrawal9fb4c2bf2018-08-01 15:37:33 +05301121 /* base delay if assigned in pointer callback */
1122 delay = runtime->delay;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001123
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001124 delay += snd_soc_dai_delay(cpu_dai, substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001125
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001126 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto1dea80d2019-07-22 10:34:09 +09001127 codec_delay = max(codec_delay,
1128 snd_soc_dai_delay(codec_dai, substream));
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001129 }
1130 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001131
Liam Girdwoodddee6272011-06-09 14:45:53 +01001132 runtime->delay = delay;
1133
1134 return offset;
1135}
1136
Liam Girdwood01d75842012-04-25 12:12:49 +01001137/* connect a FE and BE */
1138static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1139 struct snd_soc_pcm_runtime *be, int stream)
1140{
1141 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001142 unsigned long flags;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001143#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001144 char *name;
Takashi Iwaibd0b6092019-11-07 14:48:33 +01001145#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001146
1147 /* only add new dpcms */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001148 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001149 if (dpcm->be == be && dpcm->fe == fe)
1150 return 0;
1151 }
1152
1153 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1154 if (!dpcm)
1155 return -ENOMEM;
1156
1157 dpcm->be = be;
1158 dpcm->fe = fe;
1159 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1160 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001161 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001162 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1163 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001164 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001165
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001166 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001167 stream ? "capture" : "playback", fe->dai_link->name,
1168 stream ? "<-" : "->", be->dai_link->name);
1169
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001170#ifdef CONFIG_DEBUG_FS
Hans de Goede0632fa02019-10-05 23:22:02 +02001171 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1172 stream ? "capture" : "playback");
1173 if (name) {
1174 dpcm->debugfs_state = debugfs_create_dir(name,
1175 fe->debugfs_dpcm_root);
1176 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1177 &dpcm->state);
1178 kfree(name);
1179 }
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001180#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001181 return 1;
1182}
1183
1184/* reparent a BE onto another FE */
1185static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1186 struct snd_soc_pcm_runtime *be, int stream)
1187{
1188 struct snd_soc_dpcm *dpcm;
1189 struct snd_pcm_substream *fe_substream, *be_substream;
1190
1191 /* reparent if BE is connected to other FEs */
1192 if (!be->dpcm[stream].users)
1193 return;
1194
1195 be_substream = snd_soc_dpcm_get_substream(be, stream);
1196
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00001197 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001198 if (dpcm->fe == fe)
1199 continue;
1200
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001201 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001202 stream ? "capture" : "playback",
1203 dpcm->fe->dai_link->name,
1204 stream ? "<-" : "->", dpcm->be->dai_link->name);
1205
1206 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1207 be_substream->runtime = fe_substream->runtime;
1208 break;
1209 }
1210}
1211
1212/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001213void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001214{
1215 struct snd_soc_dpcm *dpcm, *d;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001216 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001217
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001218 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001219 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001220 stream ? "capture" : "playback",
1221 dpcm->be->dai_link->name);
1222
1223 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1224 continue;
1225
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001226 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001227 stream ? "capture" : "playback", fe->dai_link->name,
1228 stream ? "<-" : "->", dpcm->be->dai_link->name);
1229
1230 /* BEs still alive need new FE */
1231 dpcm_be_reparent(fe, dpcm->be, stream);
1232
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001233#ifdef CONFIG_DEBUG_FS
Greg Kroah-Hartmanfee531d2019-07-31 15:17:15 +02001234 debugfs_remove_recursive(dpcm->debugfs_state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001235#endif
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001236 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001237 list_del(&dpcm->list_be);
1238 list_del(&dpcm->list_fe);
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001239 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001240 kfree(dpcm);
1241 }
1242}
1243
1244/* get BE for DAI widget and stream */
1245static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1246 struct snd_soc_dapm_widget *widget, int stream)
1247{
1248 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001249 struct snd_soc_dapm_widget *w;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001250 struct snd_soc_dai *dai;
Mengdong Lin1a497982015-11-18 02:34:11 -05001251 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001252
Liam Girdwood3c146462018-03-14 20:43:51 +00001253 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1254
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001255 for_each_card_rtds(card, be) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001256
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001257 if (!be->dai_link->no_pcm)
1258 continue;
Liam Girdwood35ea0652012-06-05 19:26:59 +01001259
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001260 w = dai_get_widget(be->cpu_dai, stream);
Liam Girdwood3c146462018-03-14 20:43:51 +00001261
Kuninori Morimoto93597fa2020-02-17 17:27:43 +09001262 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1263 w ? w->name : "(not set)");
1264
1265 if (w == widget)
1266 return be;
1267
1268 for_each_rtd_codec_dai(be, i, dai) {
1269 w = dai_get_widget(dai, stream);
1270
1271 if (w == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001272 return be;
Liam Girdwood01d75842012-04-25 12:12:49 +01001273 }
1274 }
1275
Liam Girdwood3c146462018-03-14 20:43:51 +00001276 /* dai link name and stream name set correctly ? */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001277 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001278 stream ? "capture" : "playback", widget->name);
1279 return NULL;
1280}
1281
Liam Girdwood01d75842012-04-25 12:12:49 +01001282static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1283 struct snd_soc_dapm_widget *widget)
1284{
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001285 struct snd_soc_dapm_widget *w;
Liam Girdwood01d75842012-04-25 12:12:49 +01001286 int i;
1287
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001288 for_each_dapm_widgets(list, i, w)
1289 if (widget == w)
Liam Girdwood01d75842012-04-25 12:12:49 +01001290 return 1;
Liam Girdwood01d75842012-04-25 12:12:49 +01001291
1292 return 0;
1293}
1294
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001295static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1296 enum snd_soc_dapm_direction dir)
1297{
1298 struct snd_soc_card *card = widget->dapm->card;
1299 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001300 int stream;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001301
Kuninori Morimotoc2cd8212020-02-17 17:27:48 +09001302 /* adjust dir to stream */
1303 if (dir == SND_SOC_DAPM_DIR_OUT)
1304 stream = SNDRV_PCM_STREAM_PLAYBACK;
1305 else
1306 stream = SNDRV_PCM_STREAM_CAPTURE;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001307
Kuninori Morimoto027a4832020-02-17 17:27:53 +09001308 rtd = dpcm_get_be(card, widget, stream);
1309 if (rtd)
1310 return true;
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001311
1312 return false;
1313}
1314
Liam Girdwood23607022014-01-17 17:03:55 +00001315int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001316 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001317{
1318 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001319 int paths;
1320
Liam Girdwood01d75842012-04-25 12:12:49 +01001321 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001322 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001323 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001324
Liam Girdwood103d84a2012-11-19 14:39:15 +00001325 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001326 stream ? "capture" : "playback");
1327
Liam Girdwood01d75842012-04-25 12:12:49 +01001328 return paths;
1329}
1330
Liam Girdwood01d75842012-04-25 12:12:49 +01001331static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1332 struct snd_soc_dapm_widget_list **list_)
1333{
1334 struct snd_soc_dpcm *dpcm;
1335 struct snd_soc_dapm_widget_list *list = *list_;
1336 struct snd_soc_dapm_widget *widget;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001337 struct snd_soc_dai *dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001338 int prune = 0;
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001339 int do_prune;
Liam Girdwood01d75842012-04-25 12:12:49 +01001340
1341 /* Destroy any old FE <--> BE connections */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001342 for_each_dpcm_be(fe, stream, dpcm) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001343 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001344
1345 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001346 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001347
1348 /* prune the BE if it's no longer in our active list */
1349 if (widget && widget_in_list(list, widget))
1350 continue;
1351
1352 /* is there a valid CODEC DAI widget for this BE */
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001353 do_prune = 1;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001354 for_each_rtd_codec_dai(dpcm->be, i, dai) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001355 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001356
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001357 /* prune the BE if it's no longer in our active list */
1358 if (widget && widget_in_list(list, widget))
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001359 do_prune = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001360 }
Kuninori Morimotobed646d2019-10-15 12:59:38 +09001361 if (!do_prune)
1362 continue;
Liam Girdwood01d75842012-04-25 12:12:49 +01001363
Liam Girdwood103d84a2012-11-19 14:39:15 +00001364 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 stream ? "capture" : "playback",
1366 dpcm->be->dai_link->name, fe->dai_link->name);
1367 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1368 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1369 prune++;
1370 }
1371
Liam Girdwood103d84a2012-11-19 14:39:15 +00001372 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001373 return prune;
1374}
1375
1376static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1377 struct snd_soc_dapm_widget_list **list_)
1378{
1379 struct snd_soc_card *card = fe->card;
1380 struct snd_soc_dapm_widget_list *list = *list_;
1381 struct snd_soc_pcm_runtime *be;
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001382 struct snd_soc_dapm_widget *widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001383 int i, new = 0, err;
1384
1385 /* Create any new FE <--> BE connections */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001386 for_each_dapm_widgets(list, i, widget) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001387
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001388 switch (widget->id) {
Mark Brown46162742013-06-05 19:36:11 +01001389 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001390 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1391 continue;
1392 break;
Mark Brown46162742013-06-05 19:36:11 +01001393 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001394 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1395 continue;
Mark Brown46162742013-06-05 19:36:11 +01001396 break;
1397 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 continue;
Mark Brown46162742013-06-05 19:36:11 +01001399 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001400
1401 /* is there a valid BE rtd for this widget */
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001402 be = dpcm_get_be(card, widget, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001403 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001404 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001405 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001406 continue;
1407 }
1408
1409 /* make sure BE is a real BE */
1410 if (!be->dai_link->no_pcm)
1411 continue;
1412
1413 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001414 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001415 continue;
1416
1417 /* newly connected FE and BE */
1418 err = dpcm_be_connect(fe, be, stream);
1419 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001420 dev_err(fe->dev, "ASoC: can't connect %s\n",
Kuninori Morimoto09e88f82020-02-10 12:14:22 +09001421 widget->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001422 break;
1423 } else if (err == 0) /* already connected */
1424 continue;
1425
1426 /* new */
1427 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1428 new++;
1429 }
1430
Liam Girdwood103d84a2012-11-19 14:39:15 +00001431 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001432 return new;
1433}
1434
1435/*
1436 * Find the corresponding BE DAIs that source or sink audio to this
1437 * FE substream.
1438 */
Liam Girdwood23607022014-01-17 17:03:55 +00001439int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001440 int stream, struct snd_soc_dapm_widget_list **list, int new)
1441{
1442 if (new)
1443 return dpcm_add_paths(fe, stream, list);
1444 else
1445 return dpcm_prune_paths(fe, stream, list);
1446}
1447
Liam Girdwood23607022014-01-17 17:03:55 +00001448void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001449{
1450 struct snd_soc_dpcm *dpcm;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001451 unsigned long flags;
Liam Girdwood01d75842012-04-25 12:12:49 +01001452
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001453 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001454 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01001455 dpcm->be->dpcm[stream].runtime_update =
1456 SND_SOC_DPCM_UPDATE_NO;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08001457 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01001458}
1459
1460static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1461 int stream)
1462{
1463 struct snd_soc_dpcm *dpcm;
1464
1465 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001466 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001467
1468 struct snd_soc_pcm_runtime *be = dpcm->be;
1469 struct snd_pcm_substream *be_substream =
1470 snd_soc_dpcm_get_substream(be, stream);
1471
1472 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001473 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001474 stream ? "capture" : "playback",
1475 be->dpcm[stream].state);
1476
1477 if (--be->dpcm[stream].users != 0)
1478 continue;
1479
1480 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1481 continue;
1482
1483 soc_pcm_close(be_substream);
1484 be_substream->runtime = NULL;
1485 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1486 }
1487}
1488
Liam Girdwood23607022014-01-17 17:03:55 +00001489int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001490{
1491 struct snd_soc_dpcm *dpcm;
1492 int err, count = 0;
1493
1494 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001495 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001496
1497 struct snd_soc_pcm_runtime *be = dpcm->be;
1498 struct snd_pcm_substream *be_substream =
1499 snd_soc_dpcm_get_substream(be, stream);
1500
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001501 if (!be_substream) {
1502 dev_err(be->dev, "ASoC: no backend %s stream\n",
1503 stream ? "capture" : "playback");
1504 continue;
1505 }
1506
Liam Girdwood01d75842012-04-25 12:12:49 +01001507 /* is this op for this BE ? */
1508 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1509 continue;
1510
1511 /* first time the dpcm is open ? */
1512 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001513 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001514 stream ? "capture" : "playback",
1515 be->dpcm[stream].state);
1516
1517 if (be->dpcm[stream].users++ != 0)
1518 continue;
1519
1520 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1521 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1522 continue;
1523
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001524 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1525 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001526
1527 be_substream->runtime = be->dpcm[stream].runtime;
1528 err = soc_pcm_open(be_substream);
1529 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001530 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001531 be->dpcm[stream].users--;
1532 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001533 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001534 stream ? "capture" : "playback",
1535 be->dpcm[stream].state);
1536
1537 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1538 goto unwind;
1539 }
1540
1541 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1542 count++;
1543 }
1544
1545 return count;
1546
1547unwind:
1548 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001549 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001550 struct snd_soc_pcm_runtime *be = dpcm->be;
1551 struct snd_pcm_substream *be_substream =
1552 snd_soc_dpcm_get_substream(be, stream);
1553
1554 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1555 continue;
1556
1557 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001558 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001559 stream ? "capture" : "playback",
1560 be->dpcm[stream].state);
1561
1562 if (--be->dpcm[stream].users != 0)
1563 continue;
1564
1565 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1566 continue;
1567
1568 soc_pcm_close(be_substream);
1569 be_substream->runtime = NULL;
1570 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1571 }
1572
1573 return err;
1574}
1575
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001576static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Jerome Brunet435ffb72018-07-05 12:13:48 +02001577 struct snd_soc_pcm_stream *stream)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001578{
1579 runtime->hw.rate_min = stream->rate_min;
Charles Keepaxe33ffbd9c2018-08-27 14:26:47 +01001580 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001581 runtime->hw.channels_min = stream->channels_min;
1582 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001583 if (runtime->hw.formats)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001584 runtime->hw.formats &= stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001585 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001586 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001587 runtime->hw.rates = stream->rates;
1588}
1589
Jerome Brunet435ffb72018-07-05 12:13:48 +02001590static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1591 u64 *formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001592{
1593 struct snd_soc_pcm_runtime *fe = substream->private_data;
1594 struct snd_soc_dpcm *dpcm;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001595 struct snd_soc_dai *dai;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001596 int stream = substream->stream;
1597
1598 if (!fe->dai_link->dpcm_merged_format)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001599 return;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001600
1601 /*
1602 * It returns merged BE codec format
1603 * if FE want to use it (= dpcm_merged_format)
1604 */
1605
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001606 for_each_dpcm_be(fe, stream, dpcm) {
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001607 struct snd_soc_pcm_runtime *be = dpcm->be;
1608 struct snd_soc_dai_driver *codec_dai_drv;
1609 struct snd_soc_pcm_stream *codec_stream;
1610 int i;
1611
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001612 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunet4febced2018-06-27 17:36:38 +02001613 /*
1614 * Skip CODECs which don't support the current stream
1615 * type. See soc_pcm_init_runtime_hw() for more details
1616 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001617 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunet4febced2018-06-27 17:36:38 +02001618 continue;
1619
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001620 codec_dai_drv = dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001621 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1622 codec_stream = &codec_dai_drv->playback;
1623 else
1624 codec_stream = &codec_dai_drv->capture;
1625
Jerome Brunet435ffb72018-07-05 12:13:48 +02001626 *formats &= codec_stream->formats;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001627 }
1628 }
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001629}
1630
Jerome Brunet435ffb72018-07-05 12:13:48 +02001631static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1632 unsigned int *channels_min,
1633 unsigned int *channels_max)
Jiada Wangf4c277b2018-06-20 18:25:20 +09001634{
1635 struct snd_soc_pcm_runtime *fe = substream->private_data;
1636 struct snd_soc_dpcm *dpcm;
1637 int stream = substream->stream;
1638
1639 if (!fe->dai_link->dpcm_merged_chan)
1640 return;
1641
Jiada Wangf4c277b2018-06-20 18:25:20 +09001642 /*
1643 * It returns merged BE codec channel;
1644 * if FE want to use it (= dpcm_merged_chan)
1645 */
1646
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001647 for_each_dpcm_be(fe, stream, dpcm) {
Jiada Wangf4c277b2018-06-20 18:25:20 +09001648 struct snd_soc_pcm_runtime *be = dpcm->be;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001649 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001650 struct snd_soc_dai_driver *codec_dai_drv;
1651 struct snd_soc_pcm_stream *codec_stream;
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001652 struct snd_soc_pcm_stream *cpu_stream;
Jiada Wangf4c277b2018-06-20 18:25:20 +09001653
Jerome Brunet4f2bd182018-06-27 11:48:18 +02001654 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1655 cpu_stream = &cpu_dai_drv->playback;
1656 else
1657 cpu_stream = &cpu_dai_drv->capture;
1658
1659 *channels_min = max(*channels_min, cpu_stream->channels_min);
1660 *channels_max = min(*channels_max, cpu_stream->channels_max);
1661
1662 /*
1663 * chan min/max cannot be enforced if there are multiple CODEC
1664 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1665 */
1666 if (be->num_codecs == 1) {
1667 codec_dai_drv = be->codec_dais[0]->driver;
1668
Jiada Wangf4c277b2018-06-20 18:25:20 +09001669 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1670 codec_stream = &codec_dai_drv->playback;
1671 else
1672 codec_stream = &codec_dai_drv->capture;
1673
1674 *channels_min = max(*channels_min,
1675 codec_stream->channels_min);
1676 *channels_max = min(*channels_max,
1677 codec_stream->channels_max);
1678 }
1679 }
1680}
1681
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001682static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1683 unsigned int *rates,
1684 unsigned int *rate_min,
1685 unsigned int *rate_max)
1686{
1687 struct snd_soc_pcm_runtime *fe = substream->private_data;
1688 struct snd_soc_dpcm *dpcm;
1689 int stream = substream->stream;
1690
1691 if (!fe->dai_link->dpcm_merged_rate)
1692 return;
1693
1694 /*
1695 * It returns merged BE codec channel;
1696 * if FE want to use it (= dpcm_merged_chan)
1697 */
1698
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001699 for_each_dpcm_be(fe, stream, dpcm) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001700 struct snd_soc_pcm_runtime *be = dpcm->be;
1701 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1702 struct snd_soc_dai_driver *codec_dai_drv;
1703 struct snd_soc_pcm_stream *codec_stream;
1704 struct snd_soc_pcm_stream *cpu_stream;
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001705 struct snd_soc_dai *dai;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001706 int i;
1707
1708 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1709 cpu_stream = &cpu_dai_drv->playback;
1710 else
1711 cpu_stream = &cpu_dai_drv->capture;
1712
1713 *rate_min = max(*rate_min, cpu_stream->rate_min);
1714 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1715 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1716
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001717 for_each_rtd_codec_dai(be, i, dai) {
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001718 /*
1719 * Skip CODECs which don't support the current stream
1720 * type. See soc_pcm_init_runtime_hw() for more details
1721 */
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001722 if (!snd_soc_dai_stream_valid(dai, stream))
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001723 continue;
1724
Kuninori Morimoto7afecb32018-09-18 01:28:04 +00001725 codec_dai_drv = dai->driver;
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001726 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1727 codec_stream = &codec_dai_drv->playback;
1728 else
1729 codec_stream = &codec_dai_drv->capture;
1730
1731 *rate_min = max(*rate_min, codec_stream->rate_min);
1732 *rate_max = min_not_zero(*rate_max,
1733 codec_stream->rate_max);
1734 *rates = snd_pcm_rate_mask_intersect(*rates,
1735 codec_stream->rates);
1736 }
1737 }
1738}
1739
Mark Brown45c0a182012-05-09 21:46:27 +01001740static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001741{
1742 struct snd_pcm_runtime *runtime = substream->runtime;
1743 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1744 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1745 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1746
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001747 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Jerome Brunet435ffb72018-07-05 12:13:48 +02001748 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001749 else
Jerome Brunet435ffb72018-07-05 12:13:48 +02001750 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Jiada Wangf4c277b2018-06-20 18:25:20 +09001751
Jerome Brunet435ffb72018-07-05 12:13:48 +02001752 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1753 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1754 &runtime->hw.channels_max);
Jerome Brunetbaacd8d2018-07-05 12:13:49 +02001755 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1756 &runtime->hw.rate_min, &runtime->hw.rate_max);
Liam Girdwood01d75842012-04-25 12:12:49 +01001757}
1758
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001759static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1760
1761/* Set FE's runtime_update state; the state is protected via PCM stream lock
1762 * for avoiding the race with trigger callback.
1763 * If the state is unset and a trigger is pending while the previous operation,
1764 * process the pending trigger action here.
1765 */
1766static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1767 int stream, enum snd_soc_dpcm_update state)
1768{
1769 struct snd_pcm_substream *substream =
1770 snd_soc_dpcm_get_substream(fe, stream);
1771
1772 snd_pcm_stream_lock_irq(substream);
1773 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1774 dpcm_fe_dai_do_trigger(substream,
1775 fe->dpcm[stream].trigger_pending - 1);
1776 fe->dpcm[stream].trigger_pending = 0;
1777 }
1778 fe->dpcm[stream].runtime_update = state;
1779 snd_pcm_stream_unlock_irq(substream);
1780}
1781
PC Liao906c7d62015-12-11 11:33:51 +08001782static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1783 int stream)
1784{
1785 struct snd_soc_dpcm *dpcm;
1786 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1787 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1788 int err;
1789
1790 /* apply symmetry for FE */
1791 if (soc_pcm_has_symmetry(fe_substream))
1792 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1793
1794 /* Symmetry only applies if we've got an active stream. */
1795 if (fe_cpu_dai->active) {
1796 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1797 if (err < 0)
1798 return err;
1799 }
1800
1801 /* apply symmetry for BE */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001802 for_each_dpcm_be(fe, stream, dpcm) {
PC Liao906c7d62015-12-11 11:33:51 +08001803 struct snd_soc_pcm_runtime *be = dpcm->be;
1804 struct snd_pcm_substream *be_substream =
1805 snd_soc_dpcm_get_substream(be, stream);
Jerome Brunet6246f282019-04-01 15:03:54 +02001806 struct snd_soc_pcm_runtime *rtd;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001807 struct snd_soc_dai *codec_dai;
PC Liao906c7d62015-12-11 11:33:51 +08001808 int i;
1809
Jerome Brunet6246f282019-04-01 15:03:54 +02001810 /* A backend may not have the requested substream */
1811 if (!be_substream)
1812 continue;
1813
1814 rtd = be_substream->private_data;
Jeeja KPf1176612016-09-06 14:17:55 +05301815 if (rtd->dai_link->be_hw_params_fixup)
1816 continue;
1817
PC Liao906c7d62015-12-11 11:33:51 +08001818 if (soc_pcm_has_symmetry(be_substream))
1819 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1820
1821 /* Symmetry only applies if we've got an active stream. */
1822 if (rtd->cpu_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001823 err = soc_pcm_apply_symmetry(fe_substream,
1824 rtd->cpu_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001825 if (err < 0)
1826 return err;
1827 }
1828
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001829 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1830 if (codec_dai->active) {
Kai Chieh Chuang99bcedb2018-05-28 10:18:19 +08001831 err = soc_pcm_apply_symmetry(fe_substream,
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00001832 codec_dai);
PC Liao906c7d62015-12-11 11:33:51 +08001833 if (err < 0)
1834 return err;
1835 }
1836 }
1837 }
1838
1839 return 0;
1840}
1841
Liam Girdwood01d75842012-04-25 12:12:49 +01001842static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1843{
1844 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1845 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1846 int stream = fe_substream->stream, ret = 0;
1847
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001848 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001849
1850 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1851 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001852 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001853 goto be_err;
1854 }
1855
Liam Girdwood103d84a2012-11-19 14:39:15 +00001856 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001857
1858 /* start the DAI frontend */
1859 ret = soc_pcm_open(fe_substream);
1860 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001861 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001862 goto unwind;
1863 }
1864
1865 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1866
1867 dpcm_set_fe_runtime(fe_substream);
1868 snd_pcm_limit_hw_rates(runtime);
1869
PC Liao906c7d62015-12-11 11:33:51 +08001870 ret = dpcm_apply_symmetry(fe_substream, stream);
1871 if (ret < 0) {
1872 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1873 ret);
1874 goto unwind;
1875 }
1876
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001877 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001878 return 0;
1879
1880unwind:
1881 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1882be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001883 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001884 return ret;
1885}
1886
Liam Girdwood23607022014-01-17 17:03:55 +00001887int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001888{
1889 struct snd_soc_dpcm *dpcm;
1890
1891 /* only shutdown BEs that are either sinks or sources to this FE DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001892 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001893
1894 struct snd_soc_pcm_runtime *be = dpcm->be;
1895 struct snd_pcm_substream *be_substream =
1896 snd_soc_dpcm_get_substream(be, stream);
1897
1898 /* is this op for this BE ? */
1899 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1900 continue;
1901
1902 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001903 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001904 stream ? "capture" : "playback",
1905 be->dpcm[stream].state);
1906
1907 if (--be->dpcm[stream].users != 0)
1908 continue;
1909
1910 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Kai Chieh Chuang9c0ac702018-05-28 10:18:18 +08001911 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1912 soc_pcm_hw_free(be_substream);
1913 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1914 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001915
Liam Girdwood103d84a2012-11-19 14:39:15 +00001916 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001917 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001918
1919 soc_pcm_close(be_substream);
1920 be_substream->runtime = NULL;
1921
1922 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1923 }
1924 return 0;
1925}
1926
1927static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1928{
1929 struct snd_soc_pcm_runtime *fe = substream->private_data;
1930 int stream = substream->stream;
1931
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001932 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001933
1934 /* shutdown the BEs */
1935 dpcm_be_dai_shutdown(fe, substream->stream);
1936
Liam Girdwood103d84a2012-11-19 14:39:15 +00001937 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001938
1939 /* now shutdown the frontend */
1940 soc_pcm_close(substream);
1941
1942 /* run the stream event for each BE */
Kuninori Morimotob0edff42020-01-10 11:36:56 +09001943 snd_soc_dapm_stream_stop(fe, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001944
1945 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001946 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001947 return 0;
1948}
1949
Liam Girdwood23607022014-01-17 17:03:55 +00001950int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001951{
1952 struct snd_soc_dpcm *dpcm;
1953
1954 /* only hw_params backends that are either sinks or sources
1955 * to this frontend DAI */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00001956 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001957
1958 struct snd_soc_pcm_runtime *be = dpcm->be;
1959 struct snd_pcm_substream *be_substream =
1960 snd_soc_dpcm_get_substream(be, stream);
1961
1962 /* is this op for this BE ? */
1963 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1964 continue;
1965
1966 /* only free hw when no longer used - check all FEs */
1967 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1968 continue;
1969
Qiao Zhou36fba622014-12-03 10:13:43 +08001970 /* do not free hw if this BE is used by other FE */
1971 if (be->dpcm[stream].users > 1)
1972 continue;
1973
Liam Girdwood01d75842012-04-25 12:12:49 +01001974 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1975 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1976 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001977 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301978 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1979 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001980 continue;
1981
Liam Girdwood103d84a2012-11-19 14:39:15 +00001982 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001983 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001984
1985 soc_pcm_hw_free(be_substream);
1986
1987 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1988 }
1989
1990 return 0;
1991}
1992
Mark Brown45c0a182012-05-09 21:46:27 +01001993static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001994{
1995 struct snd_soc_pcm_runtime *fe = substream->private_data;
1996 int err, stream = substream->stream;
1997
1998 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001999 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002000
Liam Girdwood103d84a2012-11-19 14:39:15 +00002001 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
2003 /* call hw_free on the frontend */
2004 err = soc_pcm_hw_free(substream);
2005 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002006 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002007 fe->dai_link->name);
2008
2009 /* only hw_params backends that are either sinks or sources
2010 * to this frontend DAI */
2011 err = dpcm_be_dai_hw_free(fe, stream);
2012
2013 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002014 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002015
2016 mutex_unlock(&fe->card->mutex);
2017 return 0;
2018}
2019
Liam Girdwood23607022014-01-17 17:03:55 +00002020int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002021{
2022 struct snd_soc_dpcm *dpcm;
2023 int ret;
2024
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002025 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002026
2027 struct snd_soc_pcm_runtime *be = dpcm->be;
2028 struct snd_pcm_substream *be_substream =
2029 snd_soc_dpcm_get_substream(be, stream);
2030
2031 /* is this op for this BE ? */
2032 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2033 continue;
2034
Liam Girdwood01d75842012-04-25 12:12:49 +01002035 /* copy params for each dpcm */
2036 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2037 sizeof(struct snd_pcm_hw_params));
2038
2039 /* perform any hw_params fixups */
2040 if (be->dai_link->be_hw_params_fixup) {
2041 ret = be->dai_link->be_hw_params_fixup(be,
2042 &dpcm->hw_params);
2043 if (ret < 0) {
2044 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002045 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002046 ret);
2047 goto unwind;
2048 }
2049 }
2050
Libin Yangae061d22019-04-19 09:53:12 +08002051 /* copy the fixed-up hw params for BE dai */
2052 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2053 sizeof(struct snd_pcm_hw_params));
2054
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002055 /* only allow hw_params() if no connected FEs are running */
2056 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2057 continue;
2058
2059 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2060 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2061 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2062 continue;
2063
2064 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002065 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002066
Liam Girdwood01d75842012-04-25 12:12:49 +01002067 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2068 if (ret < 0) {
2069 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002070 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002071 goto unwind;
2072 }
2073
2074 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2075 }
2076 return 0;
2077
2078unwind:
2079 /* disable any enabled and non active backends */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002080 for_each_dpcm_be_rollback(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002081 struct snd_soc_pcm_runtime *be = dpcm->be;
2082 struct snd_pcm_substream *be_substream =
2083 snd_soc_dpcm_get_substream(be, stream);
2084
2085 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2086 continue;
2087
2088 /* only allow hw_free() if no connected FEs are running */
2089 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2090 continue;
2091
2092 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2093 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2094 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2096 continue;
2097
2098 soc_pcm_hw_free(be_substream);
2099 }
2100
2101 return ret;
2102}
2103
Mark Brown45c0a182012-05-09 21:46:27 +01002104static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2105 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002106{
2107 struct snd_soc_pcm_runtime *fe = substream->private_data;
2108 int ret, stream = substream->stream;
2109
2110 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002111 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002112
2113 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2114 sizeof(struct snd_pcm_hw_params));
2115 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2116 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002117 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002118 goto out;
2119 }
2120
Liam Girdwood103d84a2012-11-19 14:39:15 +00002121 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002122 fe->dai_link->name, params_rate(params),
2123 params_channels(params), params_format(params));
2124
2125 /* call hw_params on the frontend */
2126 ret = soc_pcm_hw_params(substream, params);
2127 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002128 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002129 dpcm_be_dai_hw_free(fe, stream);
2130 } else
2131 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2132
2133out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002134 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002135 mutex_unlock(&fe->card->mutex);
2136 return ret;
2137}
2138
2139static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2140 struct snd_pcm_substream *substream, int cmd)
2141{
2142 int ret;
2143
Liam Girdwood103d84a2012-11-19 14:39:15 +00002144 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002145 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002146
2147 ret = soc_pcm_trigger(substream, cmd);
2148 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002149 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002150
2151 return ret;
2152}
2153
Liam Girdwood23607022014-01-17 17:03:55 +00002154int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002155 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002156{
2157 struct snd_soc_dpcm *dpcm;
2158 int ret = 0;
2159
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002160 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002161
2162 struct snd_soc_pcm_runtime *be = dpcm->be;
2163 struct snd_pcm_substream *be_substream =
2164 snd_soc_dpcm_get_substream(be, stream);
2165
2166 /* is this op for this BE ? */
2167 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2168 continue;
2169
2170 switch (cmd) {
2171 case SNDRV_PCM_TRIGGER_START:
2172 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2173 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2174 continue;
2175
2176 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2177 if (ret)
2178 return ret;
2179
2180 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2181 break;
2182 case SNDRV_PCM_TRIGGER_RESUME:
2183 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2184 continue;
2185
2186 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2187 if (ret)
2188 return ret;
2189
2190 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2191 break;
2192 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2193 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2194 continue;
2195
2196 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2197 if (ret)
2198 return ret;
2199
2200 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2201 break;
2202 case SNDRV_PCM_TRIGGER_STOP:
2203 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2204 continue;
2205
2206 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2207 continue;
2208
2209 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2210 if (ret)
2211 return ret;
2212
2213 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2214 break;
2215 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002216 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002217 continue;
2218
2219 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2220 continue;
2221
2222 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2223 if (ret)
2224 return ret;
2225
2226 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2227 break;
2228 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2229 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2230 continue;
2231
2232 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2233 continue;
2234
2235 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2236 if (ret)
2237 return ret;
2238
2239 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2240 break;
2241 }
2242 }
2243
2244 return ret;
2245}
2246EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2247
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002248static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2249 int cmd, bool fe_first)
2250{
2251 struct snd_soc_pcm_runtime *fe = substream->private_data;
2252 int ret;
2253
2254 /* call trigger on the frontend before the backend. */
2255 if (fe_first) {
2256 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2257 fe->dai_link->name, cmd);
2258
2259 ret = soc_pcm_trigger(substream, cmd);
2260 if (ret < 0)
2261 return ret;
2262
2263 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2264 return ret;
2265 }
2266
2267 /* call trigger on the frontend after the backend. */
2268 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2269 if (ret < 0)
2270 return ret;
2271
2272 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2273 fe->dai_link->name, cmd);
2274
2275 ret = soc_pcm_trigger(substream, cmd);
2276
2277 return ret;
2278}
2279
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002280static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002281{
2282 struct snd_soc_pcm_runtime *fe = substream->private_data;
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002283 int stream = substream->stream;
2284 int ret = 0;
Liam Girdwood01d75842012-04-25 12:12:49 +01002285 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2286
2287 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2288
2289 switch (trigger) {
2290 case SND_SOC_DPCM_TRIGGER_PRE:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002291 switch (cmd) {
2292 case SNDRV_PCM_TRIGGER_START:
2293 case SNDRV_PCM_TRIGGER_RESUME:
2294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2295 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2296 break;
2297 case SNDRV_PCM_TRIGGER_STOP:
2298 case SNDRV_PCM_TRIGGER_SUSPEND:
2299 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2300 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2301 break;
2302 default:
2303 ret = -EINVAL;
2304 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002305 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002306 break;
2307 case SND_SOC_DPCM_TRIGGER_POST:
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002308 switch (cmd) {
2309 case SNDRV_PCM_TRIGGER_START:
2310 case SNDRV_PCM_TRIGGER_RESUME:
2311 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2312 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2313 break;
2314 case SNDRV_PCM_TRIGGER_STOP:
2315 case SNDRV_PCM_TRIGGER_SUSPEND:
2316 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2317 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2318 break;
2319 default:
2320 ret = -EINVAL;
2321 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002322 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002323 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002324 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2325 /* bespoke trigger() - handles both FE and BEs */
2326
Liam Girdwood103d84a2012-11-19 14:39:15 +00002327 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002328 fe->dai_link->name, cmd);
2329
2330 ret = soc_pcm_bespoke_trigger(substream, cmd);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002331 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002332 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002333 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002334 fe->dai_link->name);
2335 ret = -EINVAL;
2336 goto out;
2337 }
2338
Ranjani Sridharanacbf2772019-11-04 14:48:11 -08002339 if (ret < 0) {
2340 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2341 cmd, ret);
2342 goto out;
2343 }
2344
Liam Girdwood01d75842012-04-25 12:12:49 +01002345 switch (cmd) {
2346 case SNDRV_PCM_TRIGGER_START:
2347 case SNDRV_PCM_TRIGGER_RESUME:
2348 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2349 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2350 break;
2351 case SNDRV_PCM_TRIGGER_STOP:
2352 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002353 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2354 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002355 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2356 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2357 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002358 }
2359
2360out:
2361 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2362 return ret;
2363}
2364
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002365static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2366{
2367 struct snd_soc_pcm_runtime *fe = substream->private_data;
2368 int stream = substream->stream;
2369
2370 /* if FE's runtime_update is already set, we're in race;
2371 * process this trigger later at exit
2372 */
2373 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2374 fe->dpcm[stream].trigger_pending = cmd + 1;
2375 return 0; /* delayed, assuming it's successful */
2376 }
2377
2378 /* we're alone, let's trigger */
2379 return dpcm_fe_dai_do_trigger(substream, cmd);
2380}
2381
Liam Girdwood23607022014-01-17 17:03:55 +00002382int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002383{
2384 struct snd_soc_dpcm *dpcm;
2385 int ret = 0;
2386
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002387 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002388
2389 struct snd_soc_pcm_runtime *be = dpcm->be;
2390 struct snd_pcm_substream *be_substream =
2391 snd_soc_dpcm_get_substream(be, stream);
2392
2393 /* is this op for this BE ? */
2394 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2395 continue;
2396
2397 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002398 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
Libin Yang5087a8f2019-05-08 10:32:41 +08002399 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2400 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
Liam Girdwood01d75842012-04-25 12:12:49 +01002401 continue;
2402
Liam Girdwood103d84a2012-11-19 14:39:15 +00002403 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002404 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002405
2406 ret = soc_pcm_prepare(be_substream);
2407 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002408 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002409 ret);
2410 break;
2411 }
2412
2413 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2414 }
2415 return ret;
2416}
2417
Mark Brown45c0a182012-05-09 21:46:27 +01002418static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002419{
2420 struct snd_soc_pcm_runtime *fe = substream->private_data;
2421 int stream = substream->stream, ret = 0;
2422
2423 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2424
Liam Girdwood103d84a2012-11-19 14:39:15 +00002425 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002426
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002427 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002428
2429 /* there is no point preparing this FE if there are no BEs */
2430 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002431 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002432 fe->dai_link->name);
2433 ret = -EINVAL;
2434 goto out;
2435 }
2436
2437 ret = dpcm_be_dai_prepare(fe, substream->stream);
2438 if (ret < 0)
2439 goto out;
2440
2441 /* call prepare on the frontend */
2442 ret = soc_pcm_prepare(substream);
2443 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002444 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002445 fe->dai_link->name);
2446 goto out;
2447 }
2448
2449 /* run the stream event for each BE */
2450 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2451 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2452
2453out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002454 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002455 mutex_unlock(&fe->card->mutex);
2456
2457 return ret;
2458}
2459
Liam Girdwood618dae12012-04-25 12:12:51 +01002460static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2461{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002462 struct snd_pcm_substream *substream =
2463 snd_soc_dpcm_get_substream(fe, stream);
2464 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002465 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002466
Liam Girdwood103d84a2012-11-19 14:39:15 +00002467 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002468 stream ? "capture" : "playback", fe->dai_link->name);
2469
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002470 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2471 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002472 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002473 fe->dai_link->name);
2474
2475 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2476 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002477 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002478 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002479 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002480 fe->dai_link->name);
2481
2482 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2483 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002484 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002485 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002486
2487 err = dpcm_be_dai_hw_free(fe, stream);
2488 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002489 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002490
2491 err = dpcm_be_dai_shutdown(fe, stream);
2492 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002493 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002494
2495 /* run the stream event for each BE */
2496 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2497
2498 return 0;
2499}
2500
2501static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2502{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002503 struct snd_pcm_substream *substream =
2504 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002505 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002506 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002507 int ret;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002508 unsigned long flags;
Liam Girdwood618dae12012-04-25 12:12:51 +01002509
Liam Girdwood103d84a2012-11-19 14:39:15 +00002510 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002511 stream ? "capture" : "playback", fe->dai_link->name);
2512
2513 /* Only start the BE if the FE is ready */
2514 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2515 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2516 return -EINVAL;
2517
2518 /* startup must always be called for new BEs */
2519 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002520 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002521 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002522
2523 /* keep going if FE state is > open */
2524 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2525 return 0;
2526
2527 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002528 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002529 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002530
2531 /* keep going if FE state is > hw_params */
2532 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2533 return 0;
2534
2535
2536 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002537 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002538 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002539
2540 /* run the stream event for each BE */
2541 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2542
2543 /* keep going if FE state is > prepare */
2544 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2545 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2546 return 0;
2547
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002548 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2549 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002550 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002551 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002552
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002553 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2554 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002555 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002556 goto hw_free;
2557 }
2558 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002559 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002560 fe->dai_link->name);
2561
2562 ret = dpcm_be_dai_trigger(fe, stream,
2563 SNDRV_PCM_TRIGGER_START);
2564 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002565 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002566 goto hw_free;
2567 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002568 }
2569
2570 return 0;
2571
2572hw_free:
2573 dpcm_be_dai_hw_free(fe, stream);
2574close:
2575 dpcm_be_dai_shutdown(fe, stream);
2576disconnect:
2577 /* disconnect any non started BEs */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002578 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002579 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002580 struct snd_soc_pcm_runtime *be = dpcm->be;
2581 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2582 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2583 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002584 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood618dae12012-04-25 12:12:51 +01002585
2586 return ret;
2587}
2588
2589static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2590{
2591 int ret;
2592
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002593 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002594 ret = dpcm_run_update_startup(fe, stream);
2595 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002596 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002597 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002598
2599 return ret;
2600}
2601
2602static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2603{
2604 int ret;
2605
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002606 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002607 ret = dpcm_run_update_shutdown(fe, stream);
2608 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002609 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002610 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002611
2612 return ret;
2613}
2614
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002615static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2616{
2617 struct snd_soc_dapm_widget_list *list;
2618 int count, paths;
2619
2620 if (!fe->dai_link->dynamic)
2621 return 0;
2622
2623 /* only check active links */
2624 if (!fe->cpu_dai->active)
2625 return 0;
2626
2627 /* DAPM sync will call this to update DSP paths */
2628 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2629 new ? "new" : "old", fe->dai_link->name);
2630
2631 /* skip if FE doesn't have playback capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002632 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2633 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002634 goto capture;
2635
2636 /* skip if FE isn't currently playing */
2637 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2638 goto capture;
2639
2640 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2641 if (paths < 0) {
2642 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2643 fe->dai_link->name, "playback");
2644 return paths;
2645 }
2646
2647 /* update any playback paths */
2648 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2649 if (count) {
2650 if (new)
2651 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2652 else
2653 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2654
2655 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2656 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2657 }
2658
2659 dpcm_path_put(&list);
2660
2661capture:
2662 /* skip if FE doesn't have capture capability */
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002663 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2664 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002665 return 0;
2666
2667 /* skip if FE isn't currently capturing */
2668 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2669 return 0;
2670
2671 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2672 if (paths < 0) {
2673 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2674 fe->dai_link->name, "capture");
2675 return paths;
2676 }
2677
2678 /* update any old capture paths */
2679 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2680 if (count) {
2681 if (new)
2682 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2683 else
2684 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2685
2686 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2687 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2688 }
2689
2690 dpcm_path_put(&list);
2691
2692 return 0;
2693}
2694
Liam Girdwood618dae12012-04-25 12:12:51 +01002695/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2696 * any DAI links.
2697 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002698int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002699{
Mengdong Lin1a497982015-11-18 02:34:11 -05002700 struct snd_soc_pcm_runtime *fe;
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002701 int ret = 0;
Liam Girdwood618dae12012-04-25 12:12:51 +01002702
Liam Girdwood618dae12012-04-25 12:12:51 +01002703 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002704 /* shutdown all old paths first */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002705 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002706 ret = soc_dpcm_fe_runtime_update(fe, 0);
2707 if (ret)
2708 goto out;
Liam Girdwood618dae12012-04-25 12:12:51 +01002709 }
2710
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002711 /* bring new paths up */
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00002712 for_each_card_rtds(card, fe) {
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002713 ret = soc_dpcm_fe_runtime_update(fe, 1);
2714 if (ret)
2715 goto out;
2716 }
2717
2718out:
Liam Girdwood618dae12012-04-25 12:12:51 +01002719 mutex_unlock(&card->mutex);
Jerome Brunetde15d7ff2018-06-26 12:07:25 +02002720 return ret;
Liam Girdwood618dae12012-04-25 12:12:51 +01002721}
Liam Girdwood01d75842012-04-25 12:12:49 +01002722
Mark Brown45c0a182012-05-09 21:46:27 +01002723static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002724{
2725 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2726 struct snd_soc_dpcm *dpcm;
2727 struct snd_soc_dapm_widget_list *list;
2728 int ret;
2729 int stream = fe_substream->stream;
2730
2731 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2732 fe->dpcm[stream].runtime = fe_substream->runtime;
2733
Qiao Zhou8f70e512014-09-10 17:54:07 +08002734 ret = dpcm_path_get(fe, stream, &list);
2735 if (ret < 0) {
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002736 goto open_end;
Qiao Zhou8f70e512014-09-10 17:54:07 +08002737 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002738 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002739 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002740 }
2741
2742 /* calculate valid and active FE <-> BE dpcms */
2743 dpcm_process_paths(fe, stream, &list, 1);
2744
2745 ret = dpcm_fe_dai_startup(fe_substream);
2746 if (ret < 0) {
2747 /* clean up all links */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002748 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002749 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2750
2751 dpcm_be_disconnect(fe, stream);
2752 fe->dpcm[stream].runtime = NULL;
2753 }
2754
2755 dpcm_clear_pending_state(fe, stream);
2756 dpcm_path_put(&list);
Kuninori Morimotocae06eb2020-02-17 17:28:11 +09002757open_end:
Liam Girdwood01d75842012-04-25 12:12:49 +01002758 mutex_unlock(&fe->card->mutex);
2759 return ret;
2760}
2761
Mark Brown45c0a182012-05-09 21:46:27 +01002762static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002763{
2764 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2765 struct snd_soc_dpcm *dpcm;
2766 int stream = fe_substream->stream, ret;
2767
2768 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2769 ret = dpcm_fe_dai_shutdown(fe_substream);
2770
2771 /* mark FE's links ready to prune */
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00002772 for_each_dpcm_be(fe, stream, dpcm)
Liam Girdwood01d75842012-04-25 12:12:49 +01002773 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2774
2775 dpcm_be_disconnect(fe, stream);
2776
2777 fe->dpcm[stream].runtime = NULL;
2778 mutex_unlock(&fe->card->mutex);
2779 return ret;
2780}
2781
Liam Girdwoodddee6272011-06-09 14:45:53 +01002782/* create a new pcm */
2783int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2784{
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002785 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002786 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002787 struct snd_soc_component *component;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002788 struct snd_pcm *pcm;
2789 char new_name[64];
2790 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002791 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002792
Liam Girdwood01d75842012-04-25 12:12:49 +01002793 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002794 playback = rtd->dai_link->dpcm_playback;
2795 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002796 } else {
Jerome Bruneta3420312019-07-25 18:59:47 +02002797 /* Adapt stream for codec2codec links */
Stephan Gerholda4877a62020-02-18 11:38:24 +01002798 int cpu_capture = rtd->dai_link->params ?
2799 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2800 int cpu_playback = rtd->dai_link->params ?
2801 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Jerome Bruneta3420312019-07-25 18:59:47 +02002802
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +00002803 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002804 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002805 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002806 playback = 1;
Kuninori Morimoto467fece2019-07-22 10:36:16 +09002807 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
Stephan Gerholda4877a62020-02-18 11:38:24 +01002808 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002809 capture = 1;
2810 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002811 }
Sangsu Parka5002312012-01-02 17:15:10 +09002812
Fabio Estevamd6bead02013-08-29 10:32:13 -03002813 if (rtd->dai_link->playback_only) {
2814 playback = 1;
2815 capture = 0;
2816 }
2817
2818 if (rtd->dai_link->capture_only) {
2819 playback = 0;
2820 capture = 1;
2821 }
2822
Liam Girdwood01d75842012-04-25 12:12:49 +01002823 /* create the PCM */
Jerome Bruneta3420312019-07-25 18:59:47 +02002824 if (rtd->dai_link->params) {
2825 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2826 rtd->dai_link->stream_name);
2827
2828 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2829 playback, capture, &pcm);
2830 } else if (rtd->dai_link->no_pcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002831 snprintf(new_name, sizeof(new_name), "(%s)",
2832 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002833
Liam Girdwood01d75842012-04-25 12:12:49 +01002834 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2835 playback, capture, &pcm);
2836 } else {
2837 if (rtd->dai_link->dynamic)
2838 snprintf(new_name, sizeof(new_name), "%s (*)",
2839 rtd->dai_link->stream_name);
2840 else
2841 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002842 rtd->dai_link->stream_name,
2843 (rtd->num_codecs > 1) ?
2844 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002845
Liam Girdwood01d75842012-04-25 12:12:49 +01002846 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2847 capture, &pcm);
2848 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002849 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002850 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002851 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002852 return ret;
2853 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002854 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002855
2856 /* DAPM dai link stream work */
Jerome Bruneta3420312019-07-25 18:59:47 +02002857 if (rtd->dai_link->params)
Curtis Malainey4bf2e382019-12-03 09:30:07 -08002858 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
Jerome Bruneta3420312019-07-25 18:59:47 +02002859 else
Kuninori Morimoto83f94a22020-01-10 11:36:17 +09002860 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002861
Vinod Koul48c76992015-02-12 09:59:53 +05302862 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002863 rtd->pcm = pcm;
2864 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002865
Jerome Bruneta3420312019-07-25 18:59:47 +02002866 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002867 if (playback)
2868 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2869 if (capture)
2870 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2871 goto out;
2872 }
2873
2874 /* ASoC PCM operations */
2875 if (rtd->dai_link->dynamic) {
2876 rtd->ops.open = dpcm_fe_dai_open;
2877 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2878 rtd->ops.prepare = dpcm_fe_dai_prepare;
2879 rtd->ops.trigger = dpcm_fe_dai_trigger;
2880 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2881 rtd->ops.close = dpcm_fe_dai_close;
2882 rtd->ops.pointer = soc_pcm_pointer;
2883 } else {
2884 rtd->ops.open = soc_pcm_open;
2885 rtd->ops.hw_params = soc_pcm_hw_params;
2886 rtd->ops.prepare = soc_pcm_prepare;
2887 rtd->ops.trigger = soc_pcm_trigger;
2888 rtd->ops.hw_free = soc_pcm_hw_free;
2889 rtd->ops.close = soc_pcm_close;
2890 rtd->ops.pointer = soc_pcm_pointer;
2891 }
2892
Kuninori Morimoto613fb502020-01-10 11:35:21 +09002893 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09002894 const struct snd_soc_component_driver *drv = component->driver;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002895
Takashi Iwai3b1c9522019-11-21 20:07:08 +01002896 if (drv->ioctl)
2897 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +01002898 if (drv->sync_stop)
2899 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002900 if (drv->copy_user)
Kuninori Morimoto82d81f52019-07-26 13:51:56 +09002901 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002902 if (drv->page)
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09002903 rtd->ops.page = snd_soc_pcm_component_page;
Kuninori Morimotoe9067bb2019-10-02 14:35:13 +09002904 if (drv->mmap)
Kuninori Morimoto205875e2019-07-26 13:52:04 +09002905 rtd->ops.mmap = snd_soc_pcm_component_mmap;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002906 }
2907
Liam Girdwoodddee6272011-06-09 14:45:53 +01002908 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002909 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002910
2911 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002912 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002913
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09002914 ret = snd_soc_pcm_component_new(rtd);
Kuninori Morimoto74842912019-07-26 13:52:08 +09002915 if (ret < 0) {
2916 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2917 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002918 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02002919
Takashi Iwai3d21ef02019-01-11 15:58:39 +01002920 pcm->no_device_suspend = true;
Liam Girdwood01d75842012-04-25 12:12:49 +01002921out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002922 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2923 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2924 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002925 return ret;
2926}
Liam Girdwood01d75842012-04-25 12:12:49 +01002927
2928/* is the current PCM operation for this FE ? */
2929int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2930{
2931 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2932 return 1;
2933 return 0;
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2936
2937/* is the current PCM operation for this BE ? */
2938int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2939 struct snd_soc_pcm_runtime *be, int stream)
2940{
2941 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2942 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2943 be->dpcm[stream].runtime_update))
2944 return 1;
2945 return 0;
2946}
2947EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2948
2949/* get the substream for this BE */
2950struct snd_pcm_substream *
2951 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2952{
2953 return be->pcm->streams[stream].substream;
2954}
2955EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2956
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002957static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2958 struct snd_soc_pcm_runtime *be,
2959 int stream,
2960 const enum snd_soc_dpcm_state *states,
2961 int num_states)
Liam Girdwood01d75842012-04-25 12:12:49 +01002962{
2963 struct snd_soc_dpcm *dpcm;
2964 int state;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002965 int ret = 1;
2966 unsigned long flags;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002967 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002968
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002969 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimotod2e24d62018-09-18 01:30:54 +00002970 for_each_dpcm_fe(be, stream, dpcm) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002971
2972 if (dpcm->fe == fe)
2973 continue;
2974
2975 state = dpcm->fe->dpcm[stream].state;
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002976 for (i = 0; i < num_states; i++) {
2977 if (state == states[i]) {
2978 ret = 0;
2979 break;
2980 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002981 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002982 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002983 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwood01d75842012-04-25 12:12:49 +01002984
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002985 /* it's safe to do this BE DAI */
KaiChieh Chuanga9764862019-03-08 13:05:53 +08002986 return ret;
Liam Girdwood01d75842012-04-25 12:12:49 +01002987}
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09002988
2989/*
2990 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2991 * are not running, paused or suspended for the specified stream direction.
2992 */
2993int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2994 struct snd_soc_pcm_runtime *be, int stream)
2995{
2996 const enum snd_soc_dpcm_state state[] = {
2997 SND_SOC_DPCM_STATE_START,
2998 SND_SOC_DPCM_STATE_PAUSED,
2999 SND_SOC_DPCM_STATE_SUSPEND,
3000 };
3001
3002 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3003}
Liam Girdwood01d75842012-04-25 12:12:49 +01003004EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3005
3006/*
3007 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3008 * running, paused or suspended for the specified stream direction.
3009 */
3010int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3011 struct snd_soc_pcm_runtime *be, int stream)
3012{
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003013 const enum snd_soc_dpcm_state state[] = {
3014 SND_SOC_DPCM_STATE_START,
3015 SND_SOC_DPCM_STATE_PAUSED,
3016 SND_SOC_DPCM_STATE_SUSPEND,
3017 SND_SOC_DPCM_STATE_PREPARE,
3018 };
Liam Girdwood01d75842012-04-25 12:12:49 +01003019
Kuninori Morimoto085d22b2020-02-17 17:28:07 +09003020 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
Liam Girdwood01d75842012-04-25 12:12:49 +01003021}
3022EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003023
3024#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003025static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003026{
3027 switch (state) {
3028 case SND_SOC_DPCM_STATE_NEW:
3029 return "new";
3030 case SND_SOC_DPCM_STATE_OPEN:
3031 return "open";
3032 case SND_SOC_DPCM_STATE_HW_PARAMS:
3033 return "hw_params";
3034 case SND_SOC_DPCM_STATE_PREPARE:
3035 return "prepare";
3036 case SND_SOC_DPCM_STATE_START:
3037 return "start";
3038 case SND_SOC_DPCM_STATE_STOP:
3039 return "stop";
3040 case SND_SOC_DPCM_STATE_SUSPEND:
3041 return "suspend";
3042 case SND_SOC_DPCM_STATE_PAUSED:
3043 return "paused";
3044 case SND_SOC_DPCM_STATE_HW_FREE:
3045 return "hw_free";
3046 case SND_SOC_DPCM_STATE_CLOSE:
3047 return "close";
3048 }
3049
3050 return "unknown";
3051}
3052
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003053static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3054 int stream, char *buf, size_t size)
3055{
3056 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3057 struct snd_soc_dpcm *dpcm;
3058 ssize_t offset = 0;
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003059 unsigned long flags;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003060
3061 /* FE state */
3062 offset += snprintf(buf + offset, size - offset,
3063 "[%s - %s]\n", fe->dai_link->name,
3064 stream ? "Capture" : "Playback");
3065
3066 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3067 dpcm_state_string(fe->dpcm[stream].state));
3068
3069 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3070 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3071 offset += snprintf(buf + offset, size - offset,
3072 "Hardware Params: "
3073 "Format = %s, Channels = %d, Rate = %d\n",
3074 snd_pcm_format_name(params_format(params)),
3075 params_channels(params),
3076 params_rate(params));
3077
3078 /* BEs state */
3079 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3080
3081 if (list_empty(&fe->dpcm[stream].be_clients)) {
3082 offset += snprintf(buf + offset, size - offset,
3083 " No active DSP links\n");
3084 goto out;
3085 }
3086
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003087 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
Kuninori Morimoto8d6258a2018-09-18 01:31:09 +00003088 for_each_dpcm_be(fe, stream, dpcm) {
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003089 struct snd_soc_pcm_runtime *be = dpcm->be;
3090 params = &dpcm->hw_params;
3091
3092 offset += snprintf(buf + offset, size - offset,
3093 "- %s\n", be->dai_link->name);
3094
3095 offset += snprintf(buf + offset, size - offset,
3096 " State: %s\n",
3097 dpcm_state_string(be->dpcm[stream].state));
3098
3099 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3100 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3101 offset += snprintf(buf + offset, size - offset,
3102 " Hardware Params: "
3103 "Format = %s, Channels = %d, Rate = %d\n",
3104 snd_pcm_format_name(params_format(params)),
3105 params_channels(params),
3106 params_rate(params));
3107 }
KaiChieh Chuanga9764862019-03-08 13:05:53 +08003108 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003109out:
3110 return offset;
3111}
3112
3113static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3114 size_t count, loff_t *ppos)
3115{
3116 struct snd_soc_pcm_runtime *fe = file->private_data;
3117 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3118 char *buf;
3119
3120 buf = kmalloc(out_count, GFP_KERNEL);
3121 if (!buf)
3122 return -ENOMEM;
3123
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003124 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003125 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3126 buf + offset, out_count - offset);
3127
Kuninori Morimoto467fece2019-07-22 10:36:16 +09003128 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003129 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3130 buf + offset, out_count - offset);
3131
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003132 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003133
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003134 kfree(buf);
3135 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003136}
3137
3138static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003139 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003140 .read = dpcm_state_read_file,
3141 .llseek = default_llseek,
3142};
3143
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003144void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003145{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003146 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003147 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003148
Kuninori Morimoto596becd2019-08-07 10:31:36 +09003149 if (!rtd->dai_link->dynamic)
3150 return;
3151
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003152 if (!rtd->card->debugfs_card_root)
3153 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003154
3155 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3156 rtd->card->debugfs_card_root);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003157
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003158 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3159 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003160}
3161#endif