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