Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // 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> |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/pinctrl/consumer.h> |
| 17 | #include <linux/pm_runtime.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/workqueue.h> |
| 20 | #include <linux/export.h> |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/soc-dpcm.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 27 | #include <sound/soc-link.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | #include <sound/initval.h> |
| 29 | |
| 30 | #define DPCM_MAX_BE_USERS 8 |
| 31 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_DEBUG_FS |
| 33 | static const char *dpcm_state_string(enum snd_soc_dpcm_state state) |
| 34 | { |
| 35 | switch (state) { |
| 36 | case SND_SOC_DPCM_STATE_NEW: |
| 37 | return "new"; |
| 38 | case SND_SOC_DPCM_STATE_OPEN: |
| 39 | return "open"; |
| 40 | case SND_SOC_DPCM_STATE_HW_PARAMS: |
| 41 | return "hw_params"; |
| 42 | case SND_SOC_DPCM_STATE_PREPARE: |
| 43 | return "prepare"; |
| 44 | case SND_SOC_DPCM_STATE_START: |
| 45 | return "start"; |
| 46 | case SND_SOC_DPCM_STATE_STOP: |
| 47 | return "stop"; |
| 48 | case SND_SOC_DPCM_STATE_SUSPEND: |
| 49 | return "suspend"; |
| 50 | case SND_SOC_DPCM_STATE_PAUSED: |
| 51 | return "paused"; |
| 52 | case SND_SOC_DPCM_STATE_HW_FREE: |
| 53 | return "hw_free"; |
| 54 | case SND_SOC_DPCM_STATE_CLOSE: |
| 55 | return "close"; |
| 56 | } |
| 57 | |
| 58 | return "unknown"; |
| 59 | } |
| 60 | |
| 61 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
| 62 | int stream, char *buf, size_t size) |
| 63 | { |
| 64 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; |
| 65 | struct snd_soc_dpcm *dpcm; |
| 66 | ssize_t offset = 0; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | /* FE state */ |
| 70 | offset += scnprintf(buf + offset, size - offset, |
| 71 | "[%s - %s]\n", fe->dai_link->name, |
| 72 | stream ? "Capture" : "Playback"); |
| 73 | |
| 74 | offset += scnprintf(buf + offset, size - offset, "State: %s\n", |
| 75 | dpcm_state_string(fe->dpcm[stream].state)); |
| 76 | |
| 77 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 78 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 79 | offset += scnprintf(buf + offset, size - offset, |
| 80 | "Hardware Params: " |
| 81 | "Format = %s, Channels = %d, Rate = %d\n", |
| 82 | snd_pcm_format_name(params_format(params)), |
| 83 | params_channels(params), |
| 84 | params_rate(params)); |
| 85 | |
| 86 | /* BEs state */ |
| 87 | offset += scnprintf(buf + offset, size - offset, "Backends:\n"); |
| 88 | |
| 89 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 90 | offset += scnprintf(buf + offset, size - offset, |
| 91 | " No active DSP links\n"); |
| 92 | goto out; |
| 93 | } |
| 94 | |
| 95 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
| 96 | for_each_dpcm_be(fe, stream, dpcm) { |
| 97 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 98 | params = &dpcm->hw_params; |
| 99 | |
| 100 | offset += scnprintf(buf + offset, size - offset, |
| 101 | "- %s\n", be->dai_link->name); |
| 102 | |
| 103 | offset += scnprintf(buf + offset, size - offset, |
| 104 | " State: %s\n", |
| 105 | dpcm_state_string(be->dpcm[stream].state)); |
| 106 | |
| 107 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 108 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 109 | offset += scnprintf(buf + offset, size - offset, |
| 110 | " Hardware Params: " |
| 111 | "Format = %s, Channels = %d, Rate = %d\n", |
| 112 | snd_pcm_format_name(params_format(params)), |
| 113 | params_channels(params), |
| 114 | params_rate(params)); |
| 115 | } |
| 116 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
| 117 | out: |
| 118 | return offset; |
| 119 | } |
| 120 | |
| 121 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, |
| 122 | size_t count, loff_t *ppos) |
| 123 | { |
| 124 | struct snd_soc_pcm_runtime *fe = file->private_data; |
| 125 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; |
| 126 | int stream; |
| 127 | char *buf; |
| 128 | |
| 129 | if (fe->num_cpus > 1) { |
| 130 | dev_err(fe->dev, |
| 131 | "%s doesn't support Multi CPU yet\n", __func__); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | buf = kmalloc(out_count, GFP_KERNEL); |
| 136 | if (!buf) |
| 137 | return -ENOMEM; |
| 138 | |
| 139 | for_each_pcm_streams(stream) |
| 140 | if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream)) |
| 141 | offset += dpcm_show_state(fe, stream, |
| 142 | buf + offset, |
| 143 | out_count - offset); |
| 144 | |
| 145 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
| 146 | |
| 147 | kfree(buf); |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static const struct file_operations dpcm_state_fops = { |
| 152 | .open = simple_open, |
| 153 | .read = dpcm_state_read_file, |
| 154 | .llseek = default_llseek, |
| 155 | }; |
| 156 | |
| 157 | void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) |
| 158 | { |
| 159 | if (!rtd->dai_link) |
| 160 | return; |
| 161 | |
| 162 | if (!rtd->dai_link->dynamic) |
| 163 | return; |
| 164 | |
| 165 | if (!rtd->card->debugfs_card_root) |
| 166 | return; |
| 167 | |
| 168 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
| 169 | rtd->card->debugfs_card_root); |
| 170 | |
| 171 | debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, |
| 172 | rtd, &dpcm_state_fops); |
| 173 | } |
| 174 | |
| 175 | static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream) |
| 176 | { |
| 177 | char *name; |
| 178 | |
| 179 | name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name, |
| 180 | stream ? "capture" : "playback"); |
| 181 | if (name) { |
| 182 | dpcm->debugfs_state = debugfs_create_dir( |
| 183 | name, dpcm->fe->debugfs_dpcm_root); |
| 184 | debugfs_create_u32("state", 0644, dpcm->debugfs_state, |
| 185 | &dpcm->state); |
| 186 | kfree(name); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) |
| 191 | { |
| 192 | debugfs_remove_recursive(dpcm->debugfs_state); |
| 193 | } |
| 194 | |
| 195 | #else |
| 196 | static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, |
| 197 | int stream) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) |
| 202 | { |
| 203 | } |
| 204 | #endif |
| 205 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 206 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 207 | * snd_soc_runtime_action() - Increment/Decrement active count for |
| 208 | * PCM runtime components |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 209 | * @rtd: ASoC PCM runtime that is activated |
| 210 | * @stream: Direction of the PCM stream |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 211 | * @action: Activate stream if 1. Deactivate if -1. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 212 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 213 | * Increments/Decrements the active count for all the DAIs and components |
| 214 | * attached to a PCM runtime. |
| 215 | * Should typically be called when a stream is opened. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 216 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 217 | * Must be called with the rtd->card->pcm_mutex being held |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 218 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 219 | void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, |
| 220 | int stream, int action) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 221 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 222 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 223 | int i; |
| 224 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | lockdep_assert_held(&rtd->card->pcm_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 227 | for_each_rtd_dais(rtd, i, dai) |
| 228 | snd_soc_dai_action(dai, stream, action); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 229 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(snd_soc_runtime_action); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay |
| 234 | * @rtd: The ASoC PCM runtime that should be checked. |
| 235 | * |
| 236 | * This function checks whether the power down delay should be ignored for a |
| 237 | * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has |
| 238 | * been configured to ignore the delay, or if none of the components benefits |
| 239 | * from having the delay. |
| 240 | */ |
| 241 | bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) |
| 242 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | struct snd_soc_component *component; |
| 244 | bool ignore = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 245 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 246 | |
| 247 | if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) |
| 248 | return true; |
| 249 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 250 | for_each_rtd_components(rtd, i, component) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 251 | ignore &= !component->driver->use_pmdown_time; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 252 | |
| 253 | return ignore; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters |
| 258 | * @substream: the pcm substream |
| 259 | * @hw: the hardware parameters |
| 260 | * |
| 261 | * Sets the substream runtime hardware parameters. |
| 262 | */ |
| 263 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, |
| 264 | const struct snd_pcm_hardware *hw) |
| 265 | { |
| 266 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 267 | runtime->hw.info = hw->info; |
| 268 | runtime->hw.formats = hw->formats; |
| 269 | runtime->hw.period_bytes_min = hw->period_bytes_min; |
| 270 | runtime->hw.period_bytes_max = hw->period_bytes_max; |
| 271 | runtime->hw.periods_min = hw->periods_min; |
| 272 | runtime->hw.periods_max = hw->periods_max; |
| 273 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; |
| 274 | runtime->hw.fifo_size = hw->fifo_size; |
| 275 | return 0; |
| 276 | } |
| 277 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); |
| 278 | |
| 279 | /* DPCM stream event, send event to FE and all active BEs. */ |
| 280 | int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, |
| 281 | int event) |
| 282 | { |
| 283 | struct snd_soc_dpcm *dpcm; |
| 284 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 285 | for_each_dpcm_be(fe, dir, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 286 | |
| 287 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 288 | |
| 289 | dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", |
| 290 | be->dai_link->name, event, dir); |
| 291 | |
| 292 | if ((event == SND_SOC_DAPM_STREAM_STOP) && |
| 293 | (be->dpcm[dir].users >= 1)) |
| 294 | continue; |
| 295 | |
| 296 | snd_soc_dapm_stream_event(be, dir, event); |
| 297 | } |
| 298 | |
| 299 | snd_soc_dapm_stream_event(fe, dir, event); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, |
| 305 | struct snd_soc_dai *soc_dai) |
| 306 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 307 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 308 | int ret; |
| 309 | |
| 310 | if (soc_dai->rate && (soc_dai->driver->symmetric_rates || |
| 311 | rtd->dai_link->symmetric_rates)) { |
| 312 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", |
| 313 | soc_dai->rate); |
| 314 | |
| 315 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
| 316 | SNDRV_PCM_HW_PARAM_RATE, |
| 317 | soc_dai->rate); |
| 318 | if (ret < 0) { |
| 319 | dev_err(soc_dai->dev, |
| 320 | "ASoC: Unable to apply rate constraint: %d\n", |
| 321 | ret); |
| 322 | return ret; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (soc_dai->channels && (soc_dai->driver->symmetric_channels || |
| 327 | rtd->dai_link->symmetric_channels)) { |
| 328 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", |
| 329 | soc_dai->channels); |
| 330 | |
| 331 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
| 332 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 333 | soc_dai->channels); |
| 334 | if (ret < 0) { |
| 335 | dev_err(soc_dai->dev, |
| 336 | "ASoC: Unable to apply channel symmetry constraint: %d\n", |
| 337 | ret); |
| 338 | return ret; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || |
| 343 | rtd->dai_link->symmetric_samplebits)) { |
| 344 | dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", |
| 345 | soc_dai->sample_bits); |
| 346 | |
| 347 | ret = snd_pcm_hw_constraint_single(substream->runtime, |
| 348 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 349 | soc_dai->sample_bits); |
| 350 | if (ret < 0) { |
| 351 | dev_err(soc_dai->dev, |
| 352 | "ASoC: Unable to apply sample bits symmetry constraint: %d\n", |
| 353 | ret); |
| 354 | return ret; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, |
| 362 | struct snd_pcm_hw_params *params) |
| 363 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 364 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 365 | struct snd_soc_dai *dai; |
| 366 | struct snd_soc_dai *cpu_dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 367 | unsigned int rate, channels, sample_bits, symmetry, i; |
| 368 | |
| 369 | rate = params_rate(params); |
| 370 | channels = params_channels(params); |
| 371 | sample_bits = snd_pcm_format_physical_width(params_format(params)); |
| 372 | |
| 373 | /* reject unmatched parameters when applying symmetry */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 374 | symmetry = rtd->dai_link->symmetric_rates; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 375 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 376 | for_each_rtd_cpu_dais(rtd, i, dai) |
| 377 | symmetry |= dai->driver->symmetric_rates; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 378 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 379 | if (symmetry) { |
| 380 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 381 | if (cpu_dai->rate && cpu_dai->rate != rate) { |
| 382 | dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", |
| 383 | cpu_dai->rate, rate); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 389 | symmetry = rtd->dai_link->symmetric_channels; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 390 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 391 | for_each_rtd_dais(rtd, i, dai) |
| 392 | symmetry |= dai->driver->symmetric_channels; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 393 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 394 | if (symmetry) { |
| 395 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 396 | if (cpu_dai->channels && |
| 397 | cpu_dai->channels != channels) { |
| 398 | dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", |
| 399 | cpu_dai->channels, channels); |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 405 | symmetry = rtd->dai_link->symmetric_samplebits; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 406 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 407 | for_each_rtd_dais(rtd, i, dai) |
| 408 | symmetry |= dai->driver->symmetric_samplebits; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 409 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 410 | if (symmetry) { |
| 411 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 412 | if (cpu_dai->sample_bits && |
| 413 | cpu_dai->sample_bits != sample_bits) { |
| 414 | dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", |
| 415 | cpu_dai->sample_bits, sample_bits); |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) |
| 425 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 426 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 427 | struct snd_soc_dai_link *link = rtd->dai_link; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 428 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 429 | unsigned int symmetry, i; |
| 430 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 431 | symmetry = link->symmetric_rates || |
| 432 | link->symmetric_channels || |
| 433 | link->symmetric_samplebits; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 434 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 435 | for_each_rtd_dais(rtd, i, dai) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 436 | symmetry = symmetry || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 437 | dai->driver->symmetric_rates || |
| 438 | dai->driver->symmetric_channels || |
| 439 | dai->driver->symmetric_samplebits; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 440 | |
| 441 | return symmetry; |
| 442 | } |
| 443 | |
| 444 | static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) |
| 445 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 446 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 447 | int ret; |
| 448 | |
| 449 | if (!bits) |
| 450 | return; |
| 451 | |
| 452 | ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); |
| 453 | if (ret != 0) |
| 454 | dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", |
| 455 | bits, ret); |
| 456 | } |
| 457 | |
| 458 | static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) |
| 459 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 460 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 461 | struct snd_soc_dai *cpu_dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 462 | struct snd_soc_dai *codec_dai; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 463 | struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; |
| 464 | int stream = substream->stream; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 465 | int i; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 466 | unsigned int bits = 0, cpu_bits = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 467 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 468 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
| 469 | pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); |
| 470 | |
| 471 | if (pcm_codec->sig_bits == 0) { |
| 472 | bits = 0; |
| 473 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 474 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 475 | bits = max(pcm_codec->sig_bits, bits); |
| 476 | } |
| 477 | |
| 478 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 479 | pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); |
| 480 | |
| 481 | if (pcm_cpu->sig_bits == 0) { |
| 482 | cpu_bits = 0; |
| 483 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 484 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 485 | cpu_bits = max(pcm_cpu->sig_bits, cpu_bits); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | soc_pcm_set_msb(substream, bits); |
| 489 | soc_pcm_set_msb(substream, cpu_bits); |
| 490 | } |
| 491 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 492 | /** |
| 493 | * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream |
| 494 | * @rtd: ASoC PCM runtime |
| 495 | * @hw: PCM hardware parameters (output) |
| 496 | * @stream: Direction of the PCM stream |
| 497 | * |
| 498 | * Calculates the subset of stream parameters supported by all DAIs |
| 499 | * associated with the PCM stream. |
| 500 | */ |
| 501 | int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd, |
| 502 | struct snd_pcm_hardware *hw, int stream) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 503 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 504 | struct snd_soc_dai *codec_dai; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 505 | struct snd_soc_dai *cpu_dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 506 | struct snd_soc_pcm_stream *codec_stream; |
| 507 | struct snd_soc_pcm_stream *cpu_stream; |
| 508 | unsigned int chan_min = 0, chan_max = UINT_MAX; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 509 | unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 510 | unsigned int rate_min = 0, rate_max = UINT_MAX; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 511 | unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX; |
| 512 | unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 513 | u64 formats = ULLONG_MAX; |
| 514 | int i; |
| 515 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 516 | /* first calculate min/max only for CPUs in the DAI link */ |
| 517 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 518 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 519 | /* |
| 520 | * Skip CPUs which don't support the current stream type. |
| 521 | * Otherwise, since the rate, channel, and format values will |
| 522 | * zero in that case, we would have no usable settings left, |
| 523 | * causing the resulting setup to fail. |
| 524 | */ |
| 525 | if (!snd_soc_dai_stream_valid(cpu_dai, stream)) |
| 526 | continue; |
| 527 | |
| 528 | cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream); |
| 529 | |
| 530 | cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min); |
| 531 | cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max); |
| 532 | cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min); |
| 533 | cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max); |
| 534 | formats &= cpu_stream->formats; |
| 535 | cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates, |
| 536 | cpu_rates); |
| 537 | } |
| 538 | |
| 539 | /* second calculate min/max only for CODECs in the DAI link */ |
| 540 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Skip CODECs which don't support the current stream type. |
| 544 | * Otherwise, since the rate, channel, and format values will |
| 545 | * zero in that case, we would have no usable settings left, |
| 546 | * causing the resulting setup to fail. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 547 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 548 | if (!snd_soc_dai_stream_valid(codec_dai, stream)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 549 | continue; |
| 550 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 551 | codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); |
| 552 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 553 | chan_min = max(chan_min, codec_stream->channels_min); |
| 554 | chan_max = min(chan_max, codec_stream->channels_max); |
| 555 | rate_min = max(rate_min, codec_stream->rate_min); |
| 556 | rate_max = min_not_zero(rate_max, codec_stream->rate_max); |
| 557 | formats &= codec_stream->formats; |
| 558 | rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); |
| 559 | } |
| 560 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 561 | /* Verify both a valid CPU DAI and a valid CODEC DAI were found */ |
| 562 | if (!chan_min || !cpu_chan_min) |
| 563 | return -EINVAL; |
| 564 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 565 | /* |
| 566 | * chan min/max cannot be enforced if there are multiple CODEC DAIs |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 567 | * connected to CPU DAI(s), use CPU DAI's directly and let |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 568 | * channel allocation be fixed up later |
| 569 | */ |
| 570 | if (rtd->num_codecs > 1) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 571 | chan_min = cpu_chan_min; |
| 572 | chan_max = cpu_chan_max; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 575 | /* finally find a intersection between CODECs and CPUs */ |
| 576 | hw->channels_min = max(chan_min, cpu_chan_min); |
| 577 | hw->channels_max = min(chan_max, cpu_chan_max); |
| 578 | hw->formats = formats; |
| 579 | hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 580 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 581 | snd_pcm_hw_limit_rates(hw); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 582 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 583 | hw->rate_min = max(hw->rate_min, cpu_rate_min); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | hw->rate_min = max(hw->rate_min, rate_min); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 585 | hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 586 | hw->rate_max = min_not_zero(hw->rate_max, rate_max); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 587 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 588 | return 0; |
| 589 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 590 | EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 591 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 592 | static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 593 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 594 | struct snd_pcm_hardware *hw = &substream->runtime->hw; |
| 595 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 596 | u64 formats = hw->formats; |
| 597 | |
| 598 | /* |
| 599 | * At least one CPU and one CODEC should match. Otherwise, we should |
| 600 | * have bailed out on a higher level, since there would be no CPU or |
| 601 | * CODEC to support the transfer direction in that case. |
| 602 | */ |
| 603 | snd_soc_runtime_calc_hw(rtd, hw, substream->stream); |
| 604 | |
| 605 | if (formats) |
| 606 | hw->formats &= formats; |
| 607 | } |
| 608 | |
| 609 | static int soc_pcm_components_open(struct snd_pcm_substream *substream) |
| 610 | { |
| 611 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 612 | struct snd_soc_component *component; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 613 | int i, ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 614 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 615 | for_each_rtd_components(rtd, i, component) { |
| 616 | ret = snd_soc_component_module_get_when_open(component, substream); |
| 617 | if (ret < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 618 | break; |
| 619 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 620 | ret = snd_soc_component_open(component, substream); |
| 621 | if (ret < 0) |
| 622 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 623 | } |
| 624 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 625 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 628 | static int soc_pcm_components_close(struct snd_pcm_substream *substream, |
| 629 | int rollback) |
| 630 | { |
| 631 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 632 | struct snd_soc_component *component; |
| 633 | int i, r, ret = 0; |
| 634 | |
| 635 | for_each_rtd_components(rtd, i, component) { |
| 636 | r = snd_soc_component_close(component, substream, rollback); |
| 637 | if (r < 0) |
| 638 | ret = r; /* use last ret */ |
| 639 | |
| 640 | snd_soc_component_module_put_when_close(component, substream, rollback); |
| 641 | } |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback) |
| 647 | { |
| 648 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 649 | struct snd_soc_component *component; |
| 650 | struct snd_soc_dai *dai; |
| 651 | int i; |
| 652 | |
| 653 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
| 654 | |
| 655 | if (!rollback) |
| 656 | snd_soc_runtime_deactivate(rtd, substream->stream); |
| 657 | |
| 658 | for_each_rtd_dais(rtd, i, dai) |
| 659 | snd_soc_dai_shutdown(dai, substream, rollback); |
| 660 | |
| 661 | snd_soc_link_shutdown(substream, rollback); |
| 662 | |
| 663 | soc_pcm_components_close(substream, rollback); |
| 664 | |
| 665 | if (!rollback) |
| 666 | snd_soc_dapm_stream_stop(rtd, substream->stream); |
| 667 | |
| 668 | mutex_unlock(&rtd->card->pcm_mutex); |
| 669 | |
| 670 | snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback); |
| 671 | |
| 672 | for_each_rtd_components(rtd, i, component) |
| 673 | if (!snd_soc_component_active(component)) |
| 674 | pinctrl_pm_select_sleep_state(component->dev); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 681 | * freed here. The cpu DAI, codec DAI, machine and components are also |
| 682 | * shutdown. |
| 683 | */ |
| 684 | static int soc_pcm_close(struct snd_pcm_substream *substream) |
| 685 | { |
| 686 | return soc_pcm_clean(substream, 0); |
| 687 | } |
| 688 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 689 | /* |
| 690 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is |
| 691 | * then initialized and any private data can be allocated. This also calls |
| 692 | * startup for the cpu DAI, component, machine and codec DAI. |
| 693 | */ |
| 694 | static int soc_pcm_open(struct snd_pcm_substream *substream) |
| 695 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 696 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 697 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 698 | struct snd_soc_component *component; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 699 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 700 | const char *codec_dai_name = "multicodec"; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 701 | const char *cpu_dai_name = "multicpu"; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 702 | int i, ret = 0; |
| 703 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 704 | for_each_rtd_components(rtd, i, component) |
| 705 | pinctrl_pm_select_default_state(component->dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 706 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 707 | ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream); |
| 708 | if (ret < 0) |
| 709 | goto pm_err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 710 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 711 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 712 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 713 | ret = soc_pcm_components_open(substream); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 714 | if (ret < 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 715 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 716 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 717 | ret = snd_soc_link_startup(substream); |
| 718 | if (ret < 0) |
| 719 | goto err; |
| 720 | |
| 721 | /* startup the audio subsystem */ |
| 722 | for_each_rtd_dais(rtd, i, dai) { |
| 723 | ret = snd_soc_dai_startup(dai, substream); |
| 724 | if (ret < 0) |
| 725 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* Dynamic PCM DAI links compat checks use dynamic capabilities */ |
| 729 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) |
| 730 | goto dynamic; |
| 731 | |
| 732 | /* Check that the codec and cpu DAIs are compatible */ |
| 733 | soc_pcm_init_runtime_hw(substream); |
| 734 | |
| 735 | if (rtd->num_codecs == 1) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 736 | codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name; |
| 737 | |
| 738 | if (rtd->num_cpus == 1) |
| 739 | cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 740 | |
| 741 | if (soc_pcm_has_symmetry(substream)) |
| 742 | runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 743 | |
| 744 | ret = -EINVAL; |
| 745 | if (!runtime->hw.rates) { |
| 746 | printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 747 | codec_dai_name, cpu_dai_name); |
| 748 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 749 | } |
| 750 | if (!runtime->hw.formats) { |
| 751 | printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 752 | codec_dai_name, cpu_dai_name); |
| 753 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 754 | } |
| 755 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || |
| 756 | runtime->hw.channels_min > runtime->hw.channels_max) { |
| 757 | printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 758 | codec_dai_name, cpu_dai_name); |
| 759 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | soc_pcm_apply_msb(substream); |
| 763 | |
| 764 | /* Symmetry only applies if we've already got an active stream. */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 765 | for_each_rtd_dais(rtd, i, dai) { |
| 766 | if (snd_soc_dai_active(dai)) { |
| 767 | ret = soc_pcm_apply_symmetry(substream, dai); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 768 | if (ret != 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 769 | goto err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | pr_debug("ASoC: %s <-> %s info:\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 774 | codec_dai_name, cpu_dai_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 775 | pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); |
| 776 | pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, |
| 777 | runtime->hw.channels_max); |
| 778 | pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, |
| 779 | runtime->hw.rate_max); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | dynamic: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 781 | snd_soc_runtime_activate(rtd, substream->stream); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 782 | ret = 0; |
| 783 | err: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 784 | mutex_unlock(&rtd->card->pcm_mutex); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 785 | pm_err: |
| 786 | if (ret < 0) |
| 787 | soc_pcm_clean(substream, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 788 | |
| 789 | return ret; |
| 790 | } |
| 791 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 792 | static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 793 | { |
| 794 | /* |
| 795 | * Currently nothing to do for c2c links |
| 796 | * Since c2c links are internal nodes in the DAPM graph and |
| 797 | * don't interface with the outside world or application layer |
| 798 | * we don't have to do any special handling on close. |
| 799 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 803 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 804 | * rate, etc. This function is non atomic and can be called multiple times, |
| 805 | * it can refer to the runtime info. |
| 806 | */ |
| 807 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 808 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 809 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 810 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 811 | int i, ret = 0; |
| 812 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 813 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 814 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 815 | ret = snd_soc_link_prepare(substream); |
| 816 | if (ret < 0) |
| 817 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 818 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 819 | ret = snd_soc_pcm_component_prepare(substream); |
| 820 | if (ret < 0) |
| 821 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 822 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 823 | ret = snd_soc_pcm_dai_prepare(substream); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 824 | if (ret < 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 825 | dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 826 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* cancel any delayed stream shutdown that is pending */ |
| 830 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 831 | rtd->pop_wait) { |
| 832 | rtd->pop_wait = 0; |
| 833 | cancel_delayed_work(&rtd->delayed_work); |
| 834 | } |
| 835 | |
| 836 | snd_soc_dapm_stream_event(rtd, substream->stream, |
| 837 | SND_SOC_DAPM_STREAM_START); |
| 838 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 839 | for_each_rtd_dais(rtd, i, dai) |
| 840 | snd_soc_dai_digital_mute(dai, 0, substream->stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 841 | |
| 842 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 843 | mutex_unlock(&rtd->card->pcm_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, |
| 848 | unsigned int mask) |
| 849 | { |
| 850 | struct snd_interval *interval; |
| 851 | int channels = hweight_long(mask); |
| 852 | |
| 853 | interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 854 | interval->min = channels; |
| 855 | interval->max = channels; |
| 856 | } |
| 857 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 858 | /* |
| 859 | * Called by ALSA when the hardware params are set by application. This |
| 860 | * function can also be called multiple times and can allocate buffers |
| 861 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 862 | */ |
| 863 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 864 | struct snd_pcm_hw_params *params) |
| 865 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 866 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 867 | struct snd_soc_component *component; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 868 | struct snd_soc_dai *cpu_dai; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 869 | struct snd_soc_dai *codec_dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 870 | int i, ret = 0; |
| 871 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 872 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 873 | |
| 874 | ret = soc_pcm_params_symmetry(substream, params); |
| 875 | if (ret) |
| 876 | goto out; |
| 877 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 878 | ret = snd_soc_link_hw_params(substream, params); |
| 879 | if (ret < 0) |
| 880 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 881 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 882 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 883 | struct snd_pcm_hw_params codec_params; |
| 884 | |
| 885 | /* |
| 886 | * Skip CODECs which don't support the current stream type, |
| 887 | * the idea being that if a CODEC is not used for the currently |
| 888 | * set up transfer direction, it should not need to be |
| 889 | * configured, especially since the configuration used might |
| 890 | * not even be supported by that CODEC. There may be cases |
| 891 | * however where a CODEC needs to be set up although it is |
| 892 | * actually not being used for the transfer, e.g. if a |
| 893 | * capture-only CODEC is acting as an LRCLK and/or BCLK master |
| 894 | * for the DAI link including a playback-only CODEC. |
| 895 | * If this becomes necessary, we will have to augment the |
| 896 | * machine driver setup with information on how to act, so |
| 897 | * we can do the right thing here. |
| 898 | */ |
| 899 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) |
| 900 | continue; |
| 901 | |
| 902 | /* copy params for each codec */ |
| 903 | codec_params = *params; |
| 904 | |
| 905 | /* fixup params based on TDM slot masks */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 906 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 907 | codec_dai->tx_mask) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 908 | soc_pcm_codec_params_fixup(&codec_params, |
| 909 | codec_dai->tx_mask); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 910 | |
| 911 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && |
| 912 | codec_dai->rx_mask) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 913 | soc_pcm_codec_params_fixup(&codec_params, |
| 914 | codec_dai->rx_mask); |
| 915 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 916 | ret = snd_soc_dai_hw_params(codec_dai, substream, |
| 917 | &codec_params); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 918 | if(ret < 0) |
| 919 | goto codec_err; |
| 920 | |
| 921 | codec_dai->rate = params_rate(&codec_params); |
| 922 | codec_dai->channels = params_channels(&codec_params); |
| 923 | codec_dai->sample_bits = snd_pcm_format_physical_width( |
| 924 | params_format(&codec_params)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 925 | |
| 926 | snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 929 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 930 | /* |
| 931 | * Skip CPUs which don't support the current stream |
| 932 | * type. See soc_pcm_init_runtime_hw() for more details |
| 933 | */ |
| 934 | if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) |
| 935 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 936 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 937 | ret = snd_soc_dai_hw_params(cpu_dai, substream, params); |
| 938 | if (ret < 0) |
| 939 | goto interface_err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 940 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 941 | /* store the parameters for each DAI */ |
| 942 | cpu_dai->rate = params_rate(params); |
| 943 | cpu_dai->channels = params_channels(params); |
| 944 | cpu_dai->sample_bits = |
| 945 | snd_pcm_format_physical_width(params_format(params)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 946 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 947 | snd_soc_dapm_update_dai(substream, params, cpu_dai); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 948 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 949 | |
| 950 | ret = snd_soc_pcm_component_hw_params(substream, params, &component); |
| 951 | if (ret < 0) |
| 952 | goto component_err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 953 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 954 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 955 | mutex_unlock(&rtd->card->pcm_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 956 | return ret; |
| 957 | |
| 958 | component_err: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 959 | snd_soc_pcm_component_hw_free(substream, component); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 960 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 961 | i = rtd->num_cpus; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 962 | |
| 963 | interface_err: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 964 | for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) { |
| 965 | if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) |
| 966 | continue; |
| 967 | |
| 968 | snd_soc_dai_hw_free(cpu_dai, substream); |
| 969 | cpu_dai->rate = 0; |
| 970 | } |
| 971 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 972 | i = rtd->num_codecs; |
| 973 | |
| 974 | codec_err: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 975 | for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 976 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) |
| 977 | continue; |
| 978 | |
| 979 | snd_soc_dai_hw_free(codec_dai, substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 980 | codec_dai->rate = 0; |
| 981 | } |
| 982 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 983 | snd_soc_link_hw_free(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 984 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 985 | mutex_unlock(&rtd->card->pcm_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Frees resources allocated by hw_params, can be called multiple times |
| 991 | */ |
| 992 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 993 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 994 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 995 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 996 | int i; |
| 997 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 998 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 999 | |
| 1000 | /* clear the corresponding DAIs parameters when going to be inactive */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1001 | for_each_rtd_dais(rtd, i, dai) { |
| 1002 | int active = snd_soc_dai_stream_active(dai, substream->stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1003 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1004 | if (snd_soc_dai_active(dai) == 1) { |
| 1005 | dai->rate = 0; |
| 1006 | dai->channels = 0; |
| 1007 | dai->sample_bits = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1008 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1009 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1010 | if (active == 1) |
| 1011 | snd_soc_dai_digital_mute(dai, 1, substream->stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* free any machine hw params */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1015 | snd_soc_link_hw_free(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1016 | |
| 1017 | /* free any component resources */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1018 | snd_soc_pcm_component_hw_free(substream, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* now free hw params for the DAIs */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1021 | for_each_rtd_dais(rtd, i, dai) { |
| 1022 | if (!snd_soc_dai_stream_valid(dai, substream->stream)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1023 | continue; |
| 1024 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1025 | snd_soc_dai_hw_free(dai, substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1028 | mutex_unlock(&rtd->card->pcm_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 1033 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1034 | int ret = -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1035 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1036 | switch (cmd) { |
| 1037 | case SNDRV_PCM_TRIGGER_START: |
| 1038 | case SNDRV_PCM_TRIGGER_RESUME: |
| 1039 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1040 | ret = snd_soc_link_trigger(substream, cmd); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1041 | if (ret < 0) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1042 | break; |
| 1043 | |
| 1044 | ret = snd_soc_pcm_component_trigger(substream, cmd); |
| 1045 | if (ret < 0) |
| 1046 | break; |
| 1047 | |
| 1048 | ret = snd_soc_pcm_dai_trigger(substream, cmd); |
| 1049 | break; |
| 1050 | case SNDRV_PCM_TRIGGER_STOP: |
| 1051 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1052 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1053 | ret = snd_soc_pcm_dai_trigger(substream, cmd); |
| 1054 | if (ret < 0) |
| 1055 | break; |
| 1056 | |
| 1057 | ret = snd_soc_pcm_component_trigger(substream, cmd); |
| 1058 | if (ret < 0) |
| 1059 | break; |
| 1060 | |
| 1061 | ret = snd_soc_link_trigger(substream, cmd); |
| 1062 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1065 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1068 | /* |
| 1069 | * soc level wrapper for pointer callback |
| 1070 | * If cpu_dai, codec_dai, component driver has the delay callback, then |
| 1071 | * the runtime->delay will be updated accordingly. |
| 1072 | */ |
| 1073 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) |
| 1074 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1075 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 1076 | struct snd_soc_dai *cpu_dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1077 | struct snd_soc_dai *codec_dai; |
| 1078 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1079 | snd_pcm_uframes_t offset = 0; |
| 1080 | snd_pcm_sframes_t delay = 0; |
| 1081 | snd_pcm_sframes_t codec_delay = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1082 | snd_pcm_sframes_t cpu_delay = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1083 | int i; |
| 1084 | |
| 1085 | /* clearing the previous total delay */ |
| 1086 | runtime->delay = 0; |
| 1087 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1088 | offset = snd_soc_pcm_component_pointer(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1089 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1090 | /* base delay if assigned in pointer callback */ |
| 1091 | delay = runtime->delay; |
| 1092 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1093 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 1094 | cpu_delay = max(cpu_delay, |
| 1095 | snd_soc_dai_delay(cpu_dai, substream)); |
| 1096 | } |
| 1097 | delay += cpu_delay; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1098 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1099 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1100 | codec_delay = max(codec_delay, |
| 1101 | snd_soc_dai_delay(codec_dai, substream)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1102 | } |
| 1103 | delay += codec_delay; |
| 1104 | |
| 1105 | runtime->delay = delay; |
| 1106 | |
| 1107 | return offset; |
| 1108 | } |
| 1109 | |
| 1110 | /* connect a FE and BE */ |
| 1111 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, |
| 1112 | struct snd_soc_pcm_runtime *be, int stream) |
| 1113 | { |
| 1114 | struct snd_soc_dpcm *dpcm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1115 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* only add new dpcms */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1118 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1119 | if (dpcm->be == be && dpcm->fe == fe) |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); |
| 1124 | if (!dpcm) |
| 1125 | return -ENOMEM; |
| 1126 | |
| 1127 | dpcm->be = be; |
| 1128 | dpcm->fe = fe; |
| 1129 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; |
| 1130 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1131 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1132 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); |
| 1133 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1134 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1135 | |
| 1136 | dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", |
| 1137 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1138 | stream ? "<-" : "->", be->dai_link->name); |
| 1139 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1140 | dpcm_create_debugfs_state(dpcm, stream); |
| 1141 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1142 | return 1; |
| 1143 | } |
| 1144 | |
| 1145 | /* reparent a BE onto another FE */ |
| 1146 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, |
| 1147 | struct snd_soc_pcm_runtime *be, int stream) |
| 1148 | { |
| 1149 | struct snd_soc_dpcm *dpcm; |
| 1150 | struct snd_pcm_substream *fe_substream, *be_substream; |
| 1151 | |
| 1152 | /* reparent if BE is connected to other FEs */ |
| 1153 | if (!be->dpcm[stream].users) |
| 1154 | return; |
| 1155 | |
| 1156 | be_substream = snd_soc_dpcm_get_substream(be, stream); |
| 1157 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1158 | for_each_dpcm_fe(be, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1159 | if (dpcm->fe == fe) |
| 1160 | continue; |
| 1161 | |
| 1162 | dev_dbg(fe->dev, "reparent %s path %s %s %s\n", |
| 1163 | stream ? "capture" : "playback", |
| 1164 | dpcm->fe->dai_link->name, |
| 1165 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1166 | |
| 1167 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); |
| 1168 | be_substream->runtime = fe_substream->runtime; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /* disconnect a BE and FE */ |
| 1174 | void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
| 1175 | { |
| 1176 | struct snd_soc_dpcm *dpcm, *d; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1177 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1178 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1179 | for_each_dpcm_be_safe(fe, stream, dpcm, d) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1180 | dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", |
| 1181 | stream ? "capture" : "playback", |
| 1182 | dpcm->be->dai_link->name); |
| 1183 | |
| 1184 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) |
| 1185 | continue; |
| 1186 | |
| 1187 | dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", |
| 1188 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1189 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1190 | |
| 1191 | /* BEs still alive need new FE */ |
| 1192 | dpcm_be_reparent(fe, dpcm->be, stream); |
| 1193 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1194 | dpcm_remove_debugfs_state(dpcm); |
| 1195 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1196 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1197 | list_del(&dpcm->list_be); |
| 1198 | list_del(&dpcm->list_fe); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1199 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1200 | kfree(dpcm); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* get BE for DAI widget and stream */ |
| 1205 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, |
| 1206 | struct snd_soc_dapm_widget *widget, int stream) |
| 1207 | { |
| 1208 | struct snd_soc_pcm_runtime *be; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1209 | struct snd_soc_dapm_widget *w; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1210 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1211 | int i; |
| 1212 | |
| 1213 | dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); |
| 1214 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1215 | for_each_card_rtds(card, be) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1216 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1217 | if (!be->dai_link->no_pcm) |
| 1218 | continue; |
| 1219 | |
| 1220 | for_each_rtd_dais(be, i, dai) { |
| 1221 | w = snd_soc_dai_get_widget(dai, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1222 | |
| 1223 | dev_dbg(card->dev, "ASoC: try BE : %s\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1224 | w ? w->name : "(not set)"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1225 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1226 | if (w == widget) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1227 | return be; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1231 | /* Widget provided is not a BE */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1235 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, |
| 1236 | struct snd_soc_dapm_widget *widget) |
| 1237 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1238 | struct snd_soc_dapm_widget *w; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1239 | int i; |
| 1240 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1241 | for_each_dapm_widgets(list, i, w) |
| 1242 | if (widget == w) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1243 | return 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, |
| 1249 | enum snd_soc_dapm_direction dir) |
| 1250 | { |
| 1251 | struct snd_soc_card *card = widget->dapm->card; |
| 1252 | struct snd_soc_pcm_runtime *rtd; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1253 | int stream; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1254 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1255 | /* adjust dir to stream */ |
| 1256 | if (dir == SND_SOC_DAPM_DIR_OUT) |
| 1257 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 1258 | else |
| 1259 | stream = SNDRV_PCM_STREAM_CAPTURE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1260 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1261 | rtd = dpcm_get_be(card, widget, stream); |
| 1262 | if (rtd) |
| 1263 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1264 | |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
| 1268 | int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
| 1269 | int stream, struct snd_soc_dapm_widget_list **list) |
| 1270 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1271 | struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1272 | int paths; |
| 1273 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1274 | if (fe->num_cpus > 1) { |
| 1275 | dev_err(fe->dev, |
| 1276 | "%s doesn't support Multi CPU yet\n", __func__); |
| 1277 | return -EINVAL; |
| 1278 | } |
| 1279 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1280 | /* get number of valid DAI paths and their widgets */ |
| 1281 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, |
| 1282 | dpcm_end_walk_at_be); |
| 1283 | |
| 1284 | dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, |
| 1285 | stream ? "capture" : "playback"); |
| 1286 | |
| 1287 | return paths; |
| 1288 | } |
| 1289 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1290 | void dpcm_path_put(struct snd_soc_dapm_widget_list **list) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1291 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1292 | snd_soc_dapm_dai_free_widgets(list); |
| 1293 | } |
| 1294 | |
| 1295 | static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream, |
| 1296 | struct snd_soc_dapm_widget_list *list) |
| 1297 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1298 | struct snd_soc_dapm_widget *widget; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1299 | struct snd_soc_dai *dai; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1300 | unsigned int i; |
| 1301 | |
| 1302 | /* is there a valid DAI widget for this BE */ |
| 1303 | for_each_rtd_dais(dpcm->be, i, dai) { |
| 1304 | widget = snd_soc_dai_get_widget(dai, stream); |
| 1305 | |
| 1306 | /* |
| 1307 | * The BE is pruned only if none of the dai |
| 1308 | * widgets are in the active list. |
| 1309 | */ |
| 1310 | if (widget && widget_in_list(list, widget)) |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
| 1317 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1318 | struct snd_soc_dapm_widget_list **list_) |
| 1319 | { |
| 1320 | struct snd_soc_dpcm *dpcm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1321 | int prune = 0; |
| 1322 | |
| 1323 | /* Destroy any old FE <--> BE connections */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1324 | for_each_dpcm_be(fe, stream, dpcm) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1325 | if (dpcm_be_is_active(dpcm, stream, *list_)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1326 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1327 | |
| 1328 | dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", |
| 1329 | stream ? "capture" : "playback", |
| 1330 | dpcm->be->dai_link->name, fe->dai_link->name); |
| 1331 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 1332 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1333 | prune++; |
| 1334 | } |
| 1335 | |
| 1336 | dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); |
| 1337 | return prune; |
| 1338 | } |
| 1339 | |
| 1340 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1341 | struct snd_soc_dapm_widget_list **list_) |
| 1342 | { |
| 1343 | struct snd_soc_card *card = fe->card; |
| 1344 | struct snd_soc_dapm_widget_list *list = *list_; |
| 1345 | struct snd_soc_pcm_runtime *be; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1346 | struct snd_soc_dapm_widget *widget; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1347 | int i, new = 0, err; |
| 1348 | |
| 1349 | /* Create any new FE <--> BE connections */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1350 | for_each_dapm_widgets(list, i, widget) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1351 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1352 | switch (widget->id) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1353 | case snd_soc_dapm_dai_in: |
| 1354 | if (stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 1355 | continue; |
| 1356 | break; |
| 1357 | case snd_soc_dapm_dai_out: |
| 1358 | if (stream != SNDRV_PCM_STREAM_CAPTURE) |
| 1359 | continue; |
| 1360 | break; |
| 1361 | default: |
| 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | /* is there a valid BE rtd for this widget */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1366 | be = dpcm_get_be(card, widget, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1367 | if (!be) { |
| 1368 | dev_err(fe->dev, "ASoC: no BE found for %s\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1369 | widget->name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1370 | continue; |
| 1371 | } |
| 1372 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1373 | /* don't connect if FE is not running */ |
| 1374 | if (!fe->dpcm[stream].runtime && !fe->fe_compr) |
| 1375 | continue; |
| 1376 | |
| 1377 | /* newly connected FE and BE */ |
| 1378 | err = dpcm_be_connect(fe, be, stream); |
| 1379 | if (err < 0) { |
| 1380 | dev_err(fe->dev, "ASoC: can't connect %s\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1381 | widget->name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1382 | break; |
| 1383 | } else if (err == 0) /* already connected */ |
| 1384 | continue; |
| 1385 | |
| 1386 | /* new */ |
| 1387 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1388 | new++; |
| 1389 | } |
| 1390 | |
| 1391 | dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); |
| 1392 | return new; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Find the corresponding BE DAIs that source or sink audio to this |
| 1397 | * FE substream. |
| 1398 | */ |
| 1399 | int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
| 1400 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
| 1401 | { |
| 1402 | if (new) |
| 1403 | return dpcm_add_paths(fe, stream, list); |
| 1404 | else |
| 1405 | return dpcm_prune_paths(fe, stream, list); |
| 1406 | } |
| 1407 | |
| 1408 | void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) |
| 1409 | { |
| 1410 | struct snd_soc_dpcm *dpcm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1411 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1412 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1413 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
| 1414 | for_each_dpcm_be(fe, stream, dpcm) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1415 | dpcm->be->dpcm[stream].runtime_update = |
| 1416 | SND_SOC_DPCM_UPDATE_NO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1417 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, |
| 1421 | int stream) |
| 1422 | { |
| 1423 | struct snd_soc_dpcm *dpcm; |
| 1424 | |
| 1425 | /* disable any enabled and non active backends */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1426 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1427 | |
| 1428 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1429 | struct snd_pcm_substream *be_substream = |
| 1430 | snd_soc_dpcm_get_substream(be, stream); |
| 1431 | |
| 1432 | if (be->dpcm[stream].users == 0) |
| 1433 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
| 1434 | stream ? "capture" : "playback", |
| 1435 | be->dpcm[stream].state); |
| 1436 | |
| 1437 | if (--be->dpcm[stream].users != 0) |
| 1438 | continue; |
| 1439 | |
| 1440 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1441 | continue; |
| 1442 | |
| 1443 | soc_pcm_close(be_substream); |
| 1444 | be_substream->runtime = NULL; |
| 1445 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) |
| 1450 | { |
| 1451 | struct snd_soc_dpcm *dpcm; |
| 1452 | int err, count = 0; |
| 1453 | |
| 1454 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1455 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1456 | |
| 1457 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1458 | struct snd_pcm_substream *be_substream = |
| 1459 | snd_soc_dpcm_get_substream(be, stream); |
| 1460 | |
| 1461 | if (!be_substream) { |
| 1462 | dev_err(be->dev, "ASoC: no backend %s stream\n", |
| 1463 | stream ? "capture" : "playback"); |
| 1464 | continue; |
| 1465 | } |
| 1466 | |
| 1467 | /* is this op for this BE ? */ |
| 1468 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1469 | continue; |
| 1470 | |
| 1471 | /* first time the dpcm is open ? */ |
| 1472 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) |
| 1473 | dev_err(be->dev, "ASoC: too many users %s at open %d\n", |
| 1474 | 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_NEW) && |
| 1481 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) |
| 1482 | continue; |
| 1483 | |
| 1484 | dev_dbg(be->dev, "ASoC: open %s BE %s\n", |
| 1485 | stream ? "capture" : "playback", be->dai_link->name); |
| 1486 | |
| 1487 | be_substream->runtime = be->dpcm[stream].runtime; |
| 1488 | err = soc_pcm_open(be_substream); |
| 1489 | if (err < 0) { |
| 1490 | dev_err(be->dev, "ASoC: BE open failed %d\n", err); |
| 1491 | be->dpcm[stream].users--; |
| 1492 | if (be->dpcm[stream].users < 0) |
| 1493 | dev_err(be->dev, "ASoC: no users %s at unwind %d\n", |
| 1494 | stream ? "capture" : "playback", |
| 1495 | be->dpcm[stream].state); |
| 1496 | |
| 1497 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1498 | goto unwind; |
| 1499 | } |
| 1500 | |
| 1501 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1502 | count++; |
| 1503 | } |
| 1504 | |
| 1505 | return count; |
| 1506 | |
| 1507 | unwind: |
| 1508 | /* disable any enabled and non active backends */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1509 | for_each_dpcm_be_rollback(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1510 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1511 | struct snd_pcm_substream *be_substream = |
| 1512 | snd_soc_dpcm_get_substream(be, stream); |
| 1513 | |
| 1514 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1515 | continue; |
| 1516 | |
| 1517 | if (be->dpcm[stream].users == 0) |
| 1518 | dev_err(be->dev, "ASoC: no users %s at close %d\n", |
| 1519 | stream ? "capture" : "playback", |
| 1520 | be->dpcm[stream].state); |
| 1521 | |
| 1522 | if (--be->dpcm[stream].users != 0) |
| 1523 | continue; |
| 1524 | |
| 1525 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1526 | continue; |
| 1527 | |
| 1528 | soc_pcm_close(be_substream); |
| 1529 | be_substream->runtime = NULL; |
| 1530 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1531 | } |
| 1532 | |
| 1533 | return err; |
| 1534 | } |
| 1535 | |
| 1536 | static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
| 1537 | struct snd_soc_pcm_stream *stream) |
| 1538 | { |
| 1539 | runtime->hw.rate_min = stream->rate_min; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1540 | runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1541 | runtime->hw.channels_min = stream->channels_min; |
| 1542 | runtime->hw.channels_max = stream->channels_max; |
| 1543 | if (runtime->hw.formats) |
| 1544 | runtime->hw.formats &= stream->formats; |
| 1545 | else |
| 1546 | runtime->hw.formats = stream->formats; |
| 1547 | runtime->hw.rates = stream->rates; |
| 1548 | } |
| 1549 | |
| 1550 | static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, |
| 1551 | u64 *formats) |
| 1552 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1553 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1554 | struct snd_soc_dpcm *dpcm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1555 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1556 | int stream = substream->stream; |
| 1557 | |
| 1558 | if (!fe->dai_link->dpcm_merged_format) |
| 1559 | return; |
| 1560 | |
| 1561 | /* |
| 1562 | * It returns merged BE codec format |
| 1563 | * if FE want to use it (= dpcm_merged_format) |
| 1564 | */ |
| 1565 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1566 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1567 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1568 | struct snd_soc_pcm_stream *codec_stream; |
| 1569 | int i; |
| 1570 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1571 | for_each_rtd_codec_dais(be, i, dai) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1572 | /* |
| 1573 | * Skip CODECs which don't support the current stream |
| 1574 | * type. See soc_pcm_init_runtime_hw() for more details |
| 1575 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1576 | if (!snd_soc_dai_stream_valid(dai, stream)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1577 | continue; |
| 1578 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1579 | codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1580 | |
| 1581 | *formats &= codec_stream->formats; |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, |
| 1587 | unsigned int *channels_min, |
| 1588 | unsigned int *channels_max) |
| 1589 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1590 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1591 | struct snd_soc_dpcm *dpcm; |
| 1592 | int stream = substream->stream; |
| 1593 | |
| 1594 | if (!fe->dai_link->dpcm_merged_chan) |
| 1595 | return; |
| 1596 | |
| 1597 | /* |
| 1598 | * It returns merged BE codec channel; |
| 1599 | * if FE want to use it (= dpcm_merged_chan) |
| 1600 | */ |
| 1601 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1602 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1603 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1604 | struct snd_soc_pcm_stream *codec_stream; |
| 1605 | struct snd_soc_pcm_stream *cpu_stream; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1606 | struct snd_soc_dai *dai; |
| 1607 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1608 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1609 | for_each_rtd_cpu_dais(be, i, dai) { |
| 1610 | /* |
| 1611 | * Skip CPUs which don't support the current stream |
| 1612 | * type. See soc_pcm_init_runtime_hw() for more details |
| 1613 | */ |
| 1614 | if (!snd_soc_dai_stream_valid(dai, stream)) |
| 1615 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1616 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1617 | cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); |
| 1618 | |
| 1619 | *channels_min = max(*channels_min, |
| 1620 | cpu_stream->channels_min); |
| 1621 | *channels_max = min(*channels_max, |
| 1622 | cpu_stream->channels_max); |
| 1623 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1624 | |
| 1625 | /* |
| 1626 | * chan min/max cannot be enforced if there are multiple CODEC |
| 1627 | * DAIs connected to a single CPU DAI, use CPU DAI's directly |
| 1628 | */ |
| 1629 | if (be->num_codecs == 1) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1630 | codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1631 | |
| 1632 | *channels_min = max(*channels_min, |
| 1633 | codec_stream->channels_min); |
| 1634 | *channels_max = min(*channels_max, |
| 1635 | codec_stream->channels_max); |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, |
| 1641 | unsigned int *rates, |
| 1642 | unsigned int *rate_min, |
| 1643 | unsigned int *rate_max) |
| 1644 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1645 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1646 | struct snd_soc_dpcm *dpcm; |
| 1647 | int stream = substream->stream; |
| 1648 | |
| 1649 | if (!fe->dai_link->dpcm_merged_rate) |
| 1650 | return; |
| 1651 | |
| 1652 | /* |
| 1653 | * It returns merged BE codec channel; |
| 1654 | * if FE want to use it (= dpcm_merged_chan) |
| 1655 | */ |
| 1656 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1657 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1658 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1659 | struct snd_soc_pcm_stream *pcm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1660 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1661 | int i; |
| 1662 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1663 | for_each_rtd_dais(be, i, dai) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1664 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1665 | * Skip DAIs which don't support the current stream |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1666 | * type. See soc_pcm_init_runtime_hw() for more details |
| 1667 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1668 | if (!snd_soc_dai_stream_valid(dai, stream)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1669 | continue; |
| 1670 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1671 | pcm = snd_soc_dai_get_pcm_stream(dai, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1672 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1673 | *rate_min = max(*rate_min, pcm->rate_min); |
| 1674 | *rate_max = min_not_zero(*rate_max, pcm->rate_max); |
| 1675 | *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
| 1681 | { |
| 1682 | struct snd_pcm_runtime *runtime = substream->runtime; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1683 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
| 1684 | struct snd_soc_dai *cpu_dai; |
| 1685 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1686 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1687 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 1688 | /* |
| 1689 | * Skip CPUs which don't support the current stream |
| 1690 | * type. See soc_pcm_init_runtime_hw() for more details |
| 1691 | */ |
| 1692 | if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) |
| 1693 | continue; |
| 1694 | |
| 1695 | dpcm_init_runtime_hw(runtime, |
| 1696 | snd_soc_dai_get_pcm_stream(cpu_dai, |
| 1697 | substream->stream)); |
| 1698 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1699 | |
| 1700 | dpcm_runtime_merge_format(substream, &runtime->hw.formats); |
| 1701 | dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, |
| 1702 | &runtime->hw.channels_max); |
| 1703 | dpcm_runtime_merge_rate(substream, &runtime->hw.rates, |
| 1704 | &runtime->hw.rate_min, &runtime->hw.rate_max); |
| 1705 | } |
| 1706 | |
| 1707 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); |
| 1708 | |
| 1709 | /* Set FE's runtime_update state; the state is protected via PCM stream lock |
| 1710 | * for avoiding the race with trigger callback. |
| 1711 | * If the state is unset and a trigger is pending while the previous operation, |
| 1712 | * process the pending trigger action here. |
| 1713 | */ |
| 1714 | static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, |
| 1715 | int stream, enum snd_soc_dpcm_update state) |
| 1716 | { |
| 1717 | struct snd_pcm_substream *substream = |
| 1718 | snd_soc_dpcm_get_substream(fe, stream); |
| 1719 | |
| 1720 | snd_pcm_stream_lock_irq(substream); |
| 1721 | if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { |
| 1722 | dpcm_fe_dai_do_trigger(substream, |
| 1723 | fe->dpcm[stream].trigger_pending - 1); |
| 1724 | fe->dpcm[stream].trigger_pending = 0; |
| 1725 | } |
| 1726 | fe->dpcm[stream].runtime_update = state; |
| 1727 | snd_pcm_stream_unlock_irq(substream); |
| 1728 | } |
| 1729 | |
| 1730 | static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, |
| 1731 | int stream) |
| 1732 | { |
| 1733 | struct snd_soc_dpcm *dpcm; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1734 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream); |
| 1735 | struct snd_soc_dai *fe_cpu_dai; |
| 1736 | int err = 0; |
| 1737 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1738 | |
| 1739 | /* apply symmetry for FE */ |
| 1740 | if (soc_pcm_has_symmetry(fe_substream)) |
| 1741 | fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 1742 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1743 | for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) { |
| 1744 | /* Symmetry only applies if we've got an active stream. */ |
| 1745 | if (snd_soc_dai_active(fe_cpu_dai)) { |
| 1746 | err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); |
| 1747 | if (err < 0) |
| 1748 | return err; |
| 1749 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | /* apply symmetry for BE */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1753 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1754 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1755 | struct snd_pcm_substream *be_substream = |
| 1756 | snd_soc_dpcm_get_substream(be, stream); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1757 | struct snd_soc_pcm_runtime *rtd; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1758 | struct snd_soc_dai *dai; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1759 | int i; |
| 1760 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1761 | /* A backend may not have the requested substream */ |
| 1762 | if (!be_substream) |
| 1763 | continue; |
| 1764 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1765 | rtd = asoc_substream_to_rtd(be_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1766 | if (rtd->dai_link->be_hw_params_fixup) |
| 1767 | continue; |
| 1768 | |
| 1769 | if (soc_pcm_has_symmetry(be_substream)) |
| 1770 | be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 1771 | |
| 1772 | /* Symmetry only applies if we've got an active stream. */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1773 | for_each_rtd_dais(rtd, i, dai) { |
| 1774 | if (snd_soc_dai_active(dai)) { |
| 1775 | err = soc_pcm_apply_symmetry(fe_substream, dai); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1776 | if (err < 0) |
| 1777 | return err; |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) |
| 1786 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1787 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1788 | struct snd_pcm_runtime *runtime = fe_substream->runtime; |
| 1789 | int stream = fe_substream->stream, ret = 0; |
| 1790 | |
| 1791 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
| 1792 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1793 | ret = dpcm_be_dai_startup(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1794 | if (ret < 0) { |
| 1795 | dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); |
| 1796 | goto be_err; |
| 1797 | } |
| 1798 | |
| 1799 | dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); |
| 1800 | |
| 1801 | /* start the DAI frontend */ |
| 1802 | ret = soc_pcm_open(fe_substream); |
| 1803 | if (ret < 0) { |
| 1804 | dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); |
| 1805 | goto unwind; |
| 1806 | } |
| 1807 | |
| 1808 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1809 | |
| 1810 | dpcm_set_fe_runtime(fe_substream); |
| 1811 | snd_pcm_limit_hw_rates(runtime); |
| 1812 | |
| 1813 | ret = dpcm_apply_symmetry(fe_substream, stream); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1814 | if (ret < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1815 | dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", |
| 1816 | ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1817 | |
| 1818 | unwind: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1819 | if (ret < 0) |
| 1820 | dpcm_be_dai_startup_unwind(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1821 | be_err: |
| 1822 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 1823 | return ret; |
| 1824 | } |
| 1825 | |
| 1826 | int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
| 1827 | { |
| 1828 | struct snd_soc_dpcm *dpcm; |
| 1829 | |
| 1830 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1831 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1832 | |
| 1833 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1834 | struct snd_pcm_substream *be_substream = |
| 1835 | snd_soc_dpcm_get_substream(be, stream); |
| 1836 | |
| 1837 | /* is this op for this BE ? */ |
| 1838 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1839 | continue; |
| 1840 | |
| 1841 | if (be->dpcm[stream].users == 0) |
| 1842 | dev_err(be->dev, "ASoC: no users %s at close - state %d\n", |
| 1843 | stream ? "capture" : "playback", |
| 1844 | be->dpcm[stream].state); |
| 1845 | |
| 1846 | if (--be->dpcm[stream].users != 0) |
| 1847 | continue; |
| 1848 | |
| 1849 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1850 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { |
| 1851 | soc_pcm_hw_free(be_substream); |
| 1852 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1853 | } |
| 1854 | |
| 1855 | dev_dbg(be->dev, "ASoC: close BE %s\n", |
| 1856 | be->dai_link->name); |
| 1857 | |
| 1858 | soc_pcm_close(be_substream); |
| 1859 | be_substream->runtime = NULL; |
| 1860 | |
| 1861 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1862 | } |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) |
| 1867 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1868 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1869 | int stream = substream->stream; |
| 1870 | |
| 1871 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
| 1872 | |
| 1873 | /* shutdown the BEs */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1874 | dpcm_be_dai_shutdown(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1875 | |
| 1876 | dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); |
| 1877 | |
| 1878 | /* now shutdown the frontend */ |
| 1879 | soc_pcm_close(substream); |
| 1880 | |
| 1881 | /* run the stream event for each BE */ |
| 1882 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 1883 | |
| 1884 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1885 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
| 1889 | int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) |
| 1890 | { |
| 1891 | struct snd_soc_dpcm *dpcm; |
| 1892 | |
| 1893 | /* only hw_params backends that are either sinks or sources |
| 1894 | * to this frontend DAI */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1895 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1896 | |
| 1897 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1898 | struct snd_pcm_substream *be_substream = |
| 1899 | snd_soc_dpcm_get_substream(be, stream); |
| 1900 | |
| 1901 | /* is this op for this BE ? */ |
| 1902 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1903 | continue; |
| 1904 | |
| 1905 | /* only free hw when no longer used - check all FEs */ |
| 1906 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1907 | continue; |
| 1908 | |
| 1909 | /* do not free hw if this BE is used by other FE */ |
| 1910 | if (be->dpcm[stream].users > 1) |
| 1911 | continue; |
| 1912 | |
| 1913 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1914 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 1915 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1916 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && |
| 1917 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
| 1918 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
| 1919 | continue; |
| 1920 | |
| 1921 | dev_dbg(be->dev, "ASoC: hw_free BE %s\n", |
| 1922 | be->dai_link->name); |
| 1923 | |
| 1924 | soc_pcm_hw_free(be_substream); |
| 1925 | |
| 1926 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1927 | } |
| 1928 | |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
| 1933 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1934 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1935 | int err, stream = substream->stream; |
| 1936 | |
| 1937 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1938 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
| 1939 | |
| 1940 | dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); |
| 1941 | |
| 1942 | /* call hw_free on the frontend */ |
| 1943 | err = soc_pcm_hw_free(substream); |
| 1944 | if (err < 0) |
| 1945 | dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", |
| 1946 | fe->dai_link->name); |
| 1947 | |
| 1948 | /* only hw_params backends that are either sinks or sources |
| 1949 | * to this frontend DAI */ |
| 1950 | err = dpcm_be_dai_hw_free(fe, stream); |
| 1951 | |
| 1952 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1953 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 1954 | |
| 1955 | mutex_unlock(&fe->card->mutex); |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) |
| 1960 | { |
| 1961 | struct snd_soc_dpcm *dpcm; |
| 1962 | int ret; |
| 1963 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1964 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1965 | |
| 1966 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1967 | struct snd_pcm_substream *be_substream = |
| 1968 | snd_soc_dpcm_get_substream(be, stream); |
| 1969 | |
| 1970 | /* is this op for this BE ? */ |
| 1971 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1972 | continue; |
| 1973 | |
| 1974 | /* copy params for each dpcm */ |
| 1975 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, |
| 1976 | sizeof(struct snd_pcm_hw_params)); |
| 1977 | |
| 1978 | /* perform any hw_params fixups */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1979 | ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params); |
| 1980 | if (ret < 0) |
| 1981 | goto unwind; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1982 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1983 | /* copy the fixed-up hw params for BE dai */ |
| 1984 | memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, |
| 1985 | sizeof(struct snd_pcm_hw_params)); |
| 1986 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1987 | /* only allow hw_params() if no connected FEs are running */ |
| 1988 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) |
| 1989 | continue; |
| 1990 | |
| 1991 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 1992 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1993 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) |
| 1994 | continue; |
| 1995 | |
| 1996 | dev_dbg(be->dev, "ASoC: hw_params BE %s\n", |
| 1997 | be->dai_link->name); |
| 1998 | |
| 1999 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); |
| 2000 | if (ret < 0) { |
| 2001 | dev_err(dpcm->be->dev, |
| 2002 | "ASoC: hw_params BE failed %d\n", ret); |
| 2003 | goto unwind; |
| 2004 | } |
| 2005 | |
| 2006 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 2007 | } |
| 2008 | return 0; |
| 2009 | |
| 2010 | unwind: |
| 2011 | /* disable any enabled and non active backends */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2012 | for_each_dpcm_be_rollback(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2013 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2014 | struct snd_pcm_substream *be_substream = |
| 2015 | snd_soc_dpcm_get_substream(be, stream); |
| 2016 | |
| 2017 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2018 | continue; |
| 2019 | |
| 2020 | /* only allow hw_free() if no connected FEs are running */ |
| 2021 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2022 | continue; |
| 2023 | |
| 2024 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 2025 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2026 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 2027 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 2028 | continue; |
| 2029 | |
| 2030 | soc_pcm_hw_free(be_substream); |
| 2031 | } |
| 2032 | |
| 2033 | return ret; |
| 2034 | } |
| 2035 | |
| 2036 | static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
| 2037 | struct snd_pcm_hw_params *params) |
| 2038 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2039 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2040 | int ret, stream = substream->stream; |
| 2041 | |
| 2042 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2043 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
| 2044 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2045 | memcpy(&fe->dpcm[stream].hw_params, params, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2046 | sizeof(struct snd_pcm_hw_params)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2047 | ret = dpcm_be_dai_hw_params(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2048 | if (ret < 0) { |
| 2049 | dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); |
| 2050 | goto out; |
| 2051 | } |
| 2052 | |
| 2053 | dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", |
| 2054 | fe->dai_link->name, params_rate(params), |
| 2055 | params_channels(params), params_format(params)); |
| 2056 | |
| 2057 | /* call hw_params on the frontend */ |
| 2058 | ret = soc_pcm_hw_params(substream, params); |
| 2059 | if (ret < 0) { |
| 2060 | dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); |
| 2061 | dpcm_be_dai_hw_free(fe, stream); |
| 2062 | } else |
| 2063 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 2064 | |
| 2065 | out: |
| 2066 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 2067 | mutex_unlock(&fe->card->mutex); |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, |
| 2072 | struct snd_pcm_substream *substream, int cmd) |
| 2073 | { |
| 2074 | int ret; |
| 2075 | |
| 2076 | dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", |
| 2077 | dpcm->be->dai_link->name, cmd); |
| 2078 | |
| 2079 | ret = soc_pcm_trigger(substream, cmd); |
| 2080 | if (ret < 0) |
| 2081 | dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); |
| 2082 | |
| 2083 | return ret; |
| 2084 | } |
| 2085 | |
| 2086 | int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, |
| 2087 | int cmd) |
| 2088 | { |
| 2089 | struct snd_soc_dpcm *dpcm; |
| 2090 | int ret = 0; |
| 2091 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2092 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2093 | |
| 2094 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2095 | struct snd_pcm_substream *be_substream = |
| 2096 | snd_soc_dpcm_get_substream(be, stream); |
| 2097 | |
| 2098 | /* is this op for this BE ? */ |
| 2099 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2100 | continue; |
| 2101 | |
| 2102 | switch (cmd) { |
| 2103 | case SNDRV_PCM_TRIGGER_START: |
| 2104 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2105 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
| 2106 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2107 | continue; |
| 2108 | |
| 2109 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2110 | if (ret) |
| 2111 | return ret; |
| 2112 | |
| 2113 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2114 | break; |
| 2115 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2116 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
| 2117 | continue; |
| 2118 | |
| 2119 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2120 | if (ret) |
| 2121 | return ret; |
| 2122 | |
| 2123 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2124 | break; |
| 2125 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2126 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
| 2127 | continue; |
| 2128 | |
| 2129 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2130 | if (ret) |
| 2131 | return ret; |
| 2132 | |
| 2133 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2134 | break; |
| 2135 | case SNDRV_PCM_TRIGGER_STOP: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2136 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) && |
| 2137 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2138 | continue; |
| 2139 | |
| 2140 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2141 | continue; |
| 2142 | |
| 2143 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2144 | if (ret) |
| 2145 | return ret; |
| 2146 | |
| 2147 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 2148 | break; |
| 2149 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 2150 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2151 | continue; |
| 2152 | |
| 2153 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2154 | continue; |
| 2155 | |
| 2156 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2157 | if (ret) |
| 2158 | return ret; |
| 2159 | |
| 2160 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; |
| 2161 | break; |
| 2162 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2163 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2164 | continue; |
| 2165 | |
| 2166 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2167 | continue; |
| 2168 | |
| 2169 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2170 | if (ret) |
| 2171 | return ret; |
| 2172 | |
| 2173 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 2174 | break; |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | return ret; |
| 2179 | } |
| 2180 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); |
| 2181 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2182 | static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, |
| 2183 | int cmd, bool fe_first) |
| 2184 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2185 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2186 | int ret; |
| 2187 | |
| 2188 | /* call trigger on the frontend before the backend. */ |
| 2189 | if (fe_first) { |
| 2190 | dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", |
| 2191 | fe->dai_link->name, cmd); |
| 2192 | |
| 2193 | ret = soc_pcm_trigger(substream, cmd); |
| 2194 | if (ret < 0) |
| 2195 | return ret; |
| 2196 | |
| 2197 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 2198 | return ret; |
| 2199 | } |
| 2200 | |
| 2201 | /* call trigger on the frontend after the backend. */ |
| 2202 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 2203 | if (ret < 0) |
| 2204 | return ret; |
| 2205 | |
| 2206 | dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", |
| 2207 | fe->dai_link->name, cmd); |
| 2208 | |
| 2209 | ret = soc_pcm_trigger(substream, cmd); |
| 2210 | |
| 2211 | return ret; |
| 2212 | } |
| 2213 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2214 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) |
| 2215 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2216 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2217 | int stream = substream->stream; |
| 2218 | int ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2219 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 2220 | |
| 2221 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 2222 | |
| 2223 | switch (trigger) { |
| 2224 | case SND_SOC_DPCM_TRIGGER_PRE: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2225 | switch (cmd) { |
| 2226 | case SNDRV_PCM_TRIGGER_START: |
| 2227 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2228 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2229 | case SNDRV_PCM_TRIGGER_DRAIN: |
| 2230 | ret = dpcm_dai_trigger_fe_be(substream, cmd, true); |
| 2231 | break; |
| 2232 | case SNDRV_PCM_TRIGGER_STOP: |
| 2233 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 2234 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2235 | ret = dpcm_dai_trigger_fe_be(substream, cmd, false); |
| 2236 | break; |
| 2237 | default: |
| 2238 | ret = -EINVAL; |
| 2239 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2240 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2241 | break; |
| 2242 | case SND_SOC_DPCM_TRIGGER_POST: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2243 | switch (cmd) { |
| 2244 | case SNDRV_PCM_TRIGGER_START: |
| 2245 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2246 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2247 | case SNDRV_PCM_TRIGGER_DRAIN: |
| 2248 | ret = dpcm_dai_trigger_fe_be(substream, cmd, false); |
| 2249 | break; |
| 2250 | case SNDRV_PCM_TRIGGER_STOP: |
| 2251 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 2252 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2253 | ret = dpcm_dai_trigger_fe_be(substream, cmd, true); |
| 2254 | break; |
| 2255 | default: |
| 2256 | ret = -EINVAL; |
| 2257 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2258 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | case SND_SOC_DPCM_TRIGGER_BESPOKE: |
| 2261 | /* bespoke trigger() - handles both FE and BEs */ |
| 2262 | |
| 2263 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", |
| 2264 | fe->dai_link->name, cmd); |
| 2265 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2266 | ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2267 | break; |
| 2268 | default: |
| 2269 | dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, |
| 2270 | fe->dai_link->name); |
| 2271 | ret = -EINVAL; |
| 2272 | goto out; |
| 2273 | } |
| 2274 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2275 | if (ret < 0) { |
| 2276 | dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", |
| 2277 | cmd, ret); |
| 2278 | goto out; |
| 2279 | } |
| 2280 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2281 | switch (cmd) { |
| 2282 | case SNDRV_PCM_TRIGGER_START: |
| 2283 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2284 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2285 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2286 | break; |
| 2287 | case SNDRV_PCM_TRIGGER_STOP: |
| 2288 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 2289 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 2290 | break; |
| 2291 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2292 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 2293 | break; |
| 2294 | } |
| 2295 | |
| 2296 | out: |
| 2297 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 2298 | return ret; |
| 2299 | } |
| 2300 | |
| 2301 | static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
| 2302 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2303 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2304 | int stream = substream->stream; |
| 2305 | |
| 2306 | /* if FE's runtime_update is already set, we're in race; |
| 2307 | * process this trigger later at exit |
| 2308 | */ |
| 2309 | if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { |
| 2310 | fe->dpcm[stream].trigger_pending = cmd + 1; |
| 2311 | return 0; /* delayed, assuming it's successful */ |
| 2312 | } |
| 2313 | |
| 2314 | /* we're alone, let's trigger */ |
| 2315 | return dpcm_fe_dai_do_trigger(substream, cmd); |
| 2316 | } |
| 2317 | |
| 2318 | int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) |
| 2319 | { |
| 2320 | struct snd_soc_dpcm *dpcm; |
| 2321 | int ret = 0; |
| 2322 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2323 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2324 | |
| 2325 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2326 | struct snd_pcm_substream *be_substream = |
| 2327 | snd_soc_dpcm_get_substream(be, stream); |
| 2328 | |
| 2329 | /* is this op for this BE ? */ |
| 2330 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2331 | continue; |
| 2332 | |
| 2333 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2334 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2335 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && |
| 2336 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2337 | continue; |
| 2338 | |
| 2339 | dev_dbg(be->dev, "ASoC: prepare BE %s\n", |
| 2340 | be->dai_link->name); |
| 2341 | |
| 2342 | ret = soc_pcm_prepare(be_substream); |
| 2343 | if (ret < 0) { |
| 2344 | dev_err(be->dev, "ASoC: backend prepare failed %d\n", |
| 2345 | ret); |
| 2346 | break; |
| 2347 | } |
| 2348 | |
| 2349 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2350 | } |
| 2351 | return ret; |
| 2352 | } |
| 2353 | |
| 2354 | static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
| 2355 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2356 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2357 | int stream = substream->stream, ret = 0; |
| 2358 | |
| 2359 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2360 | |
| 2361 | dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); |
| 2362 | |
| 2363 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
| 2364 | |
| 2365 | /* there is no point preparing this FE if there are no BEs */ |
| 2366 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 2367 | dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", |
| 2368 | fe->dai_link->name); |
| 2369 | ret = -EINVAL; |
| 2370 | goto out; |
| 2371 | } |
| 2372 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2373 | ret = dpcm_be_dai_prepare(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2374 | if (ret < 0) |
| 2375 | goto out; |
| 2376 | |
| 2377 | /* call prepare on the frontend */ |
| 2378 | ret = soc_pcm_prepare(substream); |
| 2379 | if (ret < 0) { |
| 2380 | dev_err(fe->dev,"ASoC: prepare FE %s failed\n", |
| 2381 | fe->dai_link->name); |
| 2382 | goto out; |
| 2383 | } |
| 2384 | |
| 2385 | /* run the stream event for each BE */ |
| 2386 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 2387 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2388 | |
| 2389 | out: |
| 2390 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 2391 | mutex_unlock(&fe->card->mutex); |
| 2392 | |
| 2393 | return ret; |
| 2394 | } |
| 2395 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2396 | static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
| 2397 | { |
| 2398 | struct snd_pcm_substream *substream = |
| 2399 | snd_soc_dpcm_get_substream(fe, stream); |
| 2400 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 2401 | int err; |
| 2402 | |
| 2403 | dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", |
| 2404 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2405 | |
| 2406 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2407 | /* call bespoke trigger - FE takes care of all BE triggers */ |
| 2408 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", |
| 2409 | fe->dai_link->name); |
| 2410 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2411 | err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2412 | if (err < 0) |
| 2413 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
| 2414 | } else { |
| 2415 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", |
| 2416 | fe->dai_link->name); |
| 2417 | |
| 2418 | err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); |
| 2419 | if (err < 0) |
| 2420 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
| 2421 | } |
| 2422 | |
| 2423 | err = dpcm_be_dai_hw_free(fe, stream); |
| 2424 | if (err < 0) |
| 2425 | dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); |
| 2426 | |
| 2427 | err = dpcm_be_dai_shutdown(fe, stream); |
| 2428 | if (err < 0) |
| 2429 | dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); |
| 2430 | |
| 2431 | /* run the stream event for each BE */ |
| 2432 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2433 | |
| 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) |
| 2438 | { |
| 2439 | struct snd_pcm_substream *substream = |
| 2440 | snd_soc_dpcm_get_substream(fe, stream); |
| 2441 | struct snd_soc_dpcm *dpcm; |
| 2442 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 2443 | int ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2444 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2445 | |
| 2446 | dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", |
| 2447 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2448 | |
| 2449 | /* Only start the BE if the FE is ready */ |
| 2450 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || |
| 2451 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) |
| 2452 | return -EINVAL; |
| 2453 | |
| 2454 | /* startup must always be called for new BEs */ |
| 2455 | ret = dpcm_be_dai_startup(fe, stream); |
| 2456 | if (ret < 0) |
| 2457 | goto disconnect; |
| 2458 | |
| 2459 | /* keep going if FE state is > open */ |
| 2460 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) |
| 2461 | return 0; |
| 2462 | |
| 2463 | ret = dpcm_be_dai_hw_params(fe, stream); |
| 2464 | if (ret < 0) |
| 2465 | goto close; |
| 2466 | |
| 2467 | /* keep going if FE state is > hw_params */ |
| 2468 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) |
| 2469 | return 0; |
| 2470 | |
| 2471 | |
| 2472 | ret = dpcm_be_dai_prepare(fe, stream); |
| 2473 | if (ret < 0) |
| 2474 | goto hw_free; |
| 2475 | |
| 2476 | /* run the stream event for each BE */ |
| 2477 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2478 | |
| 2479 | /* keep going if FE state is > prepare */ |
| 2480 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || |
| 2481 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) |
| 2482 | return 0; |
| 2483 | |
| 2484 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2485 | /* call trigger on the frontend - FE takes care of all BE triggers */ |
| 2486 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", |
| 2487 | fe->dai_link->name); |
| 2488 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2489 | ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2490 | if (ret < 0) { |
| 2491 | dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); |
| 2492 | goto hw_free; |
| 2493 | } |
| 2494 | } else { |
| 2495 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", |
| 2496 | fe->dai_link->name); |
| 2497 | |
| 2498 | ret = dpcm_be_dai_trigger(fe, stream, |
| 2499 | SNDRV_PCM_TRIGGER_START); |
| 2500 | if (ret < 0) { |
| 2501 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
| 2502 | goto hw_free; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | return 0; |
| 2507 | |
| 2508 | hw_free: |
| 2509 | dpcm_be_dai_hw_free(fe, stream); |
| 2510 | close: |
| 2511 | dpcm_be_dai_shutdown(fe, stream); |
| 2512 | disconnect: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2513 | /* disconnect any closed BEs */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2514 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
| 2515 | for_each_dpcm_be(fe, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2516 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2517 | if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) |
| 2518 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2519 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2520 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2521 | |
| 2522 | return ret; |
| 2523 | } |
| 2524 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2525 | static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) |
| 2526 | { |
| 2527 | struct snd_soc_dapm_widget_list *list; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2528 | int stream; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2529 | int count, paths; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2530 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (!fe->dai_link->dynamic) |
| 2533 | return 0; |
| 2534 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2535 | if (fe->num_cpus > 1) { |
| 2536 | dev_err(fe->dev, |
| 2537 | "%s doesn't support Multi CPU yet\n", __func__); |
| 2538 | return -EINVAL; |
| 2539 | } |
| 2540 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2541 | /* only check active links */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2542 | if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2543 | return 0; |
| 2544 | |
| 2545 | /* DAPM sync will call this to update DSP paths */ |
| 2546 | dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", |
| 2547 | new ? "new" : "old", fe->dai_link->name); |
| 2548 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2549 | for_each_pcm_streams(stream) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2550 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2551 | /* skip if FE doesn't have playback/capture capability */ |
| 2552 | if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) || |
| 2553 | !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream)) |
| 2554 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2555 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2556 | /* skip if FE isn't currently playing/capturing */ |
| 2557 | if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) || |
| 2558 | !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream)) |
| 2559 | continue; |
| 2560 | |
| 2561 | paths = dpcm_path_get(fe, stream, &list); |
| 2562 | if (paths < 0) { |
| 2563 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
| 2564 | fe->dai_link->name, |
| 2565 | stream == SNDRV_PCM_STREAM_PLAYBACK ? |
| 2566 | "playback" : "capture"); |
| 2567 | return paths; |
| 2568 | } |
| 2569 | |
| 2570 | /* update any playback/capture paths */ |
| 2571 | count = dpcm_process_paths(fe, stream, &list, new); |
| 2572 | if (count) { |
| 2573 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
| 2574 | if (new) |
| 2575 | ret = dpcm_run_update_startup(fe, stream); |
| 2576 | else |
| 2577 | ret = dpcm_run_update_shutdown(fe, stream); |
| 2578 | if (ret < 0) |
| 2579 | dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); |
| 2580 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
| 2581 | |
| 2582 | dpcm_clear_pending_state(fe, stream); |
| 2583 | dpcm_be_disconnect(fe, stream); |
| 2584 | } |
| 2585 | |
| 2586 | dpcm_path_put(&list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | /* Called by DAPM mixer/mux changes to update audio routing between PCMs and |
| 2593 | * any DAI links. |
| 2594 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2595 | int snd_soc_dpcm_runtime_update(struct snd_soc_card *card) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2596 | { |
| 2597 | struct snd_soc_pcm_runtime *fe; |
| 2598 | int ret = 0; |
| 2599 | |
| 2600 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2601 | /* shutdown all old paths first */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2602 | for_each_card_rtds(card, fe) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2603 | ret = soc_dpcm_fe_runtime_update(fe, 0); |
| 2604 | if (ret) |
| 2605 | goto out; |
| 2606 | } |
| 2607 | |
| 2608 | /* bring new paths up */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2609 | for_each_card_rtds(card, fe) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2610 | ret = soc_dpcm_fe_runtime_update(fe, 1); |
| 2611 | if (ret) |
| 2612 | goto out; |
| 2613 | } |
| 2614 | |
| 2615 | out: |
| 2616 | mutex_unlock(&card->mutex); |
| 2617 | return ret; |
| 2618 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2619 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update); |
| 2620 | |
| 2621 | static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2622 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2623 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2624 | struct snd_soc_dpcm *dpcm; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2625 | int stream = fe_substream->stream; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2626 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2627 | /* mark FE's links ready to prune */ |
| 2628 | for_each_dpcm_be(fe, stream, dpcm) |
| 2629 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2630 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2631 | dpcm_be_disconnect(fe, stream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2632 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2633 | fe->dpcm[stream].runtime = NULL; |
| 2634 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2635 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2636 | static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
| 2637 | { |
| 2638 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream); |
| 2639 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2640 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2641 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2642 | ret = dpcm_fe_dai_shutdown(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2643 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2644 | dpcm_fe_dai_cleanup(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2645 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2646 | mutex_unlock(&fe->card->mutex); |
| 2647 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
| 2651 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2652 | struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2653 | struct snd_soc_dapm_widget_list *list; |
| 2654 | int ret; |
| 2655 | int stream = fe_substream->stream; |
| 2656 | |
| 2657 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2658 | fe->dpcm[stream].runtime = fe_substream->runtime; |
| 2659 | |
| 2660 | ret = dpcm_path_get(fe, stream, &list); |
| 2661 | if (ret < 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2662 | goto open_end; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2663 | } else if (ret == 0) { |
| 2664 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
| 2665 | fe->dai_link->name, stream ? "capture" : "playback"); |
| 2666 | } |
| 2667 | |
| 2668 | /* calculate valid and active FE <-> BE dpcms */ |
| 2669 | dpcm_process_paths(fe, stream, &list, 1); |
| 2670 | |
| 2671 | ret = dpcm_fe_dai_startup(fe_substream); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2672 | if (ret < 0) |
| 2673 | dpcm_fe_dai_cleanup(fe_substream); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2674 | |
| 2675 | dpcm_clear_pending_state(fe, stream); |
| 2676 | dpcm_path_put(&list); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2677 | open_end: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2678 | mutex_unlock(&fe->card->mutex); |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2682 | /* create a new pcm */ |
| 2683 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) |
| 2684 | { |
| 2685 | struct snd_soc_dai *codec_dai; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2686 | struct snd_soc_dai *cpu_dai; |
| 2687 | struct snd_soc_component *component; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2688 | struct snd_pcm *pcm; |
| 2689 | char new_name[64]; |
| 2690 | int ret = 0, playback = 0, capture = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2691 | int stream; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2692 | int i; |
| 2693 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2694 | if (rtd->dai_link->dynamic && rtd->num_cpus > 1) { |
| 2695 | dev_err(rtd->dev, |
| 2696 | "DPCM doesn't support Multi CPU for Front-Ends yet\n"); |
| 2697 | return -EINVAL; |
| 2698 | } |
| 2699 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2700 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2701 | if (rtd->dai_link->dpcm_playback) { |
| 2702 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 2703 | |
| 2704 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 2705 | if (snd_soc_dai_stream_valid(cpu_dai, stream)) { |
| 2706 | playback = 1; |
| 2707 | break; |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | if (!playback) { |
| 2712 | dev_err(rtd->card->dev, |
| 2713 | "No CPU DAIs support playback for stream %s\n", |
| 2714 | rtd->dai_link->stream_name); |
| 2715 | return -EINVAL; |
| 2716 | } |
| 2717 | } |
| 2718 | if (rtd->dai_link->dpcm_capture) { |
| 2719 | stream = SNDRV_PCM_STREAM_CAPTURE; |
| 2720 | |
| 2721 | for_each_rtd_cpu_dais(rtd, i, cpu_dai) { |
| 2722 | if (snd_soc_dai_stream_valid(cpu_dai, stream)) { |
| 2723 | capture = 1; |
| 2724 | break; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | if (!capture) { |
| 2729 | dev_err(rtd->card->dev, |
| 2730 | "No CPU DAIs support capture for stream %s\n", |
| 2731 | rtd->dai_link->stream_name); |
| 2732 | return -EINVAL; |
| 2733 | } |
| 2734 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2735 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2736 | /* Adapt stream for codec2codec links */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2737 | int cpu_capture = rtd->dai_link->params ? |
| 2738 | SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; |
| 2739 | int cpu_playback = rtd->dai_link->params ? |
| 2740 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2741 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2742 | for_each_rtd_codec_dais(rtd, i, codec_dai) { |
| 2743 | if (rtd->num_cpus == 1) { |
| 2744 | cpu_dai = asoc_rtd_to_cpu(rtd, 0); |
| 2745 | } else if (rtd->num_cpus == rtd->num_codecs) { |
| 2746 | cpu_dai = asoc_rtd_to_cpu(rtd, i); |
| 2747 | } else { |
| 2748 | dev_err(rtd->card->dev, |
| 2749 | "N cpus to M codecs link is not supported yet\n"); |
| 2750 | return -EINVAL; |
| 2751 | } |
| 2752 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2753 | if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2754 | snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2755 | playback = 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2756 | if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2757 | snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2758 | capture = 1; |
| 2759 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | if (rtd->dai_link->playback_only) { |
| 2763 | playback = 1; |
| 2764 | capture = 0; |
| 2765 | } |
| 2766 | |
| 2767 | if (rtd->dai_link->capture_only) { |
| 2768 | playback = 0; |
| 2769 | capture = 1; |
| 2770 | } |
| 2771 | |
| 2772 | /* create the PCM */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2773 | if (rtd->dai_link->params) { |
| 2774 | snprintf(new_name, sizeof(new_name), "codec2codec(%s)", |
| 2775 | rtd->dai_link->stream_name); |
| 2776 | |
| 2777 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 2778 | playback, capture, &pcm); |
| 2779 | } else if (rtd->dai_link->no_pcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2780 | snprintf(new_name, sizeof(new_name), "(%s)", |
| 2781 | rtd->dai_link->stream_name); |
| 2782 | |
| 2783 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 2784 | playback, capture, &pcm); |
| 2785 | } else { |
| 2786 | if (rtd->dai_link->dynamic) |
| 2787 | snprintf(new_name, sizeof(new_name), "%s (*)", |
| 2788 | rtd->dai_link->stream_name); |
| 2789 | else |
| 2790 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
| 2791 | rtd->dai_link->stream_name, |
| 2792 | (rtd->num_codecs > 1) ? |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2793 | "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2794 | |
| 2795 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, |
| 2796 | capture, &pcm); |
| 2797 | } |
| 2798 | if (ret < 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2799 | dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n", |
| 2800 | new_name, rtd->dai_link->name, ret); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2801 | return ret; |
| 2802 | } |
| 2803 | dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); |
| 2804 | |
| 2805 | /* DAPM dai link stream work */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2806 | if (rtd->dai_link->params) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2807 | rtd->close_delayed_work_func = codec2codec_close_delayed_work; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2808 | else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2809 | rtd->close_delayed_work_func = snd_soc_close_delayed_work; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2810 | |
| 2811 | pcm->nonatomic = rtd->dai_link->nonatomic; |
| 2812 | rtd->pcm = pcm; |
| 2813 | pcm->private_data = rtd; |
| 2814 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2815 | if (rtd->dai_link->no_pcm || rtd->dai_link->params) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2816 | if (playback) |
| 2817 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; |
| 2818 | if (capture) |
| 2819 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; |
| 2820 | goto out; |
| 2821 | } |
| 2822 | |
| 2823 | /* ASoC PCM operations */ |
| 2824 | if (rtd->dai_link->dynamic) { |
| 2825 | rtd->ops.open = dpcm_fe_dai_open; |
| 2826 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; |
| 2827 | rtd->ops.prepare = dpcm_fe_dai_prepare; |
| 2828 | rtd->ops.trigger = dpcm_fe_dai_trigger; |
| 2829 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; |
| 2830 | rtd->ops.close = dpcm_fe_dai_close; |
| 2831 | rtd->ops.pointer = soc_pcm_pointer; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2832 | } else { |
| 2833 | rtd->ops.open = soc_pcm_open; |
| 2834 | rtd->ops.hw_params = soc_pcm_hw_params; |
| 2835 | rtd->ops.prepare = soc_pcm_prepare; |
| 2836 | rtd->ops.trigger = soc_pcm_trigger; |
| 2837 | rtd->ops.hw_free = soc_pcm_hw_free; |
| 2838 | rtd->ops.close = soc_pcm_close; |
| 2839 | rtd->ops.pointer = soc_pcm_pointer; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2842 | for_each_rtd_components(rtd, i, component) { |
| 2843 | const struct snd_soc_component_driver *drv = component->driver; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2844 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2845 | if (drv->ioctl) |
| 2846 | rtd->ops.ioctl = snd_soc_pcm_component_ioctl; |
| 2847 | if (drv->sync_stop) |
| 2848 | rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; |
| 2849 | if (drv->copy_user) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2850 | rtd->ops.copy_user = snd_soc_pcm_component_copy_user; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2851 | if (drv->page) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2852 | rtd->ops.page = snd_soc_pcm_component_page; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2853 | if (drv->mmap) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2854 | rtd->ops.mmap = snd_soc_pcm_component_mmap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | if (playback) |
| 2858 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
| 2859 | |
| 2860 | if (capture) |
| 2861 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
| 2862 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2863 | ret = snd_soc_pcm_component_new(rtd); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2864 | if (ret < 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2865 | dev_err(rtd->dev, "ASoC: pcm %s constructor failed for dailink %s: %d\n", |
| 2866 | new_name, rtd->dai_link->name, ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2867 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2870 | pcm->no_device_suspend = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2871 | out: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2872 | dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n", |
| 2873 | (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, |
| 2874 | (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2875 | return ret; |
| 2876 | } |
| 2877 | |
| 2878 | /* is the current PCM operation for this FE ? */ |
| 2879 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2880 | { |
| 2881 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) |
| 2882 | return 1; |
| 2883 | return 0; |
| 2884 | } |
| 2885 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); |
| 2886 | |
| 2887 | /* is the current PCM operation for this BE ? */ |
| 2888 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, |
| 2889 | struct snd_soc_pcm_runtime *be, int stream) |
| 2890 | { |
| 2891 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || |
| 2892 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && |
| 2893 | be->dpcm[stream].runtime_update)) |
| 2894 | return 1; |
| 2895 | return 0; |
| 2896 | } |
| 2897 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); |
| 2898 | |
| 2899 | /* get the substream for this BE */ |
| 2900 | struct snd_pcm_substream * |
| 2901 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) |
| 2902 | { |
| 2903 | return be->pcm->streams[stream].substream; |
| 2904 | } |
| 2905 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); |
| 2906 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2907 | static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, |
| 2908 | struct snd_soc_pcm_runtime *be, |
| 2909 | int stream, |
| 2910 | const enum snd_soc_dpcm_state *states, |
| 2911 | int num_states) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2912 | { |
| 2913 | struct snd_soc_dpcm *dpcm; |
| 2914 | int state; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2915 | int ret = 1; |
| 2916 | unsigned long flags; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2917 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2918 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2919 | spin_lock_irqsave(&fe->card->dpcm_lock, flags); |
| 2920 | for_each_dpcm_fe(be, stream, dpcm) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2921 | |
| 2922 | if (dpcm->fe == fe) |
| 2923 | continue; |
| 2924 | |
| 2925 | state = dpcm->fe->dpcm[stream].state; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2926 | for (i = 0; i < num_states; i++) { |
| 2927 | if (state == states[i]) { |
| 2928 | ret = 0; |
| 2929 | break; |
| 2930 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2931 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2932 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2933 | spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2934 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2935 | /* it's safe to do this BE DAI */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2936 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2937 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2938 | |
| 2939 | /* |
| 2940 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE |
| 2941 | * are not running, paused or suspended for the specified stream direction. |
| 2942 | */ |
| 2943 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, |
| 2944 | struct snd_soc_pcm_runtime *be, int stream) |
| 2945 | { |
| 2946 | const enum snd_soc_dpcm_state state[] = { |
| 2947 | SND_SOC_DPCM_STATE_START, |
| 2948 | SND_SOC_DPCM_STATE_PAUSED, |
| 2949 | SND_SOC_DPCM_STATE_SUSPEND, |
| 2950 | }; |
| 2951 | |
| 2952 | return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); |
| 2953 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2954 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); |
| 2955 | |
| 2956 | /* |
| 2957 | * We can only change hw params a BE DAI if any of it's FE are not prepared, |
| 2958 | * running, paused or suspended for the specified stream direction. |
| 2959 | */ |
| 2960 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, |
| 2961 | struct snd_soc_pcm_runtime *be, int stream) |
| 2962 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2963 | const enum snd_soc_dpcm_state state[] = { |
| 2964 | SND_SOC_DPCM_STATE_START, |
| 2965 | SND_SOC_DPCM_STATE_PAUSED, |
| 2966 | SND_SOC_DPCM_STATE_SUSPEND, |
| 2967 | SND_SOC_DPCM_STATE_PREPARE, |
| 2968 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2969 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 2970 | return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2971 | } |
| 2972 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); |