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