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