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