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