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