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