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