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