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