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