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