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