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