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; |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 457 | struct snd_soc_component *component; |
| 458 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 459 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 460 | struct snd_soc_dai *codec_dai; |
| 461 | const char *codec_dai_name = "multicodec"; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 462 | int i, ret = 0, __ret; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 463 | |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 464 | pinctrl_pm_select_default_state(cpu_dai->dev); |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 465 | for (i = 0; i < rtd->num_codecs; i++) |
| 466 | pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev); |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 467 | |
| 468 | for_each_rtdcom(rtd, rtdcom) { |
| 469 | component = rtdcom->component; |
| 470 | |
| 471 | pm_runtime_get_sync(component->dev); |
| 472 | } |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 473 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 474 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 475 | |
| 476 | /* startup the audio subsystem */ |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 477 | if (cpu_dai->driver->ops->startup) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 478 | ret = cpu_dai->driver->ops->startup(substream, cpu_dai); |
| 479 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 480 | dev_err(cpu_dai->dev, "ASoC: can't open interface" |
| 481 | " %s: %d\n", cpu_dai->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 482 | goto out; |
| 483 | } |
| 484 | } |
| 485 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 486 | if (platform && platform->driver->ops && platform->driver->ops->open) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 487 | ret = platform->driver->ops->open(substream); |
| 488 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 489 | dev_err(platform->dev, "ASoC: can't open platform" |
Lars-Peter Clausen | f433320 | 2014-06-16 18:13:02 +0200 | [diff] [blame] | 490 | " %s: %d\n", platform->component.name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 491 | goto platform_err; |
| 492 | } |
| 493 | } |
| 494 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 495 | ret = 0; |
| 496 | for_each_rtdcom(rtd, rtdcom) { |
| 497 | component = rtdcom->component; |
| 498 | |
| 499 | /* ignore duplication for now */ |
| 500 | if (platform && (component == &platform->component)) |
| 501 | continue; |
| 502 | |
| 503 | if (!component->driver->ops || |
| 504 | !component->driver->ops->open) |
| 505 | continue; |
| 506 | |
| 507 | __ret = component->driver->ops->open(substream); |
| 508 | if (__ret < 0) { |
| 509 | dev_err(component->dev, |
| 510 | "ASoC: can't open component %s: %d\n", |
| 511 | component->name, ret); |
| 512 | ret = __ret; |
| 513 | } |
| 514 | } |
| 515 | if (ret < 0) |
| 516 | goto component_err; |
| 517 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 518 | for (i = 0; i < rtd->num_codecs; i++) { |
| 519 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 520 | if (codec_dai->driver->ops->startup) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 521 | ret = codec_dai->driver->ops->startup(substream, |
| 522 | codec_dai); |
| 523 | if (ret < 0) { |
| 524 | dev_err(codec_dai->dev, |
| 525 | "ASoC: can't open codec %s: %d\n", |
| 526 | codec_dai->name, ret); |
| 527 | goto codec_dai_err; |
| 528 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 529 | } |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 530 | |
| 531 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 532 | codec_dai->tx_mask = 0; |
| 533 | else |
| 534 | codec_dai->rx_mask = 0; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 537 | if (rtd->dai_link->ops->startup) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 538 | ret = rtd->dai_link->ops->startup(substream); |
| 539 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 540 | pr_err("ASoC: %s startup failed: %d\n", |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 541 | rtd->dai_link->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 542 | goto machine_err; |
| 543 | } |
| 544 | } |
| 545 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 546 | /* Dynamic PCM DAI links compat checks use dynamic capabilities */ |
| 547 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) |
| 548 | goto dynamic; |
| 549 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 550 | /* Check that the codec and cpu DAIs are compatible */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 551 | soc_pcm_init_runtime_hw(substream); |
| 552 | |
| 553 | if (rtd->num_codecs == 1) |
| 554 | codec_dai_name = rtd->codec_dai->name; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 555 | |
Lars-Peter Clausen | 62e5f67 | 2013-11-30 17:38:58 +0100 | [diff] [blame] | 556 | if (soc_pcm_has_symmetry(substream)) |
| 557 | runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 558 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 559 | ret = -EINVAL; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 560 | if (!runtime->hw.rates) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 561 | printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 562 | codec_dai_name, cpu_dai->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 563 | goto config_err; |
| 564 | } |
| 565 | if (!runtime->hw.formats) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 566 | printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 567 | codec_dai_name, cpu_dai->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 568 | goto config_err; |
| 569 | } |
| 570 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || |
| 571 | runtime->hw.channels_min > runtime->hw.channels_max) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 572 | printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 573 | codec_dai_name, cpu_dai->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 574 | goto config_err; |
| 575 | } |
| 576 | |
Benoit Cousson | c8dd1fe | 2014-07-01 09:47:55 +0200 | [diff] [blame] | 577 | soc_pcm_apply_msb(substream); |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame] | 578 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 579 | /* Symmetry only applies if we've already got an active stream. */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 580 | if (cpu_dai->active) { |
| 581 | ret = soc_pcm_apply_symmetry(substream, cpu_dai); |
| 582 | if (ret != 0) |
| 583 | goto config_err; |
| 584 | } |
| 585 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 586 | for (i = 0; i < rtd->num_codecs; i++) { |
| 587 | if (rtd->codec_dais[i]->active) { |
| 588 | ret = soc_pcm_apply_symmetry(substream, |
| 589 | rtd->codec_dais[i]); |
| 590 | if (ret != 0) |
| 591 | goto config_err; |
| 592 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 595 | pr_debug("ASoC: %s <-> %s info:\n", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 596 | codec_dai_name, cpu_dai->name); |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 597 | pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); |
| 598 | 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] | 599 | runtime->hw.channels_max); |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 600 | 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] | 601 | runtime->hw.rate_max); |
| 602 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 603 | dynamic: |
Lars-Peter Clausen | 24894b7 | 2014-03-05 13:17:43 +0100 | [diff] [blame] | 604 | |
| 605 | snd_soc_runtime_activate(rtd, substream->stream); |
| 606 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 607 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 608 | return 0; |
| 609 | |
| 610 | config_err: |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 611 | if (rtd->dai_link->ops->shutdown) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 612 | rtd->dai_link->ops->shutdown(substream); |
| 613 | |
| 614 | machine_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 615 | i = rtd->num_codecs; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 616 | |
| 617 | codec_dai_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 618 | while (--i >= 0) { |
| 619 | codec_dai = rtd->codec_dais[i]; |
| 620 | if (codec_dai->driver->ops->shutdown) |
| 621 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 622 | } |
| 623 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 624 | component_err: |
| 625 | for_each_rtdcom(rtd, rtdcom) { |
| 626 | component = rtdcom->component; |
| 627 | |
| 628 | /* ignore duplication for now */ |
| 629 | if (platform && (component == &platform->component)) |
| 630 | continue; |
| 631 | |
| 632 | if (!component->driver->ops || |
| 633 | !component->driver->ops->close) |
| 634 | continue; |
| 635 | |
| 636 | component->driver->ops->close(substream); |
| 637 | } |
| 638 | |
| 639 | if (platform && platform->driver->ops && platform->driver->ops->close) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 640 | platform->driver->ops->close(substream); |
| 641 | |
| 642 | platform_err: |
| 643 | if (cpu_dai->driver->ops->shutdown) |
| 644 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 645 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 646 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 647 | |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 648 | for_each_rtdcom(rtd, rtdcom) { |
| 649 | component = rtdcom->component; |
| 650 | |
| 651 | pm_runtime_mark_last_busy(component->dev); |
| 652 | pm_runtime_put_autosuspend(component->dev); |
Sanyog Kale | 3f80978 | 2016-01-05 17:14:49 +0530 | [diff] [blame] | 653 | } |
| 654 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 655 | for (i = 0; i < rtd->num_codecs; i++) { |
| 656 | if (!rtd->codec_dais[i]->active) |
| 657 | pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev); |
| 658 | } |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 659 | if (!cpu_dai->active) |
| 660 | pinctrl_pm_select_sleep_state(cpu_dai->dev); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 661 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Power down the audio subsystem pmdown_time msecs after close is called. |
| 667 | * This is to ensure there are no pops or clicks in between any music tracks |
| 668 | * due to DAPM power cycling. |
| 669 | */ |
| 670 | static void close_delayed_work(struct work_struct *work) |
| 671 | { |
| 672 | struct snd_soc_pcm_runtime *rtd = |
| 673 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 674 | struct snd_soc_dai *codec_dai = rtd->codec_dais[0]; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 675 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 676 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 677 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 678 | 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] | 679 | codec_dai->driver->playback.stream_name, |
| 680 | codec_dai->playback_active ? "active" : "inactive", |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 681 | rtd->pop_wait ? "yes" : "no"); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 682 | |
| 683 | /* are we waiting on this codec DAI stream */ |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 684 | if (rtd->pop_wait == 1) { |
| 685 | rtd->pop_wait = 0; |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 686 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 687 | SND_SOC_DAPM_STREAM_STOP); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 690 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 695 | * freed here. The cpu DAI, codec DAI, machine and platform are also |
| 696 | * shutdown. |
| 697 | */ |
Liam Girdwood | 91d5e6b | 2011-06-09 17:04:59 +0100 | [diff] [blame] | 698 | static int soc_pcm_close(struct snd_pcm_substream *substream) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 699 | { |
| 700 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 701 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 702 | struct snd_soc_component *component; |
| 703 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 704 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 705 | struct snd_soc_dai *codec_dai; |
| 706 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 707 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 708 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 709 | |
Lars-Peter Clausen | 24894b7 | 2014-03-05 13:17:43 +0100 | [diff] [blame] | 710 | snd_soc_runtime_deactivate(rtd, substream->stream); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 711 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 712 | /* clear the corresponding DAIs rate when inactive */ |
| 713 | if (!cpu_dai->active) |
| 714 | cpu_dai->rate = 0; |
| 715 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 716 | for (i = 0; i < rtd->num_codecs; i++) { |
| 717 | codec_dai = rtd->codec_dais[i]; |
| 718 | if (!codec_dai->active) |
| 719 | codec_dai->rate = 0; |
| 720 | } |
Sascha Hauer | 25b7679 | 2011-08-17 09:20:01 +0200 | [diff] [blame] | 721 | |
Ramesh Babu | ae11601 | 2014-10-15 12:34:59 +0530 | [diff] [blame] | 722 | snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream); |
| 723 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 724 | if (cpu_dai->driver->ops->shutdown) |
| 725 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 726 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 727 | for (i = 0; i < rtd->num_codecs; i++) { |
| 728 | codec_dai = rtd->codec_dais[i]; |
| 729 | if (codec_dai->driver->ops->shutdown) |
| 730 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 731 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 732 | |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 733 | if (rtd->dai_link->ops->shutdown) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 734 | rtd->dai_link->ops->shutdown(substream); |
| 735 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 736 | if (platform && platform->driver->ops && platform->driver->ops->close) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 737 | platform->driver->ops->close(substream); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 738 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 739 | for_each_rtdcom(rtd, rtdcom) { |
| 740 | component = rtdcom->component; |
| 741 | |
| 742 | /* ignore duplication for now */ |
| 743 | if (platform && (component == &platform->component)) |
| 744 | continue; |
| 745 | |
| 746 | if (!component->driver->ops || |
| 747 | !component->driver->ops->close) |
| 748 | continue; |
| 749 | |
| 750 | component->driver->ops->close(substream); |
| 751 | } |
| 752 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 753 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Lars-Peter Clausen | 208a158 | 2014-03-05 13:17:42 +0100 | [diff] [blame] | 754 | if (snd_soc_runtime_ignore_pmdown_time(rtd)) { |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 755 | /* powered down playback stream now */ |
| 756 | snd_soc_dapm_stream_event(rtd, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 757 | SNDRV_PCM_STREAM_PLAYBACK, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 758 | SND_SOC_DAPM_STREAM_STOP); |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 759 | } else { |
| 760 | /* start delayed pop wq here for playback streams */ |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 761 | rtd->pop_wait = 1; |
Mark Brown | d4e1a73 | 2013-07-18 11:52:17 +0100 | [diff] [blame] | 762 | queue_delayed_work(system_power_efficient_wq, |
| 763 | &rtd->delayed_work, |
| 764 | msecs_to_jiffies(rtd->pmdown_time)); |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 765 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 766 | } else { |
| 767 | /* capture streams can be powered down now */ |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 768 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 769 | SND_SOC_DAPM_STREAM_STOP); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 772 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 773 | |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 774 | for_each_rtdcom(rtd, rtdcom) { |
| 775 | component = rtdcom->component; |
Sanyog Kale | 3f80978 | 2016-01-05 17:14:49 +0530 | [diff] [blame] | 776 | |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 777 | pm_runtime_mark_last_busy(component->dev); |
| 778 | pm_runtime_put_autosuspend(component->dev); |
Sanyog Kale | 3f80978 | 2016-01-05 17:14:49 +0530 | [diff] [blame] | 779 | } |
| 780 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 781 | for (i = 0; i < rtd->num_codecs; i++) { |
| 782 | if (!rtd->codec_dais[i]->active) |
| 783 | pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev); |
| 784 | } |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 785 | if (!cpu_dai->active) |
| 786 | pinctrl_pm_select_sleep_state(cpu_dai->dev); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 787 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 793 | * rate, etc. This function is non atomic and can be called multiple times, |
| 794 | * it can refer to the runtime info. |
| 795 | */ |
| 796 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 797 | { |
| 798 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 799 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 800 | struct snd_soc_component *component; |
| 801 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 802 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 803 | struct snd_soc_dai *codec_dai; |
| 804 | int i, ret = 0; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 805 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 806 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 807 | |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 808 | if (rtd->dai_link->ops->prepare) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 809 | ret = rtd->dai_link->ops->prepare(substream); |
| 810 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 811 | dev_err(rtd->card->dev, "ASoC: machine prepare error:" |
| 812 | " %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 813 | goto out; |
| 814 | } |
| 815 | } |
| 816 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 817 | if (platform && platform->driver->ops && platform->driver->ops->prepare) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 818 | ret = platform->driver->ops->prepare(substream); |
| 819 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 820 | dev_err(platform->dev, "ASoC: platform prepare error:" |
| 821 | " %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 822 | goto out; |
| 823 | } |
| 824 | } |
| 825 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 826 | for_each_rtdcom(rtd, rtdcom) { |
| 827 | component = rtdcom->component; |
| 828 | |
| 829 | /* ignore duplication for now */ |
| 830 | if (platform && (component == &platform->component)) |
| 831 | continue; |
| 832 | |
| 833 | if (!component->driver->ops || |
| 834 | !component->driver->ops->prepare) |
| 835 | continue; |
| 836 | |
| 837 | ret = component->driver->ops->prepare(substream); |
| 838 | if (ret < 0) { |
| 839 | dev_err(component->dev, |
| 840 | "ASoC: platform prepare error: %d\n", ret); |
| 841 | goto out; |
| 842 | } |
| 843 | } |
| 844 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 845 | for (i = 0; i < rtd->num_codecs; i++) { |
| 846 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 847 | if (codec_dai->driver->ops->prepare) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 848 | ret = codec_dai->driver->ops->prepare(substream, |
| 849 | codec_dai); |
| 850 | if (ret < 0) { |
| 851 | dev_err(codec_dai->dev, |
Jarkko Nikula | 90cc7f1 | 2014-12-23 11:04:41 +0200 | [diff] [blame] | 852 | "ASoC: codec DAI prepare error: %d\n", |
| 853 | ret); |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 854 | goto out; |
| 855 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 859 | if (cpu_dai->driver->ops->prepare) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 860 | ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); |
| 861 | if (ret < 0) { |
Jarkko Nikula | 90cc7f1 | 2014-12-23 11:04:41 +0200 | [diff] [blame] | 862 | dev_err(cpu_dai->dev, |
| 863 | "ASoC: cpu DAI prepare error: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 864 | goto out; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /* cancel any delayed stream shutdown that is pending */ |
| 869 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 870 | rtd->pop_wait) { |
| 871 | rtd->pop_wait = 0; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 872 | cancel_delayed_work(&rtd->delayed_work); |
| 873 | } |
| 874 | |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 875 | snd_soc_dapm_stream_event(rtd, substream->stream, |
| 876 | SND_SOC_DAPM_STREAM_START); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 877 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 878 | for (i = 0; i < rtd->num_codecs; i++) |
| 879 | snd_soc_dai_digital_mute(rtd->codec_dais[i], 0, |
| 880 | substream->stream); |
Ramesh Babu | ae11601 | 2014-10-15 12:34:59 +0530 | [diff] [blame] | 881 | snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 882 | |
| 883 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 884 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 885 | return ret; |
| 886 | } |
| 887 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 888 | static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, |
| 889 | unsigned int mask) |
| 890 | { |
| 891 | struct snd_interval *interval; |
| 892 | int channels = hweight_long(mask); |
| 893 | |
| 894 | interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 895 | interval->min = channels; |
| 896 | interval->max = channels; |
| 897 | } |
| 898 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 899 | int soc_dai_hw_params(struct snd_pcm_substream *substream, |
| 900 | struct snd_pcm_hw_params *params, |
| 901 | struct snd_soc_dai *dai) |
| 902 | { |
| 903 | int ret; |
| 904 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 905 | if (dai->driver->ops->hw_params) { |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 906 | ret = dai->driver->ops->hw_params(substream, params, dai); |
| 907 | if (ret < 0) { |
| 908 | dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n", |
| 909 | dai->name, ret); |
| 910 | return ret; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 917 | /* |
| 918 | * Called by ALSA when the hardware params are set by application. This |
| 919 | * function can also be called multiple times and can allocate buffers |
| 920 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 921 | */ |
| 922 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 923 | struct snd_pcm_hw_params *params) |
| 924 | { |
| 925 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 926 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 927 | struct snd_soc_component *component; |
| 928 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 929 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 930 | int i, ret = 0, __ret; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 931 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 932 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 933 | if (rtd->dai_link->ops->hw_params) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 934 | ret = rtd->dai_link->ops->hw_params(substream, params); |
| 935 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 936 | dev_err(rtd->card->dev, "ASoC: machine hw_params" |
| 937 | " failed: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 938 | goto out; |
| 939 | } |
| 940 | } |
| 941 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 942 | for (i = 0; i < rtd->num_codecs; i++) { |
| 943 | struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; |
| 944 | struct snd_pcm_hw_params codec_params; |
| 945 | |
Ricard Wanderlof | cde7903 | 2015-08-24 14:16:51 +0200 | [diff] [blame] | 946 | /* |
| 947 | * Skip CODECs which don't support the current stream type, |
| 948 | * the idea being that if a CODEC is not used for the currently |
| 949 | * set up transfer direction, it should not need to be |
| 950 | * configured, especially since the configuration used might |
| 951 | * not even be supported by that CODEC. There may be cases |
| 952 | * however where a CODEC needs to be set up although it is |
| 953 | * actually not being used for the transfer, e.g. if a |
| 954 | * capture-only CODEC is acting as an LRCLK and/or BCLK master |
| 955 | * for the DAI link including a playback-only CODEC. |
| 956 | * If this becomes necessary, we will have to augment the |
| 957 | * machine driver setup with information on how to act, so |
| 958 | * we can do the right thing here. |
| 959 | */ |
| 960 | if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) |
| 961 | continue; |
| 962 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 963 | /* copy params for each codec */ |
| 964 | codec_params = *params; |
| 965 | |
| 966 | /* fixup params based on TDM slot masks */ |
| 967 | if (codec_dai->tx_mask) |
| 968 | soc_pcm_codec_params_fixup(&codec_params, |
| 969 | codec_dai->tx_mask); |
| 970 | if (codec_dai->rx_mask) |
| 971 | soc_pcm_codec_params_fixup(&codec_params, |
| 972 | codec_dai->rx_mask); |
| 973 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 974 | ret = soc_dai_hw_params(substream, &codec_params, codec_dai); |
| 975 | if(ret < 0) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 976 | goto codec_err; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 977 | |
| 978 | codec_dai->rate = params_rate(&codec_params); |
| 979 | codec_dai->channels = params_channels(&codec_params); |
| 980 | codec_dai->sample_bits = snd_pcm_format_physical_width( |
| 981 | params_format(&codec_params)); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Benoit Cousson | 93e6958 | 2014-07-08 23:19:38 +0200 | [diff] [blame] | 984 | ret = soc_dai_hw_params(substream, params, cpu_dai); |
| 985 | if (ret < 0) |
| 986 | goto interface_err; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 987 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 988 | if (platform && platform->driver->ops && platform->driver->ops->hw_params) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 989 | ret = platform->driver->ops->hw_params(substream, params); |
| 990 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 991 | dev_err(platform->dev, "ASoC: %s hw params failed: %d\n", |
Lars-Peter Clausen | f433320 | 2014-06-16 18:13:02 +0200 | [diff] [blame] | 992 | platform->component.name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 993 | goto platform_err; |
| 994 | } |
| 995 | } |
| 996 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 997 | ret = 0; |
| 998 | for_each_rtdcom(rtd, rtdcom) { |
| 999 | component = rtdcom->component; |
| 1000 | |
| 1001 | /* ignore duplication for now */ |
| 1002 | if (platform && (component == &platform->component)) |
| 1003 | continue; |
| 1004 | |
| 1005 | if (!component->driver->ops || |
| 1006 | !component->driver->ops->hw_params) |
| 1007 | continue; |
| 1008 | |
| 1009 | __ret = component->driver->ops->hw_params(substream, params); |
| 1010 | if (__ret < 0) { |
| 1011 | dev_err(component->dev, |
| 1012 | "ASoC: %s hw params failed: %d\n", |
| 1013 | component->name, ret); |
| 1014 | ret = __ret; |
| 1015 | } |
| 1016 | } |
| 1017 | if (ret < 0) |
| 1018 | goto component_err; |
| 1019 | |
Nicolin Chen | 3635bf0 | 2013-11-13 18:56:24 +0800 | [diff] [blame] | 1020 | /* store the parameters for each DAIs */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 1021 | cpu_dai->rate = params_rate(params); |
Nicolin Chen | 3635bf0 | 2013-11-13 18:56:24 +0800 | [diff] [blame] | 1022 | cpu_dai->channels = params_channels(params); |
| 1023 | cpu_dai->sample_bits = |
| 1024 | snd_pcm_format_physical_width(params_format(params)); |
| 1025 | |
jiada wang | 957ce0c | 2017-09-20 15:25:30 +0900 | [diff] [blame] | 1026 | ret = soc_pcm_params_symmetry(substream, params); |
| 1027 | if (ret) |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1028 | goto component_err; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1029 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 1030 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1031 | return ret; |
| 1032 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1033 | component_err: |
| 1034 | for_each_rtdcom(rtd, rtdcom) { |
| 1035 | component = rtdcom->component; |
| 1036 | |
| 1037 | /* ignore duplication */ |
| 1038 | if (platform && (component == &platform->component)) |
| 1039 | continue; |
| 1040 | |
| 1041 | if (!component->driver->ops || |
| 1042 | !component->driver->ops->hw_free) |
| 1043 | continue; |
| 1044 | |
| 1045 | component->driver->ops->hw_free(substream); |
| 1046 | } |
| 1047 | |
| 1048 | if (platform && platform->driver->ops && platform->driver->ops->hw_free) |
| 1049 | platform->driver->ops->hw_free(substream); |
| 1050 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1051 | platform_err: |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1052 | if (cpu_dai->driver->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1053 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 1054 | |
| 1055 | interface_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1056 | i = rtd->num_codecs; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1057 | |
| 1058 | codec_err: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1059 | while (--i >= 0) { |
| 1060 | struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1061 | if (codec_dai->driver->ops->hw_free) |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1062 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 1063 | codec_dai->rate = 0; |
| 1064 | } |
| 1065 | |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 1066 | if (rtd->dai_link->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1067 | rtd->dai_link->ops->hw_free(substream); |
| 1068 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 1069 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * Frees resources allocated by hw_params, can be called multiple times |
| 1075 | */ |
| 1076 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 1077 | { |
| 1078 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1079 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1080 | struct snd_soc_component *component; |
| 1081 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1082 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1083 | struct snd_soc_dai *codec_dai; |
Nicolin Chen | 7f62b6e | 2013-12-04 11:18:36 +0800 | [diff] [blame] | 1084 | bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1085 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1086 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 1087 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1088 | |
Nicolin Chen | d338342 | 2013-11-20 18:37:09 +0800 | [diff] [blame] | 1089 | /* clear the corresponding DAIs parameters when going to be inactive */ |
| 1090 | if (cpu_dai->active == 1) { |
| 1091 | cpu_dai->rate = 0; |
| 1092 | cpu_dai->channels = 0; |
| 1093 | cpu_dai->sample_bits = 0; |
| 1094 | } |
| 1095 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1096 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1097 | codec_dai = rtd->codec_dais[i]; |
| 1098 | if (codec_dai->active == 1) { |
| 1099 | codec_dai->rate = 0; |
| 1100 | codec_dai->channels = 0; |
| 1101 | codec_dai->sample_bits = 0; |
| 1102 | } |
Nicolin Chen | d338342 | 2013-11-20 18:37:09 +0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1105 | /* apply codec digital mute */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1106 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1107 | if ((playback && rtd->codec_dais[i]->playback_active == 1) || |
| 1108 | (!playback && rtd->codec_dais[i]->capture_active == 1)) |
| 1109 | snd_soc_dai_digital_mute(rtd->codec_dais[i], 1, |
| 1110 | substream->stream); |
| 1111 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1112 | |
| 1113 | /* free any machine hw params */ |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 1114 | if (rtd->dai_link->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1115 | rtd->dai_link->ops->hw_free(substream); |
| 1116 | |
| 1117 | /* free any DMA resources */ |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1118 | if (platform && platform->driver->ops && platform->driver->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1119 | platform->driver->ops->hw_free(substream); |
| 1120 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1121 | /* free any component resources */ |
| 1122 | for_each_rtdcom(rtd, rtdcom) { |
| 1123 | component = rtdcom->component; |
| 1124 | |
| 1125 | /* ignore duplication for now */ |
| 1126 | if (platform && (component == &platform->component)) |
| 1127 | continue; |
| 1128 | |
| 1129 | if (!component->driver->ops || |
| 1130 | !component->driver->ops->hw_free) |
| 1131 | continue; |
| 1132 | |
| 1133 | component->driver->ops->hw_free(substream); |
| 1134 | } |
| 1135 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1136 | /* now free hw params for the DAIs */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1137 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1138 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1139 | if (codec_dai->driver->ops->hw_free) |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1140 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 1141 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1142 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1143 | if (cpu_dai->driver->ops->hw_free) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1144 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 1145 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 1146 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 1151 | { |
| 1152 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1153 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1154 | struct snd_soc_component *component; |
| 1155 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1156 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1157 | struct snd_soc_dai *codec_dai; |
| 1158 | int i, ret; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1159 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1160 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1161 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1162 | if (codec_dai->driver->ops->trigger) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1163 | ret = codec_dai->driver->ops->trigger(substream, |
| 1164 | cmd, codec_dai); |
| 1165 | if (ret < 0) |
| 1166 | return ret; |
| 1167 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1168 | } |
| 1169 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1170 | if (platform && platform->driver->ops && platform->driver->ops->trigger) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1171 | ret = platform->driver->ops->trigger(substream, cmd); |
| 1172 | if (ret < 0) |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1176 | for_each_rtdcom(rtd, rtdcom) { |
| 1177 | component = rtdcom->component; |
| 1178 | |
| 1179 | /* ignore duplication for now */ |
| 1180 | if (platform && (component == &platform->component)) |
| 1181 | continue; |
| 1182 | |
| 1183 | if (!component->driver->ops || |
| 1184 | !component->driver->ops->trigger) |
| 1185 | continue; |
| 1186 | |
| 1187 | ret = component->driver->ops->trigger(substream, cmd); |
| 1188 | if (ret < 0) |
| 1189 | return ret; |
| 1190 | } |
| 1191 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1192 | if (cpu_dai->driver->ops->trigger) { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1193 | ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); |
| 1194 | if (ret < 0) |
| 1195 | return ret; |
| 1196 | } |
Jarkko Nikula | 4792b0d | 2014-04-28 14:17:52 +0200 | [diff] [blame] | 1197 | |
Kuninori Morimoto | 75ab9eb | 2017-09-26 00:40:42 +0000 | [diff] [blame] | 1198 | if (rtd->dai_link->ops->trigger) { |
Jarkko Nikula | 4792b0d | 2014-04-28 14:17:52 +0200 | [diff] [blame] | 1199 | ret = rtd->dai_link->ops->trigger(substream, cmd); |
| 1200 | if (ret < 0) |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1207 | static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, |
| 1208 | int cmd) |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1209 | { |
| 1210 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1211 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1212 | struct snd_soc_dai *codec_dai; |
| 1213 | int i, ret; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1214 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1215 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1216 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1217 | if (codec_dai->driver->ops->bespoke_trigger) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1218 | ret = codec_dai->driver->ops->bespoke_trigger(substream, |
| 1219 | cmd, codec_dai); |
| 1220 | if (ret < 0) |
| 1221 | return ret; |
| 1222 | } |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1225 | if (cpu_dai->driver->ops->bespoke_trigger) { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 1226 | ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai); |
| 1227 | if (ret < 0) |
| 1228 | return ret; |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1232 | /* |
| 1233 | * soc level wrapper for pointer callback |
| 1234 | * If cpu_dai, codec_dai, platform driver has the delay callback, than |
| 1235 | * the runtime->delay will be updated accordingly. |
| 1236 | */ |
| 1237 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) |
| 1238 | { |
| 1239 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1240 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1241 | struct snd_soc_component *component; |
| 1242 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1243 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1244 | struct snd_soc_dai *codec_dai; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1245 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1246 | snd_pcm_uframes_t offset = 0; |
| 1247 | snd_pcm_sframes_t delay = 0; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1248 | snd_pcm_sframes_t codec_delay = 0; |
| 1249 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1250 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1251 | if (platform && platform->driver->ops && platform->driver->ops->pointer) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1252 | offset = platform->driver->ops->pointer(substream); |
| 1253 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 1254 | for_each_rtdcom(rtd, rtdcom) { |
| 1255 | component = rtdcom->component; |
| 1256 | |
| 1257 | /* ignore duplication for now */ |
| 1258 | if (platform && (component == &platform->component)) |
| 1259 | continue; |
| 1260 | |
| 1261 | if (!component->driver->ops || |
| 1262 | !component->driver->ops->pointer) |
| 1263 | continue; |
| 1264 | |
| 1265 | /* FIXME: use 1st pointer */ |
| 1266 | offset = component->driver->ops->pointer(substream); |
| 1267 | break; |
| 1268 | } |
| 1269 | |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1270 | if (cpu_dai->driver->ops->delay) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1271 | delay += cpu_dai->driver->ops->delay(substream, cpu_dai); |
| 1272 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1273 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1274 | codec_dai = rtd->codec_dais[i]; |
Kuninori Morimoto | 9900a42 | 2017-09-25 01:38:54 +0000 | [diff] [blame] | 1275 | if (codec_dai->driver->ops->delay) |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1276 | codec_delay = max(codec_delay, |
| 1277 | codec_dai->driver->ops->delay(substream, |
| 1278 | codec_dai)); |
| 1279 | } |
| 1280 | delay += codec_delay; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1281 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1282 | runtime->delay = delay; |
| 1283 | |
| 1284 | return offset; |
| 1285 | } |
| 1286 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1287 | /* connect a FE and BE */ |
| 1288 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, |
| 1289 | struct snd_soc_pcm_runtime *be, int stream) |
| 1290 | { |
| 1291 | struct snd_soc_dpcm *dpcm; |
| 1292 | |
| 1293 | /* only add new dpcms */ |
| 1294 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1295 | if (dpcm->be == be && dpcm->fe == fe) |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); |
| 1300 | if (!dpcm) |
| 1301 | return -ENOMEM; |
| 1302 | |
| 1303 | dpcm->be = be; |
| 1304 | dpcm->fe = fe; |
| 1305 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; |
| 1306 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; |
| 1307 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); |
| 1308 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); |
| 1309 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1310 | 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] | 1311 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1312 | stream ? "<-" : "->", be->dai_link->name); |
| 1313 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 1314 | #ifdef CONFIG_DEBUG_FS |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 1315 | if (fe->debugfs_dpcm_root) |
| 1316 | dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, |
| 1317 | fe->debugfs_dpcm_root, &dpcm->state); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 1318 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1319 | return 1; |
| 1320 | } |
| 1321 | |
| 1322 | /* reparent a BE onto another FE */ |
| 1323 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, |
| 1324 | struct snd_soc_pcm_runtime *be, int stream) |
| 1325 | { |
| 1326 | struct snd_soc_dpcm *dpcm; |
| 1327 | struct snd_pcm_substream *fe_substream, *be_substream; |
| 1328 | |
| 1329 | /* reparent if BE is connected to other FEs */ |
| 1330 | if (!be->dpcm[stream].users) |
| 1331 | return; |
| 1332 | |
| 1333 | be_substream = snd_soc_dpcm_get_substream(be, stream); |
| 1334 | |
| 1335 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 1336 | if (dpcm->fe == fe) |
| 1337 | continue; |
| 1338 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1339 | dev_dbg(fe->dev, "reparent %s path %s %s %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1340 | stream ? "capture" : "playback", |
| 1341 | dpcm->fe->dai_link->name, |
| 1342 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1343 | |
| 1344 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); |
| 1345 | be_substream->runtime = fe_substream->runtime; |
| 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* disconnect a BE and FE */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1351 | void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1352 | { |
| 1353 | struct snd_soc_dpcm *dpcm, *d; |
| 1354 | |
| 1355 | 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] | 1356 | dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1357 | stream ? "capture" : "playback", |
| 1358 | dpcm->be->dai_link->name); |
| 1359 | |
| 1360 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) |
| 1361 | continue; |
| 1362 | |
Jarkko Nikula | 7cc302d | 2013-09-30 17:08:15 +0300 | [diff] [blame] | 1363 | dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1364 | stream ? "capture" : "playback", fe->dai_link->name, |
| 1365 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 1366 | |
| 1367 | /* BEs still alive need new FE */ |
| 1368 | dpcm_be_reparent(fe, dpcm->be, stream); |
| 1369 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 1370 | #ifdef CONFIG_DEBUG_FS |
| 1371 | debugfs_remove(dpcm->debugfs_state); |
| 1372 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1373 | list_del(&dpcm->list_be); |
| 1374 | list_del(&dpcm->list_fe); |
| 1375 | kfree(dpcm); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /* get BE for DAI widget and stream */ |
| 1380 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, |
| 1381 | struct snd_soc_dapm_widget *widget, int stream) |
| 1382 | { |
| 1383 | struct snd_soc_pcm_runtime *be; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1384 | int i; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1385 | |
| 1386 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1387 | list_for_each_entry(be, &card->rtd_list, list) { |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1388 | |
Liam Girdwood | 35ea065 | 2012-06-05 19:26:59 +0100 | [diff] [blame] | 1389 | if (!be->dai_link->no_pcm) |
| 1390 | continue; |
| 1391 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1392 | if (be->cpu_dai->playback_widget == widget) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1393 | return be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1394 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1395 | for (i = 0; i < be->num_codecs; i++) { |
| 1396 | struct snd_soc_dai *dai = be->codec_dais[i]; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1397 | if (dai->playback_widget == widget) |
| 1398 | return be; |
| 1399 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1400 | } |
| 1401 | } else { |
| 1402 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1403 | list_for_each_entry(be, &card->rtd_list, list) { |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1404 | |
Liam Girdwood | 35ea065 | 2012-06-05 19:26:59 +0100 | [diff] [blame] | 1405 | if (!be->dai_link->no_pcm) |
| 1406 | continue; |
| 1407 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1408 | if (be->cpu_dai->capture_widget == widget) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1409 | return be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1410 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1411 | for (i = 0; i < be->num_codecs; i++) { |
| 1412 | struct snd_soc_dai *dai = be->codec_dais[i]; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1413 | if (dai->capture_widget == widget) |
| 1414 | return be; |
| 1415 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1419 | 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] | 1420 | stream ? "capture" : "playback", widget->name); |
| 1421 | return NULL; |
| 1422 | } |
| 1423 | |
| 1424 | static inline struct snd_soc_dapm_widget * |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1425 | dai_get_widget(struct snd_soc_dai *dai, int stream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1426 | { |
| 1427 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1428 | return dai->playback_widget; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1429 | else |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1430 | return dai->capture_widget; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, |
| 1434 | struct snd_soc_dapm_widget *widget) |
| 1435 | { |
| 1436 | int i; |
| 1437 | |
| 1438 | for (i = 0; i < list->num_widgets; i++) { |
| 1439 | if (widget == list->widgets[i]) |
| 1440 | return 1; |
| 1441 | } |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Piotr Stankiewicz | 5fdd022 | 2016-05-13 17:03:56 +0100 | [diff] [blame] | 1446 | static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, |
| 1447 | enum snd_soc_dapm_direction dir) |
| 1448 | { |
| 1449 | struct snd_soc_card *card = widget->dapm->card; |
| 1450 | struct snd_soc_pcm_runtime *rtd; |
| 1451 | int i; |
| 1452 | |
| 1453 | if (dir == SND_SOC_DAPM_DIR_OUT) { |
| 1454 | list_for_each_entry(rtd, &card->rtd_list, list) { |
| 1455 | if (!rtd->dai_link->no_pcm) |
| 1456 | continue; |
| 1457 | |
| 1458 | if (rtd->cpu_dai->playback_widget == widget) |
| 1459 | return true; |
| 1460 | |
| 1461 | for (i = 0; i < rtd->num_codecs; ++i) { |
| 1462 | struct snd_soc_dai *dai = rtd->codec_dais[i]; |
| 1463 | if (dai->playback_widget == widget) |
| 1464 | return true; |
| 1465 | } |
| 1466 | } |
| 1467 | } else { /* SND_SOC_DAPM_DIR_IN */ |
| 1468 | list_for_each_entry(rtd, &card->rtd_list, list) { |
| 1469 | if (!rtd->dai_link->no_pcm) |
| 1470 | continue; |
| 1471 | |
| 1472 | if (rtd->cpu_dai->capture_widget == widget) |
| 1473 | return true; |
| 1474 | |
| 1475 | for (i = 0; i < rtd->num_codecs; ++i) { |
| 1476 | struct snd_soc_dai *dai = rtd->codec_dais[i]; |
| 1477 | if (dai->capture_widget == widget) |
| 1478 | return true; |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1486 | int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
Lars-Peter Clausen | 1ce43ac | 2015-07-26 19:04:59 +0200 | [diff] [blame] | 1487 | int stream, struct snd_soc_dapm_widget_list **list) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1488 | { |
| 1489 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1490 | int paths; |
| 1491 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1492 | /* get number of valid DAI paths and their widgets */ |
Piotr Stankiewicz | 6742064 | 2016-05-13 17:03:55 +0100 | [diff] [blame] | 1493 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, |
Piotr Stankiewicz | 5fdd022 | 2016-05-13 17:03:56 +0100 | [diff] [blame] | 1494 | dpcm_end_walk_at_be); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1495 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1496 | dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1497 | stream ? "capture" : "playback"); |
| 1498 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1499 | return paths; |
| 1500 | } |
| 1501 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1502 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1503 | struct snd_soc_dapm_widget_list **list_) |
| 1504 | { |
| 1505 | struct snd_soc_dpcm *dpcm; |
| 1506 | struct snd_soc_dapm_widget_list *list = *list_; |
| 1507 | struct snd_soc_dapm_widget *widget; |
| 1508 | int prune = 0; |
| 1509 | |
| 1510 | /* Destroy any old FE <--> BE connections */ |
| 1511 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1512 | unsigned int i; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* is there a valid CPU DAI widget for this BE */ |
Benoit Cousson | 3701861 | 2014-04-24 14:01:45 +0200 | [diff] [blame] | 1515 | widget = dai_get_widget(dpcm->be->cpu_dai, stream); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1516 | |
| 1517 | /* prune the BE if it's no longer in our active list */ |
| 1518 | if (widget && widget_in_list(list, widget)) |
| 1519 | continue; |
| 1520 | |
| 1521 | /* is there a valid CODEC DAI widget for this BE */ |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1522 | for (i = 0; i < dpcm->be->num_codecs; i++) { |
| 1523 | struct snd_soc_dai *dai = dpcm->be->codec_dais[i]; |
| 1524 | widget = dai_get_widget(dai, stream); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1525 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 1526 | /* prune the BE if it's no longer in our active list */ |
| 1527 | if (widget && widget_in_list(list, widget)) |
| 1528 | continue; |
| 1529 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1530 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1531 | dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1532 | stream ? "capture" : "playback", |
| 1533 | dpcm->be->dai_link->name, fe->dai_link->name); |
| 1534 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 1535 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1536 | prune++; |
| 1537 | } |
| 1538 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1539 | 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] | 1540 | return prune; |
| 1541 | } |
| 1542 | |
| 1543 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 1544 | struct snd_soc_dapm_widget_list **list_) |
| 1545 | { |
| 1546 | struct snd_soc_card *card = fe->card; |
| 1547 | struct snd_soc_dapm_widget_list *list = *list_; |
| 1548 | struct snd_soc_pcm_runtime *be; |
| 1549 | int i, new = 0, err; |
| 1550 | |
| 1551 | /* Create any new FE <--> BE connections */ |
| 1552 | for (i = 0; i < list->num_widgets; i++) { |
| 1553 | |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1554 | switch (list->widgets[i]->id) { |
| 1555 | case snd_soc_dapm_dai_in: |
Koro Chen | c5b8540 | 2015-07-06 10:02:10 +0800 | [diff] [blame] | 1556 | if (stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 1557 | continue; |
| 1558 | break; |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1559 | case snd_soc_dapm_dai_out: |
Koro Chen | c5b8540 | 2015-07-06 10:02:10 +0800 | [diff] [blame] | 1560 | if (stream != SNDRV_PCM_STREAM_CAPTURE) |
| 1561 | continue; |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1562 | break; |
| 1563 | default: |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1564 | continue; |
Mark Brown | 4616274 | 2013-06-05 19:36:11 +0100 | [diff] [blame] | 1565 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* is there a valid BE rtd for this widget */ |
| 1568 | be = dpcm_get_be(card, list->widgets[i], stream); |
| 1569 | if (!be) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1570 | dev_err(fe->dev, "ASoC: no BE found for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1571 | list->widgets[i]->name); |
| 1572 | continue; |
| 1573 | } |
| 1574 | |
| 1575 | /* make sure BE is a real BE */ |
| 1576 | if (!be->dai_link->no_pcm) |
| 1577 | continue; |
| 1578 | |
| 1579 | /* don't connect if FE is not running */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1580 | if (!fe->dpcm[stream].runtime && !fe->fe_compr) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1581 | continue; |
| 1582 | |
| 1583 | /* newly connected FE and BE */ |
| 1584 | err = dpcm_be_connect(fe, be, stream); |
| 1585 | if (err < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1586 | dev_err(fe->dev, "ASoC: can't connect %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1587 | list->widgets[i]->name); |
| 1588 | break; |
| 1589 | } else if (err == 0) /* already connected */ |
| 1590 | continue; |
| 1591 | |
| 1592 | /* new */ |
| 1593 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 1594 | new++; |
| 1595 | } |
| 1596 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1597 | dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1598 | return new; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * Find the corresponding BE DAIs that source or sink audio to this |
| 1603 | * FE substream. |
| 1604 | */ |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1605 | int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1606 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
| 1607 | { |
| 1608 | if (new) |
| 1609 | return dpcm_add_paths(fe, stream, list); |
| 1610 | else |
| 1611 | return dpcm_prune_paths(fe, stream, list); |
| 1612 | } |
| 1613 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1614 | 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] | 1615 | { |
| 1616 | struct snd_soc_dpcm *dpcm; |
| 1617 | |
| 1618 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 1619 | dpcm->be->dpcm[stream].runtime_update = |
| 1620 | SND_SOC_DPCM_UPDATE_NO; |
| 1621 | } |
| 1622 | |
| 1623 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, |
| 1624 | int stream) |
| 1625 | { |
| 1626 | struct snd_soc_dpcm *dpcm; |
| 1627 | |
| 1628 | /* disable any enabled and non active backends */ |
| 1629 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1630 | |
| 1631 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1632 | struct snd_pcm_substream *be_substream = |
| 1633 | snd_soc_dpcm_get_substream(be, stream); |
| 1634 | |
| 1635 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1636 | 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] | 1637 | stream ? "capture" : "playback", |
| 1638 | be->dpcm[stream].state); |
| 1639 | |
| 1640 | if (--be->dpcm[stream].users != 0) |
| 1641 | continue; |
| 1642 | |
| 1643 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1644 | continue; |
| 1645 | |
| 1646 | soc_pcm_close(be_substream); |
| 1647 | be_substream->runtime = NULL; |
| 1648 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1649 | } |
| 1650 | } |
| 1651 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1652 | 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] | 1653 | { |
| 1654 | struct snd_soc_dpcm *dpcm; |
| 1655 | int err, count = 0; |
| 1656 | |
| 1657 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ |
| 1658 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1659 | |
| 1660 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1661 | struct snd_pcm_substream *be_substream = |
| 1662 | snd_soc_dpcm_get_substream(be, stream); |
| 1663 | |
Russell King - ARM Linux | 2062b4c | 2013-10-31 15:09:20 +0000 | [diff] [blame] | 1664 | if (!be_substream) { |
| 1665 | dev_err(be->dev, "ASoC: no backend %s stream\n", |
| 1666 | stream ? "capture" : "playback"); |
| 1667 | continue; |
| 1668 | } |
| 1669 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1670 | /* is this op for this BE ? */ |
| 1671 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1672 | continue; |
| 1673 | |
| 1674 | /* first time the dpcm is open ? */ |
| 1675 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1676 | 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] | 1677 | stream ? "capture" : "playback", |
| 1678 | be->dpcm[stream].state); |
| 1679 | |
| 1680 | if (be->dpcm[stream].users++ != 0) |
| 1681 | continue; |
| 1682 | |
| 1683 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && |
| 1684 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) |
| 1685 | continue; |
| 1686 | |
Russell King - ARM Linux | 2062b4c | 2013-10-31 15:09:20 +0000 | [diff] [blame] | 1687 | dev_dbg(be->dev, "ASoC: open %s BE %s\n", |
| 1688 | stream ? "capture" : "playback", be->dai_link->name); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1689 | |
| 1690 | be_substream->runtime = be->dpcm[stream].runtime; |
| 1691 | err = soc_pcm_open(be_substream); |
| 1692 | if (err < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1693 | dev_err(be->dev, "ASoC: BE open failed %d\n", err); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1694 | be->dpcm[stream].users--; |
| 1695 | if (be->dpcm[stream].users < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1696 | dev_err(be->dev, "ASoC: no users %s at unwind %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1697 | stream ? "capture" : "playback", |
| 1698 | be->dpcm[stream].state); |
| 1699 | |
| 1700 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1701 | goto unwind; |
| 1702 | } |
| 1703 | |
| 1704 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1705 | count++; |
| 1706 | } |
| 1707 | |
| 1708 | return count; |
| 1709 | |
| 1710 | unwind: |
| 1711 | /* disable any enabled and non active backends */ |
| 1712 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1713 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1714 | struct snd_pcm_substream *be_substream = |
| 1715 | snd_soc_dpcm_get_substream(be, stream); |
| 1716 | |
| 1717 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1718 | continue; |
| 1719 | |
| 1720 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1721 | dev_err(be->dev, "ASoC: no users %s at close %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1722 | stream ? "capture" : "playback", |
| 1723 | be->dpcm[stream].state); |
| 1724 | |
| 1725 | if (--be->dpcm[stream].users != 0) |
| 1726 | continue; |
| 1727 | |
| 1728 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1729 | continue; |
| 1730 | |
| 1731 | soc_pcm_close(be_substream); |
| 1732 | be_substream->runtime = NULL; |
| 1733 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1734 | } |
| 1735 | |
| 1736 | return err; |
| 1737 | } |
| 1738 | |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1739 | static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1740 | struct snd_soc_pcm_stream *stream, |
| 1741 | u64 formats) |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1742 | { |
| 1743 | runtime->hw.rate_min = stream->rate_min; |
| 1744 | runtime->hw.rate_max = stream->rate_max; |
| 1745 | runtime->hw.channels_min = stream->channels_min; |
| 1746 | runtime->hw.channels_max = stream->channels_max; |
Lars-Peter Clausen | 002220a | 2014-01-06 14:19:07 +0100 | [diff] [blame] | 1747 | if (runtime->hw.formats) |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1748 | runtime->hw.formats &= formats & stream->formats; |
Lars-Peter Clausen | 002220a | 2014-01-06 14:19:07 +0100 | [diff] [blame] | 1749 | else |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1750 | runtime->hw.formats = formats & stream->formats; |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1751 | runtime->hw.rates = stream->rates; |
| 1752 | } |
| 1753 | |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1754 | static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream) |
| 1755 | { |
| 1756 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1757 | struct snd_soc_dpcm *dpcm; |
| 1758 | u64 formats = ULLONG_MAX; |
| 1759 | int stream = substream->stream; |
| 1760 | |
| 1761 | if (!fe->dai_link->dpcm_merged_format) |
| 1762 | return formats; |
| 1763 | |
| 1764 | /* |
| 1765 | * It returns merged BE codec format |
| 1766 | * if FE want to use it (= dpcm_merged_format) |
| 1767 | */ |
| 1768 | |
| 1769 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1770 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1771 | struct snd_soc_dai_driver *codec_dai_drv; |
| 1772 | struct snd_soc_pcm_stream *codec_stream; |
| 1773 | int i; |
| 1774 | |
| 1775 | for (i = 0; i < be->num_codecs; i++) { |
| 1776 | codec_dai_drv = be->codec_dais[i]->driver; |
| 1777 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1778 | codec_stream = &codec_dai_drv->playback; |
| 1779 | else |
| 1780 | codec_stream = &codec_dai_drv->capture; |
| 1781 | |
| 1782 | formats &= codec_stream->formats; |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | return formats; |
| 1787 | } |
| 1788 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 1789 | static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1790 | { |
| 1791 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1792 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1793 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 1794 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1795 | u64 format = dpcm_runtime_base_format(substream); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1796 | |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1797 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1798 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format); |
Lars-Peter Clausen | 08ae9b4 | 2014-01-06 14:19:06 +0100 | [diff] [blame] | 1799 | else |
Kuninori Morimoto | b073ed4 | 2015-05-12 02:03:33 +0000 | [diff] [blame] | 1800 | dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1803 | static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); |
| 1804 | |
| 1805 | /* Set FE's runtime_update state; the state is protected via PCM stream lock |
| 1806 | * for avoiding the race with trigger callback. |
| 1807 | * If the state is unset and a trigger is pending while the previous operation, |
| 1808 | * process the pending trigger action here. |
| 1809 | */ |
| 1810 | static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, |
| 1811 | int stream, enum snd_soc_dpcm_update state) |
| 1812 | { |
| 1813 | struct snd_pcm_substream *substream = |
| 1814 | snd_soc_dpcm_get_substream(fe, stream); |
| 1815 | |
| 1816 | snd_pcm_stream_lock_irq(substream); |
| 1817 | if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { |
| 1818 | dpcm_fe_dai_do_trigger(substream, |
| 1819 | fe->dpcm[stream].trigger_pending - 1); |
| 1820 | fe->dpcm[stream].trigger_pending = 0; |
| 1821 | } |
| 1822 | fe->dpcm[stream].runtime_update = state; |
| 1823 | snd_pcm_stream_unlock_irq(substream); |
| 1824 | } |
| 1825 | |
PC Liao | 906c7d6 | 2015-12-11 11:33:51 +0800 | [diff] [blame] | 1826 | static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, |
| 1827 | int stream) |
| 1828 | { |
| 1829 | struct snd_soc_dpcm *dpcm; |
| 1830 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1831 | struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai; |
| 1832 | int err; |
| 1833 | |
| 1834 | /* apply symmetry for FE */ |
| 1835 | if (soc_pcm_has_symmetry(fe_substream)) |
| 1836 | fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 1837 | |
| 1838 | /* Symmetry only applies if we've got an active stream. */ |
| 1839 | if (fe_cpu_dai->active) { |
| 1840 | err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); |
| 1841 | if (err < 0) |
| 1842 | return err; |
| 1843 | } |
| 1844 | |
| 1845 | /* apply symmetry for BE */ |
| 1846 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1847 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1848 | struct snd_pcm_substream *be_substream = |
| 1849 | snd_soc_dpcm_get_substream(be, stream); |
| 1850 | struct snd_soc_pcm_runtime *rtd = be_substream->private_data; |
| 1851 | int i; |
| 1852 | |
Jeeja KP | f117661 | 2016-09-06 14:17:55 +0530 | [diff] [blame] | 1853 | if (rtd->dai_link->be_hw_params_fixup) |
| 1854 | continue; |
| 1855 | |
PC Liao | 906c7d6 | 2015-12-11 11:33:51 +0800 | [diff] [blame] | 1856 | if (soc_pcm_has_symmetry(be_substream)) |
| 1857 | be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; |
| 1858 | |
| 1859 | /* Symmetry only applies if we've got an active stream. */ |
| 1860 | if (rtd->cpu_dai->active) { |
| 1861 | err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai); |
| 1862 | if (err < 0) |
| 1863 | return err; |
| 1864 | } |
| 1865 | |
| 1866 | for (i = 0; i < rtd->num_codecs; i++) { |
| 1867 | if (rtd->codec_dais[i]->active) { |
| 1868 | err = soc_pcm_apply_symmetry(be_substream, |
| 1869 | rtd->codec_dais[i]); |
| 1870 | if (err < 0) |
| 1871 | return err; |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1879 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) |
| 1880 | { |
| 1881 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1882 | struct snd_pcm_runtime *runtime = fe_substream->runtime; |
| 1883 | int stream = fe_substream->stream, ret = 0; |
| 1884 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1885 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1886 | |
| 1887 | ret = dpcm_be_dai_startup(fe, fe_substream->stream); |
| 1888 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1889 | 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] | 1890 | goto be_err; |
| 1891 | } |
| 1892 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1893 | 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] | 1894 | |
| 1895 | /* start the DAI frontend */ |
| 1896 | ret = soc_pcm_open(fe_substream); |
| 1897 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1898 | dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1899 | goto unwind; |
| 1900 | } |
| 1901 | |
| 1902 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1903 | |
| 1904 | dpcm_set_fe_runtime(fe_substream); |
| 1905 | snd_pcm_limit_hw_rates(runtime); |
| 1906 | |
PC Liao | 906c7d6 | 2015-12-11 11:33:51 +0800 | [diff] [blame] | 1907 | ret = dpcm_apply_symmetry(fe_substream, stream); |
| 1908 | if (ret < 0) { |
| 1909 | dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", |
| 1910 | ret); |
| 1911 | goto unwind; |
| 1912 | } |
| 1913 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1914 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1915 | return 0; |
| 1916 | |
| 1917 | unwind: |
| 1918 | dpcm_be_dai_startup_unwind(fe, fe_substream->stream); |
| 1919 | be_err: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1920 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1921 | return ret; |
| 1922 | } |
| 1923 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1924 | 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] | 1925 | { |
| 1926 | struct snd_soc_dpcm *dpcm; |
| 1927 | |
| 1928 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ |
| 1929 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1930 | |
| 1931 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1932 | struct snd_pcm_substream *be_substream = |
| 1933 | snd_soc_dpcm_get_substream(be, stream); |
| 1934 | |
| 1935 | /* is this op for this BE ? */ |
| 1936 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1937 | continue; |
| 1938 | |
| 1939 | if (be->dpcm[stream].users == 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1940 | 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] | 1941 | stream ? "capture" : "playback", |
| 1942 | be->dpcm[stream].state); |
| 1943 | |
| 1944 | if (--be->dpcm[stream].users != 0) |
| 1945 | continue; |
| 1946 | |
| 1947 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1948 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) |
| 1949 | continue; |
| 1950 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1951 | dev_dbg(be->dev, "ASoC: close BE %s\n", |
å½ä¸œæž— | 94d215c | 2016-09-26 08:29:31 +0000 | [diff] [blame] | 1952 | be->dai_link->name); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1953 | |
| 1954 | soc_pcm_close(be_substream); |
| 1955 | be_substream->runtime = NULL; |
| 1956 | |
| 1957 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1958 | } |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) |
| 1963 | { |
| 1964 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1965 | int stream = substream->stream; |
| 1966 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1967 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1968 | |
| 1969 | /* shutdown the BEs */ |
| 1970 | dpcm_be_dai_shutdown(fe, substream->stream); |
| 1971 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 1972 | 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] | 1973 | |
| 1974 | /* now shutdown the frontend */ |
| 1975 | soc_pcm_close(substream); |
| 1976 | |
| 1977 | /* run the stream event for each BE */ |
| 1978 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 1979 | |
| 1980 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 1981 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1982 | return 0; |
| 1983 | } |
| 1984 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 1985 | 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] | 1986 | { |
| 1987 | struct snd_soc_dpcm *dpcm; |
| 1988 | |
| 1989 | /* only hw_params backends that are either sinks or sources |
| 1990 | * to this frontend DAI */ |
| 1991 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1992 | |
| 1993 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1994 | struct snd_pcm_substream *be_substream = |
| 1995 | snd_soc_dpcm_get_substream(be, stream); |
| 1996 | |
| 1997 | /* is this op for this BE ? */ |
| 1998 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1999 | continue; |
| 2000 | |
| 2001 | /* only free hw when no longer used - check all FEs */ |
| 2002 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2003 | continue; |
| 2004 | |
Qiao Zhou | 36fba62 | 2014-12-03 10:13:43 +0800 | [diff] [blame] | 2005 | /* do not free hw if this BE is used by other FE */ |
| 2006 | if (be->dpcm[stream].users > 1) |
| 2007 | continue; |
| 2008 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2009 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2010 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 2011 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
Patrick Lai | 08b2784 | 2012-12-19 19:36:02 -0800 | [diff] [blame] | 2012 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && |
Vinod Koul | 5e82d2b | 2016-02-01 22:26:40 +0530 | [diff] [blame] | 2013 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
| 2014 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2015 | continue; |
| 2016 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2017 | dev_dbg(be->dev, "ASoC: hw_free BE %s\n", |
å½ä¸œæž— | 94d215c | 2016-09-26 08:29:31 +0000 | [diff] [blame] | 2018 | be->dai_link->name); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2019 | |
| 2020 | soc_pcm_hw_free(be_substream); |
| 2021 | |
| 2022 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 2023 | } |
| 2024 | |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2028 | static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2029 | { |
| 2030 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2031 | int err, stream = substream->stream; |
| 2032 | |
| 2033 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2034 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2035 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2036 | 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] | 2037 | |
| 2038 | /* call hw_free on the frontend */ |
| 2039 | err = soc_pcm_hw_free(substream); |
| 2040 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2041 | dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2042 | fe->dai_link->name); |
| 2043 | |
| 2044 | /* only hw_params backends that are either sinks or sources |
| 2045 | * to this frontend DAI */ |
| 2046 | err = dpcm_be_dai_hw_free(fe, stream); |
| 2047 | |
| 2048 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2049 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2050 | |
| 2051 | mutex_unlock(&fe->card->mutex); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 2055 | 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] | 2056 | { |
| 2057 | struct snd_soc_dpcm *dpcm; |
| 2058 | int ret; |
| 2059 | |
| 2060 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2061 | |
| 2062 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2063 | struct snd_pcm_substream *be_substream = |
| 2064 | snd_soc_dpcm_get_substream(be, stream); |
| 2065 | |
| 2066 | /* is this op for this BE ? */ |
| 2067 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2068 | continue; |
| 2069 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2070 | /* copy params for each dpcm */ |
| 2071 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, |
| 2072 | sizeof(struct snd_pcm_hw_params)); |
| 2073 | |
| 2074 | /* perform any hw_params fixups */ |
| 2075 | if (be->dai_link->be_hw_params_fixup) { |
| 2076 | ret = be->dai_link->be_hw_params_fixup(be, |
| 2077 | &dpcm->hw_params); |
| 2078 | if (ret < 0) { |
| 2079 | dev_err(be->dev, |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2080 | "ASoC: hw_params BE fixup failed %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2081 | ret); |
| 2082 | goto unwind; |
| 2083 | } |
| 2084 | } |
| 2085 | |
Kuninori Morimoto | b0639bd | 2016-01-21 01:47:12 +0000 | [diff] [blame] | 2086 | /* only allow hw_params() if no connected FEs are running */ |
| 2087 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) |
| 2088 | continue; |
| 2089 | |
| 2090 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 2091 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2092 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) |
| 2093 | continue; |
| 2094 | |
| 2095 | dev_dbg(be->dev, "ASoC: hw_params BE %s\n", |
å½ä¸œæž— | 94d215c | 2016-09-26 08:29:31 +0000 | [diff] [blame] | 2096 | be->dai_link->name); |
Kuninori Morimoto | b0639bd | 2016-01-21 01:47:12 +0000 | [diff] [blame] | 2097 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2098 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); |
| 2099 | if (ret < 0) { |
| 2100 | dev_err(dpcm->be->dev, |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2101 | "ASoC: hw_params BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2102 | goto unwind; |
| 2103 | } |
| 2104 | |
| 2105 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 2106 | } |
| 2107 | return 0; |
| 2108 | |
| 2109 | unwind: |
| 2110 | /* disable any enabled and non active backends */ |
| 2111 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2112 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2113 | struct snd_pcm_substream *be_substream = |
| 2114 | snd_soc_dpcm_get_substream(be, stream); |
| 2115 | |
| 2116 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2117 | continue; |
| 2118 | |
| 2119 | /* only allow hw_free() if no connected FEs are running */ |
| 2120 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2121 | continue; |
| 2122 | |
| 2123 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 2124 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 2125 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 2126 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 2127 | continue; |
| 2128 | |
| 2129 | soc_pcm_hw_free(be_substream); |
| 2130 | } |
| 2131 | |
| 2132 | return ret; |
| 2133 | } |
| 2134 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2135 | static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
| 2136 | struct snd_pcm_hw_params *params) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2137 | { |
| 2138 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2139 | int ret, stream = substream->stream; |
| 2140 | |
| 2141 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2142 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2143 | |
| 2144 | memcpy(&fe->dpcm[substream->stream].hw_params, params, |
| 2145 | sizeof(struct snd_pcm_hw_params)); |
| 2146 | ret = dpcm_be_dai_hw_params(fe, substream->stream); |
| 2147 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2148 | dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2149 | goto out; |
| 2150 | } |
| 2151 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2152 | 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] | 2153 | fe->dai_link->name, params_rate(params), |
| 2154 | params_channels(params), params_format(params)); |
| 2155 | |
| 2156 | /* call hw_params on the frontend */ |
| 2157 | ret = soc_pcm_hw_params(substream, params); |
| 2158 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2159 | dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2160 | dpcm_be_dai_hw_free(fe, stream); |
| 2161 | } else |
| 2162 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 2163 | |
| 2164 | out: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2165 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2166 | mutex_unlock(&fe->card->mutex); |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
| 2170 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, |
| 2171 | struct snd_pcm_substream *substream, int cmd) |
| 2172 | { |
| 2173 | int ret; |
| 2174 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2175 | dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", |
å½ä¸œæž— | 94d215c | 2016-09-26 08:29:31 +0000 | [diff] [blame] | 2176 | dpcm->be->dai_link->name, cmd); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2177 | |
| 2178 | ret = soc_pcm_trigger(substream, cmd); |
| 2179 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2180 | dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2181 | |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 2185 | 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] | 2186 | int cmd) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2187 | { |
| 2188 | struct snd_soc_dpcm *dpcm; |
| 2189 | int ret = 0; |
| 2190 | |
| 2191 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2192 | |
| 2193 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2194 | struct snd_pcm_substream *be_substream = |
| 2195 | snd_soc_dpcm_get_substream(be, stream); |
| 2196 | |
| 2197 | /* is this op for this BE ? */ |
| 2198 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2199 | continue; |
| 2200 | |
| 2201 | switch (cmd) { |
| 2202 | case SNDRV_PCM_TRIGGER_START: |
| 2203 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 2204 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 2205 | continue; |
| 2206 | |
| 2207 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2208 | if (ret) |
| 2209 | return ret; |
| 2210 | |
| 2211 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2212 | break; |
| 2213 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2214 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
| 2215 | continue; |
| 2216 | |
| 2217 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2218 | if (ret) |
| 2219 | return ret; |
| 2220 | |
| 2221 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2222 | break; |
| 2223 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2224 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
| 2225 | continue; |
| 2226 | |
| 2227 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2228 | if (ret) |
| 2229 | return ret; |
| 2230 | |
| 2231 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2232 | break; |
| 2233 | case SNDRV_PCM_TRIGGER_STOP: |
| 2234 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2235 | continue; |
| 2236 | |
| 2237 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2238 | continue; |
| 2239 | |
| 2240 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2241 | if (ret) |
| 2242 | return ret; |
| 2243 | |
| 2244 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 2245 | break; |
| 2246 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Nicolin Chen | 868a6ca | 2014-05-12 20:12:05 +0800 | [diff] [blame] | 2247 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2248 | continue; |
| 2249 | |
| 2250 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2251 | continue; |
| 2252 | |
| 2253 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2254 | if (ret) |
| 2255 | return ret; |
| 2256 | |
| 2257 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; |
| 2258 | break; |
| 2259 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2260 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2261 | continue; |
| 2262 | |
| 2263 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 2264 | continue; |
| 2265 | |
| 2266 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 2267 | if (ret) |
| 2268 | return ret; |
| 2269 | |
| 2270 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 2271 | break; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | return ret; |
| 2276 | } |
| 2277 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); |
| 2278 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2279 | 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] | 2280 | { |
| 2281 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2282 | int stream = substream->stream, ret; |
| 2283 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 2284 | |
| 2285 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 2286 | |
| 2287 | switch (trigger) { |
| 2288 | case SND_SOC_DPCM_TRIGGER_PRE: |
| 2289 | /* call trigger on the frontend before the backend. */ |
| 2290 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2291 | dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2292 | fe->dai_link->name, cmd); |
| 2293 | |
| 2294 | ret = soc_pcm_trigger(substream, cmd); |
| 2295 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2296 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2297 | goto out; |
| 2298 | } |
| 2299 | |
| 2300 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 2301 | break; |
| 2302 | case SND_SOC_DPCM_TRIGGER_POST: |
| 2303 | /* call trigger on the frontend after the backend. */ |
| 2304 | |
| 2305 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 2306 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2307 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2308 | goto out; |
| 2309 | } |
| 2310 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2311 | dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2312 | fe->dai_link->name, cmd); |
| 2313 | |
| 2314 | ret = soc_pcm_trigger(substream, cmd); |
| 2315 | break; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2316 | case SND_SOC_DPCM_TRIGGER_BESPOKE: |
| 2317 | /* bespoke trigger() - handles both FE and BEs */ |
| 2318 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2319 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2320 | fe->dai_link->name, cmd); |
| 2321 | |
| 2322 | ret = soc_pcm_bespoke_trigger(substream, cmd); |
| 2323 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2324 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2325 | goto out; |
| 2326 | } |
| 2327 | break; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2328 | default: |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2329 | 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] | 2330 | fe->dai_link->name); |
| 2331 | ret = -EINVAL; |
| 2332 | goto out; |
| 2333 | } |
| 2334 | |
| 2335 | switch (cmd) { |
| 2336 | case SNDRV_PCM_TRIGGER_START: |
| 2337 | case SNDRV_PCM_TRIGGER_RESUME: |
| 2338 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 2339 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 2340 | break; |
| 2341 | case SNDRV_PCM_TRIGGER_STOP: |
| 2342 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2343 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 2344 | break; |
Patrick Lai | 9f169b9 | 2016-12-31 22:44:39 -0800 | [diff] [blame] | 2345 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 2346 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 2347 | break; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | out: |
| 2351 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 2352 | return ret; |
| 2353 | } |
| 2354 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2355 | static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
| 2356 | { |
| 2357 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2358 | int stream = substream->stream; |
| 2359 | |
| 2360 | /* if FE's runtime_update is already set, we're in race; |
| 2361 | * process this trigger later at exit |
| 2362 | */ |
| 2363 | if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { |
| 2364 | fe->dpcm[stream].trigger_pending = cmd + 1; |
| 2365 | return 0; /* delayed, assuming it's successful */ |
| 2366 | } |
| 2367 | |
| 2368 | /* we're alone, let's trigger */ |
| 2369 | return dpcm_fe_dai_do_trigger(substream, cmd); |
| 2370 | } |
| 2371 | |
Liam Girdwood | 2360702 | 2014-01-17 17:03:55 +0000 | [diff] [blame] | 2372 | 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] | 2373 | { |
| 2374 | struct snd_soc_dpcm *dpcm; |
| 2375 | int ret = 0; |
| 2376 | |
| 2377 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2378 | |
| 2379 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2380 | struct snd_pcm_substream *be_substream = |
| 2381 | snd_soc_dpcm_get_substream(be, stream); |
| 2382 | |
| 2383 | /* is this op for this BE ? */ |
| 2384 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 2385 | continue; |
| 2386 | |
| 2387 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
Koro Chen | 95f444d | 2015-10-28 10:15:34 +0800 | [diff] [blame] | 2388 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && |
| 2389 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2390 | continue; |
| 2391 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2392 | dev_dbg(be->dev, "ASoC: prepare BE %s\n", |
å½ä¸œæž— | 94d215c | 2016-09-26 08:29:31 +0000 | [diff] [blame] | 2393 | be->dai_link->name); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2394 | |
| 2395 | ret = soc_pcm_prepare(be_substream); |
| 2396 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2397 | dev_err(be->dev, "ASoC: backend prepare failed %d\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2398 | ret); |
| 2399 | break; |
| 2400 | } |
| 2401 | |
| 2402 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2403 | } |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2407 | static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2408 | { |
| 2409 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 2410 | int stream = substream->stream, ret = 0; |
| 2411 | |
| 2412 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2413 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2414 | 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] | 2415 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2416 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2417 | |
| 2418 | /* there is no point preparing this FE if there are no BEs */ |
| 2419 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2420 | dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2421 | fe->dai_link->name); |
| 2422 | ret = -EINVAL; |
| 2423 | goto out; |
| 2424 | } |
| 2425 | |
| 2426 | ret = dpcm_be_dai_prepare(fe, substream->stream); |
| 2427 | if (ret < 0) |
| 2428 | goto out; |
| 2429 | |
| 2430 | /* call prepare on the frontend */ |
| 2431 | ret = soc_pcm_prepare(substream); |
| 2432 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2433 | dev_err(fe->dev,"ASoC: prepare FE %s failed\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2434 | fe->dai_link->name); |
| 2435 | goto out; |
| 2436 | } |
| 2437 | |
| 2438 | /* run the stream event for each BE */ |
| 2439 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 2440 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 2441 | |
| 2442 | out: |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2443 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2444 | mutex_unlock(&fe->card->mutex); |
| 2445 | |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2449 | static int soc_pcm_ioctl(struct snd_pcm_substream *substream, |
| 2450 | unsigned int cmd, void *arg) |
| 2451 | { |
| 2452 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2453 | struct snd_soc_platform *platform = rtd->platform; |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 2454 | struct snd_soc_component *component; |
| 2455 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2456 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 2457 | if (platform && platform->driver->ops && platform->driver->ops->ioctl) |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2458 | return platform->driver->ops->ioctl(substream, cmd, arg); |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 2459 | |
| 2460 | for_each_rtdcom(rtd, rtdcom) { |
| 2461 | component = rtdcom->component; |
| 2462 | |
| 2463 | /* ignore duplication for now */ |
| 2464 | if (platform && (component == &platform->component)) |
| 2465 | continue; |
| 2466 | |
| 2467 | if (!component->driver->ops || |
| 2468 | !component->driver->ops->ioctl) |
| 2469 | continue; |
| 2470 | |
| 2471 | /* FIXME: use 1st ioctl */ |
| 2472 | return component->driver->ops->ioctl(substream, cmd, arg); |
| 2473 | } |
| 2474 | |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 2475 | return snd_pcm_lib_ioctl(substream, cmd, arg); |
| 2476 | } |
| 2477 | |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2478 | static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
| 2479 | { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2480 | struct snd_pcm_substream *substream = |
| 2481 | snd_soc_dpcm_get_substream(fe, stream); |
| 2482 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2483 | int err; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2484 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2485 | dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2486 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2487 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2488 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2489 | /* call bespoke trigger - FE takes care of all BE triggers */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2490 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2491 | fe->dai_link->name); |
| 2492 | |
| 2493 | err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
| 2494 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2495 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2496 | } else { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2497 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2498 | fe->dai_link->name); |
| 2499 | |
| 2500 | err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); |
| 2501 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2502 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2503 | } |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2504 | |
| 2505 | err = dpcm_be_dai_hw_free(fe, stream); |
| 2506 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2507 | dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2508 | |
| 2509 | err = dpcm_be_dai_shutdown(fe, stream); |
| 2510 | if (err < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2511 | dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2512 | |
| 2513 | /* run the stream event for each BE */ |
| 2514 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2515 | |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
| 2519 | static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) |
| 2520 | { |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2521 | struct snd_pcm_substream *substream = |
| 2522 | snd_soc_dpcm_get_substream(fe, stream); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2523 | struct snd_soc_dpcm *dpcm; |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2524 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2525 | int ret; |
| 2526 | |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2527 | dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2528 | stream ? "capture" : "playback", fe->dai_link->name); |
| 2529 | |
| 2530 | /* Only start the BE if the FE is ready */ |
| 2531 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || |
| 2532 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) |
| 2533 | return -EINVAL; |
| 2534 | |
| 2535 | /* startup must always be called for new BEs */ |
| 2536 | ret = dpcm_be_dai_startup(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2537 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2538 | goto disconnect; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2539 | |
| 2540 | /* keep going if FE state is > open */ |
| 2541 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) |
| 2542 | return 0; |
| 2543 | |
| 2544 | ret = dpcm_be_dai_hw_params(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2545 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2546 | goto close; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2547 | |
| 2548 | /* keep going if FE state is > hw_params */ |
| 2549 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) |
| 2550 | return 0; |
| 2551 | |
| 2552 | |
| 2553 | ret = dpcm_be_dai_prepare(fe, stream); |
Dan Carpenter | fffc0ca | 2013-01-10 11:59:57 +0300 | [diff] [blame] | 2554 | if (ret < 0) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2555 | goto hw_free; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2556 | |
| 2557 | /* run the stream event for each BE */ |
| 2558 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); |
| 2559 | |
| 2560 | /* keep going if FE state is > prepare */ |
| 2561 | if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || |
| 2562 | fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) |
| 2563 | return 0; |
| 2564 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2565 | if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { |
| 2566 | /* call trigger on the frontend - FE takes care of all BE triggers */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2567 | dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2568 | fe->dai_link->name); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2569 | |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2570 | ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); |
| 2571 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2572 | dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2573 | goto hw_free; |
| 2574 | } |
| 2575 | } else { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2576 | dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2577 | fe->dai_link->name); |
| 2578 | |
| 2579 | ret = dpcm_be_dai_trigger(fe, stream, |
| 2580 | SNDRV_PCM_TRIGGER_START); |
| 2581 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2582 | dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); |
Liam Girdwood | 07bf84a | 2012-04-25 12:12:52 +0100 | [diff] [blame] | 2583 | goto hw_free; |
| 2584 | } |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | return 0; |
| 2588 | |
| 2589 | hw_free: |
| 2590 | dpcm_be_dai_hw_free(fe, stream); |
| 2591 | close: |
| 2592 | dpcm_be_dai_shutdown(fe, stream); |
| 2593 | disconnect: |
| 2594 | /* disconnect any non started BEs */ |
| 2595 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 2596 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 2597 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 2598 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2599 | } |
| 2600 | |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
| 2604 | static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2605 | { |
| 2606 | int ret; |
| 2607 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2608 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2609 | ret = dpcm_run_update_startup(fe, stream); |
| 2610 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2611 | dev_err(fe->dev, "ASoC: failed to startup some BEs\n"); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2612 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2613 | |
| 2614 | return ret; |
| 2615 | } |
| 2616 | |
| 2617 | static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 2618 | { |
| 2619 | int ret; |
| 2620 | |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2621 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2622 | ret = dpcm_run_update_shutdown(fe, stream); |
| 2623 | if (ret < 0) |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2624 | dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); |
Takashi Iwai | ea9d0d7 | 2014-11-04 16:52:28 +0100 | [diff] [blame] | 2625 | dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2626 | |
| 2627 | return ret; |
| 2628 | } |
| 2629 | |
| 2630 | /* Called by DAPM mixer/mux changes to update audio routing between PCMs and |
| 2631 | * any DAI links. |
| 2632 | */ |
Lars-Peter Clausen | c3f48ae | 2013-07-24 15:27:36 +0200 | [diff] [blame] | 2633 | int soc_dpcm_runtime_update(struct snd_soc_card *card) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2634 | { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 2635 | struct snd_soc_pcm_runtime *fe; |
| 2636 | int old, new, paths; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2637 | |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2638 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 2639 | list_for_each_entry(fe, &card->rtd_list, list) { |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2640 | struct snd_soc_dapm_widget_list *list; |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* make sure link is FE */ |
| 2643 | if (!fe->dai_link->dynamic) |
| 2644 | continue; |
| 2645 | |
| 2646 | /* only check active links */ |
| 2647 | if (!fe->cpu_dai->active) |
| 2648 | continue; |
| 2649 | |
| 2650 | /* DAPM sync will call this to update DSP paths */ |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2651 | dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2652 | fe->dai_link->name); |
| 2653 | |
| 2654 | /* skip if FE doesn't have playback capability */ |
Qiao Zhou | 075207d | 2014-11-17 16:02:57 +0800 | [diff] [blame] | 2655 | if (!fe->cpu_dai->driver->playback.channels_min |
| 2656 | || !fe->codec_dai->driver->playback.channels_min) |
| 2657 | goto capture; |
| 2658 | |
| 2659 | /* skip if FE isn't currently playing */ |
| 2660 | if (!fe->cpu_dai->playback_active |
| 2661 | || !fe->codec_dai->playback_active) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2662 | goto capture; |
| 2663 | |
| 2664 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list); |
| 2665 | if (paths < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2666 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2667 | fe->dai_link->name, "playback"); |
| 2668 | mutex_unlock(&card->mutex); |
| 2669 | return paths; |
| 2670 | } |
| 2671 | |
| 2672 | /* update any new playback paths */ |
| 2673 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1); |
| 2674 | if (new) { |
| 2675 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2676 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2677 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2678 | } |
| 2679 | |
| 2680 | /* update any old playback paths */ |
| 2681 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0); |
| 2682 | if (old) { |
| 2683 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2684 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2685 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK); |
| 2686 | } |
| 2687 | |
Qiao Zhou | 7ed9de7 | 2014-06-04 19:42:06 +0800 | [diff] [blame] | 2688 | dpcm_path_put(&list); |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2689 | capture: |
| 2690 | /* skip if FE doesn't have capture capability */ |
Qiao Zhou | 075207d | 2014-11-17 16:02:57 +0800 | [diff] [blame] | 2691 | if (!fe->cpu_dai->driver->capture.channels_min |
| 2692 | || !fe->codec_dai->driver->capture.channels_min) |
| 2693 | continue; |
| 2694 | |
| 2695 | /* skip if FE isn't currently capturing */ |
| 2696 | if (!fe->cpu_dai->capture_active |
| 2697 | || !fe->codec_dai->capture_active) |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2698 | continue; |
| 2699 | |
| 2700 | paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list); |
| 2701 | if (paths < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2702 | dev_warn(fe->dev, "ASoC: %s no valid %s path\n", |
Liam Girdwood | 618dae1 | 2012-04-25 12:12:51 +0100 | [diff] [blame] | 2703 | fe->dai_link->name, "capture"); |
| 2704 | mutex_unlock(&card->mutex); |
| 2705 | return paths; |
| 2706 | } |
| 2707 | |
| 2708 | /* update any new capture paths */ |
| 2709 | new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1); |
| 2710 | if (new) { |
| 2711 | dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2712 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2713 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2714 | } |
| 2715 | |
| 2716 | /* update any old capture paths */ |
| 2717 | old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0); |
| 2718 | if (old) { |
| 2719 | dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2720 | dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2721 | dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE); |
| 2722 | } |
| 2723 | |
| 2724 | dpcm_path_put(&list); |
| 2725 | } |
| 2726 | |
| 2727 | mutex_unlock(&card->mutex); |
| 2728 | return 0; |
| 2729 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2730 | int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) |
| 2731 | { |
| 2732 | struct snd_soc_dpcm *dpcm; |
| 2733 | struct list_head *clients = |
| 2734 | &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients; |
| 2735 | |
| 2736 | list_for_each_entry(dpcm, clients, list_be) { |
| 2737 | |
| 2738 | struct snd_soc_pcm_runtime *be = dpcm->be; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2739 | int i; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2740 | |
| 2741 | if (be->dai_link->ignore_suspend) |
| 2742 | continue; |
| 2743 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2744 | for (i = 0; i < be->num_codecs; i++) { |
| 2745 | struct snd_soc_dai *dai = be->codec_dais[i]; |
| 2746 | struct snd_soc_dai_driver *drv = dai->driver; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2747 | |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2748 | dev_dbg(be->dev, "ASoC: BE digital mute %s\n", |
| 2749 | be->dai_link->name); |
| 2750 | |
| 2751 | if (drv->ops && drv->ops->digital_mute && |
| 2752 | dai->playback_active) |
| 2753 | drv->ops->digital_mute(dai, mute); |
| 2754 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | return 0; |
| 2758 | } |
| 2759 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2760 | static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2761 | { |
| 2762 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 2763 | struct snd_soc_dpcm *dpcm; |
| 2764 | struct snd_soc_dapm_widget_list *list; |
| 2765 | int ret; |
| 2766 | int stream = fe_substream->stream; |
| 2767 | |
| 2768 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2769 | fe->dpcm[stream].runtime = fe_substream->runtime; |
| 2770 | |
Qiao Zhou | 8f70e51 | 2014-09-10 17:54:07 +0800 | [diff] [blame] | 2771 | ret = dpcm_path_get(fe, stream, &list); |
| 2772 | if (ret < 0) { |
| 2773 | mutex_unlock(&fe->card->mutex); |
| 2774 | return ret; |
| 2775 | } else if (ret == 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 2776 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2777 | fe->dai_link->name, stream ? "capture" : "playback"); |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* calculate valid and active FE <-> BE dpcms */ |
| 2781 | dpcm_process_paths(fe, stream, &list, 1); |
| 2782 | |
| 2783 | ret = dpcm_fe_dai_startup(fe_substream); |
| 2784 | if (ret < 0) { |
| 2785 | /* clean up all links */ |
| 2786 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 2787 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2788 | |
| 2789 | dpcm_be_disconnect(fe, stream); |
| 2790 | fe->dpcm[stream].runtime = NULL; |
| 2791 | } |
| 2792 | |
| 2793 | dpcm_clear_pending_state(fe, stream); |
| 2794 | dpcm_path_put(&list); |
| 2795 | mutex_unlock(&fe->card->mutex); |
| 2796 | return ret; |
| 2797 | } |
| 2798 | |
Mark Brown | 45c0a18 | 2012-05-09 21:46:27 +0100 | [diff] [blame] | 2799 | static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2800 | { |
| 2801 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 2802 | struct snd_soc_dpcm *dpcm; |
| 2803 | int stream = fe_substream->stream, ret; |
| 2804 | |
| 2805 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 2806 | ret = dpcm_fe_dai_shutdown(fe_substream); |
| 2807 | |
| 2808 | /* mark FE's links ready to prune */ |
| 2809 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 2810 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 2811 | |
| 2812 | dpcm_be_disconnect(fe, stream); |
| 2813 | |
| 2814 | fe->dpcm[stream].runtime = NULL; |
| 2815 | mutex_unlock(&fe->card->mutex); |
| 2816 | return ret; |
| 2817 | } |
| 2818 | |
Takashi Iwai | 5d61f0b | 2017-08-25 12:04:07 +0200 | [diff] [blame] | 2819 | static void soc_pcm_private_free(struct snd_pcm *pcm) |
| 2820 | { |
| 2821 | struct snd_soc_pcm_runtime *rtd = pcm->private_data; |
Kuninori Morimoto | f523ace | 2017-09-26 01:00:53 +0000 | [diff] [blame] | 2822 | struct snd_soc_rtdcom_list *rtdcom; |
| 2823 | struct snd_soc_component *component; |
Takashi Iwai | 5d61f0b | 2017-08-25 12:04:07 +0200 | [diff] [blame] | 2824 | |
Kuninori Morimoto | f523ace | 2017-09-26 01:00:53 +0000 | [diff] [blame] | 2825 | for_each_rtdcom(rtd, rtdcom) { |
| 2826 | /* need to sync the delayed work before releasing resources */ |
| 2827 | |
| 2828 | flush_delayed_work(&rtd->delayed_work); |
| 2829 | component = rtdcom->component; |
| 2830 | |
| 2831 | if (component->pcm_free) |
| 2832 | component->pcm_free(component, pcm); |
| 2833 | } |
Takashi Iwai | 5d61f0b | 2017-08-25 12:04:07 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 2836 | static int soc_rtdcom_ack(struct snd_pcm_substream *substream) |
| 2837 | { |
| 2838 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2839 | struct snd_soc_rtdcom_list *rtdcom; |
| 2840 | struct snd_soc_component *component; |
| 2841 | |
| 2842 | for_each_rtdcom(rtd, rtdcom) { |
| 2843 | component = rtdcom->component; |
| 2844 | |
| 2845 | if (!component->driver->ops || |
| 2846 | !component->driver->ops->ack) |
| 2847 | continue; |
| 2848 | |
| 2849 | /* FIXME. it returns 1st ask now */ |
| 2850 | return component->driver->ops->ack(substream); |
| 2851 | } |
| 2852 | |
| 2853 | return -EINVAL; |
| 2854 | } |
| 2855 | |
| 2856 | static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel, |
| 2857 | unsigned long pos, void __user *buf, |
| 2858 | unsigned long bytes) |
| 2859 | { |
| 2860 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2861 | struct snd_soc_rtdcom_list *rtdcom; |
| 2862 | struct snd_soc_component *component; |
| 2863 | |
| 2864 | for_each_rtdcom(rtd, rtdcom) { |
| 2865 | component = rtdcom->component; |
| 2866 | |
| 2867 | if (!component->driver->ops || |
| 2868 | !component->driver->ops->copy_user) |
| 2869 | continue; |
| 2870 | |
| 2871 | /* FIXME. it returns 1st copy now */ |
| 2872 | return component->driver->ops->copy_user(substream, channel, |
| 2873 | pos, buf, bytes); |
| 2874 | } |
| 2875 | |
| 2876 | return -EINVAL; |
| 2877 | } |
| 2878 | |
| 2879 | static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel, |
| 2880 | unsigned long pos, void *buf, unsigned long bytes) |
| 2881 | { |
| 2882 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2883 | struct snd_soc_rtdcom_list *rtdcom; |
| 2884 | struct snd_soc_component *component; |
| 2885 | |
| 2886 | for_each_rtdcom(rtd, rtdcom) { |
| 2887 | component = rtdcom->component; |
| 2888 | |
| 2889 | if (!component->driver->ops || |
| 2890 | !component->driver->ops->copy_kernel) |
| 2891 | continue; |
| 2892 | |
| 2893 | /* FIXME. it returns 1st copy now */ |
| 2894 | return component->driver->ops->copy_kernel(substream, channel, |
| 2895 | pos, buf, bytes); |
| 2896 | } |
| 2897 | |
| 2898 | return -EINVAL; |
| 2899 | } |
| 2900 | |
| 2901 | static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel, |
| 2902 | unsigned long pos, unsigned long bytes) |
| 2903 | { |
| 2904 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2905 | struct snd_soc_rtdcom_list *rtdcom; |
| 2906 | struct snd_soc_component *component; |
| 2907 | |
| 2908 | for_each_rtdcom(rtd, rtdcom) { |
| 2909 | component = rtdcom->component; |
| 2910 | |
| 2911 | if (!component->driver->ops || |
| 2912 | !component->driver->ops->fill_silence) |
| 2913 | continue; |
| 2914 | |
| 2915 | /* FIXME. it returns 1st silence now */ |
| 2916 | return component->driver->ops->fill_silence(substream, channel, |
| 2917 | pos, bytes); |
| 2918 | } |
| 2919 | |
| 2920 | return -EINVAL; |
| 2921 | } |
| 2922 | |
| 2923 | static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream, |
| 2924 | unsigned long offset) |
| 2925 | { |
| 2926 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2927 | struct snd_soc_rtdcom_list *rtdcom; |
| 2928 | struct snd_soc_component *component; |
| 2929 | struct page *page; |
| 2930 | |
| 2931 | for_each_rtdcom(rtd, rtdcom) { |
| 2932 | component = rtdcom->component; |
| 2933 | |
| 2934 | if (!component->driver->ops || |
| 2935 | !component->driver->ops->page) |
| 2936 | continue; |
| 2937 | |
| 2938 | /* FIXME. it returns 1st page now */ |
| 2939 | page = component->driver->ops->page(substream, offset); |
| 2940 | if (page) |
| 2941 | return page; |
| 2942 | } |
| 2943 | |
| 2944 | return NULL; |
| 2945 | } |
| 2946 | |
| 2947 | static int soc_rtdcom_mmap(struct snd_pcm_substream *substream, |
| 2948 | struct vm_area_struct *vma) |
| 2949 | { |
| 2950 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 2951 | struct snd_soc_rtdcom_list *rtdcom; |
| 2952 | struct snd_soc_component *component; |
| 2953 | |
| 2954 | for_each_rtdcom(rtd, rtdcom) { |
| 2955 | component = rtdcom->component; |
| 2956 | |
| 2957 | if (!component->driver->ops || |
| 2958 | !component->driver->ops->mmap) |
| 2959 | continue; |
| 2960 | |
| 2961 | /* FIXME. it returns 1st mmap now */ |
| 2962 | return component->driver->ops->mmap(substream, vma); |
| 2963 | } |
| 2964 | |
| 2965 | return -EINVAL; |
| 2966 | } |
| 2967 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2968 | /* create a new pcm */ |
| 2969 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) |
| 2970 | { |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2971 | struct snd_soc_platform *platform = rtd->platform; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2972 | struct snd_soc_dai *codec_dai; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2973 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | f523ace | 2017-09-26 01:00:53 +0000 | [diff] [blame] | 2974 | struct snd_soc_component *component; |
| 2975 | struct snd_soc_rtdcom_list *rtdcom; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2976 | struct snd_pcm *pcm; |
| 2977 | char new_name[64]; |
| 2978 | int ret = 0, playback = 0, capture = 0; |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2979 | int i; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 2980 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2981 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
Liam Girdwood | 1e9de42 | 2014-01-07 17:51:42 +0000 | [diff] [blame] | 2982 | playback = rtd->dai_link->dpcm_playback; |
| 2983 | capture = rtd->dai_link->dpcm_capture; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2984 | } else { |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 2985 | for (i = 0; i < rtd->num_codecs; i++) { |
| 2986 | codec_dai = rtd->codec_dais[i]; |
| 2987 | if (codec_dai->driver->playback.channels_min) |
| 2988 | playback = 1; |
| 2989 | if (codec_dai->driver->capture.channels_min) |
| 2990 | capture = 1; |
| 2991 | } |
| 2992 | |
| 2993 | capture = capture && cpu_dai->driver->capture.channels_min; |
| 2994 | playback = playback && cpu_dai->driver->playback.channels_min; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 2995 | } |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 2996 | |
Fabio Estevam | d6bead0 | 2013-08-29 10:32:13 -0300 | [diff] [blame] | 2997 | if (rtd->dai_link->playback_only) { |
| 2998 | playback = 1; |
| 2999 | capture = 0; |
| 3000 | } |
| 3001 | |
| 3002 | if (rtd->dai_link->capture_only) { |
| 3003 | playback = 0; |
| 3004 | capture = 1; |
| 3005 | } |
| 3006 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3007 | /* create the PCM */ |
| 3008 | if (rtd->dai_link->no_pcm) { |
| 3009 | snprintf(new_name, sizeof(new_name), "(%s)", |
| 3010 | rtd->dai_link->stream_name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3011 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3012 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 3013 | playback, capture, &pcm); |
| 3014 | } else { |
| 3015 | if (rtd->dai_link->dynamic) |
| 3016 | snprintf(new_name, sizeof(new_name), "%s (*)", |
| 3017 | rtd->dai_link->stream_name); |
| 3018 | else |
| 3019 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 3020 | rtd->dai_link->stream_name, |
| 3021 | (rtd->num_codecs > 1) ? |
| 3022 | "multicodec" : rtd->codec_dai->name, num); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3023 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3024 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, |
| 3025 | capture, &pcm); |
| 3026 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3027 | if (ret < 0) { |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 3028 | 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] | 3029 | rtd->dai_link->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3030 | return ret; |
| 3031 | } |
Liam Girdwood | 103d84a | 2012-11-19 14:39:15 +0000 | [diff] [blame] | 3032 | 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] | 3033 | |
| 3034 | /* DAPM dai link stream work */ |
| 3035 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
| 3036 | |
Vinod Koul | 48c7699 | 2015-02-12 09:59:53 +0530 | [diff] [blame] | 3037 | pcm->nonatomic = rtd->dai_link->nonatomic; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3038 | rtd->pcm = pcm; |
| 3039 | pcm->private_data = rtd; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3040 | |
| 3041 | if (rtd->dai_link->no_pcm) { |
| 3042 | if (playback) |
| 3043 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; |
| 3044 | if (capture) |
| 3045 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; |
| 3046 | goto out; |
| 3047 | } |
| 3048 | |
| 3049 | /* ASoC PCM operations */ |
| 3050 | if (rtd->dai_link->dynamic) { |
| 3051 | rtd->ops.open = dpcm_fe_dai_open; |
| 3052 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; |
| 3053 | rtd->ops.prepare = dpcm_fe_dai_prepare; |
| 3054 | rtd->ops.trigger = dpcm_fe_dai_trigger; |
| 3055 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; |
| 3056 | rtd->ops.close = dpcm_fe_dai_close; |
| 3057 | rtd->ops.pointer = soc_pcm_pointer; |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 3058 | rtd->ops.ioctl = soc_pcm_ioctl; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3059 | } else { |
| 3060 | rtd->ops.open = soc_pcm_open; |
| 3061 | rtd->ops.hw_params = soc_pcm_hw_params; |
| 3062 | rtd->ops.prepare = soc_pcm_prepare; |
| 3063 | rtd->ops.trigger = soc_pcm_trigger; |
| 3064 | rtd->ops.hw_free = soc_pcm_hw_free; |
| 3065 | rtd->ops.close = soc_pcm_close; |
| 3066 | rtd->ops.pointer = soc_pcm_pointer; |
Liam Girdwood | be3f3f2 | 2012-04-26 16:16:10 +0100 | [diff] [blame] | 3067 | rtd->ops.ioctl = soc_pcm_ioctl; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3068 | } |
| 3069 | |
Kuninori Morimoto | b813586 | 2017-10-11 01:37:23 +0000 | [diff] [blame] | 3070 | for_each_rtdcom(rtd, rtdcom) { |
| 3071 | const struct snd_pcm_ops *ops = rtdcom->component->driver->ops; |
| 3072 | |
| 3073 | if (!ops) |
| 3074 | continue; |
| 3075 | |
| 3076 | if (ops->ack) |
| 3077 | rtd->ops.ack = soc_rtdcom_ack; |
| 3078 | if (ops->copy_user) |
| 3079 | rtd->ops.copy_user = soc_rtdcom_copy_user; |
| 3080 | if (ops->copy_kernel) |
| 3081 | rtd->ops.copy_kernel = soc_rtdcom_copy_kernel; |
| 3082 | if (ops->fill_silence) |
| 3083 | rtd->ops.fill_silence = soc_rtdcom_fill_silence; |
| 3084 | if (ops->page) |
| 3085 | rtd->ops.page = soc_rtdcom_page; |
| 3086 | if (ops->mmap) |
| 3087 | rtd->ops.mmap = soc_rtdcom_mmap; |
| 3088 | } |
| 3089 | |
| 3090 | /* overwrite */ |
| 3091 | if (platform && platform->driver->ops) { |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3092 | rtd->ops.ack = platform->driver->ops->ack; |
Takashi Iwai | 29d1a87 | 2017-05-10 20:02:35 +0200 | [diff] [blame] | 3093 | rtd->ops.copy_user = platform->driver->ops->copy_user; |
| 3094 | rtd->ops.copy_kernel = platform->driver->ops->copy_kernel; |
| 3095 | rtd->ops.fill_silence = platform->driver->ops->fill_silence; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3096 | rtd->ops.page = platform->driver->ops->page; |
| 3097 | rtd->ops.mmap = platform->driver->ops->mmap; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | if (playback) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3101 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3102 | |
| 3103 | if (capture) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3104 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3105 | |
Kuninori Morimoto | f523ace | 2017-09-26 01:00:53 +0000 | [diff] [blame] | 3106 | for_each_rtdcom(rtd, rtdcom) { |
| 3107 | component = rtdcom->component; |
| 3108 | |
| 3109 | if (!component->pcm_new) |
| 3110 | continue; |
| 3111 | |
| 3112 | ret = component->pcm_new(component, rtd); |
Johan Hovold | c641e5b | 2017-07-12 17:55:29 +0200 | [diff] [blame] | 3113 | if (ret < 0) { |
Kuninori Morimoto | f523ace | 2017-09-26 01:00:53 +0000 | [diff] [blame] | 3114 | dev_err(component->dev, |
Johan Hovold | c641e5b | 2017-07-12 17:55:29 +0200 | [diff] [blame] | 3115 | "ASoC: pcm constructor failed: %d\n", |
| 3116 | ret); |
| 3117 | return ret; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3118 | } |
| 3119 | } |
Johan Hovold | c641e5b | 2017-07-12 17:55:29 +0200 | [diff] [blame] | 3120 | |
Takashi Iwai | 5d61f0b | 2017-08-25 12:04:07 +0200 | [diff] [blame] | 3121 | pcm->private_free = soc_pcm_private_free; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3122 | out: |
Benoit Cousson | 2e5894d | 2014-07-08 23:19:35 +0200 | [diff] [blame] | 3123 | dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", |
| 3124 | (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, |
| 3125 | cpu_dai->name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 3126 | return ret; |
| 3127 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 3128 | |
| 3129 | /* is the current PCM operation for this FE ? */ |
| 3130 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 3131 | { |
| 3132 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) |
| 3133 | return 1; |
| 3134 | return 0; |
| 3135 | } |
| 3136 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); |
| 3137 | |
| 3138 | /* is the current PCM operation for this BE ? */ |
| 3139 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, |
| 3140 | struct snd_soc_pcm_runtime *be, int stream) |
| 3141 | { |
| 3142 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || |
| 3143 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && |
| 3144 | be->dpcm[stream].runtime_update)) |
| 3145 | return 1; |
| 3146 | return 0; |
| 3147 | } |
| 3148 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); |
| 3149 | |
| 3150 | /* get the substream for this BE */ |
| 3151 | struct snd_pcm_substream * |
| 3152 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) |
| 3153 | { |
| 3154 | return be->pcm->streams[stream].substream; |
| 3155 | } |
| 3156 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); |
| 3157 | |
| 3158 | /* get the BE runtime state */ |
| 3159 | enum snd_soc_dpcm_state |
| 3160 | snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) |
| 3161 | { |
| 3162 | return be->dpcm[stream].state; |
| 3163 | } |
| 3164 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); |
| 3165 | |
| 3166 | /* set the BE runtime state */ |
| 3167 | void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, |
| 3168 | int stream, enum snd_soc_dpcm_state state) |
| 3169 | { |
| 3170 | be->dpcm[stream].state = state; |
| 3171 | } |
| 3172 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); |
| 3173 | |
| 3174 | /* |
| 3175 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE |
| 3176 | * are not running, paused or suspended for the specified stream direction. |
| 3177 | */ |
| 3178 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, |
| 3179 | struct snd_soc_pcm_runtime *be, int stream) |
| 3180 | { |
| 3181 | struct snd_soc_dpcm *dpcm; |
| 3182 | int state; |
| 3183 | |
| 3184 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 3185 | |
| 3186 | if (dpcm->fe == fe) |
| 3187 | continue; |
| 3188 | |
| 3189 | state = dpcm->fe->dpcm[stream].state; |
| 3190 | if (state == SND_SOC_DPCM_STATE_START || |
| 3191 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 3192 | state == SND_SOC_DPCM_STATE_SUSPEND) |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | /* it's safe to free/stop this BE DAI */ |
| 3197 | return 1; |
| 3198 | } |
| 3199 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); |
| 3200 | |
| 3201 | /* |
| 3202 | * We can only change hw params a BE DAI if any of it's FE are not prepared, |
| 3203 | * running, paused or suspended for the specified stream direction. |
| 3204 | */ |
| 3205 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, |
| 3206 | struct snd_soc_pcm_runtime *be, int stream) |
| 3207 | { |
| 3208 | struct snd_soc_dpcm *dpcm; |
| 3209 | int state; |
| 3210 | |
| 3211 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 3212 | |
| 3213 | if (dpcm->fe == fe) |
| 3214 | continue; |
| 3215 | |
| 3216 | state = dpcm->fe->dpcm[stream].state; |
| 3217 | if (state == SND_SOC_DPCM_STATE_START || |
| 3218 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 3219 | state == SND_SOC_DPCM_STATE_SUSPEND || |
| 3220 | state == SND_SOC_DPCM_STATE_PREPARE) |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
| 3224 | /* it's safe to change hw_params */ |
| 3225 | return 1; |
| 3226 | } |
| 3227 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3228 | |
| 3229 | #ifdef CONFIG_DEBUG_FS |
Lars-Peter Clausen | 85280141 | 2016-11-22 11:29:14 +0100 | [diff] [blame] | 3230 | static const char *dpcm_state_string(enum snd_soc_dpcm_state state) |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3231 | { |
| 3232 | switch (state) { |
| 3233 | case SND_SOC_DPCM_STATE_NEW: |
| 3234 | return "new"; |
| 3235 | case SND_SOC_DPCM_STATE_OPEN: |
| 3236 | return "open"; |
| 3237 | case SND_SOC_DPCM_STATE_HW_PARAMS: |
| 3238 | return "hw_params"; |
| 3239 | case SND_SOC_DPCM_STATE_PREPARE: |
| 3240 | return "prepare"; |
| 3241 | case SND_SOC_DPCM_STATE_START: |
| 3242 | return "start"; |
| 3243 | case SND_SOC_DPCM_STATE_STOP: |
| 3244 | return "stop"; |
| 3245 | case SND_SOC_DPCM_STATE_SUSPEND: |
| 3246 | return "suspend"; |
| 3247 | case SND_SOC_DPCM_STATE_PAUSED: |
| 3248 | return "paused"; |
| 3249 | case SND_SOC_DPCM_STATE_HW_FREE: |
| 3250 | return "hw_free"; |
| 3251 | case SND_SOC_DPCM_STATE_CLOSE: |
| 3252 | return "close"; |
| 3253 | } |
| 3254 | |
| 3255 | return "unknown"; |
| 3256 | } |
| 3257 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3258 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
| 3259 | int stream, char *buf, size_t size) |
| 3260 | { |
| 3261 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; |
| 3262 | struct snd_soc_dpcm *dpcm; |
| 3263 | ssize_t offset = 0; |
| 3264 | |
| 3265 | /* FE state */ |
| 3266 | offset += snprintf(buf + offset, size - offset, |
| 3267 | "[%s - %s]\n", fe->dai_link->name, |
| 3268 | stream ? "Capture" : "Playback"); |
| 3269 | |
| 3270 | offset += snprintf(buf + offset, size - offset, "State: %s\n", |
| 3271 | dpcm_state_string(fe->dpcm[stream].state)); |
| 3272 | |
| 3273 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 3274 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 3275 | offset += snprintf(buf + offset, size - offset, |
| 3276 | "Hardware Params: " |
| 3277 | "Format = %s, Channels = %d, Rate = %d\n", |
| 3278 | snd_pcm_format_name(params_format(params)), |
| 3279 | params_channels(params), |
| 3280 | params_rate(params)); |
| 3281 | |
| 3282 | /* BEs state */ |
| 3283 | offset += snprintf(buf + offset, size - offset, "Backends:\n"); |
| 3284 | |
| 3285 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 3286 | offset += snprintf(buf + offset, size - offset, |
| 3287 | " No active DSP links\n"); |
| 3288 | goto out; |
| 3289 | } |
| 3290 | |
| 3291 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 3292 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 3293 | params = &dpcm->hw_params; |
| 3294 | |
| 3295 | offset += snprintf(buf + offset, size - offset, |
| 3296 | "- %s\n", be->dai_link->name); |
| 3297 | |
| 3298 | offset += snprintf(buf + offset, size - offset, |
| 3299 | " State: %s\n", |
| 3300 | dpcm_state_string(be->dpcm[stream].state)); |
| 3301 | |
| 3302 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 3303 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 3304 | offset += snprintf(buf + offset, size - offset, |
| 3305 | " Hardware Params: " |
| 3306 | "Format = %s, Channels = %d, Rate = %d\n", |
| 3307 | snd_pcm_format_name(params_format(params)), |
| 3308 | params_channels(params), |
| 3309 | params_rate(params)); |
| 3310 | } |
| 3311 | |
| 3312 | out: |
| 3313 | return offset; |
| 3314 | } |
| 3315 | |
| 3316 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, |
| 3317 | size_t count, loff_t *ppos) |
| 3318 | { |
| 3319 | struct snd_soc_pcm_runtime *fe = file->private_data; |
| 3320 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; |
| 3321 | char *buf; |
| 3322 | |
| 3323 | buf = kmalloc(out_count, GFP_KERNEL); |
| 3324 | if (!buf) |
| 3325 | return -ENOMEM; |
| 3326 | |
| 3327 | if (fe->cpu_dai->driver->playback.channels_min) |
| 3328 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, |
| 3329 | buf + offset, out_count - offset); |
| 3330 | |
| 3331 | if (fe->cpu_dai->driver->capture.channels_min) |
| 3332 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, |
| 3333 | buf + offset, out_count - offset); |
| 3334 | |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 3335 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3336 | |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 3337 | kfree(buf); |
| 3338 | return ret; |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | static const struct file_operations dpcm_state_fops = { |
Liam Girdwood | f57b848 | 2012-04-27 11:33:46 +0100 | [diff] [blame] | 3342 | .open = simple_open, |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3343 | .read = dpcm_state_read_file, |
| 3344 | .llseek = default_llseek, |
| 3345 | }; |
| 3346 | |
Lars-Peter Clausen | 2e55b90 | 2015-04-09 10:52:37 +0200 | [diff] [blame] | 3347 | void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3348 | { |
Mark Brown | b3bba9a | 2012-05-08 10:33:47 +0100 | [diff] [blame] | 3349 | if (!rtd->dai_link) |
Lars-Peter Clausen | 2e55b90 | 2015-04-09 10:52:37 +0200 | [diff] [blame] | 3350 | return; |
Mark Brown | b3bba9a | 2012-05-08 10:33:47 +0100 | [diff] [blame] | 3351 | |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 3352 | if (!rtd->card->debugfs_card_root) |
| 3353 | return; |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3354 | |
| 3355 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
| 3356 | rtd->card->debugfs_card_root); |
| 3357 | if (!rtd->debugfs_dpcm_root) { |
| 3358 | dev_dbg(rtd->dev, |
| 3359 | "ASoC: Failed to create dpcm debugfs directory %s\n", |
| 3360 | rtd->dai_link->name); |
Lars-Peter Clausen | 2e55b90 | 2015-04-09 10:52:37 +0200 | [diff] [blame] | 3361 | return; |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3362 | } |
| 3363 | |
Fabio Estevam | f1e3f40 | 2017-07-29 11:40:55 -0300 | [diff] [blame] | 3364 | debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, |
| 3365 | rtd, &dpcm_state_fops); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame] | 3366 | } |
| 3367 | #endif |