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