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