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