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, |
Jarkko Nikula | 90cc7f1 | 2014-12-23 11:04:41 +0200 | [diff] [blame^] | 749 | "ASoC: codec DAI prepare error: %d\n", |
| 750 | ret); |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 751 | goto out; |
| 752 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 756 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 757 | ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); |
| 758 | if (ret < 0) { |
Jarkko Nikula | 90cc7f1 | 2014-12-23 11:04:41 +0200 | [diff] [blame^] | 759 | dev_err(cpu_dai->dev, |
| 760 | "ASoC: cpu DAI prepare error: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 761 | goto out; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* cancel any delayed stream shutdown that is pending */ |
| 766 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 767 | rtd->pop_wait) { |
| 768 | rtd->pop_wait = 0; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 769 | cancel_delayed_work(&rtd->delayed_work); |
| 770 | } |
| 771 | |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 772 | snd_soc_dapm_stream_event(rtd, substream->stream, |
| 773 | SND_SOC_DAPM_STREAM_START); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 774 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 775 | for (i = 0; i < rtd->num_codecs; i++) |
| 776 | snd_soc_dai_digital_mute(rtd->codec_dais[i], 0, |
| 777 | substream->stream); |
Ramesh Babu | ae11601 | 2014-10-15 12:34:59 +0530 | [diff] [blame] | 778 | snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 779 | |
| 780 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 781 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 782 | return ret; |
| 783 | } |
| 784 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 785 | static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, |
| 786 | unsigned int mask) |
| 787 | { |
| 788 | struct snd_interval *interval; |
| 789 | int channels = hweight_long(mask); |
| 790 | |
| 791 | interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 792 | interval->min = channels; |
| 793 | interval->max = channels; |
| 794 | } |
| 795 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 796 | int soc_dai_hw_params(struct snd_pcm_substream *substream, |
| 797 | struct snd_pcm_hw_params *params, |
| 798 | struct snd_soc_dai *dai) |
| 799 | { |
| 800 | int ret; |
| 801 | |
| 802 | if (dai->driver->ops && dai->driver->ops->hw_params) { |
| 803 | ret = dai->driver->ops->hw_params(substream, params, dai); |
| 804 | if (ret < 0) { |
| 805 | dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n", |
| 806 | dai->name, ret); |
| 807 | return ret; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 814 | /* |
| 815 | * Called by ALSA when the hardware params are set by application. This |
| 816 | * function can also be called multiple times and can allocate buffers |
| 817 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 818 | */ |
| 819 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 820 | struct snd_pcm_hw_params *params) |
| 821 | { |
| 822 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 823 | struct snd_soc_platform *platform = rtd->platform; |
| 824 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 825 | int i, ret = 0; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 826 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 827 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 828 | |
Nicolin Chen | 3635bf0 | 2013-11-13 18:56:24 +0800 | [diff] [blame] | 829 | ret = soc_pcm_params_symmetry(substream, params); |
| 830 | if (ret) |
| 831 | goto out; |
| 832 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 833 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { |
| 834 | ret = rtd->dai_link->ops->hw_params(substream, params); |
| 835 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 836 | dev_err(rtd->card->dev, "ASoC: machine hw_params" |
| 837 | " failed: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 838 | goto out; |
| 839 | } |
| 840 | } |
| 841 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 842 | for (i = 0; i < rtd->num_codecs; i++) { |
| 843 | struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; |
| 844 | struct snd_pcm_hw_params codec_params; |
| 845 | |
| 846 | /* copy params for each codec */ |
| 847 | codec_params = *params; |
| 848 | |
| 849 | /* fixup params based on TDM slot masks */ |
| 850 | if (codec_dai->tx_mask) |
| 851 | soc_pcm_codec_params_fixup(&codec_params, |
| 852 | codec_dai->tx_mask); |
| 853 | if (codec_dai->rx_mask) |
| 854 | soc_pcm_codec_params_fixup(&codec_params, |
| 855 | codec_dai->rx_mask); |
| 856 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 857 | ret = soc_dai_hw_params(substream, &codec_params, codec_dai); |
| 858 | if(ret < 0) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 859 | goto codec_err; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 860 | |
| 861 | codec_dai->rate = params_rate(&codec_params); |
| 862 | codec_dai->channels = params_channels(&codec_params); |
| 863 | codec_dai->sample_bits = snd_pcm_format_physical_width( |
| 864 | params_format(&codec_params)); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 867 | ret = soc_dai_hw_params(substream, params, cpu_dai); |
| 868 | if (ret < 0) |
| 869 | goto interface_err; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 870 | |
| 871 | if (platform->driver->ops && platform->driver->ops->hw_params) { |
| 872 | ret = platform->driver->ops->hw_params(substream, params); |
| 873 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 874 | dev_err(platform->dev, "ASoC: %s hw params failed: %d\n", |
Lars-Peter Clausen | f433320 | 2014-06-16 18:13:02 +0200 | [diff] [blame] | 875 | platform->component.name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 876 | goto platform_err; |
| 877 | } |
| 878 | } |
| 879 | |
Nicolin Chen | 3635bf0 | 2013-11-13 18:56:24 +0800 | [diff] [blame] | 880 | /* store the parameters for each DAIs */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 881 | cpu_dai->rate = params_rate(params); |
Nicolin Chen | 3635bf0 | 2013-11-13 18:56:24 +0800 | [diff] [blame] | 882 | cpu_dai->channels = params_channels(params); |
| 883 | cpu_dai->sample_bits = |
| 884 | snd_pcm_format_physical_width(params_format(params)); |
| 885 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 886 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 887 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 888 | return ret; |
| 889 | |
| 890 | platform_err: |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 891 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 892 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 893 | |
| 894 | interface_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 895 | i = rtd->num_codecs; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 896 | |
| 897 | codec_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 898 | while (--i >= 0) { |
| 899 | struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; |
| 900 | if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) |
| 901 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 902 | codec_dai->rate = 0; |
| 903 | } |
| 904 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 905 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 906 | rtd->dai_link->ops->hw_free(substream); |
| 907 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 908 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * Frees resources allocated by hw_params, can be called multiple times |
| 914 | */ |
| 915 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 916 | { |
| 917 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 918 | struct snd_soc_platform *platform = rtd->platform; |
| 919 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 920 | struct snd_soc_dai *codec_dai; |
Nicolin Chen | 7f62b6e | 2013-12-04 11:18:36 +0800 | [diff] [blame] | 921 | bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 922 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 923 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 924 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 925 | |
Nicolin Chen | d338342 | 2013-11-20 18:37:09 +0800 | [diff] [blame] | 926 | /* clear the corresponding DAIs parameters when going to be inactive */ |
| 927 | if (cpu_dai->active == 1) { |
| 928 | cpu_dai->rate = 0; |
| 929 | cpu_dai->channels = 0; |
| 930 | cpu_dai->sample_bits = 0; |
| 931 | } |
| 932 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 933 | for (i = 0; i < rtd->num_codecs; i++) { |
| 934 | codec_dai = rtd->codec_dais[i]; |
| 935 | if (codec_dai->active == 1) { |
| 936 | codec_dai->rate = 0; |
| 937 | codec_dai->channels = 0; |
| 938 | codec_dai->sample_bits = 0; |
| 939 | } |
Nicolin Chen | d338342 | 2013-11-20 18:37:09 +0800 | [diff] [blame] | 940 | } |
| 941 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 942 | /* apply codec digital mute */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 943 | for (i = 0; i < rtd->num_codecs; i++) { |
| 944 | if ((playback && rtd->codec_dais[i]->playback_active == 1) || |
| 945 | (!playback && rtd->codec_dais[i]->capture_active == 1)) |
| 946 | snd_soc_dai_digital_mute(rtd->codec_dais[i], 1, |
| 947 | substream->stream); |
| 948 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 949 | |
| 950 | /* free any machine hw params */ |
| 951 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 952 | rtd->dai_link->ops->hw_free(substream); |
| 953 | |
| 954 | /* free any DMA resources */ |
| 955 | if (platform->driver->ops && platform->driver->ops->hw_free) |
| 956 | platform->driver->ops->hw_free(substream); |
| 957 | |
| 958 | /* now free hw params for the DAIs */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 959 | for (i = 0; i < rtd->num_codecs; i++) { |
| 960 | codec_dai = rtd->codec_dais[i]; |
| 961 | if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free) |
| 962 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 963 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 964 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 965 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 966 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 967 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 968 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 973 | { |
| 974 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 975 | struct snd_soc_platform *platform = rtd->platform; |
| 976 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 977 | struct snd_soc_dai *codec_dai; |
| 978 | int i, ret; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 979 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 980 | for (i = 0; i < rtd->num_codecs; i++) { |
| 981 | codec_dai = rtd->codec_dais[i]; |
| 982 | if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) { |
| 983 | ret = codec_dai->driver->ops->trigger(substream, |
| 984 | cmd, codec_dai); |
| 985 | if (ret < 0) |
| 986 | return ret; |
| 987 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | if (platform->driver->ops && platform->driver->ops->trigger) { |
| 991 | ret = platform->driver->ops->trigger(substream, cmd); |
| 992 | if (ret < 0) |
| 993 | return ret; |
| 994 | } |
| 995 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 996 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 997 | ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); |
| 998 | if (ret < 0) |
| 999 | return ret; |
| 1000 | } |
Jarkko Nikula | 4792b0d | 2014-04-28 14:17:52 +0200 | [diff] [blame] | 1001 | |
| 1002 | if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) { |
| 1003 | ret = rtd->dai_link->ops->trigger(substream, cmd); |
| 1004 | if (ret < 0) |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
| 1010 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1011 | static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, |
| 1012 | int cmd) |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1013 | { |
| 1014 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1015 | struct snd_soc_platform *platform = rtd->platform; |
| 1016 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1017 | struct snd_soc_dai *codec_dai; |
| 1018 | int i, ret; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1019 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1020 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1021 | codec_dai = rtd->codec_dais[i]; |
| 1022 | if (codec_dai->driver->ops && |
| 1023 | codec_dai->driver->ops->bespoke_trigger) { |
| 1024 | ret = codec_dai->driver->ops->bespoke_trigger(substream, |
| 1025 | cmd, codec_dai); |
| 1026 | if (ret < 0) |
| 1027 | return ret; |
| 1028 | } |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
Jean-Francois Moine | dcf0fa2 | 2014-01-03 09:19:18 +0100 | [diff] [blame] | 1031 | if (platform->driver->bespoke_trigger) { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1032 | ret = platform->driver->bespoke_trigger(substream, cmd); |
| 1033 | if (ret < 0) |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 1037 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1038 | ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai); |
| 1039 | if (ret < 0) |
| 1040 | return ret; |
| 1041 | } |
| 1042 | return 0; |
| 1043 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1044 | /* |
| 1045 | * soc level wrapper for pointer callback |
| 1046 | * If cpu_dai, codec_dai, platform driver has the delay callback, than |
| 1047 | * the runtime->delay will be updated accordingly. |
| 1048 | */ |
| 1049 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) |
| 1050 | { |
| 1051 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1052 | struct snd_soc_platform *platform = rtd->platform; |
| 1053 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1054 | struct snd_soc_dai *codec_dai; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1055 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1056 | snd_pcm_uframes_t offset = 0; |
| 1057 | snd_pcm_sframes_t delay = 0; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1058 | snd_pcm_sframes_t codec_delay = 0; |
| 1059 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1060 | |
| 1061 | if (platform->driver->ops && platform->driver->ops->pointer) |
| 1062 | offset = platform->driver->ops->pointer(substream); |
| 1063 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 1064 | if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1065 | delay += cpu_dai->driver->ops->delay(substream, cpu_dai); |
| 1066 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1067 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1068 | codec_dai = rtd->codec_dais[i]; |
| 1069 | if (codec_dai->driver->ops && codec_dai->driver->ops->delay) |
| 1070 | codec_delay = max(codec_delay, |
| 1071 | codec_dai->driver->ops->delay(substream, |
| 1072 | codec_dai)); |
| 1073 | } |
| 1074 | delay += codec_delay; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1075 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1076 | /* |
| 1077 | * None of the existing platform drivers implement delay(), so |
| 1078 | * for now the codec_dai of first multicodec entry is used |
| 1079 | */ |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1080 | if (platform->driver->delay) |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1081 | delay += platform->driver->delay(substream, rtd->codec_dais[0]); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1082 | |
| 1083 | runtime->delay = delay; |
| 1084 | |
| 1085 | return offset; |
| 1086 | } |
| 1087 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1088 | /* connect a FE and BE */ |
| 1089 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, |
| 1090 | struct snd_soc_pcm_runtime *be, int stream) |
| 1091 | { |
| 1092 | struct snd_soc_dpcm *dpcm; |
| 1093 | |
| 1094 | /* only add new dpcms */ |
| 1095 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1096 | if (dpcm->be == be && dpcm->fe == fe) |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); |
| 1101 | if (!dpcm) |
| 1102 | return -ENOMEM; |
| 1103 | |
| 1104 | dpcm->be = be; |
| 1105 | dpcm->fe = fe; |
| 1106 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; |
| 1107 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; |
| 1108 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); |
| 1109 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); |
| 1110 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1111 | 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] | 1112 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1113 | stream ? "<-" : "->", be->dai_link->name); |
| 1114 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 1115 | #ifdef CONFIG_DEBUG_FS |
| 1116 | dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, |
| 1117 | fe->debugfs_dpcm_root, &dpcm->state); |
| 1118 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1119 | return 1; |
| 1120 | } |
| 1121 | |
| 1122 | /* reparent a BE onto another FE */ |
| 1123 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, |
| 1124 | struct snd_soc_pcm_runtime *be, int stream) |
| 1125 | { |
| 1126 | struct snd_soc_dpcm *dpcm; |
| 1127 | struct snd_pcm_substream *fe_substream, *be_substream; |
| 1128 | |
| 1129 | /* reparent if BE is connected to other FEs */ |
| 1130 | if (!be->dpcm[stream].users) |
| 1131 | return; |
| 1132 | |
| 1133 | be_substream = snd_soc_dpcm_get_substream(be, stream); |
| 1134 | |
| 1135 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 1136 | if (dpcm->fe == fe) |
| 1137 | continue; |
| 1138 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1139 | dev_dbg(fe->dev, "reparent %s path %s %s %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1140 | stream ? "capture" : "playback", |
| 1141 | dpcm->fe->dai_link->name, |
| 1142 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1143 | |
| 1144 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); |
| 1145 | be_substream->runtime = fe_substream->runtime; |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* disconnect a BE and FE */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1151 | void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1152 | { |
| 1153 | struct snd_soc_dpcm *dpcm, *d; |
| 1154 | |
| 1155 | 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] | 1156 | dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1157 | stream ? "capture" : "playback", |
| 1158 | dpcm->be->dai_link->name); |
| 1159 | |
| 1160 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) |
| 1161 | continue; |
| 1162 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1163 | dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1164 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1165 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1166 | |
| 1167 | /* BEs still alive need new FE */ |
| 1168 | dpcm_be_reparent(fe, dpcm->be, stream); |
| 1169 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 1170 | #ifdef CONFIG_DEBUG_FS |
| 1171 | debugfs_remove(dpcm->debugfs_state); |
| 1172 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1173 | list_del(&dpcm->list_be); |
| 1174 | list_del(&dpcm->list_fe); |
| 1175 | kfree(dpcm); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* get BE for DAI widget and stream */ |
| 1180 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, |
| 1181 | struct snd_soc_dapm_widget *widget, int stream) |
| 1182 | { |
| 1183 | struct snd_soc_pcm_runtime *be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1184 | int i, j; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1185 | |
| 1186 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1187 | for (i = 0; i < card->num_links; i++) { |
| 1188 | be = &card->rtd[i]; |
| 1189 | |
Liam Girdwood | 35ea065 | 2012-06-05 19:26:59 +0100 | [diff] [blame] | 1190 | if (!be->dai_link->no_pcm) |
| 1191 | continue; |
| 1192 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1193 | if (be->cpu_dai->playback_widget == widget) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1194 | return be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1195 | |
| 1196 | for (j = 0; j < be->num_codecs; j++) { |
| 1197 | struct snd_soc_dai *dai = be->codec_dais[j]; |
| 1198 | if (dai->playback_widget == widget) |
| 1199 | return be; |
| 1200 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1201 | } |
| 1202 | } else { |
| 1203 | |
| 1204 | for (i = 0; i < card->num_links; i++) { |
| 1205 | be = &card->rtd[i]; |
| 1206 | |
Liam Girdwood | 35ea065 | 2012-06-05 19:26:59 +0100 | [diff] [blame] | 1207 | if (!be->dai_link->no_pcm) |
| 1208 | continue; |
| 1209 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1210 | if (be->cpu_dai->capture_widget == widget) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1211 | return be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1212 | |
| 1213 | for (j = 0; j < be->num_codecs; j++) { |
| 1214 | struct snd_soc_dai *dai = be->codec_dais[j]; |
| 1215 | if (dai->capture_widget == widget) |
| 1216 | return be; |
| 1217 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1221 | 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] | 1222 | stream ? "capture" : "playback", widget->name); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | |
| 1226 | static inline struct snd_soc_dapm_widget * |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1227 | dai_get_widget(struct snd_soc_dai *dai, int stream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1228 | { |
| 1229 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1230 | return dai->playback_widget; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1231 | else |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1232 | return dai->capture_widget; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, |
| 1236 | struct snd_soc_dapm_widget *widget) |
| 1237 | { |
| 1238 | int i; |
| 1239 | |
| 1240 | for (i = 0; i < list->num_widgets; i++) { |
| 1241 | if (widget == list->widgets[i]) |
| 1242 | return 1; |
| 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1248 | int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1249 | int stream, struct snd_soc_dapm_widget_list **list_) |
| 1250 | { |
| 1251 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; |
| 1252 | struct snd_soc_dapm_widget_list *list; |
| 1253 | int paths; |
| 1254 | |
| 1255 | list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) + |
| 1256 | sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL); |
| 1257 | if (list == NULL) |
| 1258 | return -ENOMEM; |
| 1259 | |
| 1260 | /* get number of valid DAI paths and their widgets */ |
| 1261 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list); |
| 1262 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1263 | dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1264 | stream ? "capture" : "playback"); |
| 1265 | |
| 1266 | *list_ = list; |
| 1267 | return paths; |
| 1268 | } |
| 1269 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1270 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1271 | struct snd_soc_dapm_widget_list **list_) |
| 1272 | { |
| 1273 | struct snd_soc_dpcm *dpcm; |
| 1274 | struct snd_soc_dapm_widget_list *list = *list_; |
| 1275 | struct snd_soc_dapm_widget *widget; |
| 1276 | int prune = 0; |
| 1277 | |
| 1278 | /* Destroy any old FE <--> BE connections */ |
| 1279 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1280 | unsigned int i; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1281 | |
| 1282 | /* is there a valid CPU DAI widget for this BE */ |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1283 | widget = dai_get_widget(dpcm->be->cpu_dai, stream); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1284 | |
| 1285 | /* prune the BE if it's no longer in our active list */ |
| 1286 | if (widget && widget_in_list(list, widget)) |
| 1287 | continue; |
| 1288 | |
| 1289 | /* is there a valid CODEC DAI widget for this BE */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1290 | for (i = 0; i < dpcm->be->num_codecs; i++) { |
| 1291 | struct snd_soc_dai *dai = dpcm->be->codec_dais[i]; |
| 1292 | widget = dai_get_widget(dai, stream); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1293 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1294 | /* prune the BE if it's no longer in our active list */ |
| 1295 | if (widget && widget_in_list(list, widget)) |
| 1296 | continue; |
| 1297 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1298 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1299 | dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1300 | stream ? "capture" : "playback", |
| 1301 | dpcm->be->dai_link->name, fe->dai_link->name); |
| 1302 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 1303 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1304 | prune++; |
| 1305 | } |
| 1306 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1307 | 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] | 1308 | return prune; |
| 1309 | } |
| 1310 | |
| 1311 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1312 | struct snd_soc_dapm_widget_list **list_) |
| 1313 | { |
| 1314 | struct snd_soc_card *card = fe->card; |
| 1315 | struct snd_soc_dapm_widget_list *list = *list_; |
| 1316 | struct snd_soc_pcm_runtime *be; |
| 1317 | int i, new = 0, err; |
| 1318 | |
| 1319 | /* Create any new FE <--> BE connections */ |
| 1320 | for (i = 0; i < list->num_widgets; i++) { |
| 1321 | |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1322 | switch (list->widgets[i]->id) { |
| 1323 | case snd_soc_dapm_dai_in: |
| 1324 | case snd_soc_dapm_dai_out: |
| 1325 | break; |
| 1326 | default: |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1327 | continue; |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1328 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1329 | |
| 1330 | /* is there a valid BE rtd for this widget */ |
| 1331 | be = dpcm_get_be(card, list->widgets[i], stream); |
| 1332 | if (!be) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1333 | dev_err(fe->dev, "ASoC: no BE found for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1334 | list->widgets[i]->name); |
| 1335 | continue; |
| 1336 | } |
| 1337 | |
| 1338 | /* make sure BE is a real BE */ |
| 1339 | if (!be->dai_link->no_pcm) |
| 1340 | continue; |
| 1341 | |
| 1342 | /* don't connect if FE is not running */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1343 | if (!fe->dpcm[stream].runtime && !fe->fe_compr) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1344 | continue; |
| 1345 | |
| 1346 | /* newly connected FE and BE */ |
| 1347 | err = dpcm_be_connect(fe, be, stream); |
| 1348 | if (err < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1349 | dev_err(fe->dev, "ASoC: can't connect %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1350 | list->widgets[i]->name); |
| 1351 | break; |
| 1352 | } else if (err == 0) /* already connected */ |
| 1353 | continue; |
| 1354 | |
| 1355 | /* new */ |
| 1356 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1357 | new++; |
| 1358 | } |
| 1359 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1360 | dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1361 | return new; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * Find the corresponding BE DAIs that source or sink audio to this |
| 1366 | * FE substream. |
| 1367 | */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1368 | int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1369 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
| 1370 | { |
| 1371 | if (new) |
| 1372 | return dpcm_add_paths(fe, stream, list); |
| 1373 | else |
| 1374 | return dpcm_prune_paths(fe, stream, list); |
| 1375 | } |
| 1376 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1377 | 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] | 1378 | { |
| 1379 | struct snd_soc_dpcm *dpcm; |
| 1380 | |
| 1381 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 1382 | dpcm->be->dpcm[stream].runtime_update = |
| 1383 | SND_SOC_DPCM_UPDATE_NO; |
| 1384 | } |
| 1385 | |
| 1386 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, |
| 1387 | int stream) |
| 1388 | { |
| 1389 | struct snd_soc_dpcm *dpcm; |
| 1390 | |
| 1391 | /* disable any enabled and non active backends */ |
| 1392 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1393 | |
| 1394 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1395 | struct snd_pcm_substream *be_substream = |
| 1396 | snd_soc_dpcm_get_substream(be, stream); |
| 1397 | |
| 1398 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1399 | 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] | 1400 | stream ? "capture" : "playback", |
| 1401 | be->dpcm[stream].state); |
| 1402 | |
| 1403 | if (--be->dpcm[stream].users != 0) |
| 1404 | continue; |
| 1405 | |
| 1406 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1407 | continue; |
| 1408 | |
| 1409 | soc_pcm_close(be_substream); |
| 1410 | be_substream->runtime = NULL; |
| 1411 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1415 | 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] | 1416 | { |
| 1417 | struct snd_soc_dpcm *dpcm; |
| 1418 | int err, count = 0; |
| 1419 | |
| 1420 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ |
| 1421 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1422 | |
| 1423 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1424 | struct snd_pcm_substream *be_substream = |
| 1425 | snd_soc_dpcm_get_substream(be, stream); |
| 1426 | |
Russell King - ARM Linux | 2062b4c | 2013-10-31 15:09:20 +0000 | [diff] [blame] | 1427 | if (!be_substream) { |
| 1428 | dev_err(be->dev, "ASoC: no backend %s stream\n", |
| 1429 | stream ? "capture" : "playback"); |
| 1430 | continue; |
| 1431 | } |
| 1432 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1433 | /* is this op for this BE ? */ |
| 1434 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1435 | continue; |
| 1436 | |
| 1437 | /* first time the dpcm is open ? */ |
| 1438 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1439 | 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] | 1440 | stream ? "capture" : "playback", |
| 1441 | be->dpcm[stream].state); |
| 1442 | |
| 1443 | if (be->dpcm[stream].users++ != 0) |
| 1444 | continue; |
| 1445 | |
| 1446 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && |
| 1447 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) |
| 1448 | continue; |
| 1449 | |
Russell King - ARM Linux | 2062b4c | 2013-10-31 15:09:20 +0000 | [diff] [blame] | 1450 | dev_dbg(be->dev, "ASoC: open %s BE %s\n", |
| 1451 | stream ? "capture" : "playback", be->dai_link->name); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1452 | |
| 1453 | be_substream->runtime = be->dpcm[stream].runtime; |
| 1454 | err = soc_pcm_open(be_substream); |
| 1455 | if (err < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1456 | dev_err(be->dev, "ASoC: BE open failed %d\n", err); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1457 | be->dpcm[stream].users--; |
| 1458 | if (be->dpcm[stream].users < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1459 | dev_err(be->dev, "ASoC: no users %s at unwind %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1460 | stream ? "capture" : "playback", |
| 1461 | be->dpcm[stream].state); |
| 1462 | |
| 1463 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1464 | goto unwind; |
| 1465 | } |
| 1466 | |
| 1467 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1468 | count++; |
| 1469 | } |
| 1470 | |
| 1471 | return count; |
| 1472 | |
| 1473 | unwind: |
| 1474 | /* disable any enabled and non active backends */ |
| 1475 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1476 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1477 | struct snd_pcm_substream *be_substream = |
| 1478 | snd_soc_dpcm_get_substream(be, stream); |
| 1479 | |
| 1480 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1481 | continue; |
| 1482 | |
| 1483 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1484 | dev_err(be->dev, "ASoC: no users %s at close %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1485 | stream ? "capture" : "playback", |
| 1486 | be->dpcm[stream].state); |
| 1487 | |
| 1488 | if (--be->dpcm[stream].users != 0) |
| 1489 | continue; |
| 1490 | |
| 1491 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1492 | continue; |
| 1493 | |
| 1494 | soc_pcm_close(be_substream); |
| 1495 | be_substream->runtime = NULL; |
| 1496 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1497 | } |
| 1498 | |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1502 | static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
| 1503 | struct snd_soc_pcm_stream *stream) |
| 1504 | { |
| 1505 | runtime->hw.rate_min = stream->rate_min; |
| 1506 | runtime->hw.rate_max = stream->rate_max; |
| 1507 | runtime->hw.channels_min = stream->channels_min; |
| 1508 | runtime->hw.channels_max = stream->channels_max; |
Lars-Peter Clausen | 002220a | 2014-01-06 14:19:07 +0100 | [diff] [blame] | 1509 | if (runtime->hw.formats) |
| 1510 | runtime->hw.formats &= stream->formats; |
| 1511 | else |
| 1512 | runtime->hw.formats = stream->formats; |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1513 | runtime->hw.rates = stream->rates; |
| 1514 | } |
| 1515 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1516 | static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1517 | { |
| 1518 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1519 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1520 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 1521 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; |
| 1522 | |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1523 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1524 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); |
| 1525 | else |
| 1526 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1529 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); |
| 1530 | |
| 1531 | /* Set FE's runtime_update state; the state is protected via PCM stream lock |
| 1532 | * for avoiding the race with trigger callback. |
| 1533 | * If the state is unset and a trigger is pending while the previous operation, |
| 1534 | * process the pending trigger action here. |
| 1535 | */ |
| 1536 | static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, |
| 1537 | int stream, enum snd_soc_dpcm_update state) |
| 1538 | { |
| 1539 | struct snd_pcm_substream *substream = |
| 1540 | snd_soc_dpcm_get_substream(fe, stream); |
| 1541 | |
| 1542 | snd_pcm_stream_lock_irq(substream); |
| 1543 | if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { |
| 1544 | dpcm_fe_dai_do_trigger(substream, |
| 1545 | fe->dpcm[stream].trigger_pending - 1); |
| 1546 | fe->dpcm[stream].trigger_pending = 0; |
| 1547 | } |
| 1548 | fe->dpcm[stream].runtime_update = state; |
| 1549 | snd_pcm_stream_unlock_irq(substream); |
| 1550 | } |
| 1551 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1552 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) |
| 1553 | { |
| 1554 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1555 | struct snd_pcm_runtime *runtime = fe_substream->runtime; |
| 1556 | int stream = fe_substream->stream, ret = 0; |
| 1557 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1558 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1559 | |
| 1560 | ret = dpcm_be_dai_startup(fe, fe_substream->stream); |
| 1561 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1562 | 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] | 1563 | goto be_err; |
| 1564 | } |
| 1565 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1566 | 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] | 1567 | |
| 1568 | /* start the DAI frontend */ |
| 1569 | ret = soc_pcm_open(fe_substream); |
| 1570 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1571 | dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1572 | goto unwind; |
| 1573 | } |
| 1574 | |
| 1575 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1576 | |
| 1577 | dpcm_set_fe_runtime(fe_substream); |
| 1578 | snd_pcm_limit_hw_rates(runtime); |
| 1579 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1580 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1581 | return 0; |
| 1582 | |
| 1583 | unwind: |
| 1584 | dpcm_be_dai_startup_unwind(fe, fe_substream->stream); |
| 1585 | be_err: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1586 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1587 | return ret; |
| 1588 | } |
| 1589 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1590 | 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] | 1591 | { |
| 1592 | struct snd_soc_dpcm *dpcm; |
| 1593 | |
| 1594 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ |
| 1595 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1596 | |
| 1597 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1598 | struct snd_pcm_substream *be_substream = |
| 1599 | snd_soc_dpcm_get_substream(be, stream); |
| 1600 | |
| 1601 | /* is this op for this BE ? */ |
| 1602 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1603 | continue; |
| 1604 | |
| 1605 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1606 | 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] | 1607 | stream ? "capture" : "playback", |
| 1608 | be->dpcm[stream].state); |
| 1609 | |
| 1610 | if (--be->dpcm[stream].users != 0) |
| 1611 | continue; |
| 1612 | |
| 1613 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1614 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) |
| 1615 | continue; |
| 1616 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1617 | dev_dbg(be->dev, "ASoC: close BE %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1618 | dpcm->fe->dai_link->name); |
| 1619 | |
| 1620 | soc_pcm_close(be_substream); |
| 1621 | be_substream->runtime = NULL; |
| 1622 | |
| 1623 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1624 | } |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) |
| 1629 | { |
| 1630 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1631 | int stream = substream->stream; |
| 1632 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1633 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1634 | |
| 1635 | /* shutdown the BEs */ |
| 1636 | dpcm_be_dai_shutdown(fe, substream->stream); |
| 1637 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1638 | 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] | 1639 | |
| 1640 | /* now shutdown the frontend */ |
| 1641 | soc_pcm_close(substream); |
| 1642 | |
| 1643 | /* run the stream event for each BE */ |
| 1644 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 1645 | |
| 1646 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1647 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1648 | return 0; |
| 1649 | } |
| 1650 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1651 | 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] | 1652 | { |
| 1653 | struct snd_soc_dpcm *dpcm; |
| 1654 | |
| 1655 | /* only hw_params backends that are either sinks or sources |
| 1656 | * to this frontend DAI */ |
| 1657 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1658 | |
| 1659 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1660 | struct snd_pcm_substream *be_substream = |
| 1661 | snd_soc_dpcm_get_substream(be, stream); |
| 1662 | |
| 1663 | /* is this op for this BE ? */ |
| 1664 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1665 | continue; |
| 1666 | |
| 1667 | /* only free hw when no longer used - check all FEs */ |
| 1668 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1669 | continue; |
| 1670 | |
Qiao Zhou | 36fba62 | 2014-12-03 10:13:43 +0800 | [diff] [blame] | 1671 | /* do not free hw if this BE is used by other FE */ |
| 1672 | if (be->dpcm[stream].users > 1) |
| 1673 | continue; |
| 1674 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1675 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1676 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 1677 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
Patrick Lai | 08b2784 | 2012-12-19 19:36:02 -0800 | [diff] [blame] | 1678 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1679 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1680 | continue; |
| 1681 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1682 | dev_dbg(be->dev, "ASoC: hw_free BE %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1683 | dpcm->fe->dai_link->name); |
| 1684 | |
| 1685 | soc_pcm_hw_free(be_substream); |
| 1686 | |
| 1687 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1688 | } |
| 1689 | |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1693 | static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1694 | { |
| 1695 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1696 | int err, stream = substream->stream; |
| 1697 | |
| 1698 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1699 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1700 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1701 | 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] | 1702 | |
| 1703 | /* call hw_free on the frontend */ |
| 1704 | err = soc_pcm_hw_free(substream); |
| 1705 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1706 | dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1707 | fe->dai_link->name); |
| 1708 | |
| 1709 | /* only hw_params backends that are either sinks or sources |
| 1710 | * to this frontend DAI */ |
| 1711 | err = dpcm_be_dai_hw_free(fe, stream); |
| 1712 | |
| 1713 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1714 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1715 | |
| 1716 | mutex_unlock(&fe->card->mutex); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1720 | 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] | 1721 | { |
| 1722 | struct snd_soc_dpcm *dpcm; |
| 1723 | int ret; |
| 1724 | |
| 1725 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1726 | |
| 1727 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1728 | struct snd_pcm_substream *be_substream = |
| 1729 | snd_soc_dpcm_get_substream(be, stream); |
| 1730 | |
| 1731 | /* is this op for this BE ? */ |
| 1732 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1733 | continue; |
| 1734 | |
| 1735 | /* only allow hw_params() if no connected FEs are running */ |
| 1736 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) |
| 1737 | continue; |
| 1738 | |
| 1739 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 1740 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1741 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) |
| 1742 | continue; |
| 1743 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1744 | dev_dbg(be->dev, "ASoC: hw_params BE %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1745 | dpcm->fe->dai_link->name); |
| 1746 | |
| 1747 | /* copy params for each dpcm */ |
| 1748 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, |
| 1749 | sizeof(struct snd_pcm_hw_params)); |
| 1750 | |
| 1751 | /* perform any hw_params fixups */ |
| 1752 | if (be->dai_link->be_hw_params_fixup) { |
| 1753 | ret = be->dai_link->be_hw_params_fixup(be, |
| 1754 | &dpcm->hw_params); |
| 1755 | if (ret < 0) { |
| 1756 | dev_err(be->dev, |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1757 | "ASoC: hw_params BE fixup failed %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1758 | ret); |
| 1759 | goto unwind; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); |
| 1764 | if (ret < 0) { |
| 1765 | dev_err(dpcm->be->dev, |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1766 | "ASoC: hw_params BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1767 | goto unwind; |
| 1768 | } |
| 1769 | |
| 1770 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 1771 | } |
| 1772 | return 0; |
| 1773 | |
| 1774 | unwind: |
| 1775 | /* disable any enabled and non active backends */ |
| 1776 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1777 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1778 | struct snd_pcm_substream *be_substream = |
| 1779 | snd_soc_dpcm_get_substream(be, stream); |
| 1780 | |
| 1781 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1782 | continue; |
| 1783 | |
| 1784 | /* only allow hw_free() if no connected FEs are running */ |
| 1785 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1786 | continue; |
| 1787 | |
| 1788 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 1789 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1790 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1791 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1792 | continue; |
| 1793 | |
| 1794 | soc_pcm_hw_free(be_substream); |
| 1795 | } |
| 1796 | |
| 1797 | return ret; |
| 1798 | } |
| 1799 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1800 | static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
| 1801 | struct snd_pcm_hw_params *params) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1802 | { |
| 1803 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1804 | int ret, stream = substream->stream; |
| 1805 | |
| 1806 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1807 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1808 | |
| 1809 | memcpy(&fe->dpcm[substream->stream].hw_params, params, |
| 1810 | sizeof(struct snd_pcm_hw_params)); |
| 1811 | ret = dpcm_be_dai_hw_params(fe, substream->stream); |
| 1812 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1813 | dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1814 | goto out; |
| 1815 | } |
| 1816 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1817 | 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] | 1818 | fe->dai_link->name, params_rate(params), |
| 1819 | params_channels(params), params_format(params)); |
| 1820 | |
| 1821 | /* call hw_params on the frontend */ |
| 1822 | ret = soc_pcm_hw_params(substream, params); |
| 1823 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1824 | dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1825 | dpcm_be_dai_hw_free(fe, stream); |
| 1826 | } else |
| 1827 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 1828 | |
| 1829 | out: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1830 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1831 | mutex_unlock(&fe->card->mutex); |
| 1832 | return ret; |
| 1833 | } |
| 1834 | |
| 1835 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, |
| 1836 | struct snd_pcm_substream *substream, int cmd) |
| 1837 | { |
| 1838 | int ret; |
| 1839 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1840 | dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1841 | dpcm->fe->dai_link->name, cmd); |
| 1842 | |
| 1843 | ret = soc_pcm_trigger(substream, cmd); |
| 1844 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1845 | dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1850 | 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] | 1851 | int cmd) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1852 | { |
| 1853 | struct snd_soc_dpcm *dpcm; |
| 1854 | int ret = 0; |
| 1855 | |
| 1856 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1857 | |
| 1858 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1859 | struct snd_pcm_substream *be_substream = |
| 1860 | snd_soc_dpcm_get_substream(be, stream); |
| 1861 | |
| 1862 | /* is this op for this BE ? */ |
| 1863 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1864 | continue; |
| 1865 | |
| 1866 | switch (cmd) { |
| 1867 | case SNDRV_PCM_TRIGGER_START: |
| 1868 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 1869 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1870 | continue; |
| 1871 | |
| 1872 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1873 | if (ret) |
| 1874 | return ret; |
| 1875 | |
| 1876 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1877 | break; |
| 1878 | case SNDRV_PCM_TRIGGER_RESUME: |
| 1879 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
| 1880 | continue; |
| 1881 | |
| 1882 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1883 | if (ret) |
| 1884 | return ret; |
| 1885 | |
| 1886 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1887 | break; |
| 1888 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1889 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
| 1890 | continue; |
| 1891 | |
| 1892 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1893 | if (ret) |
| 1894 | return ret; |
| 1895 | |
| 1896 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1897 | break; |
| 1898 | case SNDRV_PCM_TRIGGER_STOP: |
| 1899 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 1900 | continue; |
| 1901 | |
| 1902 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1903 | continue; |
| 1904 | |
| 1905 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1906 | if (ret) |
| 1907 | return ret; |
| 1908 | |
| 1909 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 1910 | break; |
| 1911 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Nicolin Chen | 868a6ca | 2014-05-12 20:12:05 +0800 | [diff] [blame] | 1912 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1913 | continue; |
| 1914 | |
| 1915 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1916 | continue; |
| 1917 | |
| 1918 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1919 | if (ret) |
| 1920 | return ret; |
| 1921 | |
| 1922 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; |
| 1923 | break; |
| 1924 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1925 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 1926 | continue; |
| 1927 | |
| 1928 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1929 | continue; |
| 1930 | |
| 1931 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1932 | if (ret) |
| 1933 | return ret; |
| 1934 | |
| 1935 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 1936 | break; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | return ret; |
| 1941 | } |
| 1942 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); |
| 1943 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1944 | 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] | 1945 | { |
| 1946 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1947 | int stream = substream->stream, ret; |
| 1948 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 1949 | |
| 1950 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1951 | |
| 1952 | switch (trigger) { |
| 1953 | case SND_SOC_DPCM_TRIGGER_PRE: |
| 1954 | /* call trigger on the frontend before the backend. */ |
| 1955 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1956 | dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1957 | fe->dai_link->name, cmd); |
| 1958 | |
| 1959 | ret = soc_pcm_trigger(substream, cmd); |
| 1960 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1961 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1962 | goto out; |
| 1963 | } |
| 1964 | |
| 1965 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 1966 | break; |
| 1967 | case SND_SOC_DPCM_TRIGGER_POST: |
| 1968 | /* call trigger on the frontend after the backend. */ |
| 1969 | |
| 1970 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 1971 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1972 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1973 | goto out; |
| 1974 | } |
| 1975 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1976 | dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1977 | fe->dai_link->name, cmd); |
| 1978 | |
| 1979 | ret = soc_pcm_trigger(substream, cmd); |
| 1980 | break; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1981 | case SND_SOC_DPCM_TRIGGER_BESPOKE: |
| 1982 | /* bespoke trigger() - handles both FE and BEs */ |
| 1983 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1984 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1985 | fe->dai_link->name, cmd); |
| 1986 | |
| 1987 | ret = soc_pcm_bespoke_trigger(substream, cmd); |
| 1988 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1989 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1990 | goto out; |
| 1991 | } |
| 1992 | break; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1993 | default: |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1994 | 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] | 1995 | fe->dai_link->name); |
| 1996 | ret = -EINVAL; |
| 1997 | goto out; |
| 1998 | } |
| 1999 | |
| 2000 | switch (cmd) { |
| 2001 | case SNDRV_PCM_TRIGGER_START: |
| 2002 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2003 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2004 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2005 | break; |
| 2006 | case SNDRV_PCM_TRIGGER_STOP: |
| 2007 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 2008 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2009 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 2010 | break; |
| 2011 | } |
| 2012 | |
| 2013 | out: |
| 2014 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 2015 | return ret; |
| 2016 | } |
| 2017 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2018 | static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
| 2019 | { |
| 2020 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2021 | int stream = substream->stream; |
| 2022 | |
| 2023 | /* if FE's runtime_update is already set, we're in race; |
| 2024 | * process this trigger later at exit |
| 2025 | */ |
| 2026 | if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { |
| 2027 | fe->dpcm[stream].trigger_pending = cmd + 1; |
| 2028 | return 0; /* delayed, assuming it's successful */ |
| 2029 | } |
| 2030 | |
| 2031 | /* we're alone, let's trigger */ |
| 2032 | return dpcm_fe_dai_do_trigger(substream, cmd); |
| 2033 | } |
| 2034 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 2035 | 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] | 2036 | { |
| 2037 | struct snd_soc_dpcm *dpcm; |
| 2038 | int ret = 0; |
| 2039 | |
| 2040 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2041 | |
| 2042 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2043 | struct snd_pcm_substream *be_substream = |
| 2044 | snd_soc_dpcm_get_substream(be, stream); |
| 2045 | |
| 2046 | /* is this op for this BE ? */ |
| 2047 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2048 | continue; |
| 2049 | |
| 2050 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2051 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 2052 | continue; |
| 2053 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2054 | dev_dbg(be->dev, "ASoC: prepare BE %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2055 | dpcm->fe->dai_link->name); |
| 2056 | |
| 2057 | ret = soc_pcm_prepare(be_substream); |
| 2058 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2059 | dev_err(be->dev, "ASoC: backend prepare failed %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2060 | ret); |
| 2061 | break; |
| 2062 | } |
| 2063 | |
| 2064 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2065 | } |
| 2066 | return ret; |
| 2067 | } |
| 2068 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2069 | static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2070 | { |
| 2071 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2072 | int stream = substream->stream, ret = 0; |
| 2073 | |
| 2074 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2075 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2076 | 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] | 2077 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2078 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2079 | |
| 2080 | /* there is no point preparing this FE if there are no BEs */ |
| 2081 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2082 | dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2083 | fe->dai_link->name); |
| 2084 | ret = -EINVAL; |
| 2085 | goto out; |
| 2086 | } |
| 2087 | |
| 2088 | ret = dpcm_be_dai_prepare(fe, substream->stream); |
| 2089 | if (ret < 0) |
| 2090 | goto out; |
| 2091 | |
| 2092 | /* call prepare on the frontend */ |
| 2093 | ret = soc_pcm_prepare(substream); |
| 2094 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2095 | dev_err(fe->dev,"ASoC: prepare FE %s failed\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2096 | fe->dai_link->name); |
| 2097 | goto out; |
| 2098 | } |
| 2099 | |
| 2100 | /* run the stream event for each BE */ |
| 2101 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 2102 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2103 | |
| 2104 | out: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2105 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2106 | mutex_unlock(&fe->card->mutex); |
| 2107 | |
| 2108 | return ret; |
| 2109 | } |
| 2110 | |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2111 | static int soc_pcm_ioctl(struct snd_pcm_substream *substream, |
| 2112 | unsigned int cmd, void *arg) |
| 2113 | { |
| 2114 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2115 | struct snd_soc_platform *platform = rtd->platform; |
| 2116 | |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 2117 | if (platform->driver->ops && platform->driver->ops->ioctl) |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2118 | return platform->driver->ops->ioctl(substream, cmd, arg); |
| 2119 | return snd_pcm_lib_ioctl(substream, cmd, arg); |
| 2120 | } |
| 2121 | |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2122 | static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
| 2123 | { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2124 | struct snd_pcm_substream *substream = |
| 2125 | snd_soc_dpcm_get_substream(fe, stream); |
| 2126 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2127 | int err; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2128 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2129 | dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2130 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2131 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2132 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2133 | /* call bespoke trigger - FE takes care of all BE triggers */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2134 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2135 | fe->dai_link->name); |
| 2136 | |
| 2137 | err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
| 2138 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2139 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2140 | } else { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2141 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2142 | fe->dai_link->name); |
| 2143 | |
| 2144 | err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); |
| 2145 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2146 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2147 | } |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2148 | |
| 2149 | err = dpcm_be_dai_hw_free(fe, stream); |
| 2150 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2151 | dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2152 | |
| 2153 | err = dpcm_be_dai_shutdown(fe, stream); |
| 2154 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2155 | dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2156 | |
| 2157 | /* run the stream event for each BE */ |
| 2158 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) |
| 2164 | { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2165 | struct snd_pcm_substream *substream = |
| 2166 | snd_soc_dpcm_get_substream(fe, stream); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2167 | struct snd_soc_dpcm *dpcm; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2168 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2169 | int ret; |
| 2170 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2171 | dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2172 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2173 | |
| 2174 | /* Only start the BE if the FE is ready */ |
| 2175 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || |
| 2176 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) |
| 2177 | return -EINVAL; |
| 2178 | |
| 2179 | /* startup must always be called for new BEs */ |
| 2180 | ret = dpcm_be_dai_startup(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2181 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2182 | goto disconnect; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2183 | |
| 2184 | /* keep going if FE state is > open */ |
| 2185 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) |
| 2186 | return 0; |
| 2187 | |
| 2188 | ret = dpcm_be_dai_hw_params(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2189 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2190 | goto close; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2191 | |
| 2192 | /* keep going if FE state is > hw_params */ |
| 2193 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) |
| 2194 | return 0; |
| 2195 | |
| 2196 | |
| 2197 | ret = dpcm_be_dai_prepare(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2198 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2199 | goto hw_free; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2200 | |
| 2201 | /* run the stream event for each BE */ |
| 2202 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2203 | |
| 2204 | /* keep going if FE state is > prepare */ |
| 2205 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || |
| 2206 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) |
| 2207 | return 0; |
| 2208 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2209 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2210 | /* call trigger on the frontend - FE takes care of all BE triggers */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2211 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2212 | fe->dai_link->name); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2213 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2214 | ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); |
| 2215 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2216 | dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2217 | goto hw_free; |
| 2218 | } |
| 2219 | } else { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2220 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2221 | fe->dai_link->name); |
| 2222 | |
| 2223 | ret = dpcm_be_dai_trigger(fe, stream, |
| 2224 | SNDRV_PCM_TRIGGER_START); |
| 2225 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2226 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2227 | goto hw_free; |
| 2228 | } |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | return 0; |
| 2232 | |
| 2233 | hw_free: |
| 2234 | dpcm_be_dai_hw_free(fe, stream); |
| 2235 | close: |
| 2236 | dpcm_be_dai_shutdown(fe, stream); |
| 2237 | disconnect: |
| 2238 | /* disconnect any non started BEs */ |
| 2239 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2240 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2241 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2242 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2243 | } |
| 2244 | |
| 2245 | return ret; |
| 2246 | } |
| 2247 | |
| 2248 | static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2249 | { |
| 2250 | int ret; |
| 2251 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2252 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2253 | ret = dpcm_run_update_startup(fe, stream); |
| 2254 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2255 | dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2256 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2257 | |
| 2258 | return ret; |
| 2259 | } |
| 2260 | |
| 2261 | static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2262 | { |
| 2263 | int ret; |
| 2264 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2265 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2266 | ret = dpcm_run_update_shutdown(fe, stream); |
| 2267 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2268 | dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2269 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2270 | |
| 2271 | return ret; |
| 2272 | } |
| 2273 | |
| 2274 | /* Called by DAPM mixer/mux changes to update audio routing between PCMs and |
| 2275 | * any DAI links. |
| 2276 | */ |
Lars-Peter Clausen | c3f48ae | 2013-07-24 15:27:36 +0200 | [diff] [blame] | 2277 | int soc_dpcm_runtime_update(struct snd_soc_card *card) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2278 | { |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2279 | int i, old, new, paths; |
| 2280 | |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2281 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2282 | for (i = 0; i < card->num_rtd; i++) { |
| 2283 | struct snd_soc_dapm_widget_list *list; |
| 2284 | struct snd_soc_pcm_runtime *fe = &card->rtd[i]; |
| 2285 | |
| 2286 | /* make sure link is FE */ |
| 2287 | if (!fe->dai_link->dynamic) |
| 2288 | continue; |
| 2289 | |
| 2290 | /* only check active links */ |
| 2291 | if (!fe->cpu_dai->active) |
| 2292 | continue; |
| 2293 | |
| 2294 | /* DAPM sync will call this to update DSP paths */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2295 | dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2296 | fe->dai_link->name); |
| 2297 | |
| 2298 | /* skip if FE doesn't have playback capability */ |
Qiao Zhou | 075207d | 2014-11-17 16:02:57 +0800 | [diff] [blame] | 2299 | if (!fe->cpu_dai->driver->playback.channels_min |
| 2300 | || !fe->codec_dai->driver->playback.channels_min) |
| 2301 | goto capture; |
| 2302 | |
| 2303 | /* skip if FE isn't currently playing */ |
| 2304 | if (!fe->cpu_dai->playback_active |
| 2305 | || !fe->codec_dai->playback_active) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2306 | goto capture; |
| 2307 | |
| 2308 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list); |
| 2309 | if (paths < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2310 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2311 | fe->dai_link->name, "playback"); |
| 2312 | mutex_unlock(&card->mutex); |
| 2313 | return paths; |
| 2314 | } |
| 2315 | |
| 2316 | /* update any new playback paths */ |
| 2317 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1); |
| 2318 | if (new) { |
| 2319 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2320 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2321 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2322 | } |
| 2323 | |
| 2324 | /* update any old playback paths */ |
| 2325 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0); |
| 2326 | if (old) { |
| 2327 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2328 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2329 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2330 | } |
| 2331 | |
Qiao Zhou | 7ed9de7 | 2014-06-04 19:42:06 +0800 | [diff] [blame] | 2332 | dpcm_path_put(&list); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2333 | capture: |
| 2334 | /* skip if FE doesn't have capture capability */ |
Qiao Zhou | 075207d | 2014-11-17 16:02:57 +0800 | [diff] [blame] | 2335 | if (!fe->cpu_dai->driver->capture.channels_min |
| 2336 | || !fe->codec_dai->driver->capture.channels_min) |
| 2337 | continue; |
| 2338 | |
| 2339 | /* skip if FE isn't currently capturing */ |
| 2340 | if (!fe->cpu_dai->capture_active |
| 2341 | || !fe->codec_dai->capture_active) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2342 | continue; |
| 2343 | |
| 2344 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list); |
| 2345 | if (paths < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2346 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2347 | fe->dai_link->name, "capture"); |
| 2348 | mutex_unlock(&card->mutex); |
| 2349 | return paths; |
| 2350 | } |
| 2351 | |
| 2352 | /* update any new capture paths */ |
| 2353 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1); |
| 2354 | if (new) { |
| 2355 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2356 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2357 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2358 | } |
| 2359 | |
| 2360 | /* update any old capture paths */ |
| 2361 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0); |
| 2362 | if (old) { |
| 2363 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2364 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2365 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2366 | } |
| 2367 | |
| 2368 | dpcm_path_put(&list); |
| 2369 | } |
| 2370 | |
| 2371 | mutex_unlock(&card->mutex); |
| 2372 | return 0; |
| 2373 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2374 | int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) |
| 2375 | { |
| 2376 | struct snd_soc_dpcm *dpcm; |
| 2377 | struct list_head *clients = |
| 2378 | &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients; |
| 2379 | |
| 2380 | list_for_each_entry(dpcm, clients, list_be) { |
| 2381 | |
| 2382 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2383 | int i; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2384 | |
| 2385 | if (be->dai_link->ignore_suspend) |
| 2386 | continue; |
| 2387 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2388 | for (i = 0; i < be->num_codecs; i++) { |
| 2389 | struct snd_soc_dai *dai = be->codec_dais[i]; |
| 2390 | struct snd_soc_dai_driver *drv = dai->driver; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2391 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2392 | dev_dbg(be->dev, "ASoC: BE digital mute %s\n", |
| 2393 | be->dai_link->name); |
| 2394 | |
| 2395 | if (drv->ops && drv->ops->digital_mute && |
| 2396 | dai->playback_active) |
| 2397 | drv->ops->digital_mute(dai, mute); |
| 2398 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2404 | static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2405 | { |
| 2406 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 2407 | struct snd_soc_dpcm *dpcm; |
| 2408 | struct snd_soc_dapm_widget_list *list; |
| 2409 | int ret; |
| 2410 | int stream = fe_substream->stream; |
| 2411 | |
| 2412 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2413 | fe->dpcm[stream].runtime = fe_substream->runtime; |
| 2414 | |
Qiao Zhou | 8f70e51 | 2014-09-10 17:54:07 +0800 | [diff] [blame] | 2415 | ret = dpcm_path_get(fe, stream, &list); |
| 2416 | if (ret < 0) { |
| 2417 | mutex_unlock(&fe->card->mutex); |
| 2418 | return ret; |
| 2419 | } else if (ret == 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2420 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2421 | fe->dai_link->name, stream ? "capture" : "playback"); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* calculate valid and active FE <-> BE dpcms */ |
| 2425 | dpcm_process_paths(fe, stream, &list, 1); |
| 2426 | |
| 2427 | ret = dpcm_fe_dai_startup(fe_substream); |
| 2428 | if (ret < 0) { |
| 2429 | /* clean up all links */ |
| 2430 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 2431 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2432 | |
| 2433 | dpcm_be_disconnect(fe, stream); |
| 2434 | fe->dpcm[stream].runtime = NULL; |
| 2435 | } |
| 2436 | |
| 2437 | dpcm_clear_pending_state(fe, stream); |
| 2438 | dpcm_path_put(&list); |
| 2439 | mutex_unlock(&fe->card->mutex); |
| 2440 | return ret; |
| 2441 | } |
| 2442 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2443 | static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2444 | { |
| 2445 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 2446 | struct snd_soc_dpcm *dpcm; |
| 2447 | int stream = fe_substream->stream, ret; |
| 2448 | |
| 2449 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2450 | ret = dpcm_fe_dai_shutdown(fe_substream); |
| 2451 | |
| 2452 | /* mark FE's links ready to prune */ |
| 2453 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 2454 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2455 | |
| 2456 | dpcm_be_disconnect(fe, stream); |
| 2457 | |
| 2458 | fe->dpcm[stream].runtime = NULL; |
| 2459 | mutex_unlock(&fe->card->mutex); |
| 2460 | return ret; |
| 2461 | } |
| 2462 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2463 | /* create a new pcm */ |
| 2464 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) |
| 2465 | { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2466 | struct snd_soc_platform *platform = rtd->platform; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2467 | struct snd_soc_dai *codec_dai; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2468 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 2469 | struct snd_pcm *pcm; |
| 2470 | char new_name[64]; |
| 2471 | int ret = 0, playback = 0, capture = 0; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2472 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2473 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2474 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
Liam Girdwood | 1e9de42 | 2014-01-07 17:51:42 +0000 | [diff] [blame] | 2475 | playback = rtd->dai_link->dpcm_playback; |
| 2476 | capture = rtd->dai_link->dpcm_capture; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2477 | } else { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2478 | for (i = 0; i < rtd->num_codecs; i++) { |
| 2479 | codec_dai = rtd->codec_dais[i]; |
| 2480 | if (codec_dai->driver->playback.channels_min) |
| 2481 | playback = 1; |
| 2482 | if (codec_dai->driver->capture.channels_min) |
| 2483 | capture = 1; |
| 2484 | } |
| 2485 | |
| 2486 | capture = capture && cpu_dai->driver->capture.channels_min; |
| 2487 | playback = playback && cpu_dai->driver->playback.channels_min; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2488 | } |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 2489 | |
Fabio Estevam | d6bead0 | 2013-08-29 10:32:13 -0300 | [diff] [blame] | 2490 | if (rtd->dai_link->playback_only) { |
| 2491 | playback = 1; |
| 2492 | capture = 0; |
| 2493 | } |
| 2494 | |
| 2495 | if (rtd->dai_link->capture_only) { |
| 2496 | playback = 0; |
| 2497 | capture = 1; |
| 2498 | } |
| 2499 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2500 | /* create the PCM */ |
| 2501 | if (rtd->dai_link->no_pcm) { |
| 2502 | snprintf(new_name, sizeof(new_name), "(%s)", |
| 2503 | rtd->dai_link->stream_name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2504 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2505 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 2506 | playback, capture, &pcm); |
| 2507 | } else { |
| 2508 | if (rtd->dai_link->dynamic) |
| 2509 | snprintf(new_name, sizeof(new_name), "%s (*)", |
| 2510 | rtd->dai_link->stream_name); |
| 2511 | else |
| 2512 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2513 | rtd->dai_link->stream_name, |
| 2514 | (rtd->num_codecs > 1) ? |
| 2515 | "multicodec" : rtd->codec_dai->name, num); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2516 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2517 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, |
| 2518 | capture, &pcm); |
| 2519 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2520 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2521 | 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] | 2522 | rtd->dai_link->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2523 | return ret; |
| 2524 | } |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2525 | 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] | 2526 | |
| 2527 | /* DAPM dai link stream work */ |
| 2528 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
| 2529 | |
| 2530 | rtd->pcm = pcm; |
| 2531 | pcm->private_data = rtd; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2532 | |
| 2533 | if (rtd->dai_link->no_pcm) { |
| 2534 | if (playback) |
| 2535 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; |
| 2536 | if (capture) |
| 2537 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; |
| 2538 | goto out; |
| 2539 | } |
| 2540 | |
| 2541 | /* ASoC PCM operations */ |
| 2542 | if (rtd->dai_link->dynamic) { |
| 2543 | rtd->ops.open = dpcm_fe_dai_open; |
| 2544 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; |
| 2545 | rtd->ops.prepare = dpcm_fe_dai_prepare; |
| 2546 | rtd->ops.trigger = dpcm_fe_dai_trigger; |
| 2547 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; |
| 2548 | rtd->ops.close = dpcm_fe_dai_close; |
| 2549 | rtd->ops.pointer = soc_pcm_pointer; |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2550 | rtd->ops.ioctl = soc_pcm_ioctl; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2551 | } else { |
| 2552 | rtd->ops.open = soc_pcm_open; |
| 2553 | rtd->ops.hw_params = soc_pcm_hw_params; |
| 2554 | rtd->ops.prepare = soc_pcm_prepare; |
| 2555 | rtd->ops.trigger = soc_pcm_trigger; |
| 2556 | rtd->ops.hw_free = soc_pcm_hw_free; |
| 2557 | rtd->ops.close = soc_pcm_close; |
| 2558 | rtd->ops.pointer = soc_pcm_pointer; |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2559 | rtd->ops.ioctl = soc_pcm_ioctl; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2560 | } |
| 2561 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2562 | if (platform->driver->ops) { |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2563 | rtd->ops.ack = platform->driver->ops->ack; |
| 2564 | rtd->ops.copy = platform->driver->ops->copy; |
| 2565 | rtd->ops.silence = platform->driver->ops->silence; |
| 2566 | rtd->ops.page = platform->driver->ops->page; |
| 2567 | rtd->ops.mmap = platform->driver->ops->mmap; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | if (playback) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2571 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2572 | |
| 2573 | if (capture) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2574 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2575 | |
| 2576 | if (platform->driver->pcm_new) { |
| 2577 | ret = platform->driver->pcm_new(rtd); |
| 2578 | if (ret < 0) { |
Mark Brown | b1bc7b3 | 2012-09-26 14:25:17 +0100 | [diff] [blame] | 2579 | dev_err(platform->dev, |
| 2580 | "ASoC: pcm constructor failed: %d\n", |
| 2581 | ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2582 | return ret; |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | pcm->private_free = platform->driver->pcm_free; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2587 | out: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2588 | dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", |
| 2589 | (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, |
| 2590 | cpu_dai->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2591 | return ret; |
| 2592 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* is the current PCM operation for this FE ? */ |
| 2595 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2596 | { |
| 2597 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) |
| 2598 | return 1; |
| 2599 | return 0; |
| 2600 | } |
| 2601 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); |
| 2602 | |
| 2603 | /* is the current PCM operation for this BE ? */ |
| 2604 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, |
| 2605 | struct snd_soc_pcm_runtime *be, int stream) |
| 2606 | { |
| 2607 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || |
| 2608 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && |
| 2609 | be->dpcm[stream].runtime_update)) |
| 2610 | return 1; |
| 2611 | return 0; |
| 2612 | } |
| 2613 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); |
| 2614 | |
| 2615 | /* get the substream for this BE */ |
| 2616 | struct snd_pcm_substream * |
| 2617 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) |
| 2618 | { |
| 2619 | return be->pcm->streams[stream].substream; |
| 2620 | } |
| 2621 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); |
| 2622 | |
| 2623 | /* get the BE runtime state */ |
| 2624 | enum snd_soc_dpcm_state |
| 2625 | snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) |
| 2626 | { |
| 2627 | return be->dpcm[stream].state; |
| 2628 | } |
| 2629 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); |
| 2630 | |
| 2631 | /* set the BE runtime state */ |
| 2632 | void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, |
| 2633 | int stream, enum snd_soc_dpcm_state state) |
| 2634 | { |
| 2635 | be->dpcm[stream].state = state; |
| 2636 | } |
| 2637 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); |
| 2638 | |
| 2639 | /* |
| 2640 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE |
| 2641 | * are not running, paused or suspended for the specified stream direction. |
| 2642 | */ |
| 2643 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, |
| 2644 | struct snd_soc_pcm_runtime *be, int stream) |
| 2645 | { |
| 2646 | struct snd_soc_dpcm *dpcm; |
| 2647 | int state; |
| 2648 | |
| 2649 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 2650 | |
| 2651 | if (dpcm->fe == fe) |
| 2652 | continue; |
| 2653 | |
| 2654 | state = dpcm->fe->dpcm[stream].state; |
| 2655 | if (state == SND_SOC_DPCM_STATE_START || |
| 2656 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 2657 | state == SND_SOC_DPCM_STATE_SUSPEND) |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* it's safe to free/stop this BE DAI */ |
| 2662 | return 1; |
| 2663 | } |
| 2664 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); |
| 2665 | |
| 2666 | /* |
| 2667 | * We can only change hw params a BE DAI if any of it's FE are not prepared, |
| 2668 | * running, paused or suspended for the specified stream direction. |
| 2669 | */ |
| 2670 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, |
| 2671 | struct snd_soc_pcm_runtime *be, int stream) |
| 2672 | { |
| 2673 | struct snd_soc_dpcm *dpcm; |
| 2674 | int state; |
| 2675 | |
| 2676 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 2677 | |
| 2678 | if (dpcm->fe == fe) |
| 2679 | continue; |
| 2680 | |
| 2681 | state = dpcm->fe->dpcm[stream].state; |
| 2682 | if (state == SND_SOC_DPCM_STATE_START || |
| 2683 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 2684 | state == SND_SOC_DPCM_STATE_SUSPEND || |
| 2685 | state == SND_SOC_DPCM_STATE_PREPARE) |
| 2686 | return 0; |
| 2687 | } |
| 2688 | |
| 2689 | /* it's safe to change hw_params */ |
| 2690 | return 1; |
| 2691 | } |
| 2692 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2693 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2694 | int snd_soc_platform_trigger(struct snd_pcm_substream *substream, |
| 2695 | int cmd, struct snd_soc_platform *platform) |
| 2696 | { |
Mark Brown | c5914b0 | 2013-10-30 17:47:39 -0700 | [diff] [blame] | 2697 | if (platform->driver->ops && platform->driver->ops->trigger) |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2698 | return platform->driver->ops->trigger(substream, cmd); |
| 2699 | return 0; |
| 2700 | } |
| 2701 | EXPORT_SYMBOL_GPL(snd_soc_platform_trigger); |
| 2702 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2703 | #ifdef CONFIG_DEBUG_FS |
| 2704 | static char *dpcm_state_string(enum snd_soc_dpcm_state state) |
| 2705 | { |
| 2706 | switch (state) { |
| 2707 | case SND_SOC_DPCM_STATE_NEW: |
| 2708 | return "new"; |
| 2709 | case SND_SOC_DPCM_STATE_OPEN: |
| 2710 | return "open"; |
| 2711 | case SND_SOC_DPCM_STATE_HW_PARAMS: |
| 2712 | return "hw_params"; |
| 2713 | case SND_SOC_DPCM_STATE_PREPARE: |
| 2714 | return "prepare"; |
| 2715 | case SND_SOC_DPCM_STATE_START: |
| 2716 | return "start"; |
| 2717 | case SND_SOC_DPCM_STATE_STOP: |
| 2718 | return "stop"; |
| 2719 | case SND_SOC_DPCM_STATE_SUSPEND: |
| 2720 | return "suspend"; |
| 2721 | case SND_SOC_DPCM_STATE_PAUSED: |
| 2722 | return "paused"; |
| 2723 | case SND_SOC_DPCM_STATE_HW_FREE: |
| 2724 | return "hw_free"; |
| 2725 | case SND_SOC_DPCM_STATE_CLOSE: |
| 2726 | return "close"; |
| 2727 | } |
| 2728 | |
| 2729 | return "unknown"; |
| 2730 | } |
| 2731 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2732 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
| 2733 | int stream, char *buf, size_t size) |
| 2734 | { |
| 2735 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; |
| 2736 | struct snd_soc_dpcm *dpcm; |
| 2737 | ssize_t offset = 0; |
| 2738 | |
| 2739 | /* FE state */ |
| 2740 | offset += snprintf(buf + offset, size - offset, |
| 2741 | "[%s - %s]\n", fe->dai_link->name, |
| 2742 | stream ? "Capture" : "Playback"); |
| 2743 | |
| 2744 | offset += snprintf(buf + offset, size - offset, "State: %s\n", |
| 2745 | dpcm_state_string(fe->dpcm[stream].state)); |
| 2746 | |
| 2747 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2748 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 2749 | offset += snprintf(buf + offset, size - offset, |
| 2750 | "Hardware Params: " |
| 2751 | "Format = %s, Channels = %d, Rate = %d\n", |
| 2752 | snd_pcm_format_name(params_format(params)), |
| 2753 | params_channels(params), |
| 2754 | params_rate(params)); |
| 2755 | |
| 2756 | /* BEs state */ |
| 2757 | offset += snprintf(buf + offset, size - offset, "Backends:\n"); |
| 2758 | |
| 2759 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 2760 | offset += snprintf(buf + offset, size - offset, |
| 2761 | " No active DSP links\n"); |
| 2762 | goto out; |
| 2763 | } |
| 2764 | |
| 2765 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2766 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2767 | params = &dpcm->hw_params; |
| 2768 | |
| 2769 | offset += snprintf(buf + offset, size - offset, |
| 2770 | "- %s\n", be->dai_link->name); |
| 2771 | |
| 2772 | offset += snprintf(buf + offset, size - offset, |
| 2773 | " State: %s\n", |
| 2774 | dpcm_state_string(be->dpcm[stream].state)); |
| 2775 | |
| 2776 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2777 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 2778 | offset += snprintf(buf + offset, size - offset, |
| 2779 | " Hardware Params: " |
| 2780 | "Format = %s, Channels = %d, Rate = %d\n", |
| 2781 | snd_pcm_format_name(params_format(params)), |
| 2782 | params_channels(params), |
| 2783 | params_rate(params)); |
| 2784 | } |
| 2785 | |
| 2786 | out: |
| 2787 | return offset; |
| 2788 | } |
| 2789 | |
| 2790 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, |
| 2791 | size_t count, loff_t *ppos) |
| 2792 | { |
| 2793 | struct snd_soc_pcm_runtime *fe = file->private_data; |
| 2794 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; |
| 2795 | char *buf; |
| 2796 | |
| 2797 | buf = kmalloc(out_count, GFP_KERNEL); |
| 2798 | if (!buf) |
| 2799 | return -ENOMEM; |
| 2800 | |
| 2801 | if (fe->cpu_dai->driver->playback.channels_min) |
| 2802 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, |
| 2803 | buf + offset, out_count - offset); |
| 2804 | |
| 2805 | if (fe->cpu_dai->driver->capture.channels_min) |
| 2806 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, |
| 2807 | buf + offset, out_count - offset); |
| 2808 | |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 2809 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2810 | |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 2811 | kfree(buf); |
| 2812 | return ret; |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | static const struct file_operations dpcm_state_fops = { |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 2816 | .open = simple_open, |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2817 | .read = dpcm_state_read_file, |
| 2818 | .llseek = default_llseek, |
| 2819 | }; |
| 2820 | |
| 2821 | int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) |
| 2822 | { |
Mark Brown | b3bba9a | 2012-05-08 10:33:47 +0100 | [diff] [blame] | 2823 | if (!rtd->dai_link) |
| 2824 | return 0; |
| 2825 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2826 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
| 2827 | rtd->card->debugfs_card_root); |
| 2828 | if (!rtd->debugfs_dpcm_root) { |
| 2829 | dev_dbg(rtd->dev, |
| 2830 | "ASoC: Failed to create dpcm debugfs directory %s\n", |
| 2831 | rtd->dai_link->name); |
| 2832 | return -EINVAL; |
| 2833 | } |
| 2834 | |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 2835 | rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444, |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 2836 | rtd->debugfs_dpcm_root, |
| 2837 | rtd, &dpcm_state_fops); |
| 2838 | |
| 2839 | return 0; |
| 2840 | } |
| 2841 | #endif |