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