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