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