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