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