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