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