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