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