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