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