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