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> |
| 10 | * Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 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> |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/workqueue.h> |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 25 | #include <linux/export.h> |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame^] | 26 | #include <linux/debugfs.h> |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/soc.h> |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 31 | #include <sound/soc-dpcm.h> |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 32 | #include <sound/initval.h> |
| 33 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 34 | #define DPCM_MAX_BE_USERS 8 |
| 35 | |
| 36 | /* DPCM stream event, send event to FE and all active BEs. */ |
| 37 | static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, |
| 38 | int event) |
| 39 | { |
| 40 | struct snd_soc_dpcm *dpcm; |
| 41 | |
| 42 | list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) { |
| 43 | |
| 44 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 45 | |
| 46 | dev_dbg(be->dev, "pm: BE %s event %d dir %d\n", |
| 47 | be->dai_link->name, event, dir); |
| 48 | |
| 49 | snd_soc_dapm_stream_event(be, dir, event); |
| 50 | } |
| 51 | |
| 52 | snd_soc_dapm_stream_event(fe, dir, event); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 57 | static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, |
| 58 | struct snd_soc_dai *soc_dai) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 59 | { |
| 60 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 61 | int ret; |
| 62 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 63 | if (!soc_dai->driver->symmetric_rates && |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 64 | !rtd->dai_link->symmetric_rates) |
| 65 | return 0; |
| 66 | |
| 67 | /* This can happen if multiple streams are starting simultaneously - |
| 68 | * the second can need to get its constraints before the first has |
| 69 | * picked a rate. Complain and allow the application to carry on. |
| 70 | */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 71 | if (!soc_dai->rate) { |
| 72 | dev_warn(soc_dai->dev, |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 73 | "Not enforcing symmetric_rates due to race\n"); |
| 74 | return 0; |
| 75 | } |
| 76 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 77 | dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 78 | |
| 79 | ret = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 80 | SNDRV_PCM_HW_PARAM_RATE, |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 81 | soc_dai->rate, soc_dai->rate); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 82 | if (ret < 0) { |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 83 | dev_err(soc_dai->dev, |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 84 | "Unable to apply rate symmetry constraint: %d\n", ret); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame] | 92 | * List of sample sizes that might go over the bus for parameter |
| 93 | * application. There ought to be a wildcard sample size for things |
| 94 | * like the DAC/ADC resolution to use but there isn't right now. |
| 95 | */ |
| 96 | static int sample_sizes[] = { |
Peter Ujfalusi | 88e3395 | 2012-01-25 10:09:41 +0200 | [diff] [blame] | 97 | 24, 32, |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static void soc_pcm_apply_msb(struct snd_pcm_substream *substream, |
| 101 | struct snd_soc_dai *dai) |
| 102 | { |
| 103 | int ret, i, bits; |
| 104 | |
| 105 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 106 | bits = dai->driver->playback.sig_bits; |
| 107 | else |
| 108 | bits = dai->driver->capture.sig_bits; |
| 109 | |
| 110 | if (!bits) |
| 111 | return; |
| 112 | |
| 113 | for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) { |
Mark Brown | 278047f | 2012-01-19 18:04:18 +0000 | [diff] [blame] | 114 | if (bits >= sample_sizes[i]) |
| 115 | continue; |
| 116 | |
| 117 | ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, |
| 118 | sample_sizes[i], bits); |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame] | 119 | if (ret != 0) |
| 120 | dev_warn(dai->dev, |
| 121 | "Failed to set MSB %d/%d: %d\n", |
| 122 | bits, sample_sizes[i], ret); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 127 | * Called by ALSA when a PCM substream is opened, the runtime->hw record is |
| 128 | * then initialized and any private data can be allocated. This also calls |
| 129 | * startup for the cpu DAI, platform, machine and codec DAI. |
| 130 | */ |
| 131 | static int soc_pcm_open(struct snd_pcm_substream *substream) |
| 132 | { |
| 133 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 134 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 135 | struct snd_soc_platform *platform = rtd->platform; |
| 136 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 137 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 138 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; |
| 139 | struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver; |
| 140 | int ret = 0; |
| 141 | |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 142 | pm_runtime_get_sync(cpu_dai->dev); |
| 143 | pm_runtime_get_sync(codec_dai->dev); |
| 144 | pm_runtime_get_sync(platform->dev); |
| 145 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 146 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 147 | |
| 148 | /* startup the audio subsystem */ |
| 149 | if (cpu_dai->driver->ops->startup) { |
| 150 | ret = cpu_dai->driver->ops->startup(substream, cpu_dai); |
| 151 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 152 | dev_err(cpu_dai->dev, "can't open interface %s: %d\n", |
| 153 | cpu_dai->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 154 | goto out; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (platform->driver->ops && platform->driver->ops->open) { |
| 159 | ret = platform->driver->ops->open(substream); |
| 160 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 161 | dev_err(platform->dev, "can't open platform %s: %d\n", |
| 162 | platform->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 163 | goto platform_err; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (codec_dai->driver->ops->startup) { |
| 168 | ret = codec_dai->driver->ops->startup(substream, codec_dai); |
| 169 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 170 | dev_err(codec_dai->dev, "can't open codec %s: %d\n", |
| 171 | codec_dai->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 172 | goto codec_dai_err; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (rtd->dai_link->ops && rtd->dai_link->ops->startup) { |
| 177 | ret = rtd->dai_link->ops->startup(substream); |
| 178 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 179 | pr_err("asoc: %s startup failed: %d\n", |
| 180 | rtd->dai_link->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 181 | goto machine_err; |
| 182 | } |
| 183 | } |
| 184 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 185 | /* Dynamic PCM DAI links compat checks use dynamic capabilities */ |
| 186 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) |
| 187 | goto dynamic; |
| 188 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 189 | /* Check that the codec and cpu DAIs are compatible */ |
| 190 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 191 | runtime->hw.rate_min = |
| 192 | max(codec_dai_drv->playback.rate_min, |
| 193 | cpu_dai_drv->playback.rate_min); |
| 194 | runtime->hw.rate_max = |
| 195 | min(codec_dai_drv->playback.rate_max, |
| 196 | cpu_dai_drv->playback.rate_max); |
| 197 | runtime->hw.channels_min = |
| 198 | max(codec_dai_drv->playback.channels_min, |
| 199 | cpu_dai_drv->playback.channels_min); |
| 200 | runtime->hw.channels_max = |
| 201 | min(codec_dai_drv->playback.channels_max, |
| 202 | cpu_dai_drv->playback.channels_max); |
| 203 | runtime->hw.formats = |
| 204 | codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats; |
| 205 | runtime->hw.rates = |
| 206 | codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates; |
| 207 | if (codec_dai_drv->playback.rates |
| 208 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 209 | runtime->hw.rates |= cpu_dai_drv->playback.rates; |
| 210 | if (cpu_dai_drv->playback.rates |
| 211 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 212 | runtime->hw.rates |= codec_dai_drv->playback.rates; |
| 213 | } else { |
| 214 | runtime->hw.rate_min = |
| 215 | max(codec_dai_drv->capture.rate_min, |
| 216 | cpu_dai_drv->capture.rate_min); |
| 217 | runtime->hw.rate_max = |
| 218 | min(codec_dai_drv->capture.rate_max, |
| 219 | cpu_dai_drv->capture.rate_max); |
| 220 | runtime->hw.channels_min = |
| 221 | max(codec_dai_drv->capture.channels_min, |
| 222 | cpu_dai_drv->capture.channels_min); |
| 223 | runtime->hw.channels_max = |
| 224 | min(codec_dai_drv->capture.channels_max, |
| 225 | cpu_dai_drv->capture.channels_max); |
| 226 | runtime->hw.formats = |
| 227 | codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats; |
| 228 | runtime->hw.rates = |
| 229 | codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates; |
| 230 | if (codec_dai_drv->capture.rates |
| 231 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 232 | runtime->hw.rates |= cpu_dai_drv->capture.rates; |
| 233 | if (cpu_dai_drv->capture.rates |
| 234 | & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS)) |
| 235 | runtime->hw.rates |= codec_dai_drv->capture.rates; |
| 236 | } |
| 237 | |
| 238 | ret = -EINVAL; |
| 239 | snd_pcm_limit_hw_rates(runtime); |
| 240 | if (!runtime->hw.rates) { |
| 241 | printk(KERN_ERR "asoc: %s <-> %s No matching rates\n", |
| 242 | codec_dai->name, cpu_dai->name); |
| 243 | goto config_err; |
| 244 | } |
| 245 | if (!runtime->hw.formats) { |
| 246 | printk(KERN_ERR "asoc: %s <-> %s No matching formats\n", |
| 247 | codec_dai->name, cpu_dai->name); |
| 248 | goto config_err; |
| 249 | } |
| 250 | if (!runtime->hw.channels_min || !runtime->hw.channels_max || |
| 251 | runtime->hw.channels_min > runtime->hw.channels_max) { |
| 252 | printk(KERN_ERR "asoc: %s <-> %s No matching channels\n", |
| 253 | codec_dai->name, cpu_dai->name); |
| 254 | goto config_err; |
| 255 | } |
| 256 | |
Mark Brown | 58ba9b2 | 2012-01-16 18:38:51 +0000 | [diff] [blame] | 257 | soc_pcm_apply_msb(substream, codec_dai); |
| 258 | soc_pcm_apply_msb(substream, cpu_dai); |
| 259 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 260 | /* Symmetry only applies if we've already got an active stream. */ |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 261 | if (cpu_dai->active) { |
| 262 | ret = soc_pcm_apply_symmetry(substream, cpu_dai); |
| 263 | if (ret != 0) |
| 264 | goto config_err; |
| 265 | } |
| 266 | |
| 267 | if (codec_dai->active) { |
| 268 | ret = soc_pcm_apply_symmetry(substream, codec_dai); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 269 | if (ret != 0) |
| 270 | goto config_err; |
| 271 | } |
| 272 | |
| 273 | pr_debug("asoc: %s <-> %s info:\n", |
| 274 | codec_dai->name, cpu_dai->name); |
| 275 | pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates); |
| 276 | pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min, |
| 277 | runtime->hw.channels_max); |
| 278 | pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min, |
| 279 | runtime->hw.rate_max); |
| 280 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 281 | dynamic: |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 282 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 283 | cpu_dai->playback_active++; |
| 284 | codec_dai->playback_active++; |
| 285 | } else { |
| 286 | cpu_dai->capture_active++; |
| 287 | codec_dai->capture_active++; |
| 288 | } |
| 289 | cpu_dai->active++; |
| 290 | codec_dai->active++; |
| 291 | rtd->codec->active++; |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 292 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 293 | return 0; |
| 294 | |
| 295 | config_err: |
| 296 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) |
| 297 | rtd->dai_link->ops->shutdown(substream); |
| 298 | |
| 299 | machine_err: |
| 300 | if (codec_dai->driver->ops->shutdown) |
| 301 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 302 | |
| 303 | codec_dai_err: |
| 304 | if (platform->driver->ops && platform->driver->ops->close) |
| 305 | platform->driver->ops->close(substream); |
| 306 | |
| 307 | platform_err: |
| 308 | if (cpu_dai->driver->ops->shutdown) |
| 309 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 310 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 311 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 312 | |
| 313 | pm_runtime_put(platform->dev); |
| 314 | pm_runtime_put(codec_dai->dev); |
| 315 | pm_runtime_put(cpu_dai->dev); |
| 316 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Power down the audio subsystem pmdown_time msecs after close is called. |
| 322 | * This is to ensure there are no pops or clicks in between any music tracks |
| 323 | * due to DAPM power cycling. |
| 324 | */ |
| 325 | static void close_delayed_work(struct work_struct *work) |
| 326 | { |
| 327 | struct snd_soc_pcm_runtime *rtd = |
| 328 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); |
| 329 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 330 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 331 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 332 | |
| 333 | pr_debug("pop wq checking: %s status: %s waiting: %s\n", |
| 334 | codec_dai->driver->playback.stream_name, |
| 335 | codec_dai->playback_active ? "active" : "inactive", |
| 336 | codec_dai->pop_wait ? "yes" : "no"); |
| 337 | |
| 338 | /* are we waiting on this codec DAI stream */ |
| 339 | if (codec_dai->pop_wait == 1) { |
| 340 | codec_dai->pop_wait = 0; |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 341 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 342 | SND_SOC_DAPM_STREAM_STOP); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 345 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Called by ALSA when a PCM substream is closed. Private data can be |
| 350 | * freed here. The cpu DAI, codec DAI, machine and platform are also |
| 351 | * shutdown. |
| 352 | */ |
Liam Girdwood | 91d5e6b | 2011-06-09 17:04:59 +0100 | [diff] [blame] | 353 | static int soc_pcm_close(struct snd_pcm_substream *substream) |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 354 | { |
| 355 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 356 | struct snd_soc_platform *platform = rtd->platform; |
| 357 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 358 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 359 | struct snd_soc_codec *codec = rtd->codec; |
| 360 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 361 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 362 | |
| 363 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 364 | cpu_dai->playback_active--; |
| 365 | codec_dai->playback_active--; |
| 366 | } else { |
| 367 | cpu_dai->capture_active--; |
| 368 | codec_dai->capture_active--; |
| 369 | } |
| 370 | |
| 371 | cpu_dai->active--; |
| 372 | codec_dai->active--; |
| 373 | codec->active--; |
| 374 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 375 | /* clear the corresponding DAIs rate when inactive */ |
| 376 | if (!cpu_dai->active) |
| 377 | cpu_dai->rate = 0; |
| 378 | |
| 379 | if (!codec_dai->active) |
| 380 | codec_dai->rate = 0; |
Sascha Hauer | 25b7679 | 2011-08-17 09:20:01 +0200 | [diff] [blame] | 381 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 382 | /* Muting the DAC suppresses artifacts caused during digital |
| 383 | * shutdown, for example from stopping clocks. |
| 384 | */ |
| 385 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 386 | snd_soc_dai_digital_mute(codec_dai, 1); |
| 387 | |
| 388 | if (cpu_dai->driver->ops->shutdown) |
| 389 | cpu_dai->driver->ops->shutdown(substream, cpu_dai); |
| 390 | |
| 391 | if (codec_dai->driver->ops->shutdown) |
| 392 | codec_dai->driver->ops->shutdown(substream, codec_dai); |
| 393 | |
| 394 | if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown) |
| 395 | rtd->dai_link->ops->shutdown(substream); |
| 396 | |
| 397 | if (platform->driver->ops && platform->driver->ops->close) |
| 398 | platform->driver->ops->close(substream); |
| 399 | cpu_dai->runtime = NULL; |
| 400 | |
| 401 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Mark Brown | b5d1d03 | 2012-02-08 20:10:56 +0000 | [diff] [blame] | 402 | if (!rtd->pmdown_time || codec->ignore_pmdown_time || |
Mark Brown | 5b6247a | 2011-10-27 12:11:26 +0200 | [diff] [blame] | 403 | rtd->dai_link->ignore_pmdown_time) { |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 404 | /* powered down playback stream now */ |
| 405 | snd_soc_dapm_stream_event(rtd, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 406 | SNDRV_PCM_STREAM_PLAYBACK, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 407 | SND_SOC_DAPM_STREAM_STOP); |
Peter Ujfalusi | 1d69c5c | 2011-10-14 14:43:33 +0300 | [diff] [blame] | 408 | } else { |
| 409 | /* start delayed pop wq here for playback streams */ |
| 410 | codec_dai->pop_wait = 1; |
| 411 | schedule_delayed_work(&rtd->delayed_work, |
| 412 | msecs_to_jiffies(rtd->pmdown_time)); |
| 413 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 414 | } else { |
| 415 | /* capture streams can be powered down now */ |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 416 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 417 | SND_SOC_DAPM_STREAM_STOP); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 420 | mutex_unlock(&rtd->pcm_mutex); |
Mark Brown | d6652ef | 2011-12-03 20:14:31 +0000 | [diff] [blame] | 421 | |
| 422 | pm_runtime_put(platform->dev); |
| 423 | pm_runtime_put(codec_dai->dev); |
| 424 | pm_runtime_put(cpu_dai->dev); |
| 425 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Called by ALSA when the PCM substream is prepared, can set format, sample |
| 431 | * rate, etc. This function is non atomic and can be called multiple times, |
| 432 | * it can refer to the runtime info. |
| 433 | */ |
| 434 | static int soc_pcm_prepare(struct snd_pcm_substream *substream) |
| 435 | { |
| 436 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 437 | struct snd_soc_platform *platform = rtd->platform; |
| 438 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 439 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 440 | int ret = 0; |
| 441 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 442 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 443 | |
| 444 | if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) { |
| 445 | ret = rtd->dai_link->ops->prepare(substream); |
| 446 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 447 | pr_err("asoc: machine prepare error: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 448 | goto out; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if (platform->driver->ops && platform->driver->ops->prepare) { |
| 453 | ret = platform->driver->ops->prepare(substream); |
| 454 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 455 | dev_err(platform->dev, "platform prepare error: %d\n", |
| 456 | ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 457 | goto out; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (codec_dai->driver->ops->prepare) { |
| 462 | ret = codec_dai->driver->ops->prepare(substream, codec_dai); |
| 463 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 464 | dev_err(codec_dai->dev, "DAI prepare error: %d\n", |
| 465 | ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 466 | goto out; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (cpu_dai->driver->ops->prepare) { |
| 471 | ret = cpu_dai->driver->ops->prepare(substream, cpu_dai); |
| 472 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 473 | dev_err(cpu_dai->dev, "DAI prepare error: %d\n", |
| 474 | ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 475 | goto out; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /* cancel any delayed stream shutdown that is pending */ |
| 480 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
| 481 | codec_dai->pop_wait) { |
| 482 | codec_dai->pop_wait = 0; |
| 483 | cancel_delayed_work(&rtd->delayed_work); |
| 484 | } |
| 485 | |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 486 | snd_soc_dapm_stream_event(rtd, substream->stream, |
| 487 | SND_SOC_DAPM_STREAM_START); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 488 | |
| 489 | snd_soc_dai_digital_mute(codec_dai, 0); |
| 490 | |
| 491 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 492 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Called by ALSA when the hardware params are set by application. This |
| 498 | * function can also be called multiple times and can allocate buffers |
| 499 | * (using snd_pcm_lib_* ). It's non-atomic. |
| 500 | */ |
| 501 | static int soc_pcm_hw_params(struct snd_pcm_substream *substream, |
| 502 | struct snd_pcm_hw_params *params) |
| 503 | { |
| 504 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 505 | struct snd_soc_platform *platform = rtd->platform; |
| 506 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 507 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 508 | int ret = 0; |
| 509 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 510 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 511 | |
| 512 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) { |
| 513 | ret = rtd->dai_link->ops->hw_params(substream, params); |
| 514 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 515 | pr_err("asoc: machine hw_params failed: %d\n", ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 516 | goto out; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (codec_dai->driver->ops->hw_params) { |
| 521 | ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); |
| 522 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 523 | dev_err(codec_dai->dev, "can't set %s hw params: %d\n", |
| 524 | codec_dai->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 525 | goto codec_err; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (cpu_dai->driver->ops->hw_params) { |
| 530 | ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai); |
| 531 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 532 | dev_err(cpu_dai->dev, "%s hw params failed: %d\n", |
| 533 | cpu_dai->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 534 | goto interface_err; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (platform->driver->ops && platform->driver->ops->hw_params) { |
| 539 | ret = platform->driver->ops->hw_params(substream, params); |
| 540 | if (ret < 0) { |
Mark Brown | 25bfe66 | 2012-02-01 21:30:32 +0000 | [diff] [blame] | 541 | dev_err(platform->dev, "%s hw params failed: %d\n", |
| 542 | platform->name, ret); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 543 | goto platform_err; |
| 544 | } |
| 545 | } |
| 546 | |
Dong Aisheng | 1784102 | 2011-08-29 17:15:14 +0800 | [diff] [blame] | 547 | /* store the rate for each DAIs */ |
| 548 | cpu_dai->rate = params_rate(params); |
| 549 | codec_dai->rate = params_rate(params); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 550 | |
| 551 | out: |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 552 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 553 | return ret; |
| 554 | |
| 555 | platform_err: |
| 556 | if (cpu_dai->driver->ops->hw_free) |
| 557 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 558 | |
| 559 | interface_err: |
| 560 | if (codec_dai->driver->ops->hw_free) |
| 561 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 562 | |
| 563 | codec_err: |
| 564 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 565 | rtd->dai_link->ops->hw_free(substream); |
| 566 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 567 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Frees resources allocated by hw_params, can be called multiple times |
| 573 | */ |
| 574 | static int soc_pcm_hw_free(struct snd_pcm_substream *substream) |
| 575 | { |
| 576 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 577 | struct snd_soc_platform *platform = rtd->platform; |
| 578 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 579 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 580 | struct snd_soc_codec *codec = rtd->codec; |
| 581 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 582 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 583 | |
| 584 | /* apply codec digital mute */ |
| 585 | if (!codec->active) |
| 586 | snd_soc_dai_digital_mute(codec_dai, 1); |
| 587 | |
| 588 | /* free any machine hw params */ |
| 589 | if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free) |
| 590 | rtd->dai_link->ops->hw_free(substream); |
| 591 | |
| 592 | /* free any DMA resources */ |
| 593 | if (platform->driver->ops && platform->driver->ops->hw_free) |
| 594 | platform->driver->ops->hw_free(substream); |
| 595 | |
| 596 | /* now free hw params for the DAIs */ |
| 597 | if (codec_dai->driver->ops->hw_free) |
| 598 | codec_dai->driver->ops->hw_free(substream, codec_dai); |
| 599 | |
| 600 | if (cpu_dai->driver->ops->hw_free) |
| 601 | cpu_dai->driver->ops->hw_free(substream, cpu_dai); |
| 602 | |
Liam Girdwood | b8c0dab | 2011-06-09 17:04:39 +0100 | [diff] [blame] | 603 | mutex_unlock(&rtd->pcm_mutex); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 608 | { |
| 609 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 610 | struct snd_soc_platform *platform = rtd->platform; |
| 611 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 612 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 613 | int ret; |
| 614 | |
| 615 | if (codec_dai->driver->ops->trigger) { |
| 616 | ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai); |
| 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | if (platform->driver->ops && platform->driver->ops->trigger) { |
| 622 | ret = platform->driver->ops->trigger(substream, cmd); |
| 623 | if (ret < 0) |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | if (cpu_dai->driver->ops->trigger) { |
| 628 | ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai); |
| 629 | if (ret < 0) |
| 630 | return ret; |
| 631 | } |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * soc level wrapper for pointer callback |
| 637 | * If cpu_dai, codec_dai, platform driver has the delay callback, than |
| 638 | * the runtime->delay will be updated accordingly. |
| 639 | */ |
| 640 | static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) |
| 641 | { |
| 642 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 643 | struct snd_soc_platform *platform = rtd->platform; |
| 644 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 645 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 646 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 647 | snd_pcm_uframes_t offset = 0; |
| 648 | snd_pcm_sframes_t delay = 0; |
| 649 | |
| 650 | if (platform->driver->ops && platform->driver->ops->pointer) |
| 651 | offset = platform->driver->ops->pointer(substream); |
| 652 | |
| 653 | if (cpu_dai->driver->ops->delay) |
| 654 | delay += cpu_dai->driver->ops->delay(substream, cpu_dai); |
| 655 | |
| 656 | if (codec_dai->driver->ops->delay) |
| 657 | delay += codec_dai->driver->ops->delay(substream, codec_dai); |
| 658 | |
| 659 | if (platform->driver->delay) |
| 660 | delay += platform->driver->delay(substream, codec_dai); |
| 661 | |
| 662 | runtime->delay = delay; |
| 663 | |
| 664 | return offset; |
| 665 | } |
| 666 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 667 | /* connect a FE and BE */ |
| 668 | static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, |
| 669 | struct snd_soc_pcm_runtime *be, int stream) |
| 670 | { |
| 671 | struct snd_soc_dpcm *dpcm; |
| 672 | |
| 673 | /* only add new dpcms */ |
| 674 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 675 | if (dpcm->be == be && dpcm->fe == fe) |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); |
| 680 | if (!dpcm) |
| 681 | return -ENOMEM; |
| 682 | |
| 683 | dpcm->be = be; |
| 684 | dpcm->fe = fe; |
| 685 | be->dpcm[stream].runtime = fe->dpcm[stream].runtime; |
| 686 | dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; |
| 687 | list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); |
| 688 | list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); |
| 689 | |
| 690 | dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n", |
| 691 | stream ? "capture" : "playback", fe->dai_link->name, |
| 692 | stream ? "<-" : "->", be->dai_link->name); |
| 693 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame^] | 694 | #ifdef CONFIG_DEBUG_FS |
| 695 | dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644, |
| 696 | fe->debugfs_dpcm_root, &dpcm->state); |
| 697 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 698 | return 1; |
| 699 | } |
| 700 | |
| 701 | /* reparent a BE onto another FE */ |
| 702 | static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, |
| 703 | struct snd_soc_pcm_runtime *be, int stream) |
| 704 | { |
| 705 | struct snd_soc_dpcm *dpcm; |
| 706 | struct snd_pcm_substream *fe_substream, *be_substream; |
| 707 | |
| 708 | /* reparent if BE is connected to other FEs */ |
| 709 | if (!be->dpcm[stream].users) |
| 710 | return; |
| 711 | |
| 712 | be_substream = snd_soc_dpcm_get_substream(be, stream); |
| 713 | |
| 714 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 715 | if (dpcm->fe == fe) |
| 716 | continue; |
| 717 | |
| 718 | dev_dbg(fe->dev, " reparent %s path %s %s %s\n", |
| 719 | stream ? "capture" : "playback", |
| 720 | dpcm->fe->dai_link->name, |
| 721 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 722 | |
| 723 | fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); |
| 724 | be_substream->runtime = fe_substream->runtime; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* disconnect a BE and FE */ |
| 730 | static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) |
| 731 | { |
| 732 | struct snd_soc_dpcm *dpcm, *d; |
| 733 | |
| 734 | list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) { |
| 735 | dev_dbg(fe->dev, "BE %s disconnect check for %s\n", |
| 736 | stream ? "capture" : "playback", |
| 737 | dpcm->be->dai_link->name); |
| 738 | |
| 739 | if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) |
| 740 | continue; |
| 741 | |
| 742 | dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n", |
| 743 | stream ? "capture" : "playback", fe->dai_link->name, |
| 744 | stream ? "<-" : "->", dpcm->be->dai_link->name); |
| 745 | |
| 746 | /* BEs still alive need new FE */ |
| 747 | dpcm_be_reparent(fe, dpcm->be, stream); |
| 748 | |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame^] | 749 | #ifdef CONFIG_DEBUG_FS |
| 750 | debugfs_remove(dpcm->debugfs_state); |
| 751 | #endif |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 752 | list_del(&dpcm->list_be); |
| 753 | list_del(&dpcm->list_fe); |
| 754 | kfree(dpcm); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /* get BE for DAI widget and stream */ |
| 759 | static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, |
| 760 | struct snd_soc_dapm_widget *widget, int stream) |
| 761 | { |
| 762 | struct snd_soc_pcm_runtime *be; |
| 763 | int i; |
| 764 | |
| 765 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 766 | for (i = 0; i < card->num_links; i++) { |
| 767 | be = &card->rtd[i]; |
| 768 | |
| 769 | if (be->cpu_dai->playback_widget == widget || |
| 770 | be->codec_dai->playback_widget == widget) |
| 771 | return be; |
| 772 | } |
| 773 | } else { |
| 774 | |
| 775 | for (i = 0; i < card->num_links; i++) { |
| 776 | be = &card->rtd[i]; |
| 777 | |
| 778 | if (be->cpu_dai->capture_widget == widget || |
| 779 | be->codec_dai->capture_widget == widget) |
| 780 | return be; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | dev_err(card->dev, "can't get %s BE for %s\n", |
| 785 | stream ? "capture" : "playback", widget->name); |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | static inline struct snd_soc_dapm_widget * |
| 790 | rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream) |
| 791 | { |
| 792 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 793 | return rtd->cpu_dai->playback_widget; |
| 794 | else |
| 795 | return rtd->cpu_dai->capture_widget; |
| 796 | } |
| 797 | |
| 798 | static inline struct snd_soc_dapm_widget * |
| 799 | rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream) |
| 800 | { |
| 801 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 802 | return rtd->codec_dai->playback_widget; |
| 803 | else |
| 804 | return rtd->codec_dai->capture_widget; |
| 805 | } |
| 806 | |
| 807 | static int widget_in_list(struct snd_soc_dapm_widget_list *list, |
| 808 | struct snd_soc_dapm_widget *widget) |
| 809 | { |
| 810 | int i; |
| 811 | |
| 812 | for (i = 0; i < list->num_widgets; i++) { |
| 813 | if (widget == list->widgets[i]) |
| 814 | return 1; |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int dpcm_path_get(struct snd_soc_pcm_runtime *fe, |
| 821 | int stream, struct snd_soc_dapm_widget_list **list_) |
| 822 | { |
| 823 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; |
| 824 | struct snd_soc_dapm_widget_list *list; |
| 825 | int paths; |
| 826 | |
| 827 | list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) + |
| 828 | sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL); |
| 829 | if (list == NULL) |
| 830 | return -ENOMEM; |
| 831 | |
| 832 | /* get number of valid DAI paths and their widgets */ |
| 833 | paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list); |
| 834 | |
| 835 | dev_dbg(fe->dev, "found %d audio %s paths\n", paths, |
| 836 | stream ? "capture" : "playback"); |
| 837 | |
| 838 | *list_ = list; |
| 839 | return paths; |
| 840 | } |
| 841 | |
| 842 | static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list) |
| 843 | { |
| 844 | kfree(*list); |
| 845 | } |
| 846 | |
| 847 | static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 848 | struct snd_soc_dapm_widget_list **list_) |
| 849 | { |
| 850 | struct snd_soc_dpcm *dpcm; |
| 851 | struct snd_soc_dapm_widget_list *list = *list_; |
| 852 | struct snd_soc_dapm_widget *widget; |
| 853 | int prune = 0; |
| 854 | |
| 855 | /* Destroy any old FE <--> BE connections */ |
| 856 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 857 | |
| 858 | /* is there a valid CPU DAI widget for this BE */ |
| 859 | widget = rtd_get_cpu_widget(dpcm->be, stream); |
| 860 | |
| 861 | /* prune the BE if it's no longer in our active list */ |
| 862 | if (widget && widget_in_list(list, widget)) |
| 863 | continue; |
| 864 | |
| 865 | /* is there a valid CODEC DAI widget for this BE */ |
| 866 | widget = rtd_get_codec_widget(dpcm->be, stream); |
| 867 | |
| 868 | /* prune the BE if it's no longer in our active list */ |
| 869 | if (widget && widget_in_list(list, widget)) |
| 870 | continue; |
| 871 | |
| 872 | dev_dbg(fe->dev, "pruning %s BE %s for %s\n", |
| 873 | stream ? "capture" : "playback", |
| 874 | dpcm->be->dai_link->name, fe->dai_link->name); |
| 875 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 876 | dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 877 | prune++; |
| 878 | } |
| 879 | |
| 880 | dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune); |
| 881 | return prune; |
| 882 | } |
| 883 | |
| 884 | static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, |
| 885 | struct snd_soc_dapm_widget_list **list_) |
| 886 | { |
| 887 | struct snd_soc_card *card = fe->card; |
| 888 | struct snd_soc_dapm_widget_list *list = *list_; |
| 889 | struct snd_soc_pcm_runtime *be; |
| 890 | int i, new = 0, err; |
| 891 | |
| 892 | /* Create any new FE <--> BE connections */ |
| 893 | for (i = 0; i < list->num_widgets; i++) { |
| 894 | |
| 895 | if (list->widgets[i]->id != snd_soc_dapm_dai) |
| 896 | continue; |
| 897 | |
| 898 | /* is there a valid BE rtd for this widget */ |
| 899 | be = dpcm_get_be(card, list->widgets[i], stream); |
| 900 | if (!be) { |
| 901 | dev_err(fe->dev, "no BE found for %s\n", |
| 902 | list->widgets[i]->name); |
| 903 | continue; |
| 904 | } |
| 905 | |
| 906 | /* make sure BE is a real BE */ |
| 907 | if (!be->dai_link->no_pcm) |
| 908 | continue; |
| 909 | |
| 910 | /* don't connect if FE is not running */ |
| 911 | if (!fe->dpcm[stream].runtime) |
| 912 | continue; |
| 913 | |
| 914 | /* newly connected FE and BE */ |
| 915 | err = dpcm_be_connect(fe, be, stream); |
| 916 | if (err < 0) { |
| 917 | dev_err(fe->dev, "can't connect %s\n", |
| 918 | list->widgets[i]->name); |
| 919 | break; |
| 920 | } else if (err == 0) /* already connected */ |
| 921 | continue; |
| 922 | |
| 923 | /* new */ |
| 924 | be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; |
| 925 | new++; |
| 926 | } |
| 927 | |
| 928 | dev_dbg(fe->dev, "found %d new BE paths\n", new); |
| 929 | return new; |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Find the corresponding BE DAIs that source or sink audio to this |
| 934 | * FE substream. |
| 935 | */ |
| 936 | static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, |
| 937 | int stream, struct snd_soc_dapm_widget_list **list, int new) |
| 938 | { |
| 939 | if (new) |
| 940 | return dpcm_add_paths(fe, stream, list); |
| 941 | else |
| 942 | return dpcm_prune_paths(fe, stream, list); |
| 943 | } |
| 944 | |
| 945 | static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) |
| 946 | { |
| 947 | struct snd_soc_dpcm *dpcm; |
| 948 | |
| 949 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 950 | dpcm->be->dpcm[stream].runtime_update = |
| 951 | SND_SOC_DPCM_UPDATE_NO; |
| 952 | } |
| 953 | |
| 954 | static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, |
| 955 | int stream) |
| 956 | { |
| 957 | struct snd_soc_dpcm *dpcm; |
| 958 | |
| 959 | /* disable any enabled and non active backends */ |
| 960 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 961 | |
| 962 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 963 | struct snd_pcm_substream *be_substream = |
| 964 | snd_soc_dpcm_get_substream(be, stream); |
| 965 | |
| 966 | if (be->dpcm[stream].users == 0) |
| 967 | dev_err(be->dev, "no users %s at close - state %d\n", |
| 968 | stream ? "capture" : "playback", |
| 969 | be->dpcm[stream].state); |
| 970 | |
| 971 | if (--be->dpcm[stream].users != 0) |
| 972 | continue; |
| 973 | |
| 974 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 975 | continue; |
| 976 | |
| 977 | soc_pcm_close(be_substream); |
| 978 | be_substream->runtime = NULL; |
| 979 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) |
| 984 | { |
| 985 | struct snd_soc_dpcm *dpcm; |
| 986 | int err, count = 0; |
| 987 | |
| 988 | /* only startup BE DAIs that are either sinks or sources to this FE DAI */ |
| 989 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 990 | |
| 991 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 992 | struct snd_pcm_substream *be_substream = |
| 993 | snd_soc_dpcm_get_substream(be, stream); |
| 994 | |
| 995 | /* is this op for this BE ? */ |
| 996 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 997 | continue; |
| 998 | |
| 999 | /* first time the dpcm is open ? */ |
| 1000 | if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) |
| 1001 | dev_err(be->dev, "too many users %s at open %d\n", |
| 1002 | stream ? "capture" : "playback", |
| 1003 | be->dpcm[stream].state); |
| 1004 | |
| 1005 | if (be->dpcm[stream].users++ != 0) |
| 1006 | continue; |
| 1007 | |
| 1008 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && |
| 1009 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) |
| 1010 | continue; |
| 1011 | |
| 1012 | dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name); |
| 1013 | |
| 1014 | be_substream->runtime = be->dpcm[stream].runtime; |
| 1015 | err = soc_pcm_open(be_substream); |
| 1016 | if (err < 0) { |
| 1017 | dev_err(be->dev, "BE open failed %d\n", err); |
| 1018 | be->dpcm[stream].users--; |
| 1019 | if (be->dpcm[stream].users < 0) |
| 1020 | dev_err(be->dev, "no users %s at unwind %d\n", |
| 1021 | stream ? "capture" : "playback", |
| 1022 | be->dpcm[stream].state); |
| 1023 | |
| 1024 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1025 | goto unwind; |
| 1026 | } |
| 1027 | |
| 1028 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1029 | count++; |
| 1030 | } |
| 1031 | |
| 1032 | return count; |
| 1033 | |
| 1034 | unwind: |
| 1035 | /* disable any enabled and non active backends */ |
| 1036 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1037 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1038 | struct snd_pcm_substream *be_substream = |
| 1039 | snd_soc_dpcm_get_substream(be, stream); |
| 1040 | |
| 1041 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1042 | continue; |
| 1043 | |
| 1044 | if (be->dpcm[stream].users == 0) |
| 1045 | dev_err(be->dev, "no users %s at close %d\n", |
| 1046 | stream ? "capture" : "playback", |
| 1047 | be->dpcm[stream].state); |
| 1048 | |
| 1049 | if (--be->dpcm[stream].users != 0) |
| 1050 | continue; |
| 1051 | |
| 1052 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) |
| 1053 | continue; |
| 1054 | |
| 1055 | soc_pcm_close(be_substream); |
| 1056 | be_substream->runtime = NULL; |
| 1057 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1058 | } |
| 1059 | |
| 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) |
| 1064 | { |
| 1065 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1066 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 1067 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 1068 | struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; |
| 1069 | |
| 1070 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1071 | runtime->hw.rate_min = cpu_dai_drv->playback.rate_min; |
| 1072 | runtime->hw.rate_max = cpu_dai_drv->playback.rate_max; |
| 1073 | runtime->hw.channels_min = cpu_dai_drv->playback.channels_min; |
| 1074 | runtime->hw.channels_max = cpu_dai_drv->playback.channels_max; |
| 1075 | runtime->hw.formats &= cpu_dai_drv->playback.formats; |
| 1076 | runtime->hw.rates = cpu_dai_drv->playback.rates; |
| 1077 | } else { |
| 1078 | runtime->hw.rate_min = cpu_dai_drv->capture.rate_min; |
| 1079 | runtime->hw.rate_max = cpu_dai_drv->capture.rate_max; |
| 1080 | runtime->hw.channels_min = cpu_dai_drv->capture.channels_min; |
| 1081 | runtime->hw.channels_max = cpu_dai_drv->capture.channels_max; |
| 1082 | runtime->hw.formats &= cpu_dai_drv->capture.formats; |
| 1083 | runtime->hw.rates = cpu_dai_drv->capture.rates; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) |
| 1088 | { |
| 1089 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1090 | struct snd_pcm_runtime *runtime = fe_substream->runtime; |
| 1091 | int stream = fe_substream->stream, ret = 0; |
| 1092 | |
| 1093 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1094 | |
| 1095 | ret = dpcm_be_dai_startup(fe, fe_substream->stream); |
| 1096 | if (ret < 0) { |
| 1097 | dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret); |
| 1098 | goto be_err; |
| 1099 | } |
| 1100 | |
| 1101 | dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name); |
| 1102 | |
| 1103 | /* start the DAI frontend */ |
| 1104 | ret = soc_pcm_open(fe_substream); |
| 1105 | if (ret < 0) { |
| 1106 | dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret); |
| 1107 | goto unwind; |
| 1108 | } |
| 1109 | |
| 1110 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 1111 | |
| 1112 | dpcm_set_fe_runtime(fe_substream); |
| 1113 | snd_pcm_limit_hw_rates(runtime); |
| 1114 | |
| 1115 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1116 | return 0; |
| 1117 | |
| 1118 | unwind: |
| 1119 | dpcm_be_dai_startup_unwind(fe, fe_substream->stream); |
| 1120 | be_err: |
| 1121 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) |
| 1126 | { |
| 1127 | struct snd_soc_dpcm *dpcm; |
| 1128 | |
| 1129 | /* only shutdown BEs that are either sinks or sources to this FE DAI */ |
| 1130 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1131 | |
| 1132 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1133 | struct snd_pcm_substream *be_substream = |
| 1134 | snd_soc_dpcm_get_substream(be, stream); |
| 1135 | |
| 1136 | /* is this op for this BE ? */ |
| 1137 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1138 | continue; |
| 1139 | |
| 1140 | if (be->dpcm[stream].users == 0) |
| 1141 | dev_err(be->dev, "no users %s at close - state %d\n", |
| 1142 | stream ? "capture" : "playback", |
| 1143 | be->dpcm[stream].state); |
| 1144 | |
| 1145 | if (--be->dpcm[stream].users != 0) |
| 1146 | continue; |
| 1147 | |
| 1148 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1149 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) |
| 1150 | continue; |
| 1151 | |
| 1152 | dev_dbg(be->dev, "dpcm: close BE %s\n", |
| 1153 | dpcm->fe->dai_link->name); |
| 1154 | |
| 1155 | soc_pcm_close(be_substream); |
| 1156 | be_substream->runtime = NULL; |
| 1157 | |
| 1158 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1159 | } |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) |
| 1164 | { |
| 1165 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1166 | int stream = substream->stream; |
| 1167 | |
| 1168 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1169 | |
| 1170 | /* shutdown the BEs */ |
| 1171 | dpcm_be_dai_shutdown(fe, substream->stream); |
| 1172 | |
| 1173 | dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name); |
| 1174 | |
| 1175 | /* now shutdown the frontend */ |
| 1176 | soc_pcm_close(substream); |
| 1177 | |
| 1178 | /* run the stream event for each BE */ |
| 1179 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 1180 | |
| 1181 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 1182 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) |
| 1187 | { |
| 1188 | struct snd_soc_dpcm *dpcm; |
| 1189 | |
| 1190 | /* only hw_params backends that are either sinks or sources |
| 1191 | * to this frontend DAI */ |
| 1192 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1193 | |
| 1194 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1195 | struct snd_pcm_substream *be_substream = |
| 1196 | snd_soc_dpcm_get_substream(be, stream); |
| 1197 | |
| 1198 | /* is this op for this BE ? */ |
| 1199 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1200 | continue; |
| 1201 | |
| 1202 | /* only free hw when no longer used - check all FEs */ |
| 1203 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1204 | continue; |
| 1205 | |
| 1206 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1207 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 1208 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1209 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1210 | continue; |
| 1211 | |
| 1212 | dev_dbg(be->dev, "dpcm: hw_free BE %s\n", |
| 1213 | dpcm->fe->dai_link->name); |
| 1214 | |
| 1215 | soc_pcm_hw_free(be_substream); |
| 1216 | |
| 1217 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1218 | } |
| 1219 | |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) |
| 1224 | { |
| 1225 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1226 | int err, stream = substream->stream; |
| 1227 | |
| 1228 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1229 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1230 | |
| 1231 | dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name); |
| 1232 | |
| 1233 | /* call hw_free on the frontend */ |
| 1234 | err = soc_pcm_hw_free(substream); |
| 1235 | if (err < 0) |
| 1236 | dev_err(fe->dev,"dpcm: hw_free FE %s failed\n", |
| 1237 | fe->dai_link->name); |
| 1238 | |
| 1239 | /* only hw_params backends that are either sinks or sources |
| 1240 | * to this frontend DAI */ |
| 1241 | err = dpcm_be_dai_hw_free(fe, stream); |
| 1242 | |
| 1243 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; |
| 1244 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1245 | |
| 1246 | mutex_unlock(&fe->card->mutex); |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) |
| 1251 | { |
| 1252 | struct snd_soc_dpcm *dpcm; |
| 1253 | int ret; |
| 1254 | |
| 1255 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1256 | |
| 1257 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1258 | struct snd_pcm_substream *be_substream = |
| 1259 | snd_soc_dpcm_get_substream(be, stream); |
| 1260 | |
| 1261 | /* is this op for this BE ? */ |
| 1262 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1263 | continue; |
| 1264 | |
| 1265 | /* only allow hw_params() if no connected FEs are running */ |
| 1266 | if (!snd_soc_dpcm_can_be_params(fe, be, stream)) |
| 1267 | continue; |
| 1268 | |
| 1269 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 1270 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1271 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) |
| 1272 | continue; |
| 1273 | |
| 1274 | dev_dbg(be->dev, "dpcm: hw_params BE %s\n", |
| 1275 | dpcm->fe->dai_link->name); |
| 1276 | |
| 1277 | /* copy params for each dpcm */ |
| 1278 | memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, |
| 1279 | sizeof(struct snd_pcm_hw_params)); |
| 1280 | |
| 1281 | /* perform any hw_params fixups */ |
| 1282 | if (be->dai_link->be_hw_params_fixup) { |
| 1283 | ret = be->dai_link->be_hw_params_fixup(be, |
| 1284 | &dpcm->hw_params); |
| 1285 | if (ret < 0) { |
| 1286 | dev_err(be->dev, |
| 1287 | "dpcm: hw_params BE fixup failed %d\n", |
| 1288 | ret); |
| 1289 | goto unwind; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); |
| 1294 | if (ret < 0) { |
| 1295 | dev_err(dpcm->be->dev, |
| 1296 | "dpcm: hw_params BE failed %d\n", ret); |
| 1297 | goto unwind; |
| 1298 | } |
| 1299 | |
| 1300 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 1301 | } |
| 1302 | return 0; |
| 1303 | |
| 1304 | unwind: |
| 1305 | /* disable any enabled and non active backends */ |
| 1306 | list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1307 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1308 | struct snd_pcm_substream *be_substream = |
| 1309 | snd_soc_dpcm_get_substream(be, stream); |
| 1310 | |
| 1311 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1312 | continue; |
| 1313 | |
| 1314 | /* only allow hw_free() if no connected FEs are running */ |
| 1315 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1316 | continue; |
| 1317 | |
| 1318 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && |
| 1319 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1320 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && |
| 1321 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1322 | continue; |
| 1323 | |
| 1324 | soc_pcm_hw_free(be_substream); |
| 1325 | } |
| 1326 | |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, |
| 1331 | struct snd_pcm_hw_params *params) |
| 1332 | { |
| 1333 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1334 | int ret, stream = substream->stream; |
| 1335 | |
| 1336 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1337 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1338 | |
| 1339 | memcpy(&fe->dpcm[substream->stream].hw_params, params, |
| 1340 | sizeof(struct snd_pcm_hw_params)); |
| 1341 | ret = dpcm_be_dai_hw_params(fe, substream->stream); |
| 1342 | if (ret < 0) { |
| 1343 | dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret); |
| 1344 | goto out; |
| 1345 | } |
| 1346 | |
| 1347 | dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n", |
| 1348 | fe->dai_link->name, params_rate(params), |
| 1349 | params_channels(params), params_format(params)); |
| 1350 | |
| 1351 | /* call hw_params on the frontend */ |
| 1352 | ret = soc_pcm_hw_params(substream, params); |
| 1353 | if (ret < 0) { |
| 1354 | dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret); |
| 1355 | dpcm_be_dai_hw_free(fe, stream); |
| 1356 | } else |
| 1357 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; |
| 1358 | |
| 1359 | out: |
| 1360 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1361 | mutex_unlock(&fe->card->mutex); |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, |
| 1366 | struct snd_pcm_substream *substream, int cmd) |
| 1367 | { |
| 1368 | int ret; |
| 1369 | |
| 1370 | dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n", |
| 1371 | dpcm->fe->dai_link->name, cmd); |
| 1372 | |
| 1373 | ret = soc_pcm_trigger(substream, cmd); |
| 1374 | if (ret < 0) |
| 1375 | dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret); |
| 1376 | |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd) |
| 1381 | { |
| 1382 | struct snd_soc_dpcm *dpcm; |
| 1383 | int ret = 0; |
| 1384 | |
| 1385 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1386 | |
| 1387 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1388 | struct snd_pcm_substream *be_substream = |
| 1389 | snd_soc_dpcm_get_substream(be, stream); |
| 1390 | |
| 1391 | /* is this op for this BE ? */ |
| 1392 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1393 | continue; |
| 1394 | |
| 1395 | switch (cmd) { |
| 1396 | case SNDRV_PCM_TRIGGER_START: |
| 1397 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && |
| 1398 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1399 | continue; |
| 1400 | |
| 1401 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1402 | if (ret) |
| 1403 | return ret; |
| 1404 | |
| 1405 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1406 | break; |
| 1407 | case SNDRV_PCM_TRIGGER_RESUME: |
| 1408 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) |
| 1409 | continue; |
| 1410 | |
| 1411 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1412 | if (ret) |
| 1413 | return ret; |
| 1414 | |
| 1415 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1416 | break; |
| 1417 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1418 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) |
| 1419 | continue; |
| 1420 | |
| 1421 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1422 | if (ret) |
| 1423 | return ret; |
| 1424 | |
| 1425 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1426 | break; |
| 1427 | case SNDRV_PCM_TRIGGER_STOP: |
| 1428 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 1429 | continue; |
| 1430 | |
| 1431 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1432 | continue; |
| 1433 | |
| 1434 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1435 | if (ret) |
| 1436 | return ret; |
| 1437 | |
| 1438 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 1439 | break; |
| 1440 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1441 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) |
| 1442 | continue; |
| 1443 | |
| 1444 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1445 | continue; |
| 1446 | |
| 1447 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1448 | if (ret) |
| 1449 | return ret; |
| 1450 | |
| 1451 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; |
| 1452 | break; |
| 1453 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1454 | if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) |
| 1455 | continue; |
| 1456 | |
| 1457 | if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) |
| 1458 | continue; |
| 1459 | |
| 1460 | ret = dpcm_do_trigger(dpcm, be_substream, cmd); |
| 1461 | if (ret) |
| 1462 | return ret; |
| 1463 | |
| 1464 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | return ret; |
| 1470 | } |
| 1471 | EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); |
| 1472 | |
| 1473 | int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) |
| 1474 | { |
| 1475 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1476 | int stream = substream->stream, ret; |
| 1477 | enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; |
| 1478 | |
| 1479 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1480 | |
| 1481 | switch (trigger) { |
| 1482 | case SND_SOC_DPCM_TRIGGER_PRE: |
| 1483 | /* call trigger on the frontend before the backend. */ |
| 1484 | |
| 1485 | dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n", |
| 1486 | fe->dai_link->name, cmd); |
| 1487 | |
| 1488 | ret = soc_pcm_trigger(substream, cmd); |
| 1489 | if (ret < 0) { |
| 1490 | dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret); |
| 1491 | goto out; |
| 1492 | } |
| 1493 | |
| 1494 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 1495 | break; |
| 1496 | case SND_SOC_DPCM_TRIGGER_POST: |
| 1497 | /* call trigger on the frontend after the backend. */ |
| 1498 | |
| 1499 | ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); |
| 1500 | if (ret < 0) { |
| 1501 | dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret); |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
| 1505 | dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n", |
| 1506 | fe->dai_link->name, cmd); |
| 1507 | |
| 1508 | ret = soc_pcm_trigger(substream, cmd); |
| 1509 | break; |
| 1510 | default: |
| 1511 | dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd, |
| 1512 | fe->dai_link->name); |
| 1513 | ret = -EINVAL; |
| 1514 | goto out; |
| 1515 | } |
| 1516 | |
| 1517 | switch (cmd) { |
| 1518 | case SNDRV_PCM_TRIGGER_START: |
| 1519 | case SNDRV_PCM_TRIGGER_RESUME: |
| 1520 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1521 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 1522 | break; |
| 1523 | case SNDRV_PCM_TRIGGER_STOP: |
| 1524 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1525 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1526 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | out: |
| 1531 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1532 | return ret; |
| 1533 | } |
| 1534 | |
| 1535 | static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) |
| 1536 | { |
| 1537 | struct snd_soc_dpcm *dpcm; |
| 1538 | int ret = 0; |
| 1539 | |
| 1540 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1541 | |
| 1542 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1543 | struct snd_pcm_substream *be_substream = |
| 1544 | snd_soc_dpcm_get_substream(be, stream); |
| 1545 | |
| 1546 | /* is this op for this BE ? */ |
| 1547 | if (!snd_soc_dpcm_be_can_update(fe, be, stream)) |
| 1548 | continue; |
| 1549 | |
| 1550 | if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1551 | (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) |
| 1552 | continue; |
| 1553 | |
| 1554 | dev_dbg(be->dev, "dpcm: prepare BE %s\n", |
| 1555 | dpcm->fe->dai_link->name); |
| 1556 | |
| 1557 | ret = soc_pcm_prepare(be_substream); |
| 1558 | if (ret < 0) { |
| 1559 | dev_err(be->dev, "dpcm: backend prepare failed %d\n", |
| 1560 | ret); |
| 1561 | break; |
| 1562 | } |
| 1563 | |
| 1564 | be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 1565 | } |
| 1566 | return ret; |
| 1567 | } |
| 1568 | |
| 1569 | int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) |
| 1570 | { |
| 1571 | struct snd_soc_pcm_runtime *fe = substream->private_data; |
| 1572 | int stream = substream->stream, ret = 0; |
| 1573 | |
| 1574 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1575 | |
| 1576 | dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name); |
| 1577 | |
| 1578 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 1579 | |
| 1580 | /* there is no point preparing this FE if there are no BEs */ |
| 1581 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 1582 | dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n", |
| 1583 | fe->dai_link->name); |
| 1584 | ret = -EINVAL; |
| 1585 | goto out; |
| 1586 | } |
| 1587 | |
| 1588 | ret = dpcm_be_dai_prepare(fe, substream->stream); |
| 1589 | if (ret < 0) |
| 1590 | goto out; |
| 1591 | |
| 1592 | /* call prepare on the frontend */ |
| 1593 | ret = soc_pcm_prepare(substream); |
| 1594 | if (ret < 0) { |
| 1595 | dev_err(fe->dev,"dpcm: prepare FE %s failed\n", |
| 1596 | fe->dai_link->name); |
| 1597 | goto out; |
| 1598 | } |
| 1599 | |
| 1600 | /* run the stream event for each BE */ |
| 1601 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 1602 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 1603 | |
| 1604 | out: |
| 1605 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 1606 | mutex_unlock(&fe->card->mutex); |
| 1607 | |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute) |
| 1613 | { |
| 1614 | struct snd_soc_dpcm *dpcm; |
| 1615 | struct list_head *clients = |
| 1616 | &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients; |
| 1617 | |
| 1618 | list_for_each_entry(dpcm, clients, list_be) { |
| 1619 | |
| 1620 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1621 | struct snd_soc_dai *dai = be->codec_dai; |
| 1622 | struct snd_soc_dai_driver *drv = dai->driver; |
| 1623 | |
| 1624 | if (be->dai_link->ignore_suspend) |
| 1625 | continue; |
| 1626 | |
| 1627 | dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name); |
| 1628 | |
| 1629 | if (drv->ops->digital_mute && dai->playback_active) |
| 1630 | drv->ops->digital_mute(dai, mute); |
| 1631 | } |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) |
| 1637 | { |
| 1638 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1639 | struct snd_soc_dpcm *dpcm; |
| 1640 | struct snd_soc_dapm_widget_list *list; |
| 1641 | int ret; |
| 1642 | int stream = fe_substream->stream; |
| 1643 | |
| 1644 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1645 | fe->dpcm[stream].runtime = fe_substream->runtime; |
| 1646 | |
| 1647 | if (dpcm_path_get(fe, stream, &list) <= 0) { |
| 1648 | dev_warn(fe->dev, "asoc: %s no valid %s route\n", |
| 1649 | fe->dai_link->name, stream ? "capture" : "playback"); |
| 1650 | mutex_unlock(&fe->card->mutex); |
| 1651 | return -EINVAL; |
| 1652 | } |
| 1653 | |
| 1654 | /* calculate valid and active FE <-> BE dpcms */ |
| 1655 | dpcm_process_paths(fe, stream, &list, 1); |
| 1656 | |
| 1657 | ret = dpcm_fe_dai_startup(fe_substream); |
| 1658 | if (ret < 0) { |
| 1659 | /* clean up all links */ |
| 1660 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 1661 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 1662 | |
| 1663 | dpcm_be_disconnect(fe, stream); |
| 1664 | fe->dpcm[stream].runtime = NULL; |
| 1665 | } |
| 1666 | |
| 1667 | dpcm_clear_pending_state(fe, stream); |
| 1668 | dpcm_path_put(&list); |
| 1669 | mutex_unlock(&fe->card->mutex); |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) |
| 1674 | { |
| 1675 | struct snd_soc_pcm_runtime *fe = fe_substream->private_data; |
| 1676 | struct snd_soc_dpcm *dpcm; |
| 1677 | int stream = fe_substream->stream, ret; |
| 1678 | |
| 1679 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 1680 | ret = dpcm_fe_dai_shutdown(fe_substream); |
| 1681 | |
| 1682 | /* mark FE's links ready to prune */ |
| 1683 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 1684 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 1685 | |
| 1686 | dpcm_be_disconnect(fe, stream); |
| 1687 | |
| 1688 | fe->dpcm[stream].runtime = NULL; |
| 1689 | mutex_unlock(&fe->card->mutex); |
| 1690 | return ret; |
| 1691 | } |
| 1692 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1693 | /* create a new pcm */ |
| 1694 | int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) |
| 1695 | { |
| 1696 | struct snd_soc_codec *codec = rtd->codec; |
| 1697 | struct snd_soc_platform *platform = rtd->platform; |
| 1698 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 1699 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 1700 | struct snd_pcm *pcm; |
| 1701 | char new_name[64]; |
| 1702 | int ret = 0, playback = 0, capture = 0; |
| 1703 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1704 | if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { |
| 1705 | if (cpu_dai->driver->playback.channels_min) |
| 1706 | playback = 1; |
| 1707 | if (cpu_dai->driver->capture.channels_min) |
| 1708 | capture = 1; |
| 1709 | } else { |
| 1710 | if (codec_dai->driver->playback.channels_min) |
| 1711 | playback = 1; |
| 1712 | if (codec_dai->driver->capture.channels_min) |
| 1713 | capture = 1; |
| 1714 | } |
Sangsu Park | a500231 | 2012-01-02 17:15:10 +0900 | [diff] [blame] | 1715 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1716 | /* create the PCM */ |
| 1717 | if (rtd->dai_link->no_pcm) { |
| 1718 | snprintf(new_name, sizeof(new_name), "(%s)", |
| 1719 | rtd->dai_link->stream_name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1720 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1721 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 1722 | playback, capture, &pcm); |
| 1723 | } else { |
| 1724 | if (rtd->dai_link->dynamic) |
| 1725 | snprintf(new_name, sizeof(new_name), "%s (*)", |
| 1726 | rtd->dai_link->stream_name); |
| 1727 | else |
| 1728 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
| 1729 | rtd->dai_link->stream_name, codec_dai->name, num); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1730 | |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1731 | ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, |
| 1732 | capture, &pcm); |
| 1733 | } |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1734 | if (ret < 0) { |
| 1735 | printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name); |
| 1736 | return ret; |
| 1737 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1738 | dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1739 | |
| 1740 | /* DAPM dai link stream work */ |
| 1741 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
| 1742 | |
| 1743 | rtd->pcm = pcm; |
| 1744 | pcm->private_data = rtd; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1745 | |
| 1746 | if (rtd->dai_link->no_pcm) { |
| 1747 | if (playback) |
| 1748 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; |
| 1749 | if (capture) |
| 1750 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; |
| 1751 | goto out; |
| 1752 | } |
| 1753 | |
| 1754 | /* ASoC PCM operations */ |
| 1755 | if (rtd->dai_link->dynamic) { |
| 1756 | rtd->ops.open = dpcm_fe_dai_open; |
| 1757 | rtd->ops.hw_params = dpcm_fe_dai_hw_params; |
| 1758 | rtd->ops.prepare = dpcm_fe_dai_prepare; |
| 1759 | rtd->ops.trigger = dpcm_fe_dai_trigger; |
| 1760 | rtd->ops.hw_free = dpcm_fe_dai_hw_free; |
| 1761 | rtd->ops.close = dpcm_fe_dai_close; |
| 1762 | rtd->ops.pointer = soc_pcm_pointer; |
| 1763 | } else { |
| 1764 | rtd->ops.open = soc_pcm_open; |
| 1765 | rtd->ops.hw_params = soc_pcm_hw_params; |
| 1766 | rtd->ops.prepare = soc_pcm_prepare; |
| 1767 | rtd->ops.trigger = soc_pcm_trigger; |
| 1768 | rtd->ops.hw_free = soc_pcm_hw_free; |
| 1769 | rtd->ops.close = soc_pcm_close; |
| 1770 | rtd->ops.pointer = soc_pcm_pointer; |
| 1771 | } |
| 1772 | |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1773 | if (platform->driver->ops) { |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1774 | rtd->ops.ack = platform->driver->ops->ack; |
| 1775 | rtd->ops.copy = platform->driver->ops->copy; |
| 1776 | rtd->ops.silence = platform->driver->ops->silence; |
| 1777 | rtd->ops.page = platform->driver->ops->page; |
| 1778 | rtd->ops.mmap = platform->driver->ops->mmap; |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | if (playback) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1782 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1783 | |
| 1784 | if (capture) |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1785 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1786 | |
| 1787 | if (platform->driver->pcm_new) { |
| 1788 | ret = platform->driver->pcm_new(rtd); |
| 1789 | if (ret < 0) { |
| 1790 | pr_err("asoc: platform pcm constructor failed\n"); |
| 1791 | return ret; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | pcm->private_free = platform->driver->pcm_free; |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1796 | out: |
Liam Girdwood | ddee627 | 2011-06-09 14:45:53 +0100 | [diff] [blame] | 1797 | printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name, |
| 1798 | cpu_dai->name); |
| 1799 | return ret; |
| 1800 | } |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 1801 | |
| 1802 | /* is the current PCM operation for this FE ? */ |
| 1803 | int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) |
| 1804 | { |
| 1805 | if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) |
| 1806 | return 1; |
| 1807 | return 0; |
| 1808 | } |
| 1809 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); |
| 1810 | |
| 1811 | /* is the current PCM operation for this BE ? */ |
| 1812 | int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, |
| 1813 | struct snd_soc_pcm_runtime *be, int stream) |
| 1814 | { |
| 1815 | if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || |
| 1816 | ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && |
| 1817 | be->dpcm[stream].runtime_update)) |
| 1818 | return 1; |
| 1819 | return 0; |
| 1820 | } |
| 1821 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); |
| 1822 | |
| 1823 | /* get the substream for this BE */ |
| 1824 | struct snd_pcm_substream * |
| 1825 | snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) |
| 1826 | { |
| 1827 | return be->pcm->streams[stream].substream; |
| 1828 | } |
| 1829 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); |
| 1830 | |
| 1831 | /* get the BE runtime state */ |
| 1832 | enum snd_soc_dpcm_state |
| 1833 | snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream) |
| 1834 | { |
| 1835 | return be->dpcm[stream].state; |
| 1836 | } |
| 1837 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state); |
| 1838 | |
| 1839 | /* set the BE runtime state */ |
| 1840 | void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be, |
| 1841 | int stream, enum snd_soc_dpcm_state state) |
| 1842 | { |
| 1843 | be->dpcm[stream].state = state; |
| 1844 | } |
| 1845 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state); |
| 1846 | |
| 1847 | /* |
| 1848 | * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE |
| 1849 | * are not running, paused or suspended for the specified stream direction. |
| 1850 | */ |
| 1851 | int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, |
| 1852 | struct snd_soc_pcm_runtime *be, int stream) |
| 1853 | { |
| 1854 | struct snd_soc_dpcm *dpcm; |
| 1855 | int state; |
| 1856 | |
| 1857 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 1858 | |
| 1859 | if (dpcm->fe == fe) |
| 1860 | continue; |
| 1861 | |
| 1862 | state = dpcm->fe->dpcm[stream].state; |
| 1863 | if (state == SND_SOC_DPCM_STATE_START || |
| 1864 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 1865 | state == SND_SOC_DPCM_STATE_SUSPEND) |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | /* it's safe to free/stop this BE DAI */ |
| 1870 | return 1; |
| 1871 | } |
| 1872 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); |
| 1873 | |
| 1874 | /* |
| 1875 | * We can only change hw params a BE DAI if any of it's FE are not prepared, |
| 1876 | * running, paused or suspended for the specified stream direction. |
| 1877 | */ |
| 1878 | int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, |
| 1879 | struct snd_soc_pcm_runtime *be, int stream) |
| 1880 | { |
| 1881 | struct snd_soc_dpcm *dpcm; |
| 1882 | int state; |
| 1883 | |
| 1884 | list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) { |
| 1885 | |
| 1886 | if (dpcm->fe == fe) |
| 1887 | continue; |
| 1888 | |
| 1889 | state = dpcm->fe->dpcm[stream].state; |
| 1890 | if (state == SND_SOC_DPCM_STATE_START || |
| 1891 | state == SND_SOC_DPCM_STATE_PAUSED || |
| 1892 | state == SND_SOC_DPCM_STATE_SUSPEND || |
| 1893 | state == SND_SOC_DPCM_STATE_PREPARE) |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* it's safe to change hw_params */ |
| 1898 | return 1; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); |
Liam Girdwood | f86dcef | 2012-04-25 12:12:50 +0100 | [diff] [blame^] | 1901 | |
| 1902 | #ifdef CONFIG_DEBUG_FS |
| 1903 | static char *dpcm_state_string(enum snd_soc_dpcm_state state) |
| 1904 | { |
| 1905 | switch (state) { |
| 1906 | case SND_SOC_DPCM_STATE_NEW: |
| 1907 | return "new"; |
| 1908 | case SND_SOC_DPCM_STATE_OPEN: |
| 1909 | return "open"; |
| 1910 | case SND_SOC_DPCM_STATE_HW_PARAMS: |
| 1911 | return "hw_params"; |
| 1912 | case SND_SOC_DPCM_STATE_PREPARE: |
| 1913 | return "prepare"; |
| 1914 | case SND_SOC_DPCM_STATE_START: |
| 1915 | return "start"; |
| 1916 | case SND_SOC_DPCM_STATE_STOP: |
| 1917 | return "stop"; |
| 1918 | case SND_SOC_DPCM_STATE_SUSPEND: |
| 1919 | return "suspend"; |
| 1920 | case SND_SOC_DPCM_STATE_PAUSED: |
| 1921 | return "paused"; |
| 1922 | case SND_SOC_DPCM_STATE_HW_FREE: |
| 1923 | return "hw_free"; |
| 1924 | case SND_SOC_DPCM_STATE_CLOSE: |
| 1925 | return "close"; |
| 1926 | } |
| 1927 | |
| 1928 | return "unknown"; |
| 1929 | } |
| 1930 | |
| 1931 | static int dpcm_state_open_file(struct inode *inode, struct file *file) |
| 1932 | { |
| 1933 | file->private_data = inode->i_private; |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
| 1937 | static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, |
| 1938 | int stream, char *buf, size_t size) |
| 1939 | { |
| 1940 | struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; |
| 1941 | struct snd_soc_dpcm *dpcm; |
| 1942 | ssize_t offset = 0; |
| 1943 | |
| 1944 | /* FE state */ |
| 1945 | offset += snprintf(buf + offset, size - offset, |
| 1946 | "[%s - %s]\n", fe->dai_link->name, |
| 1947 | stream ? "Capture" : "Playback"); |
| 1948 | |
| 1949 | offset += snprintf(buf + offset, size - offset, "State: %s\n", |
| 1950 | dpcm_state_string(fe->dpcm[stream].state)); |
| 1951 | |
| 1952 | if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1953 | (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 1954 | offset += snprintf(buf + offset, size - offset, |
| 1955 | "Hardware Params: " |
| 1956 | "Format = %s, Channels = %d, Rate = %d\n", |
| 1957 | snd_pcm_format_name(params_format(params)), |
| 1958 | params_channels(params), |
| 1959 | params_rate(params)); |
| 1960 | |
| 1961 | /* BEs state */ |
| 1962 | offset += snprintf(buf + offset, size - offset, "Backends:\n"); |
| 1963 | |
| 1964 | if (list_empty(&fe->dpcm[stream].be_clients)) { |
| 1965 | offset += snprintf(buf + offset, size - offset, |
| 1966 | " No active DSP links\n"); |
| 1967 | goto out; |
| 1968 | } |
| 1969 | |
| 1970 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { |
| 1971 | struct snd_soc_pcm_runtime *be = dpcm->be; |
| 1972 | params = &dpcm->hw_params; |
| 1973 | |
| 1974 | offset += snprintf(buf + offset, size - offset, |
| 1975 | "- %s\n", be->dai_link->name); |
| 1976 | |
| 1977 | offset += snprintf(buf + offset, size - offset, |
| 1978 | " State: %s\n", |
| 1979 | dpcm_state_string(be->dpcm[stream].state)); |
| 1980 | |
| 1981 | if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && |
| 1982 | (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) |
| 1983 | offset += snprintf(buf + offset, size - offset, |
| 1984 | " Hardware Params: " |
| 1985 | "Format = %s, Channels = %d, Rate = %d\n", |
| 1986 | snd_pcm_format_name(params_format(params)), |
| 1987 | params_channels(params), |
| 1988 | params_rate(params)); |
| 1989 | } |
| 1990 | |
| 1991 | out: |
| 1992 | return offset; |
| 1993 | } |
| 1994 | |
| 1995 | static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, |
| 1996 | size_t count, loff_t *ppos) |
| 1997 | { |
| 1998 | struct snd_soc_pcm_runtime *fe = file->private_data; |
| 1999 | ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; |
| 2000 | char *buf; |
| 2001 | |
| 2002 | buf = kmalloc(out_count, GFP_KERNEL); |
| 2003 | if (!buf) |
| 2004 | return -ENOMEM; |
| 2005 | |
| 2006 | if (fe->cpu_dai->driver->playback.channels_min) |
| 2007 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK, |
| 2008 | buf + offset, out_count - offset); |
| 2009 | |
| 2010 | if (fe->cpu_dai->driver->capture.channels_min) |
| 2011 | offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE, |
| 2012 | buf + offset, out_count - offset); |
| 2013 | |
| 2014 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); |
| 2015 | |
| 2016 | kfree(buf); |
| 2017 | |
| 2018 | return ret; |
| 2019 | } |
| 2020 | |
| 2021 | static const struct file_operations dpcm_state_fops = { |
| 2022 | .open = dpcm_state_open_file, |
| 2023 | .read = dpcm_state_read_file, |
| 2024 | .llseek = default_llseek, |
| 2025 | }; |
| 2026 | |
| 2027 | int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) |
| 2028 | { |
| 2029 | rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, |
| 2030 | rtd->card->debugfs_card_root); |
| 2031 | if (!rtd->debugfs_dpcm_root) { |
| 2032 | dev_dbg(rtd->dev, |
| 2033 | "ASoC: Failed to create dpcm debugfs directory %s\n", |
| 2034 | rtd->dai_link->name); |
| 2035 | return -EINVAL; |
| 2036 | } |
| 2037 | |
| 2038 | rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644, |
| 2039 | rtd->debugfs_dpcm_root, |
| 2040 | rtd, &dpcm_state_fops); |
| 2041 | |
| 2042 | return 0; |
| 2043 | } |
| 2044 | #endif |