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