1ed517582SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2ed517582SKuninori Morimoto // 3ed517582SKuninori Morimoto // soc-pcm.c -- ALSA SoC PCM 4ed517582SKuninori Morimoto // 5ed517582SKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC. 6ed517582SKuninori Morimoto // Copyright 2005 Openedhand Ltd. 7ed517582SKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd. 8ed517582SKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc. 9ed517582SKuninori Morimoto // 10ed517582SKuninori Morimoto // Authors: Liam Girdwood <lrg@ti.com> 11ed517582SKuninori Morimoto // Mark Brown <broonie@opensource.wolfsonmicro.com> 12ddee627cSLiam Girdwood 13ddee627cSLiam Girdwood #include <linux/kernel.h> 14ddee627cSLiam Girdwood #include <linux/init.h> 15ddee627cSLiam Girdwood #include <linux/delay.h> 16988e8cc4SNicolin Chen #include <linux/pinctrl/consumer.h> 17d6652ef8SMark Brown #include <linux/pm_runtime.h> 18ddee627cSLiam Girdwood #include <linux/slab.h> 19ddee627cSLiam Girdwood #include <linux/workqueue.h> 2001d7584cSLiam Girdwood #include <linux/export.h> 21f86dcef8SLiam Girdwood #include <linux/debugfs.h> 22ddee627cSLiam Girdwood #include <sound/core.h> 23ddee627cSLiam Girdwood #include <sound/pcm.h> 24ddee627cSLiam Girdwood #include <sound/pcm_params.h> 25ddee627cSLiam Girdwood #include <sound/soc.h> 2601d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 27ddee627cSLiam Girdwood #include <sound/initval.h> 28ddee627cSLiam Girdwood 2901d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS 8 3001d7584cSLiam Girdwood 31c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS 32c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state) 33c3212829SKuninori Morimoto { 34c3212829SKuninori Morimoto switch (state) { 35c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_NEW: 36c3212829SKuninori Morimoto return "new"; 37c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_OPEN: 38c3212829SKuninori Morimoto return "open"; 39c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_PARAMS: 40c3212829SKuninori Morimoto return "hw_params"; 41c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PREPARE: 42c3212829SKuninori Morimoto return "prepare"; 43c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_START: 44c3212829SKuninori Morimoto return "start"; 45c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_STOP: 46c3212829SKuninori Morimoto return "stop"; 47c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_SUSPEND: 48c3212829SKuninori Morimoto return "suspend"; 49c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PAUSED: 50c3212829SKuninori Morimoto return "paused"; 51c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_FREE: 52c3212829SKuninori Morimoto return "hw_free"; 53c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_CLOSE: 54c3212829SKuninori Morimoto return "close"; 55c3212829SKuninori Morimoto } 56c3212829SKuninori Morimoto 57c3212829SKuninori Morimoto return "unknown"; 58c3212829SKuninori Morimoto } 59c3212829SKuninori Morimoto 60c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 61c3212829SKuninori Morimoto int stream, char *buf, size_t size) 62c3212829SKuninori Morimoto { 63c3212829SKuninori Morimoto struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 64c3212829SKuninori Morimoto struct snd_soc_dpcm *dpcm; 65c3212829SKuninori Morimoto ssize_t offset = 0; 66c3212829SKuninori Morimoto unsigned long flags; 67c3212829SKuninori Morimoto 68c3212829SKuninori Morimoto /* FE state */ 69d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 70c3212829SKuninori Morimoto "[%s - %s]\n", fe->dai_link->name, 71c3212829SKuninori Morimoto stream ? "Capture" : "Playback"); 72c3212829SKuninori Morimoto 73d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "State: %s\n", 74c3212829SKuninori Morimoto dpcm_state_string(fe->dpcm[stream].state)); 75c3212829SKuninori Morimoto 76c3212829SKuninori Morimoto if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 77c3212829SKuninori Morimoto (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 78d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 79c3212829SKuninori Morimoto "Hardware Params: " 80c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 81c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 82c3212829SKuninori Morimoto params_channels(params), 83c3212829SKuninori Morimoto params_rate(params)); 84c3212829SKuninori Morimoto 85c3212829SKuninori Morimoto /* BEs state */ 86d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "Backends:\n"); 87c3212829SKuninori Morimoto 88c3212829SKuninori Morimoto if (list_empty(&fe->dpcm[stream].be_clients)) { 89d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 90c3212829SKuninori Morimoto " No active DSP links\n"); 91c3212829SKuninori Morimoto goto out; 92c3212829SKuninori Morimoto } 93c3212829SKuninori Morimoto 94c3212829SKuninori Morimoto spin_lock_irqsave(&fe->card->dpcm_lock, flags); 95c3212829SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 96c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 97c3212829SKuninori Morimoto params = &dpcm->hw_params; 98c3212829SKuninori Morimoto 99d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 100c3212829SKuninori Morimoto "- %s\n", be->dai_link->name); 101c3212829SKuninori Morimoto 102d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 103c3212829SKuninori Morimoto " State: %s\n", 104c3212829SKuninori Morimoto dpcm_state_string(be->dpcm[stream].state)); 105c3212829SKuninori Morimoto 106c3212829SKuninori Morimoto if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 107c3212829SKuninori Morimoto (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 108d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 109c3212829SKuninori Morimoto " Hardware Params: " 110c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 111c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 112c3212829SKuninori Morimoto params_channels(params), 113c3212829SKuninori Morimoto params_rate(params)); 114c3212829SKuninori Morimoto } 115c3212829SKuninori Morimoto spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 116c3212829SKuninori Morimoto out: 117c3212829SKuninori Morimoto return offset; 118c3212829SKuninori Morimoto } 119c3212829SKuninori Morimoto 120c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 121c3212829SKuninori Morimoto size_t count, loff_t *ppos) 122c3212829SKuninori Morimoto { 123c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *fe = file->private_data; 124c3212829SKuninori Morimoto ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 125c3212829SKuninori Morimoto int stream; 126c3212829SKuninori Morimoto char *buf; 127c3212829SKuninori Morimoto 1286e1276a5SBard Liao if (fe->num_cpus > 1) { 1296e1276a5SBard Liao dev_err(fe->dev, 1306e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 1316e1276a5SBard Liao return -EINVAL; 1326e1276a5SBard Liao } 1336e1276a5SBard Liao 134c3212829SKuninori Morimoto buf = kmalloc(out_count, GFP_KERNEL); 135c3212829SKuninori Morimoto if (!buf) 136c3212829SKuninori Morimoto return -ENOMEM; 137c3212829SKuninori Morimoto 138c3212829SKuninori Morimoto for_each_pcm_streams(stream) 139c3212829SKuninori Morimoto if (snd_soc_dai_stream_valid(fe->cpu_dai, stream)) 140c3212829SKuninori Morimoto offset += dpcm_show_state(fe, stream, 141c3212829SKuninori Morimoto buf + offset, 142c3212829SKuninori Morimoto out_count - offset); 143c3212829SKuninori Morimoto 144c3212829SKuninori Morimoto ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 145c3212829SKuninori Morimoto 146c3212829SKuninori Morimoto kfree(buf); 147c3212829SKuninori Morimoto return ret; 148c3212829SKuninori Morimoto } 149c3212829SKuninori Morimoto 150c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = { 151c3212829SKuninori Morimoto .open = simple_open, 152c3212829SKuninori Morimoto .read = dpcm_state_read_file, 153c3212829SKuninori Morimoto .llseek = default_llseek, 154c3212829SKuninori Morimoto }; 155c3212829SKuninori Morimoto 156c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 157c3212829SKuninori Morimoto { 158c3212829SKuninori Morimoto if (!rtd->dai_link) 159c3212829SKuninori Morimoto return; 160c3212829SKuninori Morimoto 161c3212829SKuninori Morimoto if (!rtd->dai_link->dynamic) 162c3212829SKuninori Morimoto return; 163c3212829SKuninori Morimoto 164c3212829SKuninori Morimoto if (!rtd->card->debugfs_card_root) 165c3212829SKuninori Morimoto return; 166c3212829SKuninori Morimoto 167c3212829SKuninori Morimoto rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 168c3212829SKuninori Morimoto rtd->card->debugfs_card_root); 169c3212829SKuninori Morimoto 170c3212829SKuninori Morimoto debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 171c3212829SKuninori Morimoto rtd, &dpcm_state_fops); 172c3212829SKuninori Morimoto } 173154dae87SKuninori Morimoto 174154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream) 175154dae87SKuninori Morimoto { 176154dae87SKuninori Morimoto char *name; 177154dae87SKuninori Morimoto 178154dae87SKuninori Morimoto name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name, 179154dae87SKuninori Morimoto stream ? "capture" : "playback"); 180154dae87SKuninori Morimoto if (name) { 181154dae87SKuninori Morimoto dpcm->debugfs_state = debugfs_create_dir( 182154dae87SKuninori Morimoto name, dpcm->fe->debugfs_dpcm_root); 183154dae87SKuninori Morimoto debugfs_create_u32("state", 0644, dpcm->debugfs_state, 184154dae87SKuninori Morimoto &dpcm->state); 185154dae87SKuninori Morimoto kfree(name); 186154dae87SKuninori Morimoto } 187154dae87SKuninori Morimoto } 188154dae87SKuninori Morimoto 189154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 190154dae87SKuninori Morimoto { 191154dae87SKuninori Morimoto debugfs_remove_recursive(dpcm->debugfs_state); 192154dae87SKuninori Morimoto } 193154dae87SKuninori Morimoto 194154dae87SKuninori Morimoto #else 195154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, 196154dae87SKuninori Morimoto int stream) 197154dae87SKuninori Morimoto { 198154dae87SKuninori Morimoto } 199154dae87SKuninori Morimoto 200154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 201154dae87SKuninori Morimoto { 202154dae87SKuninori Morimoto } 203c3212829SKuninori Morimoto #endif 204c3212829SKuninori Morimoto 205f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd, 206f183f927SKuninori Morimoto struct snd_pcm_substream *substream) 207f183f927SKuninori Morimoto { 208f183f927SKuninori Morimoto if (rtd->dai_link->ops && 209f183f927SKuninori Morimoto rtd->dai_link->ops->startup) 210f183f927SKuninori Morimoto return rtd->dai_link->ops->startup(substream); 211f183f927SKuninori Morimoto return 0; 212f183f927SKuninori Morimoto } 213f183f927SKuninori Morimoto 2140be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd, 2150be429f9SKuninori Morimoto struct snd_pcm_substream *substream) 2160be429f9SKuninori Morimoto { 2170be429f9SKuninori Morimoto if (rtd->dai_link->ops && 2180be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown) 2190be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown(substream); 2200be429f9SKuninori Morimoto } 2210be429f9SKuninori Morimoto 22244c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd, 22344c1a75bSKuninori Morimoto struct snd_pcm_substream *substream) 22444c1a75bSKuninori Morimoto { 22544c1a75bSKuninori Morimoto if (rtd->dai_link->ops && 22644c1a75bSKuninori Morimoto rtd->dai_link->ops->prepare) 22744c1a75bSKuninori Morimoto return rtd->dai_link->ops->prepare(substream); 22844c1a75bSKuninori Morimoto return 0; 22944c1a75bSKuninori Morimoto } 23044c1a75bSKuninori Morimoto 231de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd, 232de9ad990SKuninori Morimoto struct snd_pcm_substream *substream, 233de9ad990SKuninori Morimoto struct snd_pcm_hw_params *params) 234de9ad990SKuninori Morimoto { 235de9ad990SKuninori Morimoto if (rtd->dai_link->ops && 236de9ad990SKuninori Morimoto rtd->dai_link->ops->hw_params) 237de9ad990SKuninori Morimoto return rtd->dai_link->ops->hw_params(substream, params); 238de9ad990SKuninori Morimoto return 0; 239de9ad990SKuninori Morimoto } 240de9ad990SKuninori Morimoto 24149f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd, 24249f020e5SKuninori Morimoto struct snd_pcm_substream *substream) 24349f020e5SKuninori Morimoto { 24449f020e5SKuninori Morimoto if (rtd->dai_link->ops && 24549f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free) 24649f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free(substream); 24749f020e5SKuninori Morimoto } 24849f020e5SKuninori Morimoto 249ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd, 250ad2bf9f2SKuninori Morimoto struct snd_pcm_substream *substream, 251ad2bf9f2SKuninori Morimoto int cmd) 252ad2bf9f2SKuninori Morimoto { 253ad2bf9f2SKuninori Morimoto if (rtd->dai_link->ops && 254ad2bf9f2SKuninori Morimoto rtd->dai_link->ops->trigger) 255ad2bf9f2SKuninori Morimoto return rtd->dai_link->ops->trigger(substream, cmd); 256ad2bf9f2SKuninori Morimoto return 0; 257ad2bf9f2SKuninori Morimoto } 258ad2bf9f2SKuninori Morimoto 2597a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, 2607a5aaba4SKuninori Morimoto int stream, int action) 2617a5aaba4SKuninori Morimoto { 26219bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 2637a5aaba4SKuninori Morimoto struct snd_soc_dai *codec_dai; 2647a5aaba4SKuninori Morimoto int i; 2657a5aaba4SKuninori Morimoto 2667a5aaba4SKuninori Morimoto lockdep_assert_held(&rtd->card->pcm_mutex); 2677a5aaba4SKuninori Morimoto 268a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 2690f6011fdSKuninori Morimoto cpu_dai->stream_active[stream] += action; 27019bdcc7aSShreyas NC 271a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 2720f6011fdSKuninori Morimoto codec_dai->stream_active[stream] += action; 2737a5aaba4SKuninori Morimoto 274a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 2757a5aaba4SKuninori Morimoto cpu_dai->active += action; 2767a5aaba4SKuninori Morimoto cpu_dai->component->active += action; 27719bdcc7aSShreyas NC } 278a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 2797a5aaba4SKuninori Morimoto codec_dai->active += action; 2807a5aaba4SKuninori Morimoto codec_dai->component->active += action; 2817a5aaba4SKuninori Morimoto } 2827a5aaba4SKuninori Morimoto } 2837a5aaba4SKuninori Morimoto 28490996f43SLars-Peter Clausen /** 28524894b76SLars-Peter Clausen * snd_soc_runtime_activate() - Increment active count for PCM runtime components 28624894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is activated 28724894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 28824894b76SLars-Peter Clausen * 28924894b76SLars-Peter Clausen * Increments the active count for all the DAIs and components attached to a PCM 29024894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is opened. 29124894b76SLars-Peter Clausen * 29272b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 29324894b76SLars-Peter Clausen */ 29424894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) 29524894b76SLars-Peter Clausen { 2967a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, 1); 29724894b76SLars-Peter Clausen } 298*f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_runtime_activate); 29924894b76SLars-Peter Clausen 30024894b76SLars-Peter Clausen /** 30124894b76SLars-Peter Clausen * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components 30224894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is deactivated 30324894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 30424894b76SLars-Peter Clausen * 30524894b76SLars-Peter Clausen * Decrements the active count for all the DAIs and components attached to a PCM 30624894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is closed. 30724894b76SLars-Peter Clausen * 30872b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 30924894b76SLars-Peter Clausen */ 31024894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) 31124894b76SLars-Peter Clausen { 3127a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, -1); 31324894b76SLars-Peter Clausen } 314*f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_runtime_deactivate); 31524894b76SLars-Peter Clausen 31624894b76SLars-Peter Clausen /** 317208a1589SLars-Peter Clausen * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 318208a1589SLars-Peter Clausen * @rtd: The ASoC PCM runtime that should be checked. 319208a1589SLars-Peter Clausen * 320208a1589SLars-Peter Clausen * This function checks whether the power down delay should be ignored for a 321208a1589SLars-Peter Clausen * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 322208a1589SLars-Peter Clausen * been configured to ignore the delay, or if none of the components benefits 323208a1589SLars-Peter Clausen * from having the delay. 324208a1589SLars-Peter Clausen */ 325208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 326208a1589SLars-Peter Clausen { 327fbb16563SKuninori Morimoto struct snd_soc_component *component; 3282e5894d7SBenoit Cousson bool ignore = true; 329613fb500SKuninori Morimoto int i; 3302e5894d7SBenoit Cousson 331208a1589SLars-Peter Clausen if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 332208a1589SLars-Peter Clausen return true; 333208a1589SLars-Peter Clausen 334613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 33572c38184SKuninori Morimoto ignore &= !component->driver->use_pmdown_time; 336fbb16563SKuninori Morimoto 337fbb16563SKuninori Morimoto return ignore; 338208a1589SLars-Peter Clausen } 339208a1589SLars-Peter Clausen 340208a1589SLars-Peter Clausen /** 34190996f43SLars-Peter Clausen * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 34290996f43SLars-Peter Clausen * @substream: the pcm substream 34390996f43SLars-Peter Clausen * @hw: the hardware parameters 34490996f43SLars-Peter Clausen * 34590996f43SLars-Peter Clausen * Sets the substream runtime hardware parameters. 34690996f43SLars-Peter Clausen */ 34790996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 34890996f43SLars-Peter Clausen const struct snd_pcm_hardware *hw) 34990996f43SLars-Peter Clausen { 35090996f43SLars-Peter Clausen struct snd_pcm_runtime *runtime = substream->runtime; 35190996f43SLars-Peter Clausen runtime->hw.info = hw->info; 35290996f43SLars-Peter Clausen runtime->hw.formats = hw->formats; 35390996f43SLars-Peter Clausen runtime->hw.period_bytes_min = hw->period_bytes_min; 35490996f43SLars-Peter Clausen runtime->hw.period_bytes_max = hw->period_bytes_max; 35590996f43SLars-Peter Clausen runtime->hw.periods_min = hw->periods_min; 35690996f43SLars-Peter Clausen runtime->hw.periods_max = hw->periods_max; 35790996f43SLars-Peter Clausen runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 35890996f43SLars-Peter Clausen runtime->hw.fifo_size = hw->fifo_size; 35990996f43SLars-Peter Clausen return 0; 36090996f43SLars-Peter Clausen } 36190996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 36290996f43SLars-Peter Clausen 36301d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */ 36423607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 36501d7584cSLiam Girdwood int event) 36601d7584cSLiam Girdwood { 36701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 36801d7584cSLiam Girdwood 3698d6258a4SKuninori Morimoto for_each_dpcm_be(fe, dir, dpcm) { 37001d7584cSLiam Girdwood 37101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 37201d7584cSLiam Girdwood 373103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 37401d7584cSLiam Girdwood be->dai_link->name, event, dir); 37501d7584cSLiam Girdwood 376b1cd2e34SBanajit Goswami if ((event == SND_SOC_DAPM_STREAM_STOP) && 377b1cd2e34SBanajit Goswami (be->dpcm[dir].users >= 1)) 378b1cd2e34SBanajit Goswami continue; 379b1cd2e34SBanajit Goswami 38001d7584cSLiam Girdwood snd_soc_dapm_stream_event(be, dir, event); 38101d7584cSLiam Girdwood } 38201d7584cSLiam Girdwood 38301d7584cSLiam Girdwood snd_soc_dapm_stream_event(fe, dir, event); 38401d7584cSLiam Girdwood 38501d7584cSLiam Girdwood return 0; 38601d7584cSLiam Girdwood } 38701d7584cSLiam Girdwood 38817841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 38917841020SDong Aisheng struct snd_soc_dai *soc_dai) 390ddee627cSLiam Girdwood { 391ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 392ddee627cSLiam Girdwood int ret; 393ddee627cSLiam Girdwood 3943635bf09SNicolin Chen if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 3953635bf09SNicolin Chen rtd->dai_link->symmetric_rates)) { 3963635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 3973635bf09SNicolin Chen soc_dai->rate); 398ddee627cSLiam Girdwood 3994dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 400ddee627cSLiam Girdwood SNDRV_PCM_HW_PARAM_RATE, 4014dcdd43bSLars-Peter Clausen soc_dai->rate); 402ddee627cSLiam Girdwood if (ret < 0) { 40317841020SDong Aisheng dev_err(soc_dai->dev, 4043635bf09SNicolin Chen "ASoC: Unable to apply rate constraint: %d\n", 405103d84a3SLiam Girdwood ret); 406ddee627cSLiam Girdwood return ret; 407ddee627cSLiam Girdwood } 4083635bf09SNicolin Chen } 4093635bf09SNicolin Chen 4103635bf09SNicolin Chen if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 4113635bf09SNicolin Chen rtd->dai_link->symmetric_channels)) { 4123635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 4133635bf09SNicolin Chen soc_dai->channels); 4143635bf09SNicolin Chen 4154dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 4163635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_CHANNELS, 4173635bf09SNicolin Chen soc_dai->channels); 4183635bf09SNicolin Chen if (ret < 0) { 4193635bf09SNicolin Chen dev_err(soc_dai->dev, 4203635bf09SNicolin Chen "ASoC: Unable to apply channel symmetry constraint: %d\n", 4213635bf09SNicolin Chen ret); 4223635bf09SNicolin Chen return ret; 4233635bf09SNicolin Chen } 4243635bf09SNicolin Chen } 4253635bf09SNicolin Chen 4263635bf09SNicolin Chen if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 4273635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits)) { 4283635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 4293635bf09SNicolin Chen soc_dai->sample_bits); 4303635bf09SNicolin Chen 4314dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 4323635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 4333635bf09SNicolin Chen soc_dai->sample_bits); 4343635bf09SNicolin Chen if (ret < 0) { 4353635bf09SNicolin Chen dev_err(soc_dai->dev, 4363635bf09SNicolin Chen "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 4373635bf09SNicolin Chen ret); 4383635bf09SNicolin Chen return ret; 4393635bf09SNicolin Chen } 4403635bf09SNicolin Chen } 441ddee627cSLiam Girdwood 442ddee627cSLiam Girdwood return 0; 443ddee627cSLiam Girdwood } 444ddee627cSLiam Girdwood 4453635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 4463635bf09SNicolin Chen struct snd_pcm_hw_params *params) 4473635bf09SNicolin Chen { 4483635bf09SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 44919bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 4500b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 4512e5894d7SBenoit Cousson unsigned int rate, channels, sample_bits, symmetry, i; 4523635bf09SNicolin Chen 4533635bf09SNicolin Chen rate = params_rate(params); 4543635bf09SNicolin Chen channels = params_channels(params); 4553635bf09SNicolin Chen sample_bits = snd_pcm_format_physical_width(params_format(params)); 4563635bf09SNicolin Chen 4573635bf09SNicolin Chen /* reject unmatched parameters when applying symmetry */ 45819bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_rates; 45919bdcc7aSShreyas NC 460a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 46119bdcc7aSShreyas NC symmetry |= cpu_dai->driver->symmetric_rates; 4622e5894d7SBenoit Cousson 463a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 4640b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_rates; 4652e5894d7SBenoit Cousson 46619bdcc7aSShreyas NC if (symmetry) { 467a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 46819bdcc7aSShreyas NC if (cpu_dai->rate && cpu_dai->rate != rate) { 4693635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 4703635bf09SNicolin Chen cpu_dai->rate, rate); 4713635bf09SNicolin Chen return -EINVAL; 4723635bf09SNicolin Chen } 47319bdcc7aSShreyas NC } 47419bdcc7aSShreyas NC } 4753635bf09SNicolin Chen 47619bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_channels; 47719bdcc7aSShreyas NC 478a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 47919bdcc7aSShreyas NC symmetry |= cpu_dai->driver->symmetric_channels; 4802e5894d7SBenoit Cousson 481a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 4820b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_channels; 4832e5894d7SBenoit Cousson 48419bdcc7aSShreyas NC if (symmetry) { 485a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 48619bdcc7aSShreyas NC if (cpu_dai->channels && 48719bdcc7aSShreyas NC cpu_dai->channels != channels) { 4883635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 4893635bf09SNicolin Chen cpu_dai->channels, channels); 4903635bf09SNicolin Chen return -EINVAL; 4913635bf09SNicolin Chen } 49219bdcc7aSShreyas NC } 49319bdcc7aSShreyas NC } 4943635bf09SNicolin Chen 49519bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_samplebits; 49619bdcc7aSShreyas NC 497a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 49819bdcc7aSShreyas NC symmetry |= cpu_dai->driver->symmetric_samplebits; 4992e5894d7SBenoit Cousson 500a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 5010b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_samplebits; 5022e5894d7SBenoit Cousson 50319bdcc7aSShreyas NC if (symmetry) { 504a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 50519bdcc7aSShreyas NC if (cpu_dai->sample_bits && 50619bdcc7aSShreyas NC cpu_dai->sample_bits != sample_bits) { 5073635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 5083635bf09SNicolin Chen cpu_dai->sample_bits, sample_bits); 5093635bf09SNicolin Chen return -EINVAL; 5103635bf09SNicolin Chen } 51119bdcc7aSShreyas NC } 51219bdcc7aSShreyas NC } 513ddee627cSLiam Girdwood 514ddee627cSLiam Girdwood return 0; 515ddee627cSLiam Girdwood } 516ddee627cSLiam Girdwood 51762e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 51862e5f676SLars-Peter Clausen { 51962e5f676SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = substream->private_data; 52062e5f676SLars-Peter Clausen struct snd_soc_dai_link *link = rtd->dai_link; 5210b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 52219bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 5232e5894d7SBenoit Cousson unsigned int symmetry, i; 52462e5f676SLars-Peter Clausen 52519bdcc7aSShreyas NC symmetry = link->symmetric_rates || 52619bdcc7aSShreyas NC link->symmetric_channels || 52719bdcc7aSShreyas NC link->symmetric_samplebits; 52819bdcc7aSShreyas NC 529a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 53019bdcc7aSShreyas NC symmetry = symmetry || 53119bdcc7aSShreyas NC cpu_dai->driver->symmetric_rates || 53219bdcc7aSShreyas NC cpu_dai->driver->symmetric_channels || 53319bdcc7aSShreyas NC cpu_dai->driver->symmetric_samplebits; 5342e5894d7SBenoit Cousson 535a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 5362e5894d7SBenoit Cousson symmetry = symmetry || 5370b7990e3SKuninori Morimoto codec_dai->driver->symmetric_rates || 5380b7990e3SKuninori Morimoto codec_dai->driver->symmetric_channels || 5390b7990e3SKuninori Morimoto codec_dai->driver->symmetric_samplebits; 5402e5894d7SBenoit Cousson 5412e5894d7SBenoit Cousson return symmetry; 54262e5f676SLars-Peter Clausen } 54362e5f676SLars-Peter Clausen 5442e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 54558ba9b25SMark Brown { 5462e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 547c6068d3aSTakashi Iwai int ret; 54858ba9b25SMark Brown 54958ba9b25SMark Brown if (!bits) 55058ba9b25SMark Brown return; 55158ba9b25SMark Brown 5520e2a3751SLars-Peter Clausen ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 55358ba9b25SMark Brown if (ret != 0) 5540e2a3751SLars-Peter Clausen dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 5550e2a3751SLars-Peter Clausen bits, ret); 55658ba9b25SMark Brown } 55758ba9b25SMark Brown 558c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 559bd477c31SLars-Peter Clausen { 560c8dd1fecSBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 56119bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 5622e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 56357be9206SKuninori Morimoto struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; 56457be9206SKuninori Morimoto int stream = substream->stream; 5652e5894d7SBenoit Cousson int i; 56619bdcc7aSShreyas NC unsigned int bits = 0, cpu_bits = 0; 567c8dd1fecSBenoit Cousson 568a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 56957be9206SKuninori Morimoto pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); 57057be9206SKuninori Morimoto 57157be9206SKuninori Morimoto if (pcm_codec->sig_bits == 0) { 5722e5894d7SBenoit Cousson bits = 0; 5732e5894d7SBenoit Cousson break; 5742e5894d7SBenoit Cousson } 57557be9206SKuninori Morimoto bits = max(pcm_codec->sig_bits, bits); 5762e5894d7SBenoit Cousson } 57757be9206SKuninori Morimoto 578a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 57957be9206SKuninori Morimoto pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 58019bdcc7aSShreyas NC 58119bdcc7aSShreyas NC if (pcm_cpu->sig_bits == 0) { 58219bdcc7aSShreyas NC cpu_bits = 0; 58319bdcc7aSShreyas NC break; 58419bdcc7aSShreyas NC } 58519bdcc7aSShreyas NC cpu_bits = max(pcm_cpu->sig_bits, cpu_bits); 58619bdcc7aSShreyas NC } 587c8dd1fecSBenoit Cousson 5882e5894d7SBenoit Cousson soc_pcm_set_msb(substream, bits); 5892e5894d7SBenoit Cousson soc_pcm_set_msb(substream, cpu_bits); 590c8dd1fecSBenoit Cousson } 591c8dd1fecSBenoit Cousson 5925854a464SSamuel Holland /** 5935854a464SSamuel Holland * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream 5945854a464SSamuel Holland * @rtd: ASoC PCM runtime 5955854a464SSamuel Holland * @hw: PCM hardware parameters (output) 5965854a464SSamuel Holland * @stream: Direction of the PCM stream 5975854a464SSamuel Holland * 5985854a464SSamuel Holland * Calculates the subset of stream parameters supported by all DAIs 5995854a464SSamuel Holland * associated with the PCM stream. 6005854a464SSamuel Holland */ 6015854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd, 6025854a464SSamuel Holland struct snd_pcm_hardware *hw, int stream) 603bd477c31SLars-Peter Clausen { 6040b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 60519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 6062e5894d7SBenoit Cousson struct snd_soc_pcm_stream *codec_stream; 6072e5894d7SBenoit Cousson struct snd_soc_pcm_stream *cpu_stream; 6082e5894d7SBenoit Cousson unsigned int chan_min = 0, chan_max = UINT_MAX; 60919bdcc7aSShreyas NC unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX; 6102e5894d7SBenoit Cousson unsigned int rate_min = 0, rate_max = UINT_MAX; 61119bdcc7aSShreyas NC unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX; 61219bdcc7aSShreyas NC unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX; 6132e5894d7SBenoit Cousson u64 formats = ULLONG_MAX; 6142e5894d7SBenoit Cousson int i; 61578e45c99SLars-Peter Clausen 61619bdcc7aSShreyas NC /* first calculate min/max only for CPUs in the DAI link */ 617a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 6180e9cf4c4SBard Liao 6190e9cf4c4SBard Liao /* 6200e9cf4c4SBard Liao * Skip CPUs which don't support the current stream type. 6210e9cf4c4SBard Liao * Otherwise, since the rate, channel, and format values will 6220e9cf4c4SBard Liao * zero in that case, we would have no usable settings left, 6230e9cf4c4SBard Liao * causing the resulting setup to fail. 6240e9cf4c4SBard Liao */ 6255854a464SSamuel Holland if (!snd_soc_dai_stream_valid(cpu_dai, stream)) 6260e9cf4c4SBard Liao continue; 6270e9cf4c4SBard Liao 62819bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 62978e45c99SLars-Peter Clausen 63019bdcc7aSShreyas NC cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min); 63119bdcc7aSShreyas NC cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max); 63219bdcc7aSShreyas NC cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min); 63319bdcc7aSShreyas NC cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max); 63419bdcc7aSShreyas NC formats &= cpu_stream->formats; 63519bdcc7aSShreyas NC cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates, 63619bdcc7aSShreyas NC cpu_rates); 63719bdcc7aSShreyas NC } 63819bdcc7aSShreyas NC 63919bdcc7aSShreyas NC /* second calculate min/max only for CODECs in the DAI link */ 640a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 641cde79035SRicard Wanderlof 642cde79035SRicard Wanderlof /* 643cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type. 644cde79035SRicard Wanderlof * Otherwise, since the rate, channel, and format values will 645cde79035SRicard Wanderlof * zero in that case, we would have no usable settings left, 646cde79035SRicard Wanderlof * causing the resulting setup to fail. 647cde79035SRicard Wanderlof */ 64825c2f515SKuninori Morimoto if (!snd_soc_dai_stream_valid(codec_dai, stream)) 649cde79035SRicard Wanderlof continue; 650cde79035SRicard Wanderlof 651acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); 652acf253c1SKuninori Morimoto 6532e5894d7SBenoit Cousson chan_min = max(chan_min, codec_stream->channels_min); 6542e5894d7SBenoit Cousson chan_max = min(chan_max, codec_stream->channels_max); 6552e5894d7SBenoit Cousson rate_min = max(rate_min, codec_stream->rate_min); 6562e5894d7SBenoit Cousson rate_max = min_not_zero(rate_max, codec_stream->rate_max); 6572e5894d7SBenoit Cousson formats &= codec_stream->formats; 6582e5894d7SBenoit Cousson rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); 6592e5894d7SBenoit Cousson } 6602e5894d7SBenoit Cousson 6615854a464SSamuel Holland /* Verify both a valid CPU DAI and a valid CODEC DAI were found */ 6625854a464SSamuel Holland if (!chan_min || !cpu_chan_min) 6635854a464SSamuel Holland return -EINVAL; 6645854a464SSamuel Holland 6652e5894d7SBenoit Cousson /* 6662e5894d7SBenoit Cousson * chan min/max cannot be enforced if there are multiple CODEC DAIs 66719bdcc7aSShreyas NC * connected to CPU DAI(s), use CPU DAI's directly and let 6682e5894d7SBenoit Cousson * channel allocation be fixed up later 6692e5894d7SBenoit Cousson */ 6702e5894d7SBenoit Cousson if (rtd->num_codecs > 1) { 67119bdcc7aSShreyas NC chan_min = cpu_chan_min; 67219bdcc7aSShreyas NC chan_max = cpu_chan_max; 6732e5894d7SBenoit Cousson } 6742e5894d7SBenoit Cousson 67519bdcc7aSShreyas NC /* finally find a intersection between CODECs and CPUs */ 67619bdcc7aSShreyas NC hw->channels_min = max(chan_min, cpu_chan_min); 67719bdcc7aSShreyas NC hw->channels_max = min(chan_max, cpu_chan_max); 67819bdcc7aSShreyas NC hw->formats = formats; 67919bdcc7aSShreyas NC hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates); 68078e45c99SLars-Peter Clausen 6815854a464SSamuel Holland snd_pcm_hw_limit_rates(hw); 68278e45c99SLars-Peter Clausen 68319bdcc7aSShreyas NC hw->rate_min = max(hw->rate_min, cpu_rate_min); 6842e5894d7SBenoit Cousson hw->rate_min = max(hw->rate_min, rate_min); 68519bdcc7aSShreyas NC hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max); 6862e5894d7SBenoit Cousson hw->rate_max = min_not_zero(hw->rate_max, rate_max); 6875854a464SSamuel Holland 6885854a464SSamuel Holland return 0; 6895854a464SSamuel Holland } 6905854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw); 6915854a464SSamuel Holland 6925854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 6935854a464SSamuel Holland { 6945854a464SSamuel Holland struct snd_pcm_hardware *hw = &substream->runtime->hw; 6955854a464SSamuel Holland struct snd_soc_pcm_runtime *rtd = substream->private_data; 6965854a464SSamuel Holland u64 formats = hw->formats; 6975854a464SSamuel Holland 6985854a464SSamuel Holland /* 6995854a464SSamuel Holland * At least one CPU and one CODEC should match. Otherwise, we should 7005854a464SSamuel Holland * have bailed out on a higher level, since there would be no CPU or 7015854a464SSamuel Holland * CODEC to support the transfer direction in that case. 7025854a464SSamuel Holland */ 7035854a464SSamuel Holland snd_soc_runtime_calc_hw(rtd, hw, substream->stream); 7045854a464SSamuel Holland 7055854a464SSamuel Holland if (formats) 7065854a464SSamuel Holland hw->formats &= formats; 707bd477c31SLars-Peter Clausen } 708bd477c31SLars-Peter Clausen 709dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream) 710e7ecfdb7SKuninori Morimoto { 711e7ecfdb7SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 712d2aaa8d8SKai Vehmanen struct snd_soc_component *last = NULL; 713e7ecfdb7SKuninori Morimoto struct snd_soc_component *component; 714613fb500SKuninori Morimoto int i, ret = 0; 715e7ecfdb7SKuninori Morimoto 716613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 717d2aaa8d8SKai Vehmanen last = component; 718d2aaa8d8SKai Vehmanen 7194a81e8f3SKuninori Morimoto ret = snd_soc_component_module_get_when_open(component); 7204a81e8f3SKuninori Morimoto if (ret < 0) { 721e7ecfdb7SKuninori Morimoto dev_err(component->dev, 722e7ecfdb7SKuninori Morimoto "ASoC: can't get module %s\n", 723e7ecfdb7SKuninori Morimoto component->name); 724d2aaa8d8SKai Vehmanen break; 725e7ecfdb7SKuninori Morimoto } 726e7ecfdb7SKuninori Morimoto 727ae2f4849SKuninori Morimoto ret = snd_soc_component_open(component, substream); 728e7ecfdb7SKuninori Morimoto if (ret < 0) { 729d2aaa8d8SKai Vehmanen snd_soc_component_module_put_when_close(component); 730e7ecfdb7SKuninori Morimoto dev_err(component->dev, 731e7ecfdb7SKuninori Morimoto "ASoC: can't open component %s: %d\n", 732e7ecfdb7SKuninori Morimoto component->name, ret); 733d2aaa8d8SKai Vehmanen break; 734e7ecfdb7SKuninori Morimoto } 735e7ecfdb7SKuninori Morimoto } 736dd03907bSKuninori Morimoto 737d2aaa8d8SKai Vehmanen if (ret < 0) { 738d2aaa8d8SKai Vehmanen /* rollback on error */ 739d2aaa8d8SKai Vehmanen for_each_rtd_components(rtd, i, component) { 740d2aaa8d8SKai Vehmanen if (component == last) 741d2aaa8d8SKai Vehmanen break; 742d2aaa8d8SKai Vehmanen 743d2aaa8d8SKai Vehmanen snd_soc_component_close(component, substream); 744d2aaa8d8SKai Vehmanen snd_soc_component_module_put_when_close(component); 745d2aaa8d8SKai Vehmanen } 746d2aaa8d8SKai Vehmanen } 747d2aaa8d8SKai Vehmanen 748d2aaa8d8SKai Vehmanen return ret; 749e7ecfdb7SKuninori Morimoto } 750e7ecfdb7SKuninori Morimoto 751dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream) 752244e2936SCharles Keepax { 753244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 754244e2936SCharles Keepax struct snd_soc_component *component; 755e82ebffcSKuninori Morimoto int i, r, ret = 0; 756244e2936SCharles Keepax 757613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 758e82ebffcSKuninori Morimoto r = snd_soc_component_close(component, substream); 759e82ebffcSKuninori Morimoto if (r < 0) 760e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 761e82ebffcSKuninori Morimoto 7624a81e8f3SKuninori Morimoto snd_soc_component_module_put_when_close(component); 763244e2936SCharles Keepax } 764244e2936SCharles Keepax 7653672beb8SKuninori Morimoto return ret; 766244e2936SCharles Keepax } 767244e2936SCharles Keepax 76858ba9b25SMark Brown /* 76962c86d1dSKuninori Morimoto * Called by ALSA when a PCM substream is closed. Private data can be 77062c86d1dSKuninori Morimoto * freed here. The cpu DAI, codec DAI, machine and components are also 77162c86d1dSKuninori Morimoto * shutdown. 77262c86d1dSKuninori Morimoto */ 77362c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream) 77462c86d1dSKuninori Morimoto { 77562c86d1dSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 77662c86d1dSKuninori Morimoto struct snd_soc_component *component; 77719bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 77862c86d1dSKuninori Morimoto struct snd_soc_dai *codec_dai; 77962c86d1dSKuninori Morimoto int i; 78062c86d1dSKuninori Morimoto 78162c86d1dSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 78262c86d1dSKuninori Morimoto 78362c86d1dSKuninori Morimoto snd_soc_runtime_deactivate(rtd, substream->stream); 78462c86d1dSKuninori Morimoto 785a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 78662c86d1dSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 78762c86d1dSKuninori Morimoto 788a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 78962c86d1dSKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 79062c86d1dSKuninori Morimoto 79162c86d1dSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 79262c86d1dSKuninori Morimoto 79362c86d1dSKuninori Morimoto soc_pcm_components_close(substream); 79462c86d1dSKuninori Morimoto 79562c86d1dSKuninori Morimoto snd_soc_dapm_stream_stop(rtd, substream->stream); 79662c86d1dSKuninori Morimoto 79762c86d1dSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 79862c86d1dSKuninori Morimoto 79962c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 80062c86d1dSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 80162c86d1dSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 80262c86d1dSKuninori Morimoto } 80362c86d1dSKuninori Morimoto 80462c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) 80562c86d1dSKuninori Morimoto if (!component->active) 80662c86d1dSKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 80762c86d1dSKuninori Morimoto 80862c86d1dSKuninori Morimoto return 0; 80962c86d1dSKuninori Morimoto } 81062c86d1dSKuninori Morimoto 81162c86d1dSKuninori Morimoto /* 812ddee627cSLiam Girdwood * Called by ALSA when a PCM substream is opened, the runtime->hw record is 813ddee627cSLiam Girdwood * then initialized and any private data can be allocated. This also calls 814ef050becSCharles Keepax * startup for the cpu DAI, component, machine and codec DAI. 815ddee627cSLiam Girdwood */ 816ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream) 817ddee627cSLiam Girdwood { 818ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 819ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 82090be711eSKuninori Morimoto struct snd_soc_component *component; 82119bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 8222e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 8232e5894d7SBenoit Cousson const char *codec_dai_name = "multicodec"; 82419bdcc7aSShreyas NC const char *cpu_dai_name = "multicpu"; 825244e2936SCharles Keepax int i, ret = 0; 826ddee627cSLiam Girdwood 82776c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 82876c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 82990be711eSKuninori Morimoto 830613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 83190be711eSKuninori Morimoto pm_runtime_get_sync(component->dev); 832d6652ef8SMark Brown 83372b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 834ddee627cSLiam Girdwood 8355d9fa03eSKuninori Morimoto ret = soc_pcm_components_open(substream); 8365d9fa03eSKuninori Morimoto if (ret < 0) 8375d9fa03eSKuninori Morimoto goto component_err; 8385d9fa03eSKuninori Morimoto 8395d9fa03eSKuninori Morimoto ret = soc_rtd_startup(rtd, substream); 8405d9fa03eSKuninori Morimoto if (ret < 0) { 8415d9fa03eSKuninori Morimoto pr_err("ASoC: %s startup failed: %d\n", 8425d9fa03eSKuninori Morimoto rtd->dai_link->name, ret); 843d2aaa8d8SKai Vehmanen goto rtd_startup_err; 8445d9fa03eSKuninori Morimoto } 8455d9fa03eSKuninori Morimoto 846ddee627cSLiam Girdwood /* startup the audio subsystem */ 847a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 8485a52a045SKuninori Morimoto ret = snd_soc_dai_startup(cpu_dai, substream); 849ddee627cSLiam Girdwood if (ret < 0) { 8505a52a045SKuninori Morimoto dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n", 8515a52a045SKuninori Morimoto cpu_dai->name, ret); 8525d9fa03eSKuninori Morimoto goto cpu_dai_err; 853ddee627cSLiam Girdwood } 85419bdcc7aSShreyas NC } 855ddee627cSLiam Girdwood 856a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 8575a52a045SKuninori Morimoto ret = snd_soc_dai_startup(codec_dai, substream); 858ddee627cSLiam Girdwood if (ret < 0) { 8592e5894d7SBenoit Cousson dev_err(codec_dai->dev, 8602e5894d7SBenoit Cousson "ASoC: can't open codec %s: %d\n", 8612e5894d7SBenoit Cousson codec_dai->name, ret); 8625d9fa03eSKuninori Morimoto goto config_err; 863ddee627cSLiam Girdwood } 864ddee627cSLiam Girdwood 8652e5894d7SBenoit Cousson if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 8662e5894d7SBenoit Cousson codec_dai->tx_mask = 0; 8672e5894d7SBenoit Cousson else 8682e5894d7SBenoit Cousson codec_dai->rx_mask = 0; 8692e5894d7SBenoit Cousson } 8702e5894d7SBenoit Cousson 87101d7584cSLiam Girdwood /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 87201d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 87301d7584cSLiam Girdwood goto dynamic; 87401d7584cSLiam Girdwood 875ddee627cSLiam Girdwood /* Check that the codec and cpu DAIs are compatible */ 8762e5894d7SBenoit Cousson soc_pcm_init_runtime_hw(substream); 8772e5894d7SBenoit Cousson 8782e5894d7SBenoit Cousson if (rtd->num_codecs == 1) 8792e5894d7SBenoit Cousson codec_dai_name = rtd->codec_dai->name; 880ddee627cSLiam Girdwood 88119bdcc7aSShreyas NC if (rtd->num_cpus == 1) 88219bdcc7aSShreyas NC cpu_dai_name = rtd->cpu_dai->name; 88319bdcc7aSShreyas NC 88462e5f676SLars-Peter Clausen if (soc_pcm_has_symmetry(substream)) 88562e5f676SLars-Peter Clausen runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 88662e5f676SLars-Peter Clausen 887ddee627cSLiam Girdwood ret = -EINVAL; 888ddee627cSLiam Girdwood if (!runtime->hw.rates) { 889103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 89019bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 891ddee627cSLiam Girdwood goto config_err; 892ddee627cSLiam Girdwood } 893ddee627cSLiam Girdwood if (!runtime->hw.formats) { 894103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 89519bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 896ddee627cSLiam Girdwood goto config_err; 897ddee627cSLiam Girdwood } 898ddee627cSLiam Girdwood if (!runtime->hw.channels_min || !runtime->hw.channels_max || 899ddee627cSLiam Girdwood runtime->hw.channels_min > runtime->hw.channels_max) { 900103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 90119bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 902ddee627cSLiam Girdwood goto config_err; 903ddee627cSLiam Girdwood } 904ddee627cSLiam Girdwood 905c8dd1fecSBenoit Cousson soc_pcm_apply_msb(substream); 90658ba9b25SMark Brown 907ddee627cSLiam Girdwood /* Symmetry only applies if we've already got an active stream. */ 908a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 90917841020SDong Aisheng if (cpu_dai->active) { 91017841020SDong Aisheng ret = soc_pcm_apply_symmetry(substream, cpu_dai); 91117841020SDong Aisheng if (ret != 0) 91217841020SDong Aisheng goto config_err; 91317841020SDong Aisheng } 91419bdcc7aSShreyas NC } 91517841020SDong Aisheng 916a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 9170b7990e3SKuninori Morimoto if (codec_dai->active) { 9180b7990e3SKuninori Morimoto ret = soc_pcm_apply_symmetry(substream, codec_dai); 919ddee627cSLiam Girdwood if (ret != 0) 920ddee627cSLiam Girdwood goto config_err; 921ddee627cSLiam Girdwood } 9222e5894d7SBenoit Cousson } 923ddee627cSLiam Girdwood 924103d84a3SLiam Girdwood pr_debug("ASoC: %s <-> %s info:\n", 92519bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 926103d84a3SLiam Girdwood pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 927103d84a3SLiam Girdwood pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 928ddee627cSLiam Girdwood runtime->hw.channels_max); 929103d84a3SLiam Girdwood pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 930ddee627cSLiam Girdwood runtime->hw.rate_max); 931ddee627cSLiam Girdwood 93201d7584cSLiam Girdwood dynamic: 93324894b76SLars-Peter Clausen 93424894b76SLars-Peter Clausen snd_soc_runtime_activate(rtd, substream->stream); 93524894b76SLars-Peter Clausen 93672b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 937ddee627cSLiam Girdwood return 0; 938ddee627cSLiam Girdwood 939ddee627cSLiam Girdwood config_err: 940a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 941330fcb51SKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 9425d9fa03eSKuninori Morimoto cpu_dai_err: 943a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 9445d9fa03eSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 9452e5894d7SBenoit Cousson 9465d9fa03eSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 947d2aaa8d8SKai Vehmanen rtd_startup_err: 948dd03907bSKuninori Morimoto soc_pcm_components_close(substream); 949d2aaa8d8SKai Vehmanen component_err: 95072b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 951d6652ef8SMark Brown 952613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 95390be711eSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 95490be711eSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 9553f809783SSanyog Kale } 9563f809783SSanyog Kale 95776c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 95876c39e86SKuninori Morimoto if (!component->active) 95976c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 960d6652ef8SMark Brown 961ddee627cSLiam Girdwood return ret; 962ddee627cSLiam Girdwood } 963ddee627cSLiam Girdwood 9644bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 965a342031cSJerome Brunet { 966a342031cSJerome Brunet /* 967a342031cSJerome Brunet * Currently nothing to do for c2c links 968a342031cSJerome Brunet * Since c2c links are internal nodes in the DAPM graph and 969a342031cSJerome Brunet * don't interface with the outside world or application layer 970a342031cSJerome Brunet * we don't have to do any special handling on close. 971a342031cSJerome Brunet */ 972a342031cSJerome Brunet } 973a342031cSJerome Brunet 974ddee627cSLiam Girdwood /* 975ddee627cSLiam Girdwood * Called by ALSA when the PCM substream is prepared, can set format, sample 976ddee627cSLiam Girdwood * rate, etc. This function is non atomic and can be called multiple times, 977ddee627cSLiam Girdwood * it can refer to the runtime info. 978ddee627cSLiam Girdwood */ 979ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream) 980ddee627cSLiam Girdwood { 981ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 982b8135864SKuninori Morimoto struct snd_soc_component *component; 98319bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 9842e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 9852e5894d7SBenoit Cousson int i, ret = 0; 986ddee627cSLiam Girdwood 98772b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 988ddee627cSLiam Girdwood 98944c1a75bSKuninori Morimoto ret = soc_rtd_prepare(rtd, substream); 990ddee627cSLiam Girdwood if (ret < 0) { 99144c1a75bSKuninori Morimoto dev_err(rtd->card->dev, 99244c1a75bSKuninori Morimoto "ASoC: machine prepare error: %d\n", ret); 993ddee627cSLiam Girdwood goto out; 994ddee627cSLiam Girdwood } 995ddee627cSLiam Girdwood 996613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 9976d537233SKuninori Morimoto ret = snd_soc_component_prepare(component, substream); 998b8135864SKuninori Morimoto if (ret < 0) { 999b8135864SKuninori Morimoto dev_err(component->dev, 1000b8135864SKuninori Morimoto "ASoC: platform prepare error: %d\n", ret); 1001b8135864SKuninori Morimoto goto out; 1002b8135864SKuninori Morimoto } 1003b8135864SKuninori Morimoto } 1004b8135864SKuninori Morimoto 1005a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 10064beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(codec_dai, substream); 1007ddee627cSLiam Girdwood if (ret < 0) { 10082e5894d7SBenoit Cousson dev_err(codec_dai->dev, 100990cc7f1cSJarkko Nikula "ASoC: codec DAI prepare error: %d\n", 101090cc7f1cSJarkko Nikula ret); 1011ddee627cSLiam Girdwood goto out; 1012ddee627cSLiam Girdwood } 1013ddee627cSLiam Girdwood } 1014ddee627cSLiam Girdwood 1015a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 10164beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(cpu_dai, substream); 1017ddee627cSLiam Girdwood if (ret < 0) { 101890cc7f1cSJarkko Nikula dev_err(cpu_dai->dev, 101990cc7f1cSJarkko Nikula "ASoC: cpu DAI prepare error: %d\n", ret); 1020ddee627cSLiam Girdwood goto out; 1021ddee627cSLiam Girdwood } 102219bdcc7aSShreyas NC } 1023ddee627cSLiam Girdwood 1024ddee627cSLiam Girdwood /* cancel any delayed stream shutdown that is pending */ 1025ddee627cSLiam Girdwood if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 10269bffb1fbSMisael Lopez Cruz rtd->pop_wait) { 10279bffb1fbSMisael Lopez Cruz rtd->pop_wait = 0; 1028ddee627cSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 1029ddee627cSLiam Girdwood } 1030ddee627cSLiam Girdwood 1031d9b0951bSLiam Girdwood snd_soc_dapm_stream_event(rtd, substream->stream, 1032ddee627cSLiam Girdwood SND_SOC_DAPM_STREAM_START); 1033ddee627cSLiam Girdwood 1034a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) 10350b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 0, 10362e5894d7SBenoit Cousson substream->stream); 1037a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) 1038ae11601bSRamesh Babu snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); 1039ddee627cSLiam Girdwood 1040ddee627cSLiam Girdwood out: 104172b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1042ddee627cSLiam Girdwood return ret; 1043ddee627cSLiam Girdwood } 1044ddee627cSLiam Girdwood 10452e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 10462e5894d7SBenoit Cousson unsigned int mask) 10472e5894d7SBenoit Cousson { 10482e5894d7SBenoit Cousson struct snd_interval *interval; 10492e5894d7SBenoit Cousson int channels = hweight_long(mask); 10502e5894d7SBenoit Cousson 10512e5894d7SBenoit Cousson interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 10522e5894d7SBenoit Cousson interval->min = channels; 10532e5894d7SBenoit Cousson interval->max = channels; 10542e5894d7SBenoit Cousson } 10552e5894d7SBenoit Cousson 1056244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, 1057244e2936SCharles Keepax struct snd_soc_component *last) 1058244e2936SCharles Keepax { 1059244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 1060244e2936SCharles Keepax struct snd_soc_component *component; 1061e82ebffcSKuninori Morimoto int i, r, ret = 0; 1062244e2936SCharles Keepax 1063613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1064244e2936SCharles Keepax if (component == last) 1065244e2936SCharles Keepax break; 1066244e2936SCharles Keepax 1067e82ebffcSKuninori Morimoto r = snd_soc_component_hw_free(component, substream); 1068e82ebffcSKuninori Morimoto if (r < 0) 1069e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 1070244e2936SCharles Keepax } 1071244e2936SCharles Keepax 1072eae7136aSKuninori Morimoto return ret; 1073244e2936SCharles Keepax } 1074244e2936SCharles Keepax 1075ddee627cSLiam Girdwood /* 1076ddee627cSLiam Girdwood * Called by ALSA when the hardware params are set by application. This 1077ddee627cSLiam Girdwood * function can also be called multiple times and can allocate buffers 1078ddee627cSLiam Girdwood * (using snd_pcm_lib_* ). It's non-atomic. 1079ddee627cSLiam Girdwood */ 1080ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 1081ddee627cSLiam Girdwood struct snd_pcm_hw_params *params) 1082ddee627cSLiam Girdwood { 1083ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1084b8135864SKuninori Morimoto struct snd_soc_component *component; 108519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 10860b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 1087244e2936SCharles Keepax int i, ret = 0; 1088ddee627cSLiam Girdwood 108972b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 10905cca5951SShengjiu Wang 10915cca5951SShengjiu Wang ret = soc_pcm_params_symmetry(substream, params); 10925cca5951SShengjiu Wang if (ret) 10935cca5951SShengjiu Wang goto out; 10945cca5951SShengjiu Wang 1095de9ad990SKuninori Morimoto ret = soc_rtd_hw_params(rtd, substream, params); 1096ddee627cSLiam Girdwood if (ret < 0) { 1097de9ad990SKuninori Morimoto dev_err(rtd->card->dev, 1098de9ad990SKuninori Morimoto "ASoC: machine hw_params failed: %d\n", ret); 1099ddee627cSLiam Girdwood goto out; 1100ddee627cSLiam Girdwood } 1101ddee627cSLiam Girdwood 1102a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 11032e5894d7SBenoit Cousson struct snd_pcm_hw_params codec_params; 11042e5894d7SBenoit Cousson 1105cde79035SRicard Wanderlof /* 1106cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type, 1107cde79035SRicard Wanderlof * the idea being that if a CODEC is not used for the currently 1108cde79035SRicard Wanderlof * set up transfer direction, it should not need to be 1109cde79035SRicard Wanderlof * configured, especially since the configuration used might 1110cde79035SRicard Wanderlof * not even be supported by that CODEC. There may be cases 1111cde79035SRicard Wanderlof * however where a CODEC needs to be set up although it is 1112cde79035SRicard Wanderlof * actually not being used for the transfer, e.g. if a 1113cde79035SRicard Wanderlof * capture-only CODEC is acting as an LRCLK and/or BCLK master 1114cde79035SRicard Wanderlof * for the DAI link including a playback-only CODEC. 1115cde79035SRicard Wanderlof * If this becomes necessary, we will have to augment the 1116cde79035SRicard Wanderlof * machine driver setup with information on how to act, so 1117cde79035SRicard Wanderlof * we can do the right thing here. 1118cde79035SRicard Wanderlof */ 1119cde79035SRicard Wanderlof if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1120cde79035SRicard Wanderlof continue; 1121cde79035SRicard Wanderlof 11222e5894d7SBenoit Cousson /* copy params for each codec */ 11232e5894d7SBenoit Cousson codec_params = *params; 11242e5894d7SBenoit Cousson 11252e5894d7SBenoit Cousson /* fixup params based on TDM slot masks */ 1126570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1127570f18b6SRander Wang codec_dai->tx_mask) 11282e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 11292e5894d7SBenoit Cousson codec_dai->tx_mask); 1130570f18b6SRander Wang 1131570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 1132570f18b6SRander Wang codec_dai->rx_mask) 11332e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 11342e5894d7SBenoit Cousson codec_dai->rx_mask); 11352e5894d7SBenoit Cousson 1136aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(codec_dai, substream, 1137aa6166c2SKuninori Morimoto &codec_params); 113893e6958aSBenoit Cousson if(ret < 0) 1139ddee627cSLiam Girdwood goto codec_err; 1140ddee627cSLiam Girdwood 11412e5894d7SBenoit Cousson codec_dai->rate = params_rate(&codec_params); 11422e5894d7SBenoit Cousson codec_dai->channels = params_channels(&codec_params); 11432e5894d7SBenoit Cousson codec_dai->sample_bits = snd_pcm_format_physical_width( 11442e5894d7SBenoit Cousson params_format(&codec_params)); 1145078a85f2SCharles Keepax 1146078a85f2SCharles Keepax snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); 1147ddee627cSLiam Girdwood } 1148ddee627cSLiam Girdwood 1149a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 11500e9cf4c4SBard Liao /* 11510e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 11520e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 11530e9cf4c4SBard Liao */ 11540e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 11550e9cf4c4SBard Liao continue; 11560e9cf4c4SBard Liao 1157aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(cpu_dai, substream, params); 115893e6958aSBenoit Cousson if (ret < 0) 1159ddee627cSLiam Girdwood goto interface_err; 1160ddee627cSLiam Girdwood 116119bdcc7aSShreyas NC /* store the parameters for each DAI */ 1162ca58221dSKuninori Morimoto cpu_dai->rate = params_rate(params); 1163ca58221dSKuninori Morimoto cpu_dai->channels = params_channels(params); 1164ca58221dSKuninori Morimoto cpu_dai->sample_bits = 1165ca58221dSKuninori Morimoto snd_pcm_format_physical_width(params_format(params)); 1166ca58221dSKuninori Morimoto 1167ca58221dSKuninori Morimoto snd_soc_dapm_update_dai(substream, params, cpu_dai); 116819bdcc7aSShreyas NC } 1169ca58221dSKuninori Morimoto 1170613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1171245c539aSKuninori Morimoto ret = snd_soc_component_hw_params(component, substream, params); 1172244e2936SCharles Keepax if (ret < 0) { 1173b8135864SKuninori Morimoto dev_err(component->dev, 1174b8135864SKuninori Morimoto "ASoC: %s hw params failed: %d\n", 1175244e2936SCharles Keepax component->name, ret); 1176b8135864SKuninori Morimoto goto component_err; 1177244e2936SCharles Keepax } 1178244e2936SCharles Keepax } 1179244e2936SCharles Keepax component = NULL; 1180b8135864SKuninori Morimoto 1181ddee627cSLiam Girdwood out: 118272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1183ddee627cSLiam Girdwood return ret; 1184ddee627cSLiam Girdwood 1185b8135864SKuninori Morimoto component_err: 1186244e2936SCharles Keepax soc_pcm_components_hw_free(substream, component); 1187b8135864SKuninori Morimoto 118819bdcc7aSShreyas NC i = rtd->num_cpus; 1189ddee627cSLiam Girdwood 1190ddee627cSLiam Girdwood interface_err: 1191a4be4187SKuninori Morimoto for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) { 11920e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 11930e9cf4c4SBard Liao continue; 11940e9cf4c4SBard Liao 119519bdcc7aSShreyas NC snd_soc_dai_hw_free(cpu_dai, substream); 119619bdcc7aSShreyas NC cpu_dai->rate = 0; 119719bdcc7aSShreyas NC } 119819bdcc7aSShreyas NC 11992e5894d7SBenoit Cousson i = rtd->num_codecs; 1200ddee627cSLiam Girdwood 1201ddee627cSLiam Girdwood codec_err: 1202a4be4187SKuninori Morimoto for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) { 1203f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1204f47b9ad9SJerome Brunet continue; 1205f47b9ad9SJerome Brunet 1206846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 12072e5894d7SBenoit Cousson codec_dai->rate = 0; 12082e5894d7SBenoit Cousson } 12092e5894d7SBenoit Cousson 121049f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1211ddee627cSLiam Girdwood 121272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1213ddee627cSLiam Girdwood return ret; 1214ddee627cSLiam Girdwood } 1215ddee627cSLiam Girdwood 1216ddee627cSLiam Girdwood /* 1217ddee627cSLiam Girdwood * Frees resources allocated by hw_params, can be called multiple times 1218ddee627cSLiam Girdwood */ 1219ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1220ddee627cSLiam Girdwood { 1221ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 122219bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 12232e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 12242e5894d7SBenoit Cousson int i; 1225ddee627cSLiam Girdwood 122672b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 1227ddee627cSLiam Girdwood 1228d3383420SNicolin Chen /* clear the corresponding DAIs parameters when going to be inactive */ 1229a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1230d3383420SNicolin Chen if (cpu_dai->active == 1) { 1231d3383420SNicolin Chen cpu_dai->rate = 0; 1232d3383420SNicolin Chen cpu_dai->channels = 0; 1233d3383420SNicolin Chen cpu_dai->sample_bits = 0; 1234d3383420SNicolin Chen } 123519bdcc7aSShreyas NC } 1236d3383420SNicolin Chen 1237a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 1238d3383420SNicolin Chen if (codec_dai->active == 1) { 1239d3383420SNicolin Chen codec_dai->rate = 0; 1240d3383420SNicolin Chen codec_dai->channels = 0; 1241d3383420SNicolin Chen codec_dai->sample_bits = 0; 1242d3383420SNicolin Chen } 12432e5894d7SBenoit Cousson } 1244d3383420SNicolin Chen 1245ddee627cSLiam Girdwood /* apply codec digital mute */ 1246a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 124767ad8777SKuninori Morimoto int active = codec_dai->stream_active[substream->stream]; 12480f6011fdSKuninori Morimoto 124967ad8777SKuninori Morimoto if (active == 1) 12500b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 1, 12512e5894d7SBenoit Cousson substream->stream); 12522e5894d7SBenoit Cousson } 1253ddee627cSLiam Girdwood 1254a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1255a9ee331bSKuninori Morimoto int active = cpu_dai->stream_active[substream->stream]; 1256a9ee331bSKuninori Morimoto 1257a9ee331bSKuninori Morimoto if (active == 1) 1258a9ee331bSKuninori Morimoto snd_soc_dai_digital_mute(cpu_dai, 1, 1259a9ee331bSKuninori Morimoto substream->stream); 1260a9ee331bSKuninori Morimoto } 1261a9ee331bSKuninori Morimoto 1262ddee627cSLiam Girdwood /* free any machine hw params */ 126349f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1264ddee627cSLiam Girdwood 1265b8135864SKuninori Morimoto /* free any component resources */ 1266244e2936SCharles Keepax soc_pcm_components_hw_free(substream, NULL); 1267b8135864SKuninori Morimoto 1268ddee627cSLiam Girdwood /* now free hw params for the DAIs */ 1269a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 1270f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1271f47b9ad9SJerome Brunet continue; 1272f47b9ad9SJerome Brunet 1273846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 12742e5894d7SBenoit Cousson } 1275ddee627cSLiam Girdwood 1276a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 12770e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 12780e9cf4c4SBard Liao continue; 12790e9cf4c4SBard Liao 1280846faaedSKuninori Morimoto snd_soc_dai_hw_free(cpu_dai, substream); 12810e9cf4c4SBard Liao } 1282ddee627cSLiam Girdwood 128372b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1284ddee627cSLiam Girdwood return 0; 1285ddee627cSLiam Girdwood } 1286ddee627cSLiam Girdwood 12874378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd) 1288ddee627cSLiam Girdwood { 1289ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1290b8135864SKuninori Morimoto struct snd_soc_component *component; 129119bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 12922e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 12932e5894d7SBenoit Cousson int i, ret; 1294ddee627cSLiam Girdwood 1295ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 1296ddee627cSLiam Girdwood if (ret < 0) 1297ddee627cSLiam Girdwood return ret; 1298ddee627cSLiam Girdwood 1299613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 13005693d50cSKuninori Morimoto ret = snd_soc_component_trigger(component, substream, cmd); 1301b8135864SKuninori Morimoto if (ret < 0) 1302b8135864SKuninori Morimoto return ret; 1303b8135864SKuninori Morimoto } 1304b8135864SKuninori Morimoto 1305a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1306901e822bSDan Carpenter ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 1307ddee627cSLiam Girdwood if (ret < 0) 1308ddee627cSLiam Girdwood return ret; 130919bdcc7aSShreyas NC } 13104792b0dbSJarkko Nikula 1311a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 13124378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 13134378f1fbSPeter Ujfalusi if (ret < 0) 13144378f1fbSPeter Ujfalusi return ret; 13154378f1fbSPeter Ujfalusi } 13164378f1fbSPeter Ujfalusi 13174378f1fbSPeter Ujfalusi return 0; 13184378f1fbSPeter Ujfalusi } 13194378f1fbSPeter Ujfalusi 13204378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd) 13214378f1fbSPeter Ujfalusi { 13224378f1fbSPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 13234378f1fbSPeter Ujfalusi struct snd_soc_component *component; 132419bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 13254378f1fbSPeter Ujfalusi struct snd_soc_dai *codec_dai; 13264378f1fbSPeter Ujfalusi int i, ret; 13274378f1fbSPeter Ujfalusi 1328a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 13294378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 13304378f1fbSPeter Ujfalusi if (ret < 0) 13314378f1fbSPeter Ujfalusi return ret; 13324378f1fbSPeter Ujfalusi } 13334378f1fbSPeter Ujfalusi 1334a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 13354378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 13364378f1fbSPeter Ujfalusi if (ret < 0) 13374378f1fbSPeter Ujfalusi return ret; 133819bdcc7aSShreyas NC } 13394378f1fbSPeter Ujfalusi 1340613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 13414378f1fbSPeter Ujfalusi ret = snd_soc_component_trigger(component, substream, cmd); 13424378f1fbSPeter Ujfalusi if (ret < 0) 13434378f1fbSPeter Ujfalusi return ret; 13444378f1fbSPeter Ujfalusi } 13454378f1fbSPeter Ujfalusi 1346ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 13474792b0dbSJarkko Nikula if (ret < 0) 13484792b0dbSJarkko Nikula return ret; 13494792b0dbSJarkko Nikula 1350ddee627cSLiam Girdwood return 0; 1351ddee627cSLiam Girdwood } 1352ddee627cSLiam Girdwood 13534378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 13544378f1fbSPeter Ujfalusi { 13554378f1fbSPeter Ujfalusi int ret; 13564378f1fbSPeter Ujfalusi 13574378f1fbSPeter Ujfalusi switch (cmd) { 13584378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_START: 13594378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_RESUME: 13604378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 13614378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_start(substream, cmd); 13624378f1fbSPeter Ujfalusi break; 13634378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_STOP: 13644378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_SUSPEND: 13654378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 13664378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_stop(substream, cmd); 13674378f1fbSPeter Ujfalusi break; 13684378f1fbSPeter Ujfalusi default: 13694378f1fbSPeter Ujfalusi return -EINVAL; 13704378f1fbSPeter Ujfalusi } 13714378f1fbSPeter Ujfalusi 13724378f1fbSPeter Ujfalusi return ret; 13734378f1fbSPeter Ujfalusi } 13744378f1fbSPeter Ujfalusi 137545c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, 137645c0a188SMark Brown int cmd) 137707bf84aaSLiam Girdwood { 137807bf84aaSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 137919bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 13802e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 13812e5894d7SBenoit Cousson int i, ret; 138207bf84aaSLiam Girdwood 1383a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 13845c0769afSKuninori Morimoto ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd); 138507bf84aaSLiam Girdwood if (ret < 0) 138607bf84aaSLiam Girdwood return ret; 138707bf84aaSLiam Girdwood } 138807bf84aaSLiam Girdwood 1389a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1390901e822bSDan Carpenter ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd); 139107bf84aaSLiam Girdwood if (ret < 0) 139207bf84aaSLiam Girdwood return ret; 139319bdcc7aSShreyas NC } 13945c0769afSKuninori Morimoto 139507bf84aaSLiam Girdwood return 0; 139607bf84aaSLiam Girdwood } 1397ddee627cSLiam Girdwood /* 1398ddee627cSLiam Girdwood * soc level wrapper for pointer callback 1399ef050becSCharles Keepax * If cpu_dai, codec_dai, component driver has the delay callback, then 1400ddee627cSLiam Girdwood * the runtime->delay will be updated accordingly. 1401ddee627cSLiam Girdwood */ 1402ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1403ddee627cSLiam Girdwood { 1404ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 140519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 14062e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 1407ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 1408ddee627cSLiam Girdwood snd_pcm_uframes_t offset = 0; 1409ddee627cSLiam Girdwood snd_pcm_sframes_t delay = 0; 14102e5894d7SBenoit Cousson snd_pcm_sframes_t codec_delay = 0; 141119bdcc7aSShreyas NC snd_pcm_sframes_t cpu_delay = 0; 14122e5894d7SBenoit Cousson int i; 1413ddee627cSLiam Girdwood 14149fb4c2bfSAkshu Agrawal /* clearing the previous total delay */ 14159fb4c2bfSAkshu Agrawal runtime->delay = 0; 14169fb4c2bfSAkshu Agrawal 14170035e256SKuninori Morimoto offset = snd_soc_pcm_component_pointer(substream); 1418b8135864SKuninori Morimoto 14199fb4c2bfSAkshu Agrawal /* base delay if assigned in pointer callback */ 14209fb4c2bfSAkshu Agrawal delay = runtime->delay; 1421b8135864SKuninori Morimoto 1422a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 142319bdcc7aSShreyas NC cpu_delay = max(cpu_delay, 142419bdcc7aSShreyas NC snd_soc_dai_delay(cpu_dai, substream)); 142519bdcc7aSShreyas NC } 142619bdcc7aSShreyas NC delay += cpu_delay; 1427ddee627cSLiam Girdwood 1428a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 14292e5894d7SBenoit Cousson codec_delay = max(codec_delay, 14301dea80d4SKuninori Morimoto snd_soc_dai_delay(codec_dai, substream)); 14312e5894d7SBenoit Cousson } 14322e5894d7SBenoit Cousson delay += codec_delay; 1433ddee627cSLiam Girdwood 1434ddee627cSLiam Girdwood runtime->delay = delay; 1435ddee627cSLiam Girdwood 1436ddee627cSLiam Girdwood return offset; 1437ddee627cSLiam Girdwood } 1438ddee627cSLiam Girdwood 143901d7584cSLiam Girdwood /* connect a FE and BE */ 144001d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 144101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 144201d7584cSLiam Girdwood { 144301d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1444a9764869SKaiChieh Chuang unsigned long flags; 144501d7584cSLiam Girdwood 144601d7584cSLiam Girdwood /* only add new dpcms */ 14478d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 144801d7584cSLiam Girdwood if (dpcm->be == be && dpcm->fe == fe) 144901d7584cSLiam Girdwood return 0; 145001d7584cSLiam Girdwood } 145101d7584cSLiam Girdwood 145201d7584cSLiam Girdwood dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 145301d7584cSLiam Girdwood if (!dpcm) 145401d7584cSLiam Girdwood return -ENOMEM; 145501d7584cSLiam Girdwood 145601d7584cSLiam Girdwood dpcm->be = be; 145701d7584cSLiam Girdwood dpcm->fe = fe; 145801d7584cSLiam Girdwood be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 145901d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1460a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 146101d7584cSLiam Girdwood list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 146201d7584cSLiam Girdwood list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1463a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 146401d7584cSLiam Girdwood 146501d7584cSLiam Girdwood dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 146601d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 146701d7584cSLiam Girdwood stream ? "<-" : "->", be->dai_link->name); 146801d7584cSLiam Girdwood 1469154dae87SKuninori Morimoto dpcm_create_debugfs_state(dpcm, stream); 1470154dae87SKuninori Morimoto 147101d7584cSLiam Girdwood return 1; 147201d7584cSLiam Girdwood } 147301d7584cSLiam Girdwood 147401d7584cSLiam Girdwood /* reparent a BE onto another FE */ 147501d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 147601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 147701d7584cSLiam Girdwood { 147801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 147901d7584cSLiam Girdwood struct snd_pcm_substream *fe_substream, *be_substream; 148001d7584cSLiam Girdwood 148101d7584cSLiam Girdwood /* reparent if BE is connected to other FEs */ 148201d7584cSLiam Girdwood if (!be->dpcm[stream].users) 148301d7584cSLiam Girdwood return; 148401d7584cSLiam Girdwood 148501d7584cSLiam Girdwood be_substream = snd_soc_dpcm_get_substream(be, stream); 148601d7584cSLiam Girdwood 1487d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 148801d7584cSLiam Girdwood if (dpcm->fe == fe) 148901d7584cSLiam Girdwood continue; 149001d7584cSLiam Girdwood 149101d7584cSLiam Girdwood dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 149201d7584cSLiam Girdwood stream ? "capture" : "playback", 149301d7584cSLiam Girdwood dpcm->fe->dai_link->name, 149401d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 149501d7584cSLiam Girdwood 149601d7584cSLiam Girdwood fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 149701d7584cSLiam Girdwood be_substream->runtime = fe_substream->runtime; 149801d7584cSLiam Girdwood break; 149901d7584cSLiam Girdwood } 150001d7584cSLiam Girdwood } 150101d7584cSLiam Girdwood 150201d7584cSLiam Girdwood /* disconnect a BE and FE */ 150323607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 150401d7584cSLiam Girdwood { 150501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm, *d; 1506a9764869SKaiChieh Chuang unsigned long flags; 150701d7584cSLiam Girdwood 15088d6258a4SKuninori Morimoto for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1509103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 151001d7584cSLiam Girdwood stream ? "capture" : "playback", 151101d7584cSLiam Girdwood dpcm->be->dai_link->name); 151201d7584cSLiam Girdwood 151301d7584cSLiam Girdwood if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 151401d7584cSLiam Girdwood continue; 151501d7584cSLiam Girdwood 151601d7584cSLiam Girdwood dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 151701d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 151801d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 151901d7584cSLiam Girdwood 152001d7584cSLiam Girdwood /* BEs still alive need new FE */ 152101d7584cSLiam Girdwood dpcm_be_reparent(fe, dpcm->be, stream); 152201d7584cSLiam Girdwood 1523154dae87SKuninori Morimoto dpcm_remove_debugfs_state(dpcm); 1524154dae87SKuninori Morimoto 1525a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 152601d7584cSLiam Girdwood list_del(&dpcm->list_be); 152701d7584cSLiam Girdwood list_del(&dpcm->list_fe); 1528a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 152901d7584cSLiam Girdwood kfree(dpcm); 153001d7584cSLiam Girdwood } 153101d7584cSLiam Girdwood } 153201d7584cSLiam Girdwood 153301d7584cSLiam Girdwood /* get BE for DAI widget and stream */ 153401d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 153501d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget, int stream) 153601d7584cSLiam Girdwood { 153701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 153893597faeSKuninori Morimoto struct snd_soc_dapm_widget *w; 15397afecb30SKuninori Morimoto struct snd_soc_dai *dai; 15401a497983SMengdong Lin int i; 154101d7584cSLiam Girdwood 15423c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 15433c146465SLiam Girdwood 1544bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, be) { 154501d7584cSLiam Girdwood 154635ea0655SLiam Girdwood if (!be->dai_link->no_pcm) 154735ea0655SLiam Girdwood continue; 154835ea0655SLiam Girdwood 1549a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(be, i, dai) { 155019bdcc7aSShreyas NC w = snd_soc_dai_get_widget(dai, stream); 155193597faeSKuninori Morimoto 15523c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: try BE : %s\n", 155393597faeSKuninori Morimoto w ? w->name : "(not set)"); 15543c146465SLiam Girdwood 155593597faeSKuninori Morimoto if (w == widget) 155601d7584cSLiam Girdwood return be; 155719bdcc7aSShreyas NC } 15582e5894d7SBenoit Cousson 1559a4be4187SKuninori Morimoto for_each_rtd_codec_dais(be, i, dai) { 15600c01f6caSKuninori Morimoto w = snd_soc_dai_get_widget(dai, stream); 156193597faeSKuninori Morimoto 156293597faeSKuninori Morimoto if (w == widget) 15632e5894d7SBenoit Cousson return be; 15642e5894d7SBenoit Cousson } 156501d7584cSLiam Girdwood } 156601d7584cSLiam Girdwood 15679d6ee365SJerome Brunet /* Widget provided is not a BE */ 156801d7584cSLiam Girdwood return NULL; 156901d7584cSLiam Girdwood } 157001d7584cSLiam Girdwood 157101d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list, 157201d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget) 157301d7584cSLiam Girdwood { 157409e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *w; 157501d7584cSLiam Girdwood int i; 157601d7584cSLiam Girdwood 157709e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, w) 157809e88f8aSKuninori Morimoto if (widget == w) 157901d7584cSLiam Girdwood return 1; 158001d7584cSLiam Girdwood 158101d7584cSLiam Girdwood return 0; 158201d7584cSLiam Girdwood } 158301d7584cSLiam Girdwood 15845fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, 15855fdd022cSPiotr Stankiewicz enum snd_soc_dapm_direction dir) 15865fdd022cSPiotr Stankiewicz { 15875fdd022cSPiotr Stankiewicz struct snd_soc_card *card = widget->dapm->card; 15885fdd022cSPiotr Stankiewicz struct snd_soc_pcm_runtime *rtd; 1589c2cd8216SKuninori Morimoto int stream; 15905fdd022cSPiotr Stankiewicz 1591c2cd8216SKuninori Morimoto /* adjust dir to stream */ 1592c2cd8216SKuninori Morimoto if (dir == SND_SOC_DAPM_DIR_OUT) 1593c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK; 1594c2cd8216SKuninori Morimoto else 1595c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE; 1596c2cd8216SKuninori Morimoto 1597027a4838SKuninori Morimoto rtd = dpcm_get_be(card, widget, stream); 1598027a4838SKuninori Morimoto if (rtd) 15995fdd022cSPiotr Stankiewicz return true; 16005fdd022cSPiotr Stankiewicz 16015fdd022cSPiotr Stankiewicz return false; 16025fdd022cSPiotr Stankiewicz } 16035fdd022cSPiotr Stankiewicz 160423607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 16051ce43acfSLars-Peter Clausen int stream, struct snd_soc_dapm_widget_list **list) 160601d7584cSLiam Girdwood { 160701d7584cSLiam Girdwood struct snd_soc_dai *cpu_dai = fe->cpu_dai; 160801d7584cSLiam Girdwood int paths; 160901d7584cSLiam Girdwood 16106e1276a5SBard Liao if (fe->num_cpus > 1) { 16116e1276a5SBard Liao dev_err(fe->dev, 16126e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 16136e1276a5SBard Liao return -EINVAL; 16146e1276a5SBard Liao } 16156e1276a5SBard Liao 161601d7584cSLiam Girdwood /* get number of valid DAI paths and their widgets */ 16176742064aSPiotr Stankiewicz paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 16185fdd022cSPiotr Stankiewicz dpcm_end_walk_at_be); 161901d7584cSLiam Girdwood 1620103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 162101d7584cSLiam Girdwood stream ? "capture" : "playback"); 162201d7584cSLiam Girdwood 162301d7584cSLiam Girdwood return paths; 162401d7584cSLiam Girdwood } 162501d7584cSLiam Girdwood 162652645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list) 162752645e33SKuninori Morimoto { 162852645e33SKuninori Morimoto snd_soc_dapm_dai_free_widgets(list); 162952645e33SKuninori Morimoto } 163052645e33SKuninori Morimoto 16318cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream, 16328cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list *list) 163301d7584cSLiam Girdwood { 163401d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget; 16357afecb30SKuninori Morimoto struct snd_soc_dai *dai; 16362e5894d7SBenoit Cousson unsigned int i; 163701d7584cSLiam Girdwood 163801d7584cSLiam Girdwood /* is there a valid CPU DAI widget for this BE */ 1639a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(dpcm->be, i, dai) { 164019bdcc7aSShreyas NC widget = snd_soc_dai_get_widget(dai, stream); 164101d7584cSLiam Girdwood 164219bdcc7aSShreyas NC /* 164319bdcc7aSShreyas NC * The BE is pruned only if none of the cpu_dai 164419bdcc7aSShreyas NC * widgets are in the active list. 164519bdcc7aSShreyas NC */ 164601d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 16478cce6569SGuennadi Liakhovetski return true; 164819bdcc7aSShreyas NC } 164901d7584cSLiam Girdwood 165001d7584cSLiam Girdwood /* is there a valid CODEC DAI widget for this BE */ 1651a4be4187SKuninori Morimoto for_each_rtd_codec_dais(dpcm->be, i, dai) { 16520c01f6caSKuninori Morimoto widget = snd_soc_dai_get_widget(dai, stream); 165301d7584cSLiam Girdwood 165401d7584cSLiam Girdwood /* prune the BE if it's no longer in our active list */ 165501d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 16568cce6569SGuennadi Liakhovetski return true; 16572e5894d7SBenoit Cousson } 16588cce6569SGuennadi Liakhovetski 16598cce6569SGuennadi Liakhovetski return false; 16608cce6569SGuennadi Liakhovetski } 16618cce6569SGuennadi Liakhovetski 16628cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 16638cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list **list_) 16648cce6569SGuennadi Liakhovetski { 16658cce6569SGuennadi Liakhovetski struct snd_soc_dpcm *dpcm; 16668cce6569SGuennadi Liakhovetski int prune = 0; 16678cce6569SGuennadi Liakhovetski 16688cce6569SGuennadi Liakhovetski /* Destroy any old FE <--> BE connections */ 16698cce6569SGuennadi Liakhovetski for_each_dpcm_be(fe, stream, dpcm) { 16708cce6569SGuennadi Liakhovetski if (dpcm_be_is_active(dpcm, stream, *list_)) 1671bed646dcSKuninori Morimoto continue; 167201d7584cSLiam Girdwood 1673103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 167401d7584cSLiam Girdwood stream ? "capture" : "playback", 167501d7584cSLiam Girdwood dpcm->be->dai_link->name, fe->dai_link->name); 167601d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 167701d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 167801d7584cSLiam Girdwood prune++; 167901d7584cSLiam Girdwood } 168001d7584cSLiam Girdwood 1681103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 168201d7584cSLiam Girdwood return prune; 168301d7584cSLiam Girdwood } 168401d7584cSLiam Girdwood 168501d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 168601d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 168701d7584cSLiam Girdwood { 168801d7584cSLiam Girdwood struct snd_soc_card *card = fe->card; 168901d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 169001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 169109e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *widget; 169201d7584cSLiam Girdwood int i, new = 0, err; 169301d7584cSLiam Girdwood 169401d7584cSLiam Girdwood /* Create any new FE <--> BE connections */ 169509e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, widget) { 169601d7584cSLiam Girdwood 169709e88f8aSKuninori Morimoto switch (widget->id) { 16984616274dSMark Brown case snd_soc_dapm_dai_in: 1699c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1700c5b8540dSKoro Chen continue; 1701c5b8540dSKoro Chen break; 17024616274dSMark Brown case snd_soc_dapm_dai_out: 1703c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_CAPTURE) 1704c5b8540dSKoro Chen continue; 17054616274dSMark Brown break; 17064616274dSMark Brown default: 170701d7584cSLiam Girdwood continue; 17084616274dSMark Brown } 170901d7584cSLiam Girdwood 171001d7584cSLiam Girdwood /* is there a valid BE rtd for this widget */ 171109e88f8aSKuninori Morimoto be = dpcm_get_be(card, widget, stream); 171201d7584cSLiam Girdwood if (!be) { 1713103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no BE found for %s\n", 171409e88f8aSKuninori Morimoto widget->name); 171501d7584cSLiam Girdwood continue; 171601d7584cSLiam Girdwood } 171701d7584cSLiam Girdwood 171801d7584cSLiam Girdwood /* don't connect if FE is not running */ 171923607025SLiam Girdwood if (!fe->dpcm[stream].runtime && !fe->fe_compr) 172001d7584cSLiam Girdwood continue; 172101d7584cSLiam Girdwood 172201d7584cSLiam Girdwood /* newly connected FE and BE */ 172301d7584cSLiam Girdwood err = dpcm_be_connect(fe, be, stream); 172401d7584cSLiam Girdwood if (err < 0) { 1725103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: can't connect %s\n", 172609e88f8aSKuninori Morimoto widget->name); 172701d7584cSLiam Girdwood break; 172801d7584cSLiam Girdwood } else if (err == 0) /* already connected */ 172901d7584cSLiam Girdwood continue; 173001d7584cSLiam Girdwood 173101d7584cSLiam Girdwood /* new */ 173201d7584cSLiam Girdwood be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 173301d7584cSLiam Girdwood new++; 173401d7584cSLiam Girdwood } 173501d7584cSLiam Girdwood 1736103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 173701d7584cSLiam Girdwood return new; 173801d7584cSLiam Girdwood } 173901d7584cSLiam Girdwood 174001d7584cSLiam Girdwood /* 174101d7584cSLiam Girdwood * Find the corresponding BE DAIs that source or sink audio to this 174201d7584cSLiam Girdwood * FE substream. 174301d7584cSLiam Girdwood */ 174423607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 174501d7584cSLiam Girdwood int stream, struct snd_soc_dapm_widget_list **list, int new) 174601d7584cSLiam Girdwood { 174701d7584cSLiam Girdwood if (new) 174801d7584cSLiam Girdwood return dpcm_add_paths(fe, stream, list); 174901d7584cSLiam Girdwood else 175001d7584cSLiam Girdwood return dpcm_prune_paths(fe, stream, list); 175101d7584cSLiam Girdwood } 175201d7584cSLiam Girdwood 175323607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 175401d7584cSLiam Girdwood { 175501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1756a9764869SKaiChieh Chuang unsigned long flags; 175701d7584cSLiam Girdwood 1758a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 17598d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 176001d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = 176101d7584cSLiam Girdwood SND_SOC_DPCM_UPDATE_NO; 1762a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 176301d7584cSLiam Girdwood } 176401d7584cSLiam Girdwood 176501d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 176601d7584cSLiam Girdwood int stream) 176701d7584cSLiam Girdwood { 176801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 176901d7584cSLiam Girdwood 177001d7584cSLiam Girdwood /* disable any enabled and non active backends */ 17718d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 177201d7584cSLiam Girdwood 177301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 177401d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 177501d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 177601d7584cSLiam Girdwood 177701d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1778103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 177901d7584cSLiam Girdwood stream ? "capture" : "playback", 178001d7584cSLiam Girdwood be->dpcm[stream].state); 178101d7584cSLiam Girdwood 178201d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 178301d7584cSLiam Girdwood continue; 178401d7584cSLiam Girdwood 178501d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 178601d7584cSLiam Girdwood continue; 178701d7584cSLiam Girdwood 178801d7584cSLiam Girdwood soc_pcm_close(be_substream); 178901d7584cSLiam Girdwood be_substream->runtime = NULL; 179001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 179101d7584cSLiam Girdwood } 179201d7584cSLiam Girdwood } 179301d7584cSLiam Girdwood 179423607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 179501d7584cSLiam Girdwood { 179601d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 179701d7584cSLiam Girdwood int err, count = 0; 179801d7584cSLiam Girdwood 179901d7584cSLiam Girdwood /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 18008d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 180101d7584cSLiam Girdwood 180201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 180301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 180401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 180501d7584cSLiam Girdwood 18062062b4c5SRussell King - ARM Linux if (!be_substream) { 18072062b4c5SRussell King - ARM Linux dev_err(be->dev, "ASoC: no backend %s stream\n", 18082062b4c5SRussell King - ARM Linux stream ? "capture" : "playback"); 18092062b4c5SRussell King - ARM Linux continue; 18102062b4c5SRussell King - ARM Linux } 18112062b4c5SRussell King - ARM Linux 181201d7584cSLiam Girdwood /* is this op for this BE ? */ 181301d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 181401d7584cSLiam Girdwood continue; 181501d7584cSLiam Girdwood 181601d7584cSLiam Girdwood /* first time the dpcm is open ? */ 181701d7584cSLiam Girdwood if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1818103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: too many users %s at open %d\n", 181901d7584cSLiam Girdwood stream ? "capture" : "playback", 182001d7584cSLiam Girdwood be->dpcm[stream].state); 182101d7584cSLiam Girdwood 182201d7584cSLiam Girdwood if (be->dpcm[stream].users++ != 0) 182301d7584cSLiam Girdwood continue; 182401d7584cSLiam Girdwood 182501d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 182601d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 182701d7584cSLiam Girdwood continue; 182801d7584cSLiam Girdwood 18292062b4c5SRussell King - ARM Linux dev_dbg(be->dev, "ASoC: open %s BE %s\n", 18302062b4c5SRussell King - ARM Linux stream ? "capture" : "playback", be->dai_link->name); 183101d7584cSLiam Girdwood 183201d7584cSLiam Girdwood be_substream->runtime = be->dpcm[stream].runtime; 183301d7584cSLiam Girdwood err = soc_pcm_open(be_substream); 183401d7584cSLiam Girdwood if (err < 0) { 1835103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: BE open failed %d\n", err); 183601d7584cSLiam Girdwood be->dpcm[stream].users--; 183701d7584cSLiam Girdwood if (be->dpcm[stream].users < 0) 1838103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 183901d7584cSLiam Girdwood stream ? "capture" : "playback", 184001d7584cSLiam Girdwood be->dpcm[stream].state); 184101d7584cSLiam Girdwood 184201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 184301d7584cSLiam Girdwood goto unwind; 184401d7584cSLiam Girdwood } 184501d7584cSLiam Girdwood 184601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 184701d7584cSLiam Girdwood count++; 184801d7584cSLiam Girdwood } 184901d7584cSLiam Girdwood 185001d7584cSLiam Girdwood return count; 185101d7584cSLiam Girdwood 185201d7584cSLiam Girdwood unwind: 185301d7584cSLiam Girdwood /* disable any enabled and non active backends */ 18548d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 185501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 185601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 185701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 185801d7584cSLiam Girdwood 185901d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 186001d7584cSLiam Girdwood continue; 186101d7584cSLiam Girdwood 186201d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1863103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close %d\n", 186401d7584cSLiam Girdwood stream ? "capture" : "playback", 186501d7584cSLiam Girdwood be->dpcm[stream].state); 186601d7584cSLiam Girdwood 186701d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 186801d7584cSLiam Girdwood continue; 186901d7584cSLiam Girdwood 187001d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 187101d7584cSLiam Girdwood continue; 187201d7584cSLiam Girdwood 187301d7584cSLiam Girdwood soc_pcm_close(be_substream); 187401d7584cSLiam Girdwood be_substream->runtime = NULL; 187501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 187601d7584cSLiam Girdwood } 187701d7584cSLiam Girdwood 187801d7584cSLiam Girdwood return err; 187901d7584cSLiam Girdwood } 188001d7584cSLiam Girdwood 188108ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1882435ffb76SJerome Brunet struct snd_soc_pcm_stream *stream) 188308ae9b45SLars-Peter Clausen { 188408ae9b45SLars-Peter Clausen runtime->hw.rate_min = stream->rate_min; 1885e33ffbd9SCharles Keepax runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); 188608ae9b45SLars-Peter Clausen runtime->hw.channels_min = stream->channels_min; 188708ae9b45SLars-Peter Clausen runtime->hw.channels_max = stream->channels_max; 1888002220a9SLars-Peter Clausen if (runtime->hw.formats) 1889435ffb76SJerome Brunet runtime->hw.formats &= stream->formats; 1890002220a9SLars-Peter Clausen else 1891435ffb76SJerome Brunet runtime->hw.formats = stream->formats; 189208ae9b45SLars-Peter Clausen runtime->hw.rates = stream->rates; 189308ae9b45SLars-Peter Clausen } 189408ae9b45SLars-Peter Clausen 1895435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, 1896435ffb76SJerome Brunet u64 *formats) 1897b073ed4eSKuninori Morimoto { 1898b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *fe = substream->private_data; 1899b073ed4eSKuninori Morimoto struct snd_soc_dpcm *dpcm; 19007afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1901b073ed4eSKuninori Morimoto int stream = substream->stream; 1902b073ed4eSKuninori Morimoto 1903b073ed4eSKuninori Morimoto if (!fe->dai_link->dpcm_merged_format) 1904435ffb76SJerome Brunet return; 1905b073ed4eSKuninori Morimoto 1906b073ed4eSKuninori Morimoto /* 1907b073ed4eSKuninori Morimoto * It returns merged BE codec format 1908b073ed4eSKuninori Morimoto * if FE want to use it (= dpcm_merged_format) 1909b073ed4eSKuninori Morimoto */ 1910b073ed4eSKuninori Morimoto 19118d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1912b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 1913b073ed4eSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream; 1914b073ed4eSKuninori Morimoto int i; 1915b073ed4eSKuninori Morimoto 1916a4be4187SKuninori Morimoto for_each_rtd_codec_dais(be, i, dai) { 19174febced1SJerome Brunet /* 19184febced1SJerome Brunet * Skip CODECs which don't support the current stream 19194febced1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 19204febced1SJerome Brunet */ 19217afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 19224febced1SJerome Brunet continue; 19234febced1SJerome Brunet 1924acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1925b073ed4eSKuninori Morimoto 1926435ffb76SJerome Brunet *formats &= codec_stream->formats; 1927435ffb76SJerome Brunet } 1928b073ed4eSKuninori Morimoto } 1929b073ed4eSKuninori Morimoto } 1930b073ed4eSKuninori Morimoto 1931435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, 1932f4c277b8SJiada Wang unsigned int *channels_min, 1933f4c277b8SJiada Wang unsigned int *channels_max) 1934f4c277b8SJiada Wang { 1935f4c277b8SJiada Wang struct snd_soc_pcm_runtime *fe = substream->private_data; 1936f4c277b8SJiada Wang struct snd_soc_dpcm *dpcm; 1937f4c277b8SJiada Wang int stream = substream->stream; 1938f4c277b8SJiada Wang 1939f4c277b8SJiada Wang if (!fe->dai_link->dpcm_merged_chan) 1940f4c277b8SJiada Wang return; 1941f4c277b8SJiada Wang 1942f4c277b8SJiada Wang /* 1943f4c277b8SJiada Wang * It returns merged BE codec channel; 1944f4c277b8SJiada Wang * if FE want to use it (= dpcm_merged_chan) 1945f4c277b8SJiada Wang */ 1946f4c277b8SJiada Wang 19478d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1948f4c277b8SJiada Wang struct snd_soc_pcm_runtime *be = dpcm->be; 1949f4c277b8SJiada Wang struct snd_soc_pcm_stream *codec_stream; 19504f2bd18bSJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 195119bdcc7aSShreyas NC struct snd_soc_dai *dai; 195219bdcc7aSShreyas NC int i; 1953f4c277b8SJiada Wang 1954a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(be, i, dai) { 19550e9cf4c4SBard Liao /* 19560e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 19570e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 19580e9cf4c4SBard Liao */ 19590e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream)) 19600e9cf4c4SBard Liao continue; 19610e9cf4c4SBard Liao 196219bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); 19634f2bd18bSJerome Brunet 196419bdcc7aSShreyas NC *channels_min = max(*channels_min, 196519bdcc7aSShreyas NC cpu_stream->channels_min); 196619bdcc7aSShreyas NC *channels_max = min(*channels_max, 196719bdcc7aSShreyas NC cpu_stream->channels_max); 196819bdcc7aSShreyas NC } 19694f2bd18bSJerome Brunet 19704f2bd18bSJerome Brunet /* 19714f2bd18bSJerome Brunet * chan min/max cannot be enforced if there are multiple CODEC 19724f2bd18bSJerome Brunet * DAIs connected to a single CPU DAI, use CPU DAI's directly 19734f2bd18bSJerome Brunet */ 19744f2bd18bSJerome Brunet if (be->num_codecs == 1) { 1975acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream); 1976f4c277b8SJiada Wang 1977f4c277b8SJiada Wang *channels_min = max(*channels_min, 1978f4c277b8SJiada Wang codec_stream->channels_min); 1979f4c277b8SJiada Wang *channels_max = min(*channels_max, 1980f4c277b8SJiada Wang codec_stream->channels_max); 1981f4c277b8SJiada Wang } 1982f4c277b8SJiada Wang } 1983f4c277b8SJiada Wang } 1984f4c277b8SJiada Wang 1985baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, 1986baacd8d1SJerome Brunet unsigned int *rates, 1987baacd8d1SJerome Brunet unsigned int *rate_min, 1988baacd8d1SJerome Brunet unsigned int *rate_max) 1989baacd8d1SJerome Brunet { 1990baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *fe = substream->private_data; 1991baacd8d1SJerome Brunet struct snd_soc_dpcm *dpcm; 1992baacd8d1SJerome Brunet int stream = substream->stream; 1993baacd8d1SJerome Brunet 1994baacd8d1SJerome Brunet if (!fe->dai_link->dpcm_merged_rate) 1995baacd8d1SJerome Brunet return; 1996baacd8d1SJerome Brunet 1997baacd8d1SJerome Brunet /* 1998baacd8d1SJerome Brunet * It returns merged BE codec channel; 1999baacd8d1SJerome Brunet * if FE want to use it (= dpcm_merged_chan) 2000baacd8d1SJerome Brunet */ 2001baacd8d1SJerome Brunet 20028d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2003baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *be = dpcm->be; 2004baacd8d1SJerome Brunet struct snd_soc_pcm_stream *codec_stream; 2005baacd8d1SJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 20067afecb30SKuninori Morimoto struct snd_soc_dai *dai; 2007baacd8d1SJerome Brunet int i; 2008baacd8d1SJerome Brunet 2009a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(be, i, dai) { 20100e9cf4c4SBard Liao /* 20110e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 20120e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 20130e9cf4c4SBard Liao */ 20140e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream)) 20150e9cf4c4SBard Liao continue; 20160e9cf4c4SBard Liao 201719bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); 2018baacd8d1SJerome Brunet 2019baacd8d1SJerome Brunet *rate_min = max(*rate_min, cpu_stream->rate_min); 202019bdcc7aSShreyas NC *rate_max = min_not_zero(*rate_max, 202119bdcc7aSShreyas NC cpu_stream->rate_max); 202219bdcc7aSShreyas NC *rates = snd_pcm_rate_mask_intersect(*rates, 202319bdcc7aSShreyas NC cpu_stream->rates); 202419bdcc7aSShreyas NC } 2025baacd8d1SJerome Brunet 2026a4be4187SKuninori Morimoto for_each_rtd_codec_dais(be, i, dai) { 2027baacd8d1SJerome Brunet /* 2028baacd8d1SJerome Brunet * Skip CODECs which don't support the current stream 2029baacd8d1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 2030baacd8d1SJerome Brunet */ 20317afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 2032baacd8d1SJerome Brunet continue; 2033baacd8d1SJerome Brunet 2034acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 2035baacd8d1SJerome Brunet 2036baacd8d1SJerome Brunet *rate_min = max(*rate_min, codec_stream->rate_min); 2037baacd8d1SJerome Brunet *rate_max = min_not_zero(*rate_max, 2038baacd8d1SJerome Brunet codec_stream->rate_max); 2039baacd8d1SJerome Brunet *rates = snd_pcm_rate_mask_intersect(*rates, 2040baacd8d1SJerome Brunet codec_stream->rates); 2041baacd8d1SJerome Brunet } 2042baacd8d1SJerome Brunet } 2043baacd8d1SJerome Brunet } 2044baacd8d1SJerome Brunet 204545c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 204601d7584cSLiam Girdwood { 204701d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 204801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 204919bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 205019bdcc7aSShreyas NC int i; 205101d7584cSLiam Girdwood 2052a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 20530e9cf4c4SBard Liao /* 20540e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 20550e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 20560e9cf4c4SBard Liao */ 20570e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 20580e9cf4c4SBard Liao continue; 20590e9cf4c4SBard Liao 20600c9ba720SKuninori Morimoto dpcm_init_runtime_hw(runtime, 20610c9ba720SKuninori Morimoto snd_soc_dai_get_pcm_stream(cpu_dai, 20620c9ba720SKuninori Morimoto substream->stream)); 206319bdcc7aSShreyas NC } 2064f4c277b8SJiada Wang 2065435ffb76SJerome Brunet dpcm_runtime_merge_format(substream, &runtime->hw.formats); 2066435ffb76SJerome Brunet dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, 2067435ffb76SJerome Brunet &runtime->hw.channels_max); 2068baacd8d1SJerome Brunet dpcm_runtime_merge_rate(substream, &runtime->hw.rates, 2069baacd8d1SJerome Brunet &runtime->hw.rate_min, &runtime->hw.rate_max); 207001d7584cSLiam Girdwood } 207101d7584cSLiam Girdwood 2072ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 2073ea9d0d77STakashi Iwai 2074ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock 2075ea9d0d77STakashi Iwai * for avoiding the race with trigger callback. 2076ea9d0d77STakashi Iwai * If the state is unset and a trigger is pending while the previous operation, 2077ea9d0d77STakashi Iwai * process the pending trigger action here. 2078ea9d0d77STakashi Iwai */ 2079ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 2080ea9d0d77STakashi Iwai int stream, enum snd_soc_dpcm_update state) 2081ea9d0d77STakashi Iwai { 2082ea9d0d77STakashi Iwai struct snd_pcm_substream *substream = 2083ea9d0d77STakashi Iwai snd_soc_dpcm_get_substream(fe, stream); 2084ea9d0d77STakashi Iwai 2085ea9d0d77STakashi Iwai snd_pcm_stream_lock_irq(substream); 2086ea9d0d77STakashi Iwai if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 2087ea9d0d77STakashi Iwai dpcm_fe_dai_do_trigger(substream, 2088ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending - 1); 2089ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = 0; 2090ea9d0d77STakashi Iwai } 2091ea9d0d77STakashi Iwai fe->dpcm[stream].runtime_update = state; 2092ea9d0d77STakashi Iwai snd_pcm_stream_unlock_irq(substream); 2093ea9d0d77STakashi Iwai } 2094ea9d0d77STakashi Iwai 2095906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 2096906c7d69SPC Liao int stream) 2097906c7d69SPC Liao { 2098906c7d69SPC Liao struct snd_soc_dpcm *dpcm; 2099906c7d69SPC Liao struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 210019bdcc7aSShreyas NC struct snd_soc_dai *fe_cpu_dai; 2101906c7d69SPC Liao int err; 210219bdcc7aSShreyas NC int i; 2103906c7d69SPC Liao 2104906c7d69SPC Liao /* apply symmetry for FE */ 2105906c7d69SPC Liao if (soc_pcm_has_symmetry(fe_substream)) 2106906c7d69SPC Liao fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 2107906c7d69SPC Liao 2108a4be4187SKuninori Morimoto for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) { 2109906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 2110906c7d69SPC Liao if (fe_cpu_dai->active) { 2111906c7d69SPC Liao err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 2112906c7d69SPC Liao if (err < 0) 2113906c7d69SPC Liao return err; 2114906c7d69SPC Liao } 211519bdcc7aSShreyas NC } 2116906c7d69SPC Liao 2117906c7d69SPC Liao /* apply symmetry for BE */ 21188d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2119906c7d69SPC Liao struct snd_soc_pcm_runtime *be = dpcm->be; 2120906c7d69SPC Liao struct snd_pcm_substream *be_substream = 2121906c7d69SPC Liao snd_soc_dpcm_get_substream(be, stream); 21226246f283SJerome Brunet struct snd_soc_pcm_runtime *rtd; 21230b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 212419bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 2125906c7d69SPC Liao int i; 2126906c7d69SPC Liao 21276246f283SJerome Brunet /* A backend may not have the requested substream */ 21286246f283SJerome Brunet if (!be_substream) 21296246f283SJerome Brunet continue; 21306246f283SJerome Brunet 21316246f283SJerome Brunet rtd = be_substream->private_data; 2132f1176614SJeeja KP if (rtd->dai_link->be_hw_params_fixup) 2133f1176614SJeeja KP continue; 2134f1176614SJeeja KP 2135906c7d69SPC Liao if (soc_pcm_has_symmetry(be_substream)) 2136906c7d69SPC Liao be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 2137906c7d69SPC Liao 2138906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 2139a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 214019bdcc7aSShreyas NC if (cpu_dai->active) { 214199bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 214219bdcc7aSShreyas NC cpu_dai); 2143906c7d69SPC Liao if (err < 0) 2144906c7d69SPC Liao return err; 2145906c7d69SPC Liao } 214619bdcc7aSShreyas NC } 2147906c7d69SPC Liao 2148a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 21490b7990e3SKuninori Morimoto if (codec_dai->active) { 215099bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 21510b7990e3SKuninori Morimoto codec_dai); 2152906c7d69SPC Liao if (err < 0) 2153906c7d69SPC Liao return err; 2154906c7d69SPC Liao } 2155906c7d69SPC Liao } 2156906c7d69SPC Liao } 2157906c7d69SPC Liao 2158906c7d69SPC Liao return 0; 2159906c7d69SPC Liao } 2160906c7d69SPC Liao 216101d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 216201d7584cSLiam Girdwood { 216301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 216401d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = fe_substream->runtime; 216501d7584cSLiam Girdwood int stream = fe_substream->stream, ret = 0; 216601d7584cSLiam Girdwood 2167ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 216801d7584cSLiam Girdwood 216925c2f515SKuninori Morimoto ret = dpcm_be_dai_startup(fe, stream); 217001d7584cSLiam Girdwood if (ret < 0) { 2171103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 217201d7584cSLiam Girdwood goto be_err; 217301d7584cSLiam Girdwood } 217401d7584cSLiam Girdwood 2175103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 217601d7584cSLiam Girdwood 217701d7584cSLiam Girdwood /* start the DAI frontend */ 217801d7584cSLiam Girdwood ret = soc_pcm_open(fe_substream); 217901d7584cSLiam Girdwood if (ret < 0) { 2180103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 218101d7584cSLiam Girdwood goto unwind; 218201d7584cSLiam Girdwood } 218301d7584cSLiam Girdwood 218401d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 218501d7584cSLiam Girdwood 218601d7584cSLiam Girdwood dpcm_set_fe_runtime(fe_substream); 218701d7584cSLiam Girdwood snd_pcm_limit_hw_rates(runtime); 218801d7584cSLiam Girdwood 2189906c7d69SPC Liao ret = dpcm_apply_symmetry(fe_substream, stream); 21908a01fbf0SKuninori Morimoto if (ret < 0) 2191906c7d69SPC Liao dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", 2192906c7d69SPC Liao ret); 219301d7584cSLiam Girdwood 219401d7584cSLiam Girdwood unwind: 21958a01fbf0SKuninori Morimoto if (ret < 0) 219625c2f515SKuninori Morimoto dpcm_be_dai_startup_unwind(fe, stream); 219701d7584cSLiam Girdwood be_err: 2198ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 219901d7584cSLiam Girdwood return ret; 220001d7584cSLiam Girdwood } 220101d7584cSLiam Girdwood 220223607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 220301d7584cSLiam Girdwood { 220401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 220501d7584cSLiam Girdwood 220601d7584cSLiam Girdwood /* only shutdown BEs that are either sinks or sources to this FE DAI */ 22078d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 220801d7584cSLiam Girdwood 220901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 221001d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 221101d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 221201d7584cSLiam Girdwood 221301d7584cSLiam Girdwood /* is this op for this BE ? */ 221401d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 221501d7584cSLiam Girdwood continue; 221601d7584cSLiam Girdwood 221701d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 2218103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 221901d7584cSLiam Girdwood stream ? "capture" : "playback", 222001d7584cSLiam Girdwood be->dpcm[stream].state); 222101d7584cSLiam Girdwood 222201d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 222301d7584cSLiam Girdwood continue; 222401d7584cSLiam Girdwood 222501d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 22269c0ac70aSKai Chieh Chuang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { 22279c0ac70aSKai Chieh Chuang soc_pcm_hw_free(be_substream); 22289c0ac70aSKai Chieh Chuang be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 22299c0ac70aSKai Chieh Chuang } 223001d7584cSLiam Girdwood 2231103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: close BE %s\n", 223294d215ccS彭东林 be->dai_link->name); 223301d7584cSLiam Girdwood 223401d7584cSLiam Girdwood soc_pcm_close(be_substream); 223501d7584cSLiam Girdwood be_substream->runtime = NULL; 223601d7584cSLiam Girdwood 223701d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 223801d7584cSLiam Girdwood } 223901d7584cSLiam Girdwood return 0; 224001d7584cSLiam Girdwood } 224101d7584cSLiam Girdwood 224201d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 224301d7584cSLiam Girdwood { 224401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 224501d7584cSLiam Girdwood int stream = substream->stream; 224601d7584cSLiam Girdwood 2247ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 224801d7584cSLiam Girdwood 224901d7584cSLiam Girdwood /* shutdown the BEs */ 225025c2f515SKuninori Morimoto dpcm_be_dai_shutdown(fe, stream); 225101d7584cSLiam Girdwood 2252103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 225301d7584cSLiam Girdwood 225401d7584cSLiam Girdwood /* now shutdown the frontend */ 225501d7584cSLiam Girdwood soc_pcm_close(substream); 225601d7584cSLiam Girdwood 225701d7584cSLiam Girdwood /* run the stream event for each BE */ 22581c531230SKuninori Morimoto dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 225901d7584cSLiam Girdwood 226001d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2261ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 226201d7584cSLiam Girdwood return 0; 226301d7584cSLiam Girdwood } 226401d7584cSLiam Girdwood 226523607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 226601d7584cSLiam Girdwood { 226701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 226801d7584cSLiam Girdwood 226901d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 227001d7584cSLiam Girdwood * to this frontend DAI */ 22718d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 227201d7584cSLiam Girdwood 227301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 227401d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 227501d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 227601d7584cSLiam Girdwood 227701d7584cSLiam Girdwood /* is this op for this BE ? */ 227801d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 227901d7584cSLiam Girdwood continue; 228001d7584cSLiam Girdwood 228101d7584cSLiam Girdwood /* only free hw when no longer used - check all FEs */ 228201d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 228301d7584cSLiam Girdwood continue; 228401d7584cSLiam Girdwood 228536fba62cSQiao Zhou /* do not free hw if this BE is used by other FE */ 228636fba62cSQiao Zhou if (be->dpcm[stream].users > 1) 228736fba62cSQiao Zhou continue; 228836fba62cSQiao Zhou 228901d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 229001d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 229101d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 229208b27848SPatrick Lai (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 22935e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 22945e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 229501d7584cSLiam Girdwood continue; 229601d7584cSLiam Girdwood 2297103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 229894d215ccS彭东林 be->dai_link->name); 229901d7584cSLiam Girdwood 230001d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 230101d7584cSLiam Girdwood 230201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 230301d7584cSLiam Girdwood } 230401d7584cSLiam Girdwood 230501d7584cSLiam Girdwood return 0; 230601d7584cSLiam Girdwood } 230701d7584cSLiam Girdwood 230845c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 230901d7584cSLiam Girdwood { 231001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 231101d7584cSLiam Girdwood int err, stream = substream->stream; 231201d7584cSLiam Girdwood 231301d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2314ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 231501d7584cSLiam Girdwood 2316103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 231701d7584cSLiam Girdwood 231801d7584cSLiam Girdwood /* call hw_free on the frontend */ 231901d7584cSLiam Girdwood err = soc_pcm_hw_free(substream); 232001d7584cSLiam Girdwood if (err < 0) 2321103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 232201d7584cSLiam Girdwood fe->dai_link->name); 232301d7584cSLiam Girdwood 232401d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 232501d7584cSLiam Girdwood * to this frontend DAI */ 232601d7584cSLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 232701d7584cSLiam Girdwood 232801d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2329ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 233001d7584cSLiam Girdwood 233101d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 233201d7584cSLiam Girdwood return 0; 233301d7584cSLiam Girdwood } 233401d7584cSLiam Girdwood 233523607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 233601d7584cSLiam Girdwood { 233701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 233801d7584cSLiam Girdwood int ret; 233901d7584cSLiam Girdwood 23408d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 234101d7584cSLiam Girdwood 234201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 234301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 234401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 234501d7584cSLiam Girdwood 234601d7584cSLiam Girdwood /* is this op for this BE ? */ 234701d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 234801d7584cSLiam Girdwood continue; 234901d7584cSLiam Girdwood 235001d7584cSLiam Girdwood /* copy params for each dpcm */ 235101d7584cSLiam Girdwood memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 235201d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 235301d7584cSLiam Girdwood 235401d7584cSLiam Girdwood /* perform any hw_params fixups */ 235501d7584cSLiam Girdwood if (be->dai_link->be_hw_params_fixup) { 235601d7584cSLiam Girdwood ret = be->dai_link->be_hw_params_fixup(be, 235701d7584cSLiam Girdwood &dpcm->hw_params); 235801d7584cSLiam Girdwood if (ret < 0) { 235901d7584cSLiam Girdwood dev_err(be->dev, 2360103d84a3SLiam Girdwood "ASoC: hw_params BE fixup failed %d\n", 236101d7584cSLiam Girdwood ret); 236201d7584cSLiam Girdwood goto unwind; 236301d7584cSLiam Girdwood } 236401d7584cSLiam Girdwood } 236501d7584cSLiam Girdwood 2366ae061d2aSLibin Yang /* copy the fixed-up hw params for BE dai */ 2367ae061d2aSLibin Yang memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, 2368ae061d2aSLibin Yang sizeof(struct snd_pcm_hw_params)); 2369ae061d2aSLibin Yang 2370b0639bd2SKuninori Morimoto /* only allow hw_params() if no connected FEs are running */ 2371b0639bd2SKuninori Morimoto if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2372b0639bd2SKuninori Morimoto continue; 2373b0639bd2SKuninori Morimoto 2374b0639bd2SKuninori Morimoto if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2375b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2376b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2377b0639bd2SKuninori Morimoto continue; 2378b0639bd2SKuninori Morimoto 2379b0639bd2SKuninori Morimoto dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 238094d215ccS彭东林 be->dai_link->name); 2381b0639bd2SKuninori Morimoto 238201d7584cSLiam Girdwood ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 238301d7584cSLiam Girdwood if (ret < 0) { 238401d7584cSLiam Girdwood dev_err(dpcm->be->dev, 2385103d84a3SLiam Girdwood "ASoC: hw_params BE failed %d\n", ret); 238601d7584cSLiam Girdwood goto unwind; 238701d7584cSLiam Girdwood } 238801d7584cSLiam Girdwood 238901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 239001d7584cSLiam Girdwood } 239101d7584cSLiam Girdwood return 0; 239201d7584cSLiam Girdwood 239301d7584cSLiam Girdwood unwind: 239401d7584cSLiam Girdwood /* disable any enabled and non active backends */ 23958d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 239601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 239701d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 239801d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 239901d7584cSLiam Girdwood 240001d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 240101d7584cSLiam Girdwood continue; 240201d7584cSLiam Girdwood 240301d7584cSLiam Girdwood /* only allow hw_free() if no connected FEs are running */ 240401d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 240501d7584cSLiam Girdwood continue; 240601d7584cSLiam Girdwood 240701d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 240801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 240901d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 241001d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 241101d7584cSLiam Girdwood continue; 241201d7584cSLiam Girdwood 241301d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 241401d7584cSLiam Girdwood } 241501d7584cSLiam Girdwood 241601d7584cSLiam Girdwood return ret; 241701d7584cSLiam Girdwood } 241801d7584cSLiam Girdwood 241945c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 242001d7584cSLiam Girdwood struct snd_pcm_hw_params *params) 242101d7584cSLiam Girdwood { 242201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 242301d7584cSLiam Girdwood int ret, stream = substream->stream; 242401d7584cSLiam Girdwood 242501d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2426ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 242701d7584cSLiam Girdwood 242825c2f515SKuninori Morimoto memcpy(&fe->dpcm[stream].hw_params, params, 242901d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 243025c2f515SKuninori Morimoto ret = dpcm_be_dai_hw_params(fe, stream); 243101d7584cSLiam Girdwood if (ret < 0) { 2432103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 243301d7584cSLiam Girdwood goto out; 243401d7584cSLiam Girdwood } 243501d7584cSLiam Girdwood 2436103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 243701d7584cSLiam Girdwood fe->dai_link->name, params_rate(params), 243801d7584cSLiam Girdwood params_channels(params), params_format(params)); 243901d7584cSLiam Girdwood 244001d7584cSLiam Girdwood /* call hw_params on the frontend */ 244101d7584cSLiam Girdwood ret = soc_pcm_hw_params(substream, params); 244201d7584cSLiam Girdwood if (ret < 0) { 2443103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 244401d7584cSLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 244501d7584cSLiam Girdwood } else 244601d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 244701d7584cSLiam Girdwood 244801d7584cSLiam Girdwood out: 2449ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 245001d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 245101d7584cSLiam Girdwood return ret; 245201d7584cSLiam Girdwood } 245301d7584cSLiam Girdwood 245401d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 245501d7584cSLiam Girdwood struct snd_pcm_substream *substream, int cmd) 245601d7584cSLiam Girdwood { 245701d7584cSLiam Girdwood int ret; 245801d7584cSLiam Girdwood 2459103d84a3SLiam Girdwood dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 246094d215ccS彭东林 dpcm->be->dai_link->name, cmd); 246101d7584cSLiam Girdwood 246201d7584cSLiam Girdwood ret = soc_pcm_trigger(substream, cmd); 246301d7584cSLiam Girdwood if (ret < 0) 2464103d84a3SLiam Girdwood dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 246501d7584cSLiam Girdwood 246601d7584cSLiam Girdwood return ret; 246701d7584cSLiam Girdwood } 246801d7584cSLiam Girdwood 246923607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 247045c0a188SMark Brown int cmd) 247101d7584cSLiam Girdwood { 247201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 247301d7584cSLiam Girdwood int ret = 0; 247401d7584cSLiam Girdwood 24758d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 247601d7584cSLiam Girdwood 247701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 247801d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 247901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 248001d7584cSLiam Girdwood 248101d7584cSLiam Girdwood /* is this op for this BE ? */ 248201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 248301d7584cSLiam Girdwood continue; 248401d7584cSLiam Girdwood 248501d7584cSLiam Girdwood switch (cmd) { 248601d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 248701d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 248801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 248901d7584cSLiam Girdwood continue; 249001d7584cSLiam Girdwood 249101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 249201d7584cSLiam Girdwood if (ret) 249301d7584cSLiam Girdwood return ret; 249401d7584cSLiam Girdwood 249501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 249601d7584cSLiam Girdwood break; 249701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 249801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 249901d7584cSLiam Girdwood continue; 250001d7584cSLiam Girdwood 250101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 250201d7584cSLiam Girdwood if (ret) 250301d7584cSLiam Girdwood return ret; 250401d7584cSLiam Girdwood 250501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 250601d7584cSLiam Girdwood break; 250701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 250801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 250901d7584cSLiam Girdwood continue; 251001d7584cSLiam Girdwood 251101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 251201d7584cSLiam Girdwood if (ret) 251301d7584cSLiam Girdwood return ret; 251401d7584cSLiam Girdwood 251501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 251601d7584cSLiam Girdwood break; 251701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 251801d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 251901d7584cSLiam Girdwood continue; 252001d7584cSLiam Girdwood 252101d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 252201d7584cSLiam Girdwood continue; 252301d7584cSLiam Girdwood 252401d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 252501d7584cSLiam Girdwood if (ret) 252601d7584cSLiam Girdwood return ret; 252701d7584cSLiam Girdwood 252801d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 252901d7584cSLiam Girdwood break; 253001d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 2531868a6ca8SNicolin Chen if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 253201d7584cSLiam Girdwood continue; 253301d7584cSLiam Girdwood 253401d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 253501d7584cSLiam Girdwood continue; 253601d7584cSLiam Girdwood 253701d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 253801d7584cSLiam Girdwood if (ret) 253901d7584cSLiam Girdwood return ret; 254001d7584cSLiam Girdwood 254101d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 254201d7584cSLiam Girdwood break; 254301d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 254401d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 254501d7584cSLiam Girdwood continue; 254601d7584cSLiam Girdwood 254701d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 254801d7584cSLiam Girdwood continue; 254901d7584cSLiam Girdwood 255001d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 255101d7584cSLiam Girdwood if (ret) 255201d7584cSLiam Girdwood return ret; 255301d7584cSLiam Girdwood 255401d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 255501d7584cSLiam Girdwood break; 255601d7584cSLiam Girdwood } 255701d7584cSLiam Girdwood } 255801d7584cSLiam Girdwood 255901d7584cSLiam Girdwood return ret; 256001d7584cSLiam Girdwood } 256101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 256201d7584cSLiam Girdwood 2563acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, 2564acbf2774SRanjani Sridharan int cmd, bool fe_first) 2565acbf2774SRanjani Sridharan { 2566acbf2774SRanjani Sridharan struct snd_soc_pcm_runtime *fe = substream->private_data; 2567acbf2774SRanjani Sridharan int ret; 2568acbf2774SRanjani Sridharan 2569acbf2774SRanjani Sridharan /* call trigger on the frontend before the backend. */ 2570acbf2774SRanjani Sridharan if (fe_first) { 2571acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2572acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2573acbf2774SRanjani Sridharan 2574acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2575acbf2774SRanjani Sridharan if (ret < 0) 2576acbf2774SRanjani Sridharan return ret; 2577acbf2774SRanjani Sridharan 2578acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2579acbf2774SRanjani Sridharan return ret; 2580acbf2774SRanjani Sridharan } 2581acbf2774SRanjani Sridharan 2582acbf2774SRanjani Sridharan /* call trigger on the frontend after the backend. */ 2583acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2584acbf2774SRanjani Sridharan if (ret < 0) 2585acbf2774SRanjani Sridharan return ret; 2586acbf2774SRanjani Sridharan 2587acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2588acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2589acbf2774SRanjani Sridharan 2590acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2591acbf2774SRanjani Sridharan 2592acbf2774SRanjani Sridharan return ret; 2593acbf2774SRanjani Sridharan } 2594acbf2774SRanjani Sridharan 2595ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 259601d7584cSLiam Girdwood { 259701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 2598acbf2774SRanjani Sridharan int stream = substream->stream; 2599acbf2774SRanjani Sridharan int ret = 0; 260001d7584cSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 260101d7584cSLiam Girdwood 260201d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 260301d7584cSLiam Girdwood 260401d7584cSLiam Girdwood switch (trigger) { 260501d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_PRE: 2606acbf2774SRanjani Sridharan switch (cmd) { 2607acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2608acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2609acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2610acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2611acbf2774SRanjani Sridharan break; 2612acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2613acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2614acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2615acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2616acbf2774SRanjani Sridharan break; 2617acbf2774SRanjani Sridharan default: 2618acbf2774SRanjani Sridharan ret = -EINVAL; 2619acbf2774SRanjani Sridharan break; 262001d7584cSLiam Girdwood } 262101d7584cSLiam Girdwood break; 262201d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_POST: 2623acbf2774SRanjani Sridharan switch (cmd) { 2624acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2625acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2626acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2627acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2628acbf2774SRanjani Sridharan break; 2629acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2630acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2631acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2632acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2633acbf2774SRanjani Sridharan break; 2634acbf2774SRanjani Sridharan default: 2635acbf2774SRanjani Sridharan ret = -EINVAL; 2636acbf2774SRanjani Sridharan break; 263701d7584cSLiam Girdwood } 263801d7584cSLiam Girdwood break; 263907bf84aaSLiam Girdwood case SND_SOC_DPCM_TRIGGER_BESPOKE: 264007bf84aaSLiam Girdwood /* bespoke trigger() - handles both FE and BEs */ 264107bf84aaSLiam Girdwood 2642103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 264307bf84aaSLiam Girdwood fe->dai_link->name, cmd); 264407bf84aaSLiam Girdwood 264507bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, cmd); 264607bf84aaSLiam Girdwood break; 264701d7584cSLiam Girdwood default: 2648103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 264901d7584cSLiam Girdwood fe->dai_link->name); 265001d7584cSLiam Girdwood ret = -EINVAL; 265101d7584cSLiam Girdwood goto out; 265201d7584cSLiam Girdwood } 265301d7584cSLiam Girdwood 2654acbf2774SRanjani Sridharan if (ret < 0) { 2655acbf2774SRanjani Sridharan dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", 2656acbf2774SRanjani Sridharan cmd, ret); 2657acbf2774SRanjani Sridharan goto out; 2658acbf2774SRanjani Sridharan } 2659acbf2774SRanjani Sridharan 266001d7584cSLiam Girdwood switch (cmd) { 266101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 266201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 266301d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 266401d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 266501d7584cSLiam Girdwood break; 266601d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 266701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 266801d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 266901d7584cSLiam Girdwood break; 26709f169b9fSPatrick Lai case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 26719f169b9fSPatrick Lai fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 26729f169b9fSPatrick Lai break; 267301d7584cSLiam Girdwood } 267401d7584cSLiam Girdwood 267501d7584cSLiam Girdwood out: 267601d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 267701d7584cSLiam Girdwood return ret; 267801d7584cSLiam Girdwood } 267901d7584cSLiam Girdwood 2680ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2681ea9d0d77STakashi Iwai { 2682ea9d0d77STakashi Iwai struct snd_soc_pcm_runtime *fe = substream->private_data; 2683ea9d0d77STakashi Iwai int stream = substream->stream; 2684ea9d0d77STakashi Iwai 2685ea9d0d77STakashi Iwai /* if FE's runtime_update is already set, we're in race; 2686ea9d0d77STakashi Iwai * process this trigger later at exit 2687ea9d0d77STakashi Iwai */ 2688ea9d0d77STakashi Iwai if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2689ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = cmd + 1; 2690ea9d0d77STakashi Iwai return 0; /* delayed, assuming it's successful */ 2691ea9d0d77STakashi Iwai } 2692ea9d0d77STakashi Iwai 2693ea9d0d77STakashi Iwai /* we're alone, let's trigger */ 2694ea9d0d77STakashi Iwai return dpcm_fe_dai_do_trigger(substream, cmd); 2695ea9d0d77STakashi Iwai } 2696ea9d0d77STakashi Iwai 269723607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 269801d7584cSLiam Girdwood { 269901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 270001d7584cSLiam Girdwood int ret = 0; 270101d7584cSLiam Girdwood 27028d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 270301d7584cSLiam Girdwood 270401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 270501d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 270601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 270701d7584cSLiam Girdwood 270801d7584cSLiam Girdwood /* is this op for this BE ? */ 270901d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 271001d7584cSLiam Girdwood continue; 271101d7584cSLiam Girdwood 271201d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 271395f444dcSKoro Chen (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 27145087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && 27155087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 271601d7584cSLiam Girdwood continue; 271701d7584cSLiam Girdwood 2718103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: prepare BE %s\n", 271994d215ccS彭东林 be->dai_link->name); 272001d7584cSLiam Girdwood 272101d7584cSLiam Girdwood ret = soc_pcm_prepare(be_substream); 272201d7584cSLiam Girdwood if (ret < 0) { 2723103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: backend prepare failed %d\n", 272401d7584cSLiam Girdwood ret); 272501d7584cSLiam Girdwood break; 272601d7584cSLiam Girdwood } 272701d7584cSLiam Girdwood 272801d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 272901d7584cSLiam Girdwood } 273001d7584cSLiam Girdwood return ret; 273101d7584cSLiam Girdwood } 273201d7584cSLiam Girdwood 273345c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 273401d7584cSLiam Girdwood { 273501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 273601d7584cSLiam Girdwood int stream = substream->stream, ret = 0; 273701d7584cSLiam Girdwood 273801d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 273901d7584cSLiam Girdwood 2740103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 274101d7584cSLiam Girdwood 2742ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 274301d7584cSLiam Girdwood 274401d7584cSLiam Girdwood /* there is no point preparing this FE if there are no BEs */ 274501d7584cSLiam Girdwood if (list_empty(&fe->dpcm[stream].be_clients)) { 2746103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 274701d7584cSLiam Girdwood fe->dai_link->name); 274801d7584cSLiam Girdwood ret = -EINVAL; 274901d7584cSLiam Girdwood goto out; 275001d7584cSLiam Girdwood } 275101d7584cSLiam Girdwood 275225c2f515SKuninori Morimoto ret = dpcm_be_dai_prepare(fe, stream); 275301d7584cSLiam Girdwood if (ret < 0) 275401d7584cSLiam Girdwood goto out; 275501d7584cSLiam Girdwood 275601d7584cSLiam Girdwood /* call prepare on the frontend */ 275701d7584cSLiam Girdwood ret = soc_pcm_prepare(substream); 275801d7584cSLiam Girdwood if (ret < 0) { 2759103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 276001d7584cSLiam Girdwood fe->dai_link->name); 276101d7584cSLiam Girdwood goto out; 276201d7584cSLiam Girdwood } 276301d7584cSLiam Girdwood 276401d7584cSLiam Girdwood /* run the stream event for each BE */ 276501d7584cSLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 276601d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 276701d7584cSLiam Girdwood 276801d7584cSLiam Girdwood out: 2769ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 277001d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 277101d7584cSLiam Girdwood 277201d7584cSLiam Girdwood return ret; 277301d7584cSLiam Girdwood } 277401d7584cSLiam Girdwood 2775618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2776618dae11SLiam Girdwood { 277707bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 277807bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 277907bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2780618dae11SLiam Girdwood int err; 278101d7584cSLiam Girdwood 2782103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2783618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2784618dae11SLiam Girdwood 278507bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 278607bf84aaSLiam Girdwood /* call bespoke trigger - FE takes care of all BE triggers */ 2787103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 278807bf84aaSLiam Girdwood fe->dai_link->name); 278907bf84aaSLiam Girdwood 279007bf84aaSLiam Girdwood err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 279107bf84aaSLiam Girdwood if (err < 0) 2792103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 279307bf84aaSLiam Girdwood } else { 2794103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 279507bf84aaSLiam Girdwood fe->dai_link->name); 279607bf84aaSLiam Girdwood 2797618dae11SLiam Girdwood err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2798618dae11SLiam Girdwood if (err < 0) 2799103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 280007bf84aaSLiam Girdwood } 2801618dae11SLiam Girdwood 2802618dae11SLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 2803618dae11SLiam Girdwood if (err < 0) 2804103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 2805618dae11SLiam Girdwood 2806618dae11SLiam Girdwood err = dpcm_be_dai_shutdown(fe, stream); 2807618dae11SLiam Girdwood if (err < 0) 2808103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 2809618dae11SLiam Girdwood 2810618dae11SLiam Girdwood /* run the stream event for each BE */ 2811618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2812618dae11SLiam Girdwood 2813618dae11SLiam Girdwood return 0; 2814618dae11SLiam Girdwood } 2815618dae11SLiam Girdwood 2816618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2817618dae11SLiam Girdwood { 281807bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 281907bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 2820618dae11SLiam Girdwood struct snd_soc_dpcm *dpcm; 282107bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2822618dae11SLiam Girdwood int ret; 2823a9764869SKaiChieh Chuang unsigned long flags; 2824618dae11SLiam Girdwood 2825103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2826618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2827618dae11SLiam Girdwood 2828618dae11SLiam Girdwood /* Only start the BE if the FE is ready */ 2829618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2830618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 2831618dae11SLiam Girdwood return -EINVAL; 2832618dae11SLiam Girdwood 2833618dae11SLiam Girdwood /* startup must always be called for new BEs */ 2834618dae11SLiam Girdwood ret = dpcm_be_dai_startup(fe, stream); 2835fffc0ca2SDan Carpenter if (ret < 0) 2836618dae11SLiam Girdwood goto disconnect; 2837618dae11SLiam Girdwood 2838618dae11SLiam Girdwood /* keep going if FE state is > open */ 2839618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2840618dae11SLiam Girdwood return 0; 2841618dae11SLiam Girdwood 2842618dae11SLiam Girdwood ret = dpcm_be_dai_hw_params(fe, stream); 2843fffc0ca2SDan Carpenter if (ret < 0) 2844618dae11SLiam Girdwood goto close; 2845618dae11SLiam Girdwood 2846618dae11SLiam Girdwood /* keep going if FE state is > hw_params */ 2847618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2848618dae11SLiam Girdwood return 0; 2849618dae11SLiam Girdwood 2850618dae11SLiam Girdwood 2851618dae11SLiam Girdwood ret = dpcm_be_dai_prepare(fe, stream); 2852fffc0ca2SDan Carpenter if (ret < 0) 2853618dae11SLiam Girdwood goto hw_free; 2854618dae11SLiam Girdwood 2855618dae11SLiam Girdwood /* run the stream event for each BE */ 2856618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2857618dae11SLiam Girdwood 2858618dae11SLiam Girdwood /* keep going if FE state is > prepare */ 2859618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2860618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2861618dae11SLiam Girdwood return 0; 2862618dae11SLiam Girdwood 286307bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 286407bf84aaSLiam Girdwood /* call trigger on the frontend - FE takes care of all BE triggers */ 2865103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 286607bf84aaSLiam Girdwood fe->dai_link->name); 286707bf84aaSLiam Girdwood 286807bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 286907bf84aaSLiam Girdwood if (ret < 0) { 2870103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 287107bf84aaSLiam Girdwood goto hw_free; 287207bf84aaSLiam Girdwood } 287307bf84aaSLiam Girdwood } else { 2874103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 2875618dae11SLiam Girdwood fe->dai_link->name); 2876618dae11SLiam Girdwood 2877618dae11SLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream, 2878618dae11SLiam Girdwood SNDRV_PCM_TRIGGER_START); 2879618dae11SLiam Girdwood if (ret < 0) { 2880103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2881618dae11SLiam Girdwood goto hw_free; 2882618dae11SLiam Girdwood } 288307bf84aaSLiam Girdwood } 2884618dae11SLiam Girdwood 2885618dae11SLiam Girdwood return 0; 2886618dae11SLiam Girdwood 2887618dae11SLiam Girdwood hw_free: 2888618dae11SLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 2889618dae11SLiam Girdwood close: 2890618dae11SLiam Girdwood dpcm_be_dai_shutdown(fe, stream); 2891618dae11SLiam Girdwood disconnect: 2892618dae11SLiam Girdwood /* disconnect any non started BEs */ 2893a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 28948d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2895618dae11SLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 2896618dae11SLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2897618dae11SLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2898618dae11SLiam Girdwood } 2899a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 2900618dae11SLiam Girdwood 2901618dae11SLiam Girdwood return ret; 2902618dae11SLiam Girdwood } 2903618dae11SLiam Girdwood 2904de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2905618dae11SLiam Girdwood { 2906618dae11SLiam Girdwood struct snd_soc_dapm_widget_list *list; 29077083f877SKuninori Morimoto int stream; 2908de15d7ffSJerome Brunet int count, paths; 2909580dff36SKuninori Morimoto int ret; 2910618dae11SLiam Girdwood 29116e1276a5SBard Liao if (fe->num_cpus > 1) { 29126e1276a5SBard Liao dev_err(fe->dev, 29136e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 29146e1276a5SBard Liao return -EINVAL; 29156e1276a5SBard Liao } 29166e1276a5SBard Liao 2917618dae11SLiam Girdwood if (!fe->dai_link->dynamic) 2918de15d7ffSJerome Brunet return 0; 2919618dae11SLiam Girdwood 2920618dae11SLiam Girdwood /* only check active links */ 2921618dae11SLiam Girdwood if (!fe->cpu_dai->active) 2922de15d7ffSJerome Brunet return 0; 2923618dae11SLiam Girdwood 2924618dae11SLiam Girdwood /* DAPM sync will call this to update DSP paths */ 2925de15d7ffSJerome Brunet dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2926de15d7ffSJerome Brunet new ? "new" : "old", fe->dai_link->name); 2927618dae11SLiam Girdwood 29287083f877SKuninori Morimoto for_each_pcm_streams(stream) { 2929075207d2SQiao Zhou 29307083f877SKuninori Morimoto /* skip if FE doesn't have playback/capture capability */ 29317083f877SKuninori Morimoto if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) || 29327083f877SKuninori Morimoto !snd_soc_dai_stream_valid(fe->codec_dai, stream)) 29337083f877SKuninori Morimoto continue; 2934618dae11SLiam Girdwood 29357083f877SKuninori Morimoto /* skip if FE isn't currently playing/capturing */ 29367083f877SKuninori Morimoto if (!fe->cpu_dai->stream_active[stream] || 29377083f877SKuninori Morimoto !fe->codec_dai->stream_active[stream]) 29387083f877SKuninori Morimoto continue; 29397083f877SKuninori Morimoto 29407083f877SKuninori Morimoto paths = dpcm_path_get(fe, stream, &list); 2941618dae11SLiam Girdwood if (paths < 0) { 2942103d84a3SLiam Girdwood dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 29437083f877SKuninori Morimoto fe->dai_link->name, 29447083f877SKuninori Morimoto stream == SNDRV_PCM_STREAM_PLAYBACK ? 29457083f877SKuninori Morimoto "playback" : "capture"); 2946618dae11SLiam Girdwood return paths; 2947618dae11SLiam Girdwood } 2948618dae11SLiam Girdwood 29497083f877SKuninori Morimoto /* update any playback/capture paths */ 29507083f877SKuninori Morimoto count = dpcm_process_paths(fe, stream, &list, new); 2951de15d7ffSJerome Brunet if (count) { 2952580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2953de15d7ffSJerome Brunet if (new) 2954580dff36SKuninori Morimoto ret = dpcm_run_update_startup(fe, stream); 2955de15d7ffSJerome Brunet else 2956580dff36SKuninori Morimoto ret = dpcm_run_update_shutdown(fe, stream); 2957580dff36SKuninori Morimoto if (ret < 0) 2958580dff36SKuninori Morimoto dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2959580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2960de15d7ffSJerome Brunet 29617083f877SKuninori Morimoto dpcm_clear_pending_state(fe, stream); 29627083f877SKuninori Morimoto dpcm_be_disconnect(fe, stream); 2963618dae11SLiam Girdwood } 2964618dae11SLiam Girdwood 29657ed9de76SQiao Zhou dpcm_path_put(&list); 2966618dae11SLiam Girdwood } 2967618dae11SLiam Girdwood 2968de15d7ffSJerome Brunet return 0; 2969618dae11SLiam Girdwood } 2970618dae11SLiam Girdwood 2971de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2972de15d7ffSJerome Brunet * any DAI links. 2973de15d7ffSJerome Brunet */ 2974*f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card) 2975de15d7ffSJerome Brunet { 2976de15d7ffSJerome Brunet struct snd_soc_pcm_runtime *fe; 2977de15d7ffSJerome Brunet int ret = 0; 2978de15d7ffSJerome Brunet 2979de15d7ffSJerome Brunet mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2980de15d7ffSJerome Brunet /* shutdown all old paths first */ 2981bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2982de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 0); 2983de15d7ffSJerome Brunet if (ret) 2984de15d7ffSJerome Brunet goto out; 2985de15d7ffSJerome Brunet } 2986de15d7ffSJerome Brunet 2987de15d7ffSJerome Brunet /* bring new paths up */ 2988bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2989de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 1); 2990de15d7ffSJerome Brunet if (ret) 2991de15d7ffSJerome Brunet goto out; 2992de15d7ffSJerome Brunet } 2993de15d7ffSJerome Brunet 2994de15d7ffSJerome Brunet out: 2995618dae11SLiam Girdwood mutex_unlock(&card->mutex); 2996de15d7ffSJerome Brunet return ret; 2997618dae11SLiam Girdwood } 2998*f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update); 299901d7584cSLiam Girdwood 3000265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream) 300101d7584cSLiam Girdwood { 300201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 300301d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 3004265694b6SKuninori Morimoto int stream = fe_substream->stream; 300530fca26fSKuninori Morimoto 300630fca26fSKuninori Morimoto /* mark FE's links ready to prune */ 300730fca26fSKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 300830fca26fSKuninori Morimoto dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 300930fca26fSKuninori Morimoto 301030fca26fSKuninori Morimoto dpcm_be_disconnect(fe, stream); 301130fca26fSKuninori Morimoto 301230fca26fSKuninori Morimoto fe->dpcm[stream].runtime = NULL; 3013265694b6SKuninori Morimoto } 3014265694b6SKuninori Morimoto 3015265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 3016265694b6SKuninori Morimoto { 3017265694b6SKuninori Morimoto struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 3018265694b6SKuninori Morimoto int ret; 3019265694b6SKuninori Morimoto 3020265694b6SKuninori Morimoto mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 3021265694b6SKuninori Morimoto ret = dpcm_fe_dai_shutdown(fe_substream); 3022265694b6SKuninori Morimoto 3023265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream); 3024265694b6SKuninori Morimoto 302530fca26fSKuninori Morimoto mutex_unlock(&fe->card->mutex); 302630fca26fSKuninori Morimoto return ret; 302730fca26fSKuninori Morimoto } 302830fca26fSKuninori Morimoto 302901d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 303001d7584cSLiam Girdwood { 303101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 303201d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list; 303301d7584cSLiam Girdwood int ret; 303401d7584cSLiam Girdwood int stream = fe_substream->stream; 303501d7584cSLiam Girdwood 303601d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 303701d7584cSLiam Girdwood fe->dpcm[stream].runtime = fe_substream->runtime; 303801d7584cSLiam Girdwood 30398f70e515SQiao Zhou ret = dpcm_path_get(fe, stream, &list); 30408f70e515SQiao Zhou if (ret < 0) { 3041cae06eb9SKuninori Morimoto goto open_end; 30428f70e515SQiao Zhou } else if (ret == 0) { 3043103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 304401d7584cSLiam Girdwood fe->dai_link->name, stream ? "capture" : "playback"); 304501d7584cSLiam Girdwood } 304601d7584cSLiam Girdwood 304701d7584cSLiam Girdwood /* calculate valid and active FE <-> BE dpcms */ 304801d7584cSLiam Girdwood dpcm_process_paths(fe, stream, &list, 1); 304901d7584cSLiam Girdwood 305001d7584cSLiam Girdwood ret = dpcm_fe_dai_startup(fe_substream); 3051265694b6SKuninori Morimoto if (ret < 0) 3052265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream); 305301d7584cSLiam Girdwood 305401d7584cSLiam Girdwood dpcm_clear_pending_state(fe, stream); 305501d7584cSLiam Girdwood dpcm_path_put(&list); 3056cae06eb9SKuninori Morimoto open_end: 305701d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 305801d7584cSLiam Girdwood return ret; 305901d7584cSLiam Girdwood } 306001d7584cSLiam Girdwood 3061ddee627cSLiam Girdwood /* create a new pcm */ 3062ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 3063ddee627cSLiam Girdwood { 30642e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 306519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 30662b544dd7SKuninori Morimoto struct snd_soc_component *component; 3067ddee627cSLiam Girdwood struct snd_pcm *pcm; 3068ddee627cSLiam Girdwood char new_name[64]; 3069ddee627cSLiam Girdwood int ret = 0, playback = 0, capture = 0; 30702e5894d7SBenoit Cousson int i; 3071ddee627cSLiam Girdwood 307201d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 30731e9de42fSLiam Girdwood playback = rtd->dai_link->dpcm_playback; 30741e9de42fSLiam Girdwood capture = rtd->dai_link->dpcm_capture; 307501d7584cSLiam Girdwood } else { 3076a342031cSJerome Brunet /* Adapt stream for codec2codec links */ 3077a4877a6fSStephan Gerhold int cpu_capture = rtd->dai_link->params ? 3078a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 3079a4877a6fSStephan Gerhold int cpu_playback = rtd->dai_link->params ? 3080a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 3081a342031cSJerome Brunet 3082a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 308319bdcc7aSShreyas NC if (rtd->num_cpus == 1) { 308419bdcc7aSShreyas NC cpu_dai = rtd->cpu_dais[0]; 308519bdcc7aSShreyas NC } else if (rtd->num_cpus == rtd->num_codecs) { 308619bdcc7aSShreyas NC cpu_dai = rtd->cpu_dais[i]; 308719bdcc7aSShreyas NC } else { 308819bdcc7aSShreyas NC dev_err(rtd->card->dev, 308919bdcc7aSShreyas NC "N cpus to M codecs link is not supported yet\n"); 309019bdcc7aSShreyas NC return -EINVAL; 309119bdcc7aSShreyas NC } 309219bdcc7aSShreyas NC 3093467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 3094a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) 3095ddee627cSLiam Girdwood playback = 1; 3096467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 3097a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) 3098ddee627cSLiam Girdwood capture = 1; 309901d7584cSLiam Girdwood } 31002e5894d7SBenoit Cousson } 31012e5894d7SBenoit Cousson 3102d6bead02SFabio Estevam if (rtd->dai_link->playback_only) { 3103d6bead02SFabio Estevam playback = 1; 3104d6bead02SFabio Estevam capture = 0; 3105d6bead02SFabio Estevam } 3106d6bead02SFabio Estevam 3107d6bead02SFabio Estevam if (rtd->dai_link->capture_only) { 3108d6bead02SFabio Estevam playback = 0; 3109d6bead02SFabio Estevam capture = 1; 3110d6bead02SFabio Estevam } 3111d6bead02SFabio Estevam 311201d7584cSLiam Girdwood /* create the PCM */ 3113a342031cSJerome Brunet if (rtd->dai_link->params) { 3114a342031cSJerome Brunet snprintf(new_name, sizeof(new_name), "codec2codec(%s)", 3115a342031cSJerome Brunet rtd->dai_link->stream_name); 3116a342031cSJerome Brunet 3117a342031cSJerome Brunet ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 3118a342031cSJerome Brunet playback, capture, &pcm); 3119a342031cSJerome Brunet } else if (rtd->dai_link->no_pcm) { 312001d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)", 312101d7584cSLiam Girdwood rtd->dai_link->stream_name); 312201d7584cSLiam Girdwood 312301d7584cSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 312401d7584cSLiam Girdwood playback, capture, &pcm); 312501d7584cSLiam Girdwood } else { 312601d7584cSLiam Girdwood if (rtd->dai_link->dynamic) 312701d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s (*)", 312801d7584cSLiam Girdwood rtd->dai_link->stream_name); 312901d7584cSLiam Girdwood else 313001d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s %s-%d", 31312e5894d7SBenoit Cousson rtd->dai_link->stream_name, 31322e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? 31332e5894d7SBenoit Cousson "multicodec" : rtd->codec_dai->name, num); 313401d7584cSLiam Girdwood 313501d7584cSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 313601d7584cSLiam Girdwood capture, &pcm); 313701d7584cSLiam Girdwood } 3138ddee627cSLiam Girdwood if (ret < 0) { 3139103d84a3SLiam Girdwood dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 31405cb9b748SLiam Girdwood rtd->dai_link->name); 3141ddee627cSLiam Girdwood return ret; 3142ddee627cSLiam Girdwood } 3143103d84a3SLiam Girdwood dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 3144ddee627cSLiam Girdwood 3145ddee627cSLiam Girdwood /* DAPM dai link stream work */ 3146a342031cSJerome Brunet if (rtd->dai_link->params) 31474bf2e385SCurtis Malainey rtd->close_delayed_work_func = codec2codec_close_delayed_work; 3148a342031cSJerome Brunet else 314983f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work; 3150ddee627cSLiam Girdwood 315148c7699fSVinod Koul pcm->nonatomic = rtd->dai_link->nonatomic; 3152ddee627cSLiam Girdwood rtd->pcm = pcm; 3153ddee627cSLiam Girdwood pcm->private_data = rtd; 315401d7584cSLiam Girdwood 3155a342031cSJerome Brunet if (rtd->dai_link->no_pcm || rtd->dai_link->params) { 315601d7584cSLiam Girdwood if (playback) 315701d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 315801d7584cSLiam Girdwood if (capture) 315901d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 316001d7584cSLiam Girdwood goto out; 316101d7584cSLiam Girdwood } 316201d7584cSLiam Girdwood 316301d7584cSLiam Girdwood /* ASoC PCM operations */ 316401d7584cSLiam Girdwood if (rtd->dai_link->dynamic) { 316501d7584cSLiam Girdwood rtd->ops.open = dpcm_fe_dai_open; 316601d7584cSLiam Girdwood rtd->ops.hw_params = dpcm_fe_dai_hw_params; 316701d7584cSLiam Girdwood rtd->ops.prepare = dpcm_fe_dai_prepare; 316801d7584cSLiam Girdwood rtd->ops.trigger = dpcm_fe_dai_trigger; 316901d7584cSLiam Girdwood rtd->ops.hw_free = dpcm_fe_dai_hw_free; 317001d7584cSLiam Girdwood rtd->ops.close = dpcm_fe_dai_close; 317101d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 317201d7584cSLiam Girdwood } else { 317301d7584cSLiam Girdwood rtd->ops.open = soc_pcm_open; 317401d7584cSLiam Girdwood rtd->ops.hw_params = soc_pcm_hw_params; 317501d7584cSLiam Girdwood rtd->ops.prepare = soc_pcm_prepare; 317601d7584cSLiam Girdwood rtd->ops.trigger = soc_pcm_trigger; 317701d7584cSLiam Girdwood rtd->ops.hw_free = soc_pcm_hw_free; 317801d7584cSLiam Girdwood rtd->ops.close = soc_pcm_close; 317901d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 318001d7584cSLiam Girdwood } 318101d7584cSLiam Girdwood 3182613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 31832b544dd7SKuninori Morimoto const struct snd_soc_component_driver *drv = component->driver; 3184b8135864SKuninori Morimoto 31853b1c952cSTakashi Iwai if (drv->ioctl) 31863b1c952cSTakashi Iwai rtd->ops.ioctl = snd_soc_pcm_component_ioctl; 31871e5ddb6bSTakashi Iwai if (drv->sync_stop) 31881e5ddb6bSTakashi Iwai rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; 3189e9067bb5SKuninori Morimoto if (drv->copy_user) 319082d81f5cSKuninori Morimoto rtd->ops.copy_user = snd_soc_pcm_component_copy_user; 3191e9067bb5SKuninori Morimoto if (drv->page) 31929c712e4fSKuninori Morimoto rtd->ops.page = snd_soc_pcm_component_page; 3193e9067bb5SKuninori Morimoto if (drv->mmap) 3194205875e1SKuninori Morimoto rtd->ops.mmap = snd_soc_pcm_component_mmap; 3195b8135864SKuninori Morimoto } 3196b8135864SKuninori Morimoto 3197ddee627cSLiam Girdwood if (playback) 319801d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 3199ddee627cSLiam Girdwood 3200ddee627cSLiam Girdwood if (capture) 320101d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 3202ddee627cSLiam Girdwood 3203b2b2afbbSKuninori Morimoto ret = snd_soc_pcm_component_new(rtd); 3204ddee627cSLiam Girdwood if (ret < 0) { 32057484291eSKuninori Morimoto dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret); 3206ddee627cSLiam Girdwood return ret; 3207ddee627cSLiam Girdwood } 3208c641e5b2SJohan Hovold 32093d21ef0bSTakashi Iwai pcm->no_device_suspend = true; 321001d7584cSLiam Girdwood out: 32112e5894d7SBenoit Cousson dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", 32122e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, 321319bdcc7aSShreyas NC (rtd->num_cpus > 1) ? "multicpu" : rtd->cpu_dai->name); 3214ddee627cSLiam Girdwood return ret; 3215ddee627cSLiam Girdwood } 321601d7584cSLiam Girdwood 321701d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */ 321801d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 321901d7584cSLiam Girdwood { 322001d7584cSLiam Girdwood if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 322101d7584cSLiam Girdwood return 1; 322201d7584cSLiam Girdwood return 0; 322301d7584cSLiam Girdwood } 322401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 322501d7584cSLiam Girdwood 322601d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */ 322701d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 322801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 322901d7584cSLiam Girdwood { 323001d7584cSLiam Girdwood if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 323101d7584cSLiam Girdwood ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 323201d7584cSLiam Girdwood be->dpcm[stream].runtime_update)) 323301d7584cSLiam Girdwood return 1; 323401d7584cSLiam Girdwood return 0; 323501d7584cSLiam Girdwood } 323601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 323701d7584cSLiam Girdwood 323801d7584cSLiam Girdwood /* get the substream for this BE */ 323901d7584cSLiam Girdwood struct snd_pcm_substream * 324001d7584cSLiam Girdwood snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 324101d7584cSLiam Girdwood { 324201d7584cSLiam Girdwood return be->pcm->streams[stream].substream; 324301d7584cSLiam Girdwood } 324401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 324501d7584cSLiam Girdwood 3246085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, 3247085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, 3248085d22beSKuninori Morimoto int stream, 3249085d22beSKuninori Morimoto const enum snd_soc_dpcm_state *states, 3250085d22beSKuninori Morimoto int num_states) 325101d7584cSLiam Girdwood { 325201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 325301d7584cSLiam Girdwood int state; 3254a9764869SKaiChieh Chuang int ret = 1; 3255a9764869SKaiChieh Chuang unsigned long flags; 3256085d22beSKuninori Morimoto int i; 325701d7584cSLiam Girdwood 3258a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3259d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 326001d7584cSLiam Girdwood 326101d7584cSLiam Girdwood if (dpcm->fe == fe) 326201d7584cSLiam Girdwood continue; 326301d7584cSLiam Girdwood 326401d7584cSLiam Girdwood state = dpcm->fe->dpcm[stream].state; 3265085d22beSKuninori Morimoto for (i = 0; i < num_states; i++) { 3266085d22beSKuninori Morimoto if (state == states[i]) { 3267a9764869SKaiChieh Chuang ret = 0; 3268a9764869SKaiChieh Chuang break; 326901d7584cSLiam Girdwood } 3270a9764869SKaiChieh Chuang } 3271085d22beSKuninori Morimoto } 3272a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 327301d7584cSLiam Girdwood 3274085d22beSKuninori Morimoto /* it's safe to do this BE DAI */ 3275a9764869SKaiChieh Chuang return ret; 327601d7584cSLiam Girdwood } 3277085d22beSKuninori Morimoto 3278085d22beSKuninori Morimoto /* 3279085d22beSKuninori Morimoto * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 3280085d22beSKuninori Morimoto * are not running, paused or suspended for the specified stream direction. 3281085d22beSKuninori Morimoto */ 3282085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 3283085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, int stream) 3284085d22beSKuninori Morimoto { 3285085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3286085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3287085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3288085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3289085d22beSKuninori Morimoto }; 3290085d22beSKuninori Morimoto 3291085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 3292085d22beSKuninori Morimoto } 329301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 329401d7584cSLiam Girdwood 329501d7584cSLiam Girdwood /* 329601d7584cSLiam Girdwood * We can only change hw params a BE DAI if any of it's FE are not prepared, 329701d7584cSLiam Girdwood * running, paused or suspended for the specified stream direction. 329801d7584cSLiam Girdwood */ 329901d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 330001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 330101d7584cSLiam Girdwood { 3302085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3303085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3304085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3305085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3306085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PREPARE, 3307085d22beSKuninori Morimoto }; 330801d7584cSLiam Girdwood 3309085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 331001d7584cSLiam Girdwood } 331101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 3312