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 */ 69c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 70c3212829SKuninori Morimoto "[%s - %s]\n", fe->dai_link->name, 71c3212829SKuninori Morimoto stream ? "Capture" : "Playback"); 72c3212829SKuninori Morimoto 73c3212829SKuninori Morimoto offset += snprintf(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)) 78c3212829SKuninori Morimoto offset += snprintf(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 */ 86c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, "Backends:\n"); 87c3212829SKuninori Morimoto 88c3212829SKuninori Morimoto if (list_empty(&fe->dpcm[stream].be_clients)) { 89c3212829SKuninori Morimoto offset += snprintf(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 99c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 100c3212829SKuninori Morimoto "- %s\n", be->dai_link->name); 101c3212829SKuninori Morimoto 102c3212829SKuninori Morimoto offset += snprintf(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)) 108c3212829SKuninori Morimoto offset += snprintf(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 128c3212829SKuninori Morimoto buf = kmalloc(out_count, GFP_KERNEL); 129c3212829SKuninori Morimoto if (!buf) 130c3212829SKuninori Morimoto return -ENOMEM; 131c3212829SKuninori Morimoto 132c3212829SKuninori Morimoto for_each_pcm_streams(stream) 133c3212829SKuninori Morimoto if (snd_soc_dai_stream_valid(fe->cpu_dai, stream)) 134c3212829SKuninori Morimoto offset += dpcm_show_state(fe, stream, 135c3212829SKuninori Morimoto buf + offset, 136c3212829SKuninori Morimoto out_count - offset); 137c3212829SKuninori Morimoto 138c3212829SKuninori Morimoto ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 139c3212829SKuninori Morimoto 140c3212829SKuninori Morimoto kfree(buf); 141c3212829SKuninori Morimoto return ret; 142c3212829SKuninori Morimoto } 143c3212829SKuninori Morimoto 144c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = { 145c3212829SKuninori Morimoto .open = simple_open, 146c3212829SKuninori Morimoto .read = dpcm_state_read_file, 147c3212829SKuninori Morimoto .llseek = default_llseek, 148c3212829SKuninori Morimoto }; 149c3212829SKuninori Morimoto 150c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 151c3212829SKuninori Morimoto { 152c3212829SKuninori Morimoto if (!rtd->dai_link) 153c3212829SKuninori Morimoto return; 154c3212829SKuninori Morimoto 155c3212829SKuninori Morimoto if (!rtd->dai_link->dynamic) 156c3212829SKuninori Morimoto return; 157c3212829SKuninori Morimoto 158c3212829SKuninori Morimoto if (!rtd->card->debugfs_card_root) 159c3212829SKuninori Morimoto return; 160c3212829SKuninori Morimoto 161c3212829SKuninori Morimoto rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 162c3212829SKuninori Morimoto rtd->card->debugfs_card_root); 163c3212829SKuninori Morimoto 164c3212829SKuninori Morimoto debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 165c3212829SKuninori Morimoto rtd, &dpcm_state_fops); 166c3212829SKuninori Morimoto } 167*154dae87SKuninori Morimoto 168*154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream) 169*154dae87SKuninori Morimoto { 170*154dae87SKuninori Morimoto char *name; 171*154dae87SKuninori Morimoto 172*154dae87SKuninori Morimoto name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name, 173*154dae87SKuninori Morimoto stream ? "capture" : "playback"); 174*154dae87SKuninori Morimoto if (name) { 175*154dae87SKuninori Morimoto dpcm->debugfs_state = debugfs_create_dir( 176*154dae87SKuninori Morimoto name, dpcm->fe->debugfs_dpcm_root); 177*154dae87SKuninori Morimoto debugfs_create_u32("state", 0644, dpcm->debugfs_state, 178*154dae87SKuninori Morimoto &dpcm->state); 179*154dae87SKuninori Morimoto kfree(name); 180*154dae87SKuninori Morimoto } 181*154dae87SKuninori Morimoto } 182*154dae87SKuninori Morimoto 183*154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 184*154dae87SKuninori Morimoto { 185*154dae87SKuninori Morimoto debugfs_remove_recursive(dpcm->debugfs_state); 186*154dae87SKuninori Morimoto } 187*154dae87SKuninori Morimoto 188*154dae87SKuninori Morimoto #else 189*154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, 190*154dae87SKuninori Morimoto int stream) 191*154dae87SKuninori Morimoto { 192*154dae87SKuninori Morimoto } 193*154dae87SKuninori Morimoto 194*154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 195*154dae87SKuninori Morimoto { 196*154dae87SKuninori Morimoto } 197c3212829SKuninori Morimoto #endif 198c3212829SKuninori Morimoto 199f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd, 200f183f927SKuninori Morimoto struct snd_pcm_substream *substream) 201f183f927SKuninori Morimoto { 202f183f927SKuninori Morimoto if (rtd->dai_link->ops && 203f183f927SKuninori Morimoto rtd->dai_link->ops->startup) 204f183f927SKuninori Morimoto return rtd->dai_link->ops->startup(substream); 205f183f927SKuninori Morimoto return 0; 206f183f927SKuninori Morimoto } 207f183f927SKuninori Morimoto 2080be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd, 2090be429f9SKuninori Morimoto struct snd_pcm_substream *substream) 2100be429f9SKuninori Morimoto { 2110be429f9SKuninori Morimoto if (rtd->dai_link->ops && 2120be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown) 2130be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown(substream); 2140be429f9SKuninori Morimoto } 2150be429f9SKuninori Morimoto 21644c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd, 21744c1a75bSKuninori Morimoto struct snd_pcm_substream *substream) 21844c1a75bSKuninori Morimoto { 21944c1a75bSKuninori Morimoto if (rtd->dai_link->ops && 22044c1a75bSKuninori Morimoto rtd->dai_link->ops->prepare) 22144c1a75bSKuninori Morimoto return rtd->dai_link->ops->prepare(substream); 22244c1a75bSKuninori Morimoto return 0; 22344c1a75bSKuninori Morimoto } 22444c1a75bSKuninori Morimoto 225de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd, 226de9ad990SKuninori Morimoto struct snd_pcm_substream *substream, 227de9ad990SKuninori Morimoto struct snd_pcm_hw_params *params) 228de9ad990SKuninori Morimoto { 229de9ad990SKuninori Morimoto if (rtd->dai_link->ops && 230de9ad990SKuninori Morimoto rtd->dai_link->ops->hw_params) 231de9ad990SKuninori Morimoto return rtd->dai_link->ops->hw_params(substream, params); 232de9ad990SKuninori Morimoto return 0; 233de9ad990SKuninori Morimoto } 234de9ad990SKuninori Morimoto 23549f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd, 23649f020e5SKuninori Morimoto struct snd_pcm_substream *substream) 23749f020e5SKuninori Morimoto { 23849f020e5SKuninori Morimoto if (rtd->dai_link->ops && 23949f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free) 24049f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free(substream); 24149f020e5SKuninori Morimoto } 24249f020e5SKuninori Morimoto 243ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd, 244ad2bf9f2SKuninori Morimoto struct snd_pcm_substream *substream, 245ad2bf9f2SKuninori Morimoto int cmd) 246ad2bf9f2SKuninori Morimoto { 247ad2bf9f2SKuninori Morimoto if (rtd->dai_link->ops && 248ad2bf9f2SKuninori Morimoto rtd->dai_link->ops->trigger) 249ad2bf9f2SKuninori Morimoto return rtd->dai_link->ops->trigger(substream, cmd); 250ad2bf9f2SKuninori Morimoto return 0; 251ad2bf9f2SKuninori Morimoto } 252ad2bf9f2SKuninori Morimoto 2537a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, 2547a5aaba4SKuninori Morimoto int stream, int action) 2557a5aaba4SKuninori Morimoto { 2567a5aaba4SKuninori Morimoto struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2577a5aaba4SKuninori Morimoto struct snd_soc_dai *codec_dai; 2587a5aaba4SKuninori Morimoto int i; 2597a5aaba4SKuninori Morimoto 2607a5aaba4SKuninori Morimoto lockdep_assert_held(&rtd->card->pcm_mutex); 2617a5aaba4SKuninori Morimoto 2620f6011fdSKuninori Morimoto cpu_dai->stream_active[stream] += action; 2637a5aaba4SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 2640f6011fdSKuninori Morimoto codec_dai->stream_active[stream] += action; 2657a5aaba4SKuninori Morimoto 2667a5aaba4SKuninori Morimoto cpu_dai->active += action; 2677a5aaba4SKuninori Morimoto cpu_dai->component->active += action; 2687a5aaba4SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 2697a5aaba4SKuninori Morimoto codec_dai->active += action; 2707a5aaba4SKuninori Morimoto codec_dai->component->active += action; 2717a5aaba4SKuninori Morimoto } 2727a5aaba4SKuninori Morimoto } 2737a5aaba4SKuninori Morimoto 27490996f43SLars-Peter Clausen /** 27524894b76SLars-Peter Clausen * snd_soc_runtime_activate() - Increment active count for PCM runtime components 27624894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is activated 27724894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 27824894b76SLars-Peter Clausen * 27924894b76SLars-Peter Clausen * Increments the active count for all the DAIs and components attached to a PCM 28024894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is opened. 28124894b76SLars-Peter Clausen * 28272b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 28324894b76SLars-Peter Clausen */ 28424894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) 28524894b76SLars-Peter Clausen { 2867a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, 1); 28724894b76SLars-Peter Clausen } 28824894b76SLars-Peter Clausen 28924894b76SLars-Peter Clausen /** 29024894b76SLars-Peter Clausen * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components 29124894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is deactivated 29224894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 29324894b76SLars-Peter Clausen * 29424894b76SLars-Peter Clausen * Decrements the active count for all the DAIs and components attached to a PCM 29524894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is closed. 29624894b76SLars-Peter Clausen * 29772b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 29824894b76SLars-Peter Clausen */ 29924894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) 30024894b76SLars-Peter Clausen { 3017a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, -1); 30224894b76SLars-Peter Clausen } 30324894b76SLars-Peter Clausen 30424894b76SLars-Peter Clausen /** 305208a1589SLars-Peter Clausen * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 306208a1589SLars-Peter Clausen * @rtd: The ASoC PCM runtime that should be checked. 307208a1589SLars-Peter Clausen * 308208a1589SLars-Peter Clausen * This function checks whether the power down delay should be ignored for a 309208a1589SLars-Peter Clausen * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 310208a1589SLars-Peter Clausen * been configured to ignore the delay, or if none of the components benefits 311208a1589SLars-Peter Clausen * from having the delay. 312208a1589SLars-Peter Clausen */ 313208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 314208a1589SLars-Peter Clausen { 315fbb16563SKuninori Morimoto struct snd_soc_component *component; 3162e5894d7SBenoit Cousson bool ignore = true; 317613fb500SKuninori Morimoto int i; 3182e5894d7SBenoit Cousson 319208a1589SLars-Peter Clausen if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 320208a1589SLars-Peter Clausen return true; 321208a1589SLars-Peter Clausen 322613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 32372c38184SKuninori Morimoto ignore &= !component->driver->use_pmdown_time; 324fbb16563SKuninori Morimoto 325fbb16563SKuninori Morimoto return ignore; 326208a1589SLars-Peter Clausen } 327208a1589SLars-Peter Clausen 328208a1589SLars-Peter Clausen /** 32990996f43SLars-Peter Clausen * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 33090996f43SLars-Peter Clausen * @substream: the pcm substream 33190996f43SLars-Peter Clausen * @hw: the hardware parameters 33290996f43SLars-Peter Clausen * 33390996f43SLars-Peter Clausen * Sets the substream runtime hardware parameters. 33490996f43SLars-Peter Clausen */ 33590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 33690996f43SLars-Peter Clausen const struct snd_pcm_hardware *hw) 33790996f43SLars-Peter Clausen { 33890996f43SLars-Peter Clausen struct snd_pcm_runtime *runtime = substream->runtime; 33990996f43SLars-Peter Clausen runtime->hw.info = hw->info; 34090996f43SLars-Peter Clausen runtime->hw.formats = hw->formats; 34190996f43SLars-Peter Clausen runtime->hw.period_bytes_min = hw->period_bytes_min; 34290996f43SLars-Peter Clausen runtime->hw.period_bytes_max = hw->period_bytes_max; 34390996f43SLars-Peter Clausen runtime->hw.periods_min = hw->periods_min; 34490996f43SLars-Peter Clausen runtime->hw.periods_max = hw->periods_max; 34590996f43SLars-Peter Clausen runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 34690996f43SLars-Peter Clausen runtime->hw.fifo_size = hw->fifo_size; 34790996f43SLars-Peter Clausen return 0; 34890996f43SLars-Peter Clausen } 34990996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 35090996f43SLars-Peter Clausen 35101d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */ 35223607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 35301d7584cSLiam Girdwood int event) 35401d7584cSLiam Girdwood { 35501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 35601d7584cSLiam Girdwood 3578d6258a4SKuninori Morimoto for_each_dpcm_be(fe, dir, dpcm) { 35801d7584cSLiam Girdwood 35901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 36001d7584cSLiam Girdwood 361103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 36201d7584cSLiam Girdwood be->dai_link->name, event, dir); 36301d7584cSLiam Girdwood 364b1cd2e34SBanajit Goswami if ((event == SND_SOC_DAPM_STREAM_STOP) && 365b1cd2e34SBanajit Goswami (be->dpcm[dir].users >= 1)) 366b1cd2e34SBanajit Goswami continue; 367b1cd2e34SBanajit Goswami 36801d7584cSLiam Girdwood snd_soc_dapm_stream_event(be, dir, event); 36901d7584cSLiam Girdwood } 37001d7584cSLiam Girdwood 37101d7584cSLiam Girdwood snd_soc_dapm_stream_event(fe, dir, event); 37201d7584cSLiam Girdwood 37301d7584cSLiam Girdwood return 0; 37401d7584cSLiam Girdwood } 37501d7584cSLiam Girdwood 37617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 37717841020SDong Aisheng struct snd_soc_dai *soc_dai) 378ddee627cSLiam Girdwood { 379ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 380ddee627cSLiam Girdwood int ret; 381ddee627cSLiam Girdwood 3823635bf09SNicolin Chen if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 3833635bf09SNicolin Chen rtd->dai_link->symmetric_rates)) { 3843635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 3853635bf09SNicolin Chen soc_dai->rate); 386ddee627cSLiam Girdwood 3874dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 388ddee627cSLiam Girdwood SNDRV_PCM_HW_PARAM_RATE, 3894dcdd43bSLars-Peter Clausen soc_dai->rate); 390ddee627cSLiam Girdwood if (ret < 0) { 39117841020SDong Aisheng dev_err(soc_dai->dev, 3923635bf09SNicolin Chen "ASoC: Unable to apply rate constraint: %d\n", 393103d84a3SLiam Girdwood ret); 394ddee627cSLiam Girdwood return ret; 395ddee627cSLiam Girdwood } 3963635bf09SNicolin Chen } 3973635bf09SNicolin Chen 3983635bf09SNicolin Chen if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 3993635bf09SNicolin Chen rtd->dai_link->symmetric_channels)) { 4003635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 4013635bf09SNicolin Chen soc_dai->channels); 4023635bf09SNicolin Chen 4034dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 4043635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_CHANNELS, 4053635bf09SNicolin Chen soc_dai->channels); 4063635bf09SNicolin Chen if (ret < 0) { 4073635bf09SNicolin Chen dev_err(soc_dai->dev, 4083635bf09SNicolin Chen "ASoC: Unable to apply channel symmetry constraint: %d\n", 4093635bf09SNicolin Chen ret); 4103635bf09SNicolin Chen return ret; 4113635bf09SNicolin Chen } 4123635bf09SNicolin Chen } 4133635bf09SNicolin Chen 4143635bf09SNicolin Chen if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 4153635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits)) { 4163635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 4173635bf09SNicolin Chen soc_dai->sample_bits); 4183635bf09SNicolin Chen 4194dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 4203635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 4213635bf09SNicolin Chen soc_dai->sample_bits); 4223635bf09SNicolin Chen if (ret < 0) { 4233635bf09SNicolin Chen dev_err(soc_dai->dev, 4243635bf09SNicolin Chen "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 4253635bf09SNicolin Chen ret); 4263635bf09SNicolin Chen return ret; 4273635bf09SNicolin Chen } 4283635bf09SNicolin Chen } 429ddee627cSLiam Girdwood 430ddee627cSLiam Girdwood return 0; 431ddee627cSLiam Girdwood } 432ddee627cSLiam Girdwood 4333635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 4343635bf09SNicolin Chen struct snd_pcm_hw_params *params) 4353635bf09SNicolin Chen { 4363635bf09SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 4373635bf09SNicolin Chen struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 4380b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 4392e5894d7SBenoit Cousson unsigned int rate, channels, sample_bits, symmetry, i; 4403635bf09SNicolin Chen 4413635bf09SNicolin Chen rate = params_rate(params); 4423635bf09SNicolin Chen channels = params_channels(params); 4433635bf09SNicolin Chen sample_bits = snd_pcm_format_physical_width(params_format(params)); 4443635bf09SNicolin Chen 4453635bf09SNicolin Chen /* reject unmatched parameters when applying symmetry */ 4463635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_rates || 4473635bf09SNicolin Chen rtd->dai_link->symmetric_rates; 4482e5894d7SBenoit Cousson 4490b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4500b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_rates; 4512e5894d7SBenoit Cousson 4523635bf09SNicolin Chen if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { 4533635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 4543635bf09SNicolin Chen cpu_dai->rate, rate); 4553635bf09SNicolin Chen return -EINVAL; 4563635bf09SNicolin Chen } 4573635bf09SNicolin Chen 4583635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_channels || 4593635bf09SNicolin Chen rtd->dai_link->symmetric_channels; 4602e5894d7SBenoit Cousson 4610b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4620b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_channels; 4632e5894d7SBenoit Cousson 4643635bf09SNicolin Chen if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { 4653635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 4663635bf09SNicolin Chen cpu_dai->channels, channels); 4673635bf09SNicolin Chen return -EINVAL; 4683635bf09SNicolin Chen } 4693635bf09SNicolin Chen 4703635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_samplebits || 4713635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits; 4722e5894d7SBenoit Cousson 4730b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4740b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_samplebits; 4752e5894d7SBenoit Cousson 4763635bf09SNicolin Chen if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { 4773635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 4783635bf09SNicolin Chen cpu_dai->sample_bits, sample_bits); 4793635bf09SNicolin Chen return -EINVAL; 4803635bf09SNicolin Chen } 481ddee627cSLiam Girdwood 482ddee627cSLiam Girdwood return 0; 483ddee627cSLiam Girdwood } 484ddee627cSLiam Girdwood 48562e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 48662e5f676SLars-Peter Clausen { 48762e5f676SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = substream->private_data; 48862e5f676SLars-Peter Clausen struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; 48962e5f676SLars-Peter Clausen struct snd_soc_dai_link *link = rtd->dai_link; 4900b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 4912e5894d7SBenoit Cousson unsigned int symmetry, i; 49262e5f676SLars-Peter Clausen 4932e5894d7SBenoit Cousson symmetry = cpu_driver->symmetric_rates || link->symmetric_rates || 4942e5894d7SBenoit Cousson cpu_driver->symmetric_channels || link->symmetric_channels || 4952e5894d7SBenoit Cousson cpu_driver->symmetric_samplebits || link->symmetric_samplebits; 4962e5894d7SBenoit Cousson 4970b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4982e5894d7SBenoit Cousson symmetry = symmetry || 4990b7990e3SKuninori Morimoto codec_dai->driver->symmetric_rates || 5000b7990e3SKuninori Morimoto codec_dai->driver->symmetric_channels || 5010b7990e3SKuninori Morimoto codec_dai->driver->symmetric_samplebits; 5022e5894d7SBenoit Cousson 5032e5894d7SBenoit Cousson return symmetry; 50462e5f676SLars-Peter Clausen } 50562e5f676SLars-Peter Clausen 5062e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 50758ba9b25SMark Brown { 5082e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 509c6068d3aSTakashi Iwai int ret; 51058ba9b25SMark Brown 51158ba9b25SMark Brown if (!bits) 51258ba9b25SMark Brown return; 51358ba9b25SMark Brown 5140e2a3751SLars-Peter Clausen ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 51558ba9b25SMark Brown if (ret != 0) 5160e2a3751SLars-Peter Clausen dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 5170e2a3751SLars-Peter Clausen bits, ret); 51858ba9b25SMark Brown } 51958ba9b25SMark Brown 520c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 521bd477c31SLars-Peter Clausen { 522c8dd1fecSBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 523c8dd1fecSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 5242e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 52557be9206SKuninori Morimoto struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; 52657be9206SKuninori Morimoto int stream = substream->stream; 5272e5894d7SBenoit Cousson int i; 528c8dd1fecSBenoit Cousson unsigned int bits = 0, cpu_bits; 529c8dd1fecSBenoit Cousson 5300b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 53157be9206SKuninori Morimoto pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); 53257be9206SKuninori Morimoto 53357be9206SKuninori Morimoto if (pcm_codec->sig_bits == 0) { 5342e5894d7SBenoit Cousson bits = 0; 5352e5894d7SBenoit Cousson break; 5362e5894d7SBenoit Cousson } 53757be9206SKuninori Morimoto bits = max(pcm_codec->sig_bits, bits); 5382e5894d7SBenoit Cousson } 53957be9206SKuninori Morimoto 54057be9206SKuninori Morimoto pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 54157be9206SKuninori Morimoto cpu_bits = pcm_cpu->sig_bits; 542c8dd1fecSBenoit Cousson 5432e5894d7SBenoit Cousson soc_pcm_set_msb(substream, bits); 5442e5894d7SBenoit Cousson soc_pcm_set_msb(substream, cpu_bits); 545c8dd1fecSBenoit Cousson } 546c8dd1fecSBenoit Cousson 5472e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 548bd477c31SLars-Peter Clausen { 5492e5894d7SBenoit Cousson struct snd_pcm_runtime *runtime = substream->runtime; 55078e45c99SLars-Peter Clausen struct snd_pcm_hardware *hw = &runtime->hw; 5512e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 5520b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 5532e5894d7SBenoit Cousson struct snd_soc_pcm_stream *codec_stream; 5542e5894d7SBenoit Cousson struct snd_soc_pcm_stream *cpu_stream; 5552e5894d7SBenoit Cousson unsigned int chan_min = 0, chan_max = UINT_MAX; 5562e5894d7SBenoit Cousson unsigned int rate_min = 0, rate_max = UINT_MAX; 5572e5894d7SBenoit Cousson unsigned int rates = UINT_MAX; 5582e5894d7SBenoit Cousson u64 formats = ULLONG_MAX; 559acf253c1SKuninori Morimoto int stream = substream->stream; 5602e5894d7SBenoit Cousson int i; 56178e45c99SLars-Peter Clausen 562acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(rtd->cpu_dai, stream); 56378e45c99SLars-Peter Clausen 5642e5894d7SBenoit Cousson /* first calculate min/max only for CODECs in the DAI link */ 5650b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 566cde79035SRicard Wanderlof 567cde79035SRicard Wanderlof /* 568cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type. 569cde79035SRicard Wanderlof * Otherwise, since the rate, channel, and format values will 570cde79035SRicard Wanderlof * zero in that case, we would have no usable settings left, 571cde79035SRicard Wanderlof * causing the resulting setup to fail. 572cde79035SRicard Wanderlof * At least one CODEC should match, otherwise we should have 573cde79035SRicard Wanderlof * bailed out on a higher level, since there would be no 574cde79035SRicard Wanderlof * CODEC to support the transfer direction in that case. 575cde79035SRicard Wanderlof */ 5760b7990e3SKuninori Morimoto if (!snd_soc_dai_stream_valid(codec_dai, 577cde79035SRicard Wanderlof substream->stream)) 578cde79035SRicard Wanderlof continue; 579cde79035SRicard Wanderlof 580acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); 581acf253c1SKuninori Morimoto 5822e5894d7SBenoit Cousson chan_min = max(chan_min, codec_stream->channels_min); 5832e5894d7SBenoit Cousson chan_max = min(chan_max, codec_stream->channels_max); 5842e5894d7SBenoit Cousson rate_min = max(rate_min, codec_stream->rate_min); 5852e5894d7SBenoit Cousson rate_max = min_not_zero(rate_max, codec_stream->rate_max); 5862e5894d7SBenoit Cousson formats &= codec_stream->formats; 5872e5894d7SBenoit Cousson rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); 5882e5894d7SBenoit Cousson } 5892e5894d7SBenoit Cousson 5902e5894d7SBenoit Cousson /* 5912e5894d7SBenoit Cousson * chan min/max cannot be enforced if there are multiple CODEC DAIs 5922e5894d7SBenoit Cousson * connected to a single CPU DAI, use CPU DAI's directly and let 5932e5894d7SBenoit Cousson * channel allocation be fixed up later 5942e5894d7SBenoit Cousson */ 5952e5894d7SBenoit Cousson if (rtd->num_codecs > 1) { 5962e5894d7SBenoit Cousson chan_min = cpu_stream->channels_min; 5972e5894d7SBenoit Cousson chan_max = cpu_stream->channels_max; 5982e5894d7SBenoit Cousson } 5992e5894d7SBenoit Cousson 6002e5894d7SBenoit Cousson hw->channels_min = max(chan_min, cpu_stream->channels_min); 6012e5894d7SBenoit Cousson hw->channels_max = min(chan_max, cpu_stream->channels_max); 6022e5894d7SBenoit Cousson if (hw->formats) 6032e5894d7SBenoit Cousson hw->formats &= formats & cpu_stream->formats; 6042e5894d7SBenoit Cousson else 6052e5894d7SBenoit Cousson hw->formats = formats & cpu_stream->formats; 6062e5894d7SBenoit Cousson hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates); 60778e45c99SLars-Peter Clausen 60878e45c99SLars-Peter Clausen snd_pcm_limit_hw_rates(runtime); 60978e45c99SLars-Peter Clausen 61078e45c99SLars-Peter Clausen hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); 6112e5894d7SBenoit Cousson hw->rate_min = max(hw->rate_min, rate_min); 61278e45c99SLars-Peter Clausen hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); 6132e5894d7SBenoit Cousson hw->rate_max = min_not_zero(hw->rate_max, rate_max); 614bd477c31SLars-Peter Clausen } 615bd477c31SLars-Peter Clausen 616dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream) 617e7ecfdb7SKuninori Morimoto { 618e7ecfdb7SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 619e7ecfdb7SKuninori Morimoto struct snd_soc_component *component; 620613fb500SKuninori Morimoto int i, ret = 0; 621e7ecfdb7SKuninori Morimoto 622613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 6234a81e8f3SKuninori Morimoto ret = snd_soc_component_module_get_when_open(component); 6244a81e8f3SKuninori Morimoto if (ret < 0) { 625e7ecfdb7SKuninori Morimoto dev_err(component->dev, 626e7ecfdb7SKuninori Morimoto "ASoC: can't get module %s\n", 627e7ecfdb7SKuninori Morimoto component->name); 6284a81e8f3SKuninori Morimoto return ret; 629e7ecfdb7SKuninori Morimoto } 630e7ecfdb7SKuninori Morimoto 631ae2f4849SKuninori Morimoto ret = snd_soc_component_open(component, substream); 632e7ecfdb7SKuninori Morimoto if (ret < 0) { 633e7ecfdb7SKuninori Morimoto dev_err(component->dev, 634e7ecfdb7SKuninori Morimoto "ASoC: can't open component %s: %d\n", 635e7ecfdb7SKuninori Morimoto component->name, ret); 636e7ecfdb7SKuninori Morimoto return ret; 637e7ecfdb7SKuninori Morimoto } 638e7ecfdb7SKuninori Morimoto } 639dd03907bSKuninori Morimoto 640e7ecfdb7SKuninori Morimoto return 0; 641e7ecfdb7SKuninori Morimoto } 642e7ecfdb7SKuninori Morimoto 643dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream) 644244e2936SCharles Keepax { 645244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 646244e2936SCharles Keepax struct snd_soc_component *component; 647e82ebffcSKuninori Morimoto int i, r, ret = 0; 648244e2936SCharles Keepax 649613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 650e82ebffcSKuninori Morimoto r = snd_soc_component_close(component, substream); 651e82ebffcSKuninori Morimoto if (r < 0) 652e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 653e82ebffcSKuninori Morimoto 6544a81e8f3SKuninori Morimoto snd_soc_component_module_put_when_close(component); 655244e2936SCharles Keepax } 656244e2936SCharles Keepax 6573672beb8SKuninori Morimoto return ret; 658244e2936SCharles Keepax } 659244e2936SCharles Keepax 66058ba9b25SMark Brown /* 66162c86d1dSKuninori Morimoto * Called by ALSA when a PCM substream is closed. Private data can be 66262c86d1dSKuninori Morimoto * freed here. The cpu DAI, codec DAI, machine and components are also 66362c86d1dSKuninori Morimoto * shutdown. 66462c86d1dSKuninori Morimoto */ 66562c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream) 66662c86d1dSKuninori Morimoto { 66762c86d1dSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 66862c86d1dSKuninori Morimoto struct snd_soc_component *component; 66962c86d1dSKuninori Morimoto struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 67062c86d1dSKuninori Morimoto struct snd_soc_dai *codec_dai; 67162c86d1dSKuninori Morimoto int i; 67262c86d1dSKuninori Morimoto 67362c86d1dSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 67462c86d1dSKuninori Morimoto 67562c86d1dSKuninori Morimoto snd_soc_runtime_deactivate(rtd, substream->stream); 67662c86d1dSKuninori Morimoto 67762c86d1dSKuninori Morimoto snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream); 67862c86d1dSKuninori Morimoto 67962c86d1dSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 68062c86d1dSKuninori Morimoto 68162c86d1dSKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 68262c86d1dSKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 68362c86d1dSKuninori Morimoto 68462c86d1dSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 68562c86d1dSKuninori Morimoto 68662c86d1dSKuninori Morimoto soc_pcm_components_close(substream); 68762c86d1dSKuninori Morimoto 68862c86d1dSKuninori Morimoto snd_soc_dapm_stream_stop(rtd, substream->stream); 68962c86d1dSKuninori Morimoto 69062c86d1dSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 69162c86d1dSKuninori Morimoto 69262c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 69362c86d1dSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 69462c86d1dSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 69562c86d1dSKuninori Morimoto } 69662c86d1dSKuninori Morimoto 69762c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) 69862c86d1dSKuninori Morimoto if (!component->active) 69962c86d1dSKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 70062c86d1dSKuninori Morimoto 70162c86d1dSKuninori Morimoto return 0; 70262c86d1dSKuninori Morimoto } 70362c86d1dSKuninori Morimoto 70462c86d1dSKuninori Morimoto /* 705ddee627cSLiam Girdwood * Called by ALSA when a PCM substream is opened, the runtime->hw record is 706ddee627cSLiam Girdwood * then initialized and any private data can be allocated. This also calls 707ef050becSCharles Keepax * startup for the cpu DAI, component, machine and codec DAI. 708ddee627cSLiam Girdwood */ 709ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream) 710ddee627cSLiam Girdwood { 711ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 712ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 71390be711eSKuninori Morimoto struct snd_soc_component *component; 714ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 7152e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 7162e5894d7SBenoit Cousson const char *codec_dai_name = "multicodec"; 717244e2936SCharles Keepax int i, ret = 0; 718ddee627cSLiam Girdwood 71976c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 72076c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 72190be711eSKuninori Morimoto 722613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 72390be711eSKuninori Morimoto pm_runtime_get_sync(component->dev); 724d6652ef8SMark Brown 72572b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 726ddee627cSLiam Girdwood 7275d9fa03eSKuninori Morimoto ret = soc_pcm_components_open(substream); 7285d9fa03eSKuninori Morimoto if (ret < 0) 7295d9fa03eSKuninori Morimoto goto component_err; 7305d9fa03eSKuninori Morimoto 7315d9fa03eSKuninori Morimoto ret = soc_rtd_startup(rtd, substream); 7325d9fa03eSKuninori Morimoto if (ret < 0) { 7335d9fa03eSKuninori Morimoto pr_err("ASoC: %s startup failed: %d\n", 7345d9fa03eSKuninori Morimoto rtd->dai_link->name, ret); 7355d9fa03eSKuninori Morimoto goto component_err; 7365d9fa03eSKuninori Morimoto } 7375d9fa03eSKuninori Morimoto 738ddee627cSLiam Girdwood /* startup the audio subsystem */ 7395a52a045SKuninori Morimoto ret = snd_soc_dai_startup(cpu_dai, substream); 740ddee627cSLiam Girdwood if (ret < 0) { 7415a52a045SKuninori Morimoto dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n", 7425a52a045SKuninori Morimoto cpu_dai->name, ret); 7435d9fa03eSKuninori Morimoto goto cpu_dai_err; 744ddee627cSLiam Girdwood } 745ddee627cSLiam Girdwood 7460b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 7475a52a045SKuninori Morimoto ret = snd_soc_dai_startup(codec_dai, substream); 748ddee627cSLiam Girdwood if (ret < 0) { 7492e5894d7SBenoit Cousson dev_err(codec_dai->dev, 7502e5894d7SBenoit Cousson "ASoC: can't open codec %s: %d\n", 7512e5894d7SBenoit Cousson codec_dai->name, ret); 7525d9fa03eSKuninori Morimoto goto config_err; 753ddee627cSLiam Girdwood } 754ddee627cSLiam Girdwood 7552e5894d7SBenoit Cousson if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 7562e5894d7SBenoit Cousson codec_dai->tx_mask = 0; 7572e5894d7SBenoit Cousson else 7582e5894d7SBenoit Cousson codec_dai->rx_mask = 0; 7592e5894d7SBenoit Cousson } 7602e5894d7SBenoit Cousson 76101d7584cSLiam Girdwood /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 76201d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 76301d7584cSLiam Girdwood goto dynamic; 76401d7584cSLiam Girdwood 765ddee627cSLiam Girdwood /* Check that the codec and cpu DAIs are compatible */ 7662e5894d7SBenoit Cousson soc_pcm_init_runtime_hw(substream); 7672e5894d7SBenoit Cousson 7682e5894d7SBenoit Cousson if (rtd->num_codecs == 1) 7692e5894d7SBenoit Cousson codec_dai_name = rtd->codec_dai->name; 770ddee627cSLiam Girdwood 77162e5f676SLars-Peter Clausen if (soc_pcm_has_symmetry(substream)) 77262e5f676SLars-Peter Clausen runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 77362e5f676SLars-Peter Clausen 774ddee627cSLiam Girdwood ret = -EINVAL; 775ddee627cSLiam Girdwood if (!runtime->hw.rates) { 776103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 7772e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 778ddee627cSLiam Girdwood goto config_err; 779ddee627cSLiam Girdwood } 780ddee627cSLiam Girdwood if (!runtime->hw.formats) { 781103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 7822e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 783ddee627cSLiam Girdwood goto config_err; 784ddee627cSLiam Girdwood } 785ddee627cSLiam Girdwood if (!runtime->hw.channels_min || !runtime->hw.channels_max || 786ddee627cSLiam Girdwood runtime->hw.channels_min > runtime->hw.channels_max) { 787103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 7882e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 789ddee627cSLiam Girdwood goto config_err; 790ddee627cSLiam Girdwood } 791ddee627cSLiam Girdwood 792c8dd1fecSBenoit Cousson soc_pcm_apply_msb(substream); 79358ba9b25SMark Brown 794ddee627cSLiam Girdwood /* Symmetry only applies if we've already got an active stream. */ 79517841020SDong Aisheng if (cpu_dai->active) { 79617841020SDong Aisheng ret = soc_pcm_apply_symmetry(substream, cpu_dai); 79717841020SDong Aisheng if (ret != 0) 79817841020SDong Aisheng goto config_err; 79917841020SDong Aisheng } 80017841020SDong Aisheng 8010b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 8020b7990e3SKuninori Morimoto if (codec_dai->active) { 8030b7990e3SKuninori Morimoto ret = soc_pcm_apply_symmetry(substream, codec_dai); 804ddee627cSLiam Girdwood if (ret != 0) 805ddee627cSLiam Girdwood goto config_err; 806ddee627cSLiam Girdwood } 8072e5894d7SBenoit Cousson } 808ddee627cSLiam Girdwood 809103d84a3SLiam Girdwood pr_debug("ASoC: %s <-> %s info:\n", 8102e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 811103d84a3SLiam Girdwood pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 812103d84a3SLiam Girdwood pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 813ddee627cSLiam Girdwood runtime->hw.channels_max); 814103d84a3SLiam Girdwood pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 815ddee627cSLiam Girdwood runtime->hw.rate_max); 816ddee627cSLiam Girdwood 81701d7584cSLiam Girdwood dynamic: 81824894b76SLars-Peter Clausen 81924894b76SLars-Peter Clausen snd_soc_runtime_activate(rtd, substream->stream); 82024894b76SLars-Peter Clausen 82172b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 822ddee627cSLiam Girdwood return 0; 823ddee627cSLiam Girdwood 824ddee627cSLiam Girdwood config_err: 825b56be800SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 826330fcb51SKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 8275d9fa03eSKuninori Morimoto cpu_dai_err: 8285d9fa03eSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 8292e5894d7SBenoit Cousson 8305d9fa03eSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 831b8135864SKuninori Morimoto component_err: 832dd03907bSKuninori Morimoto soc_pcm_components_close(substream); 833e7ecfdb7SKuninori Morimoto 83472b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 835d6652ef8SMark Brown 836613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 83790be711eSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 83890be711eSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 8393f809783SSanyog Kale } 8403f809783SSanyog Kale 84176c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 84276c39e86SKuninori Morimoto if (!component->active) 84376c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 844d6652ef8SMark Brown 845ddee627cSLiam Girdwood return ret; 846ddee627cSLiam Girdwood } 847ddee627cSLiam Girdwood 8484bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 849a342031cSJerome Brunet { 850a342031cSJerome Brunet /* 851a342031cSJerome Brunet * Currently nothing to do for c2c links 852a342031cSJerome Brunet * Since c2c links are internal nodes in the DAPM graph and 853a342031cSJerome Brunet * don't interface with the outside world or application layer 854a342031cSJerome Brunet * we don't have to do any special handling on close. 855a342031cSJerome Brunet */ 856a342031cSJerome Brunet } 857a342031cSJerome Brunet 858ddee627cSLiam Girdwood /* 859ddee627cSLiam Girdwood * Called by ALSA when the PCM substream is prepared, can set format, sample 860ddee627cSLiam Girdwood * rate, etc. This function is non atomic and can be called multiple times, 861ddee627cSLiam Girdwood * it can refer to the runtime info. 862ddee627cSLiam Girdwood */ 863ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream) 864ddee627cSLiam Girdwood { 865ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 866b8135864SKuninori Morimoto struct snd_soc_component *component; 867ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 8682e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 8692e5894d7SBenoit Cousson int i, ret = 0; 870ddee627cSLiam Girdwood 87172b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 872ddee627cSLiam Girdwood 87344c1a75bSKuninori Morimoto ret = soc_rtd_prepare(rtd, substream); 874ddee627cSLiam Girdwood if (ret < 0) { 87544c1a75bSKuninori Morimoto dev_err(rtd->card->dev, 87644c1a75bSKuninori Morimoto "ASoC: machine prepare error: %d\n", ret); 877ddee627cSLiam Girdwood goto out; 878ddee627cSLiam Girdwood } 879ddee627cSLiam Girdwood 880613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 8816d537233SKuninori Morimoto ret = snd_soc_component_prepare(component, substream); 882b8135864SKuninori Morimoto if (ret < 0) { 883b8135864SKuninori Morimoto dev_err(component->dev, 884b8135864SKuninori Morimoto "ASoC: platform prepare error: %d\n", ret); 885b8135864SKuninori Morimoto goto out; 886b8135864SKuninori Morimoto } 887b8135864SKuninori Morimoto } 888b8135864SKuninori Morimoto 8890b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 8904beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(codec_dai, substream); 891ddee627cSLiam Girdwood if (ret < 0) { 8922e5894d7SBenoit Cousson dev_err(codec_dai->dev, 89390cc7f1cSJarkko Nikula "ASoC: codec DAI prepare error: %d\n", 89490cc7f1cSJarkko Nikula ret); 895ddee627cSLiam Girdwood goto out; 896ddee627cSLiam Girdwood } 897ddee627cSLiam Girdwood } 898ddee627cSLiam Girdwood 8994beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(cpu_dai, substream); 900ddee627cSLiam Girdwood if (ret < 0) { 90190cc7f1cSJarkko Nikula dev_err(cpu_dai->dev, 90290cc7f1cSJarkko Nikula "ASoC: cpu DAI prepare error: %d\n", ret); 903ddee627cSLiam Girdwood goto out; 904ddee627cSLiam Girdwood } 905ddee627cSLiam Girdwood 906ddee627cSLiam Girdwood /* cancel any delayed stream shutdown that is pending */ 907ddee627cSLiam Girdwood if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 9089bffb1fbSMisael Lopez Cruz rtd->pop_wait) { 9099bffb1fbSMisael Lopez Cruz rtd->pop_wait = 0; 910ddee627cSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 911ddee627cSLiam Girdwood } 912ddee627cSLiam Girdwood 913d9b0951bSLiam Girdwood snd_soc_dapm_stream_event(rtd, substream->stream, 914ddee627cSLiam Girdwood SND_SOC_DAPM_STREAM_START); 915ddee627cSLiam Girdwood 9160b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 9170b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 0, 9182e5894d7SBenoit Cousson substream->stream); 919ae11601bSRamesh Babu snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); 920ddee627cSLiam Girdwood 921ddee627cSLiam Girdwood out: 92272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 923ddee627cSLiam Girdwood return ret; 924ddee627cSLiam Girdwood } 925ddee627cSLiam Girdwood 9262e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 9272e5894d7SBenoit Cousson unsigned int mask) 9282e5894d7SBenoit Cousson { 9292e5894d7SBenoit Cousson struct snd_interval *interval; 9302e5894d7SBenoit Cousson int channels = hweight_long(mask); 9312e5894d7SBenoit Cousson 9322e5894d7SBenoit Cousson interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 9332e5894d7SBenoit Cousson interval->min = channels; 9342e5894d7SBenoit Cousson interval->max = channels; 9352e5894d7SBenoit Cousson } 9362e5894d7SBenoit Cousson 937244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, 938244e2936SCharles Keepax struct snd_soc_component *last) 939244e2936SCharles Keepax { 940244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 941244e2936SCharles Keepax struct snd_soc_component *component; 942e82ebffcSKuninori Morimoto int i, r, ret = 0; 943244e2936SCharles Keepax 944613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 945244e2936SCharles Keepax if (component == last) 946244e2936SCharles Keepax break; 947244e2936SCharles Keepax 948e82ebffcSKuninori Morimoto r = snd_soc_component_hw_free(component, substream); 949e82ebffcSKuninori Morimoto if (r < 0) 950e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 951244e2936SCharles Keepax } 952244e2936SCharles Keepax 953eae7136aSKuninori Morimoto return ret; 954244e2936SCharles Keepax } 955244e2936SCharles Keepax 956ddee627cSLiam Girdwood /* 957ddee627cSLiam Girdwood * Called by ALSA when the hardware params are set by application. This 958ddee627cSLiam Girdwood * function can also be called multiple times and can allocate buffers 959ddee627cSLiam Girdwood * (using snd_pcm_lib_* ). It's non-atomic. 960ddee627cSLiam Girdwood */ 961ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 962ddee627cSLiam Girdwood struct snd_pcm_hw_params *params) 963ddee627cSLiam Girdwood { 964ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 965b8135864SKuninori Morimoto struct snd_soc_component *component; 966ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 9670b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 968244e2936SCharles Keepax int i, ret = 0; 969ddee627cSLiam Girdwood 97072b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 9715cca5951SShengjiu Wang 9725cca5951SShengjiu Wang ret = soc_pcm_params_symmetry(substream, params); 9735cca5951SShengjiu Wang if (ret) 9745cca5951SShengjiu Wang goto out; 9755cca5951SShengjiu Wang 976de9ad990SKuninori Morimoto ret = soc_rtd_hw_params(rtd, substream, params); 977ddee627cSLiam Girdwood if (ret < 0) { 978de9ad990SKuninori Morimoto dev_err(rtd->card->dev, 979de9ad990SKuninori Morimoto "ASoC: machine hw_params failed: %d\n", ret); 980ddee627cSLiam Girdwood goto out; 981ddee627cSLiam Girdwood } 982ddee627cSLiam Girdwood 9830b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 9842e5894d7SBenoit Cousson struct snd_pcm_hw_params codec_params; 9852e5894d7SBenoit Cousson 986cde79035SRicard Wanderlof /* 987cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type, 988cde79035SRicard Wanderlof * the idea being that if a CODEC is not used for the currently 989cde79035SRicard Wanderlof * set up transfer direction, it should not need to be 990cde79035SRicard Wanderlof * configured, especially since the configuration used might 991cde79035SRicard Wanderlof * not even be supported by that CODEC. There may be cases 992cde79035SRicard Wanderlof * however where a CODEC needs to be set up although it is 993cde79035SRicard Wanderlof * actually not being used for the transfer, e.g. if a 994cde79035SRicard Wanderlof * capture-only CODEC is acting as an LRCLK and/or BCLK master 995cde79035SRicard Wanderlof * for the DAI link including a playback-only CODEC. 996cde79035SRicard Wanderlof * If this becomes necessary, we will have to augment the 997cde79035SRicard Wanderlof * machine driver setup with information on how to act, so 998cde79035SRicard Wanderlof * we can do the right thing here. 999cde79035SRicard Wanderlof */ 1000cde79035SRicard Wanderlof if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1001cde79035SRicard Wanderlof continue; 1002cde79035SRicard Wanderlof 10032e5894d7SBenoit Cousson /* copy params for each codec */ 10042e5894d7SBenoit Cousson codec_params = *params; 10052e5894d7SBenoit Cousson 10062e5894d7SBenoit Cousson /* fixup params based on TDM slot masks */ 1007570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1008570f18b6SRander Wang codec_dai->tx_mask) 10092e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 10102e5894d7SBenoit Cousson codec_dai->tx_mask); 1011570f18b6SRander Wang 1012570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 1013570f18b6SRander Wang codec_dai->rx_mask) 10142e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 10152e5894d7SBenoit Cousson codec_dai->rx_mask); 10162e5894d7SBenoit Cousson 1017aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(codec_dai, substream, 1018aa6166c2SKuninori Morimoto &codec_params); 101993e6958aSBenoit Cousson if(ret < 0) 1020ddee627cSLiam Girdwood goto codec_err; 1021ddee627cSLiam Girdwood 10222e5894d7SBenoit Cousson codec_dai->rate = params_rate(&codec_params); 10232e5894d7SBenoit Cousson codec_dai->channels = params_channels(&codec_params); 10242e5894d7SBenoit Cousson codec_dai->sample_bits = snd_pcm_format_physical_width( 10252e5894d7SBenoit Cousson params_format(&codec_params)); 1026078a85f2SCharles Keepax 1027078a85f2SCharles Keepax snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); 1028ddee627cSLiam Girdwood } 1029ddee627cSLiam Girdwood 1030aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(cpu_dai, substream, params); 103193e6958aSBenoit Cousson if (ret < 0) 1032ddee627cSLiam Girdwood goto interface_err; 1033ddee627cSLiam Girdwood 1034ca58221dSKuninori Morimoto /* store the parameters for each DAIs */ 1035ca58221dSKuninori Morimoto cpu_dai->rate = params_rate(params); 1036ca58221dSKuninori Morimoto cpu_dai->channels = params_channels(params); 1037ca58221dSKuninori Morimoto cpu_dai->sample_bits = 1038ca58221dSKuninori Morimoto snd_pcm_format_physical_width(params_format(params)); 1039ca58221dSKuninori Morimoto 1040ca58221dSKuninori Morimoto snd_soc_dapm_update_dai(substream, params, cpu_dai); 1041ca58221dSKuninori Morimoto 1042613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1043245c539aSKuninori Morimoto ret = snd_soc_component_hw_params(component, substream, params); 1044244e2936SCharles Keepax if (ret < 0) { 1045b8135864SKuninori Morimoto dev_err(component->dev, 1046b8135864SKuninori Morimoto "ASoC: %s hw params failed: %d\n", 1047244e2936SCharles Keepax component->name, ret); 1048b8135864SKuninori Morimoto goto component_err; 1049244e2936SCharles Keepax } 1050244e2936SCharles Keepax } 1051244e2936SCharles Keepax component = NULL; 1052b8135864SKuninori Morimoto 1053ddee627cSLiam Girdwood out: 105472b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1055ddee627cSLiam Girdwood return ret; 1056ddee627cSLiam Girdwood 1057b8135864SKuninori Morimoto component_err: 1058244e2936SCharles Keepax soc_pcm_components_hw_free(substream, component); 1059b8135864SKuninori Morimoto 1060846faaedSKuninori Morimoto snd_soc_dai_hw_free(cpu_dai, substream); 10612371abdcSKuninori Morimoto cpu_dai->rate = 0; 1062ddee627cSLiam Girdwood 1063ddee627cSLiam Girdwood interface_err: 10642e5894d7SBenoit Cousson i = rtd->num_codecs; 1065ddee627cSLiam Girdwood 1066ddee627cSLiam Girdwood codec_err: 10676d11b128SKuninori Morimoto for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) { 1068f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1069f47b9ad9SJerome Brunet continue; 1070f47b9ad9SJerome Brunet 1071846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 10722e5894d7SBenoit Cousson codec_dai->rate = 0; 10732e5894d7SBenoit Cousson } 10742e5894d7SBenoit Cousson 107549f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1076ddee627cSLiam Girdwood 107772b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1078ddee627cSLiam Girdwood return ret; 1079ddee627cSLiam Girdwood } 1080ddee627cSLiam Girdwood 1081ddee627cSLiam Girdwood /* 1082ddee627cSLiam Girdwood * Frees resources allocated by hw_params, can be called multiple times 1083ddee627cSLiam Girdwood */ 1084ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1085ddee627cSLiam Girdwood { 1086ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1087ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 10882e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 10897f62b6eeSNicolin Chen bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 10902e5894d7SBenoit Cousson int i; 1091ddee627cSLiam Girdwood 109272b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 1093ddee627cSLiam Girdwood 1094d3383420SNicolin Chen /* clear the corresponding DAIs parameters when going to be inactive */ 1095d3383420SNicolin Chen if (cpu_dai->active == 1) { 1096d3383420SNicolin Chen cpu_dai->rate = 0; 1097d3383420SNicolin Chen cpu_dai->channels = 0; 1098d3383420SNicolin Chen cpu_dai->sample_bits = 0; 1099d3383420SNicolin Chen } 1100d3383420SNicolin Chen 11010b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 1102d3383420SNicolin Chen if (codec_dai->active == 1) { 1103d3383420SNicolin Chen codec_dai->rate = 0; 1104d3383420SNicolin Chen codec_dai->channels = 0; 1105d3383420SNicolin Chen codec_dai->sample_bits = 0; 1106d3383420SNicolin Chen } 11072e5894d7SBenoit Cousson } 1108d3383420SNicolin Chen 1109ddee627cSLiam Girdwood /* apply codec digital mute */ 11100b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 11110f6011fdSKuninori Morimoto int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]; 11120f6011fdSKuninori Morimoto int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE]; 11130f6011fdSKuninori Morimoto 11140f6011fdSKuninori Morimoto if ((playback && playback_active == 1) || 11150f6011fdSKuninori Morimoto (!playback && capture_active == 1)) 11160b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 1, 11172e5894d7SBenoit Cousson substream->stream); 11182e5894d7SBenoit Cousson } 1119ddee627cSLiam Girdwood 1120ddee627cSLiam Girdwood /* free any machine hw params */ 112149f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1122ddee627cSLiam Girdwood 1123b8135864SKuninori Morimoto /* free any component resources */ 1124244e2936SCharles Keepax soc_pcm_components_hw_free(substream, NULL); 1125b8135864SKuninori Morimoto 1126ddee627cSLiam Girdwood /* now free hw params for the DAIs */ 11270b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 1128f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1129f47b9ad9SJerome Brunet continue; 1130f47b9ad9SJerome Brunet 1131846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 11322e5894d7SBenoit Cousson } 1133ddee627cSLiam Girdwood 1134846faaedSKuninori Morimoto snd_soc_dai_hw_free(cpu_dai, substream); 1135ddee627cSLiam Girdwood 113672b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1137ddee627cSLiam Girdwood return 0; 1138ddee627cSLiam Girdwood } 1139ddee627cSLiam Girdwood 11404378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd) 1141ddee627cSLiam Girdwood { 1142ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1143b8135864SKuninori Morimoto struct snd_soc_component *component; 1144ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 11452e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 11462e5894d7SBenoit Cousson int i, ret; 1147ddee627cSLiam Girdwood 1148ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 1149ddee627cSLiam Girdwood if (ret < 0) 1150ddee627cSLiam Girdwood return ret; 1151ddee627cSLiam Girdwood 1152613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11535693d50cSKuninori Morimoto ret = snd_soc_component_trigger(component, substream, cmd); 1154b8135864SKuninori Morimoto if (ret < 0) 1155b8135864SKuninori Morimoto return ret; 1156b8135864SKuninori Morimoto } 1157b8135864SKuninori Morimoto 1158901e822bSDan Carpenter ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 1159ddee627cSLiam Girdwood if (ret < 0) 1160ddee627cSLiam Girdwood return ret; 11614792b0dbSJarkko Nikula 11624378f1fbSPeter Ujfalusi for_each_rtd_codec_dai(rtd, i, codec_dai) { 11634378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 11644378f1fbSPeter Ujfalusi if (ret < 0) 11654378f1fbSPeter Ujfalusi return ret; 11664378f1fbSPeter Ujfalusi } 11674378f1fbSPeter Ujfalusi 11684378f1fbSPeter Ujfalusi return 0; 11694378f1fbSPeter Ujfalusi } 11704378f1fbSPeter Ujfalusi 11714378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd) 11724378f1fbSPeter Ujfalusi { 11734378f1fbSPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 11744378f1fbSPeter Ujfalusi struct snd_soc_component *component; 11754378f1fbSPeter Ujfalusi struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 11764378f1fbSPeter Ujfalusi struct snd_soc_dai *codec_dai; 11774378f1fbSPeter Ujfalusi int i, ret; 11784378f1fbSPeter Ujfalusi 11794378f1fbSPeter Ujfalusi for_each_rtd_codec_dai(rtd, i, codec_dai) { 11804378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 11814378f1fbSPeter Ujfalusi if (ret < 0) 11824378f1fbSPeter Ujfalusi return ret; 11834378f1fbSPeter Ujfalusi } 11844378f1fbSPeter Ujfalusi 11854378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 11864378f1fbSPeter Ujfalusi if (ret < 0) 11874378f1fbSPeter Ujfalusi return ret; 11884378f1fbSPeter Ujfalusi 1189613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11904378f1fbSPeter Ujfalusi ret = snd_soc_component_trigger(component, substream, cmd); 11914378f1fbSPeter Ujfalusi if (ret < 0) 11924378f1fbSPeter Ujfalusi return ret; 11934378f1fbSPeter Ujfalusi } 11944378f1fbSPeter Ujfalusi 1195ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 11964792b0dbSJarkko Nikula if (ret < 0) 11974792b0dbSJarkko Nikula return ret; 11984792b0dbSJarkko Nikula 1199ddee627cSLiam Girdwood return 0; 1200ddee627cSLiam Girdwood } 1201ddee627cSLiam Girdwood 12024378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 12034378f1fbSPeter Ujfalusi { 12044378f1fbSPeter Ujfalusi int ret; 12054378f1fbSPeter Ujfalusi 12064378f1fbSPeter Ujfalusi switch (cmd) { 12074378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_START: 12084378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_RESUME: 12094378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 12104378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_start(substream, cmd); 12114378f1fbSPeter Ujfalusi break; 12124378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_STOP: 12134378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_SUSPEND: 12144378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 12154378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_stop(substream, cmd); 12164378f1fbSPeter Ujfalusi break; 12174378f1fbSPeter Ujfalusi default: 12184378f1fbSPeter Ujfalusi return -EINVAL; 12194378f1fbSPeter Ujfalusi } 12204378f1fbSPeter Ujfalusi 12214378f1fbSPeter Ujfalusi return ret; 12224378f1fbSPeter Ujfalusi } 12234378f1fbSPeter Ujfalusi 122445c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, 122545c0a188SMark Brown int cmd) 122607bf84aaSLiam Girdwood { 122707bf84aaSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 122807bf84aaSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 12292e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 12302e5894d7SBenoit Cousson int i, ret; 123107bf84aaSLiam Girdwood 12320b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 12335c0769afSKuninori Morimoto ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd); 123407bf84aaSLiam Girdwood if (ret < 0) 123507bf84aaSLiam Girdwood return ret; 123607bf84aaSLiam Girdwood } 123707bf84aaSLiam Girdwood 1238901e822bSDan Carpenter ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd); 123907bf84aaSLiam Girdwood if (ret < 0) 124007bf84aaSLiam Girdwood return ret; 12415c0769afSKuninori Morimoto 124207bf84aaSLiam Girdwood return 0; 124307bf84aaSLiam Girdwood } 1244ddee627cSLiam Girdwood /* 1245ddee627cSLiam Girdwood * soc level wrapper for pointer callback 1246ef050becSCharles Keepax * If cpu_dai, codec_dai, component driver has the delay callback, then 1247ddee627cSLiam Girdwood * the runtime->delay will be updated accordingly. 1248ddee627cSLiam Girdwood */ 1249ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1250ddee627cSLiam Girdwood { 1251ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1252ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 12532e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 1254ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 1255ddee627cSLiam Girdwood snd_pcm_uframes_t offset = 0; 1256ddee627cSLiam Girdwood snd_pcm_sframes_t delay = 0; 12572e5894d7SBenoit Cousson snd_pcm_sframes_t codec_delay = 0; 12582e5894d7SBenoit Cousson int i; 1259ddee627cSLiam Girdwood 12609fb4c2bfSAkshu Agrawal /* clearing the previous total delay */ 12619fb4c2bfSAkshu Agrawal runtime->delay = 0; 12629fb4c2bfSAkshu Agrawal 12630035e256SKuninori Morimoto offset = snd_soc_pcm_component_pointer(substream); 1264b8135864SKuninori Morimoto 12659fb4c2bfSAkshu Agrawal /* base delay if assigned in pointer callback */ 12669fb4c2bfSAkshu Agrawal delay = runtime->delay; 1267b8135864SKuninori Morimoto 12681dea80d4SKuninori Morimoto delay += snd_soc_dai_delay(cpu_dai, substream); 1269ddee627cSLiam Girdwood 12700b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 12712e5894d7SBenoit Cousson codec_delay = max(codec_delay, 12721dea80d4SKuninori Morimoto snd_soc_dai_delay(codec_dai, substream)); 12732e5894d7SBenoit Cousson } 12742e5894d7SBenoit Cousson delay += codec_delay; 1275ddee627cSLiam Girdwood 1276ddee627cSLiam Girdwood runtime->delay = delay; 1277ddee627cSLiam Girdwood 1278ddee627cSLiam Girdwood return offset; 1279ddee627cSLiam Girdwood } 1280ddee627cSLiam Girdwood 128101d7584cSLiam Girdwood /* connect a FE and BE */ 128201d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 128301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 128401d7584cSLiam Girdwood { 128501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1286a9764869SKaiChieh Chuang unsigned long flags; 128701d7584cSLiam Girdwood 128801d7584cSLiam Girdwood /* only add new dpcms */ 12898d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 129001d7584cSLiam Girdwood if (dpcm->be == be && dpcm->fe == fe) 129101d7584cSLiam Girdwood return 0; 129201d7584cSLiam Girdwood } 129301d7584cSLiam Girdwood 129401d7584cSLiam Girdwood dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 129501d7584cSLiam Girdwood if (!dpcm) 129601d7584cSLiam Girdwood return -ENOMEM; 129701d7584cSLiam Girdwood 129801d7584cSLiam Girdwood dpcm->be = be; 129901d7584cSLiam Girdwood dpcm->fe = fe; 130001d7584cSLiam Girdwood be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 130101d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1302a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 130301d7584cSLiam Girdwood list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 130401d7584cSLiam Girdwood list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1305a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 130601d7584cSLiam Girdwood 130701d7584cSLiam Girdwood dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 130801d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 130901d7584cSLiam Girdwood stream ? "<-" : "->", be->dai_link->name); 131001d7584cSLiam Girdwood 1311*154dae87SKuninori Morimoto dpcm_create_debugfs_state(dpcm, stream); 1312*154dae87SKuninori Morimoto 131301d7584cSLiam Girdwood return 1; 131401d7584cSLiam Girdwood } 131501d7584cSLiam Girdwood 131601d7584cSLiam Girdwood /* reparent a BE onto another FE */ 131701d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 131801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 131901d7584cSLiam Girdwood { 132001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 132101d7584cSLiam Girdwood struct snd_pcm_substream *fe_substream, *be_substream; 132201d7584cSLiam Girdwood 132301d7584cSLiam Girdwood /* reparent if BE is connected to other FEs */ 132401d7584cSLiam Girdwood if (!be->dpcm[stream].users) 132501d7584cSLiam Girdwood return; 132601d7584cSLiam Girdwood 132701d7584cSLiam Girdwood be_substream = snd_soc_dpcm_get_substream(be, stream); 132801d7584cSLiam Girdwood 1329d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 133001d7584cSLiam Girdwood if (dpcm->fe == fe) 133101d7584cSLiam Girdwood continue; 133201d7584cSLiam Girdwood 133301d7584cSLiam Girdwood dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 133401d7584cSLiam Girdwood stream ? "capture" : "playback", 133501d7584cSLiam Girdwood dpcm->fe->dai_link->name, 133601d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 133701d7584cSLiam Girdwood 133801d7584cSLiam Girdwood fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 133901d7584cSLiam Girdwood be_substream->runtime = fe_substream->runtime; 134001d7584cSLiam Girdwood break; 134101d7584cSLiam Girdwood } 134201d7584cSLiam Girdwood } 134301d7584cSLiam Girdwood 134401d7584cSLiam Girdwood /* disconnect a BE and FE */ 134523607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 134601d7584cSLiam Girdwood { 134701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm, *d; 1348a9764869SKaiChieh Chuang unsigned long flags; 134901d7584cSLiam Girdwood 13508d6258a4SKuninori Morimoto for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1351103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 135201d7584cSLiam Girdwood stream ? "capture" : "playback", 135301d7584cSLiam Girdwood dpcm->be->dai_link->name); 135401d7584cSLiam Girdwood 135501d7584cSLiam Girdwood if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 135601d7584cSLiam Girdwood continue; 135701d7584cSLiam Girdwood 135801d7584cSLiam Girdwood dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 135901d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 136001d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 136101d7584cSLiam Girdwood 136201d7584cSLiam Girdwood /* BEs still alive need new FE */ 136301d7584cSLiam Girdwood dpcm_be_reparent(fe, dpcm->be, stream); 136401d7584cSLiam Girdwood 1365*154dae87SKuninori Morimoto dpcm_remove_debugfs_state(dpcm); 1366*154dae87SKuninori Morimoto 1367a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 136801d7584cSLiam Girdwood list_del(&dpcm->list_be); 136901d7584cSLiam Girdwood list_del(&dpcm->list_fe); 1370a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 137101d7584cSLiam Girdwood kfree(dpcm); 137201d7584cSLiam Girdwood } 137301d7584cSLiam Girdwood } 137401d7584cSLiam Girdwood 137501d7584cSLiam Girdwood /* get BE for DAI widget and stream */ 137601d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 137701d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget, int stream) 137801d7584cSLiam Girdwood { 137901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 138093597faeSKuninori Morimoto struct snd_soc_dapm_widget *w; 13817afecb30SKuninori Morimoto struct snd_soc_dai *dai; 13821a497983SMengdong Lin int i; 138301d7584cSLiam Girdwood 13843c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 13853c146465SLiam Girdwood 1386bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, be) { 138701d7584cSLiam Girdwood 138835ea0655SLiam Girdwood if (!be->dai_link->no_pcm) 138935ea0655SLiam Girdwood continue; 139035ea0655SLiam Girdwood 13910c01f6caSKuninori Morimoto w = snd_soc_dai_get_widget(be->cpu_dai, stream); 139293597faeSKuninori Morimoto 13933c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: try BE : %s\n", 139493597faeSKuninori Morimoto w ? w->name : "(not set)"); 13953c146465SLiam Girdwood 139693597faeSKuninori Morimoto if (w == widget) 139701d7584cSLiam Girdwood return be; 13982e5894d7SBenoit Cousson 13997afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 14000c01f6caSKuninori Morimoto w = snd_soc_dai_get_widget(dai, stream); 140193597faeSKuninori Morimoto 140293597faeSKuninori Morimoto if (w == widget) 14032e5894d7SBenoit Cousson return be; 14042e5894d7SBenoit Cousson } 140501d7584cSLiam Girdwood } 140601d7584cSLiam Girdwood 14079d6ee365SJerome Brunet /* Widget provided is not a BE */ 140801d7584cSLiam Girdwood return NULL; 140901d7584cSLiam Girdwood } 141001d7584cSLiam Girdwood 141101d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list, 141201d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget) 141301d7584cSLiam Girdwood { 141409e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *w; 141501d7584cSLiam Girdwood int i; 141601d7584cSLiam Girdwood 141709e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, w) 141809e88f8aSKuninori Morimoto if (widget == w) 141901d7584cSLiam Girdwood return 1; 142001d7584cSLiam Girdwood 142101d7584cSLiam Girdwood return 0; 142201d7584cSLiam Girdwood } 142301d7584cSLiam Girdwood 14245fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, 14255fdd022cSPiotr Stankiewicz enum snd_soc_dapm_direction dir) 14265fdd022cSPiotr Stankiewicz { 14275fdd022cSPiotr Stankiewicz struct snd_soc_card *card = widget->dapm->card; 14285fdd022cSPiotr Stankiewicz struct snd_soc_pcm_runtime *rtd; 1429c2cd8216SKuninori Morimoto int stream; 14305fdd022cSPiotr Stankiewicz 1431c2cd8216SKuninori Morimoto /* adjust dir to stream */ 1432c2cd8216SKuninori Morimoto if (dir == SND_SOC_DAPM_DIR_OUT) 1433c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK; 1434c2cd8216SKuninori Morimoto else 1435c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE; 1436c2cd8216SKuninori Morimoto 1437027a4838SKuninori Morimoto rtd = dpcm_get_be(card, widget, stream); 1438027a4838SKuninori Morimoto if (rtd) 14395fdd022cSPiotr Stankiewicz return true; 14405fdd022cSPiotr Stankiewicz 14415fdd022cSPiotr Stankiewicz return false; 14425fdd022cSPiotr Stankiewicz } 14435fdd022cSPiotr Stankiewicz 144423607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 14451ce43acfSLars-Peter Clausen int stream, struct snd_soc_dapm_widget_list **list) 144601d7584cSLiam Girdwood { 144701d7584cSLiam Girdwood struct snd_soc_dai *cpu_dai = fe->cpu_dai; 144801d7584cSLiam Girdwood int paths; 144901d7584cSLiam Girdwood 145001d7584cSLiam Girdwood /* get number of valid DAI paths and their widgets */ 14516742064aSPiotr Stankiewicz paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 14525fdd022cSPiotr Stankiewicz dpcm_end_walk_at_be); 145301d7584cSLiam Girdwood 1454103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 145501d7584cSLiam Girdwood stream ? "capture" : "playback"); 145601d7584cSLiam Girdwood 145701d7584cSLiam Girdwood return paths; 145801d7584cSLiam Girdwood } 145901d7584cSLiam Girdwood 146052645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list) 146152645e33SKuninori Morimoto { 146252645e33SKuninori Morimoto snd_soc_dapm_dai_free_widgets(list); 146352645e33SKuninori Morimoto } 146452645e33SKuninori Morimoto 146501d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 146601d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 146701d7584cSLiam Girdwood { 146801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 146901d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 147001d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget; 14717afecb30SKuninori Morimoto struct snd_soc_dai *dai; 147201d7584cSLiam Girdwood int prune = 0; 1473bed646dcSKuninori Morimoto int do_prune; 147401d7584cSLiam Girdwood 147501d7584cSLiam Girdwood /* Destroy any old FE <--> BE connections */ 14768d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 14772e5894d7SBenoit Cousson unsigned int i; 147801d7584cSLiam Girdwood 147901d7584cSLiam Girdwood /* is there a valid CPU DAI widget for this BE */ 14800c01f6caSKuninori Morimoto widget = snd_soc_dai_get_widget(dpcm->be->cpu_dai, stream); 148101d7584cSLiam Girdwood 148201d7584cSLiam Girdwood /* prune the BE if it's no longer in our active list */ 148301d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 148401d7584cSLiam Girdwood continue; 148501d7584cSLiam Girdwood 148601d7584cSLiam Girdwood /* is there a valid CODEC DAI widget for this BE */ 1487bed646dcSKuninori Morimoto do_prune = 1; 14887afecb30SKuninori Morimoto for_each_rtd_codec_dai(dpcm->be, i, dai) { 14890c01f6caSKuninori Morimoto widget = snd_soc_dai_get_widget(dai, stream); 149001d7584cSLiam Girdwood 149101d7584cSLiam Girdwood /* prune the BE if it's no longer in our active list */ 149201d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 1493bed646dcSKuninori Morimoto do_prune = 0; 14942e5894d7SBenoit Cousson } 1495bed646dcSKuninori Morimoto if (!do_prune) 1496bed646dcSKuninori Morimoto continue; 149701d7584cSLiam Girdwood 1498103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 149901d7584cSLiam Girdwood stream ? "capture" : "playback", 150001d7584cSLiam Girdwood dpcm->be->dai_link->name, fe->dai_link->name); 150101d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 150201d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 150301d7584cSLiam Girdwood prune++; 150401d7584cSLiam Girdwood } 150501d7584cSLiam Girdwood 1506103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 150701d7584cSLiam Girdwood return prune; 150801d7584cSLiam Girdwood } 150901d7584cSLiam Girdwood 151001d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 151101d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 151201d7584cSLiam Girdwood { 151301d7584cSLiam Girdwood struct snd_soc_card *card = fe->card; 151401d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 151501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 151609e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *widget; 151701d7584cSLiam Girdwood int i, new = 0, err; 151801d7584cSLiam Girdwood 151901d7584cSLiam Girdwood /* Create any new FE <--> BE connections */ 152009e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, widget) { 152101d7584cSLiam Girdwood 152209e88f8aSKuninori Morimoto switch (widget->id) { 15234616274dSMark Brown case snd_soc_dapm_dai_in: 1524c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1525c5b8540dSKoro Chen continue; 1526c5b8540dSKoro Chen break; 15274616274dSMark Brown case snd_soc_dapm_dai_out: 1528c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_CAPTURE) 1529c5b8540dSKoro Chen continue; 15304616274dSMark Brown break; 15314616274dSMark Brown default: 153201d7584cSLiam Girdwood continue; 15334616274dSMark Brown } 153401d7584cSLiam Girdwood 153501d7584cSLiam Girdwood /* is there a valid BE rtd for this widget */ 153609e88f8aSKuninori Morimoto be = dpcm_get_be(card, widget, stream); 153701d7584cSLiam Girdwood if (!be) { 1538103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no BE found for %s\n", 153909e88f8aSKuninori Morimoto widget->name); 154001d7584cSLiam Girdwood continue; 154101d7584cSLiam Girdwood } 154201d7584cSLiam Girdwood 154301d7584cSLiam Girdwood /* make sure BE is a real BE */ 154401d7584cSLiam Girdwood if (!be->dai_link->no_pcm) 154501d7584cSLiam Girdwood continue; 154601d7584cSLiam Girdwood 154701d7584cSLiam Girdwood /* don't connect if FE is not running */ 154823607025SLiam Girdwood if (!fe->dpcm[stream].runtime && !fe->fe_compr) 154901d7584cSLiam Girdwood continue; 155001d7584cSLiam Girdwood 155101d7584cSLiam Girdwood /* newly connected FE and BE */ 155201d7584cSLiam Girdwood err = dpcm_be_connect(fe, be, stream); 155301d7584cSLiam Girdwood if (err < 0) { 1554103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: can't connect %s\n", 155509e88f8aSKuninori Morimoto widget->name); 155601d7584cSLiam Girdwood break; 155701d7584cSLiam Girdwood } else if (err == 0) /* already connected */ 155801d7584cSLiam Girdwood continue; 155901d7584cSLiam Girdwood 156001d7584cSLiam Girdwood /* new */ 156101d7584cSLiam Girdwood be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 156201d7584cSLiam Girdwood new++; 156301d7584cSLiam Girdwood } 156401d7584cSLiam Girdwood 1565103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 156601d7584cSLiam Girdwood return new; 156701d7584cSLiam Girdwood } 156801d7584cSLiam Girdwood 156901d7584cSLiam Girdwood /* 157001d7584cSLiam Girdwood * Find the corresponding BE DAIs that source or sink audio to this 157101d7584cSLiam Girdwood * FE substream. 157201d7584cSLiam Girdwood */ 157323607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 157401d7584cSLiam Girdwood int stream, struct snd_soc_dapm_widget_list **list, int new) 157501d7584cSLiam Girdwood { 157601d7584cSLiam Girdwood if (new) 157701d7584cSLiam Girdwood return dpcm_add_paths(fe, stream, list); 157801d7584cSLiam Girdwood else 157901d7584cSLiam Girdwood return dpcm_prune_paths(fe, stream, list); 158001d7584cSLiam Girdwood } 158101d7584cSLiam Girdwood 158223607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 158301d7584cSLiam Girdwood { 158401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1585a9764869SKaiChieh Chuang unsigned long flags; 158601d7584cSLiam Girdwood 1587a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 15888d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 158901d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = 159001d7584cSLiam Girdwood SND_SOC_DPCM_UPDATE_NO; 1591a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 159201d7584cSLiam Girdwood } 159301d7584cSLiam Girdwood 159401d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 159501d7584cSLiam Girdwood int stream) 159601d7584cSLiam Girdwood { 159701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 159801d7584cSLiam Girdwood 159901d7584cSLiam Girdwood /* disable any enabled and non active backends */ 16008d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 160101d7584cSLiam Girdwood 160201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 160301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 160401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 160501d7584cSLiam Girdwood 160601d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1607103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 160801d7584cSLiam Girdwood stream ? "capture" : "playback", 160901d7584cSLiam Girdwood be->dpcm[stream].state); 161001d7584cSLiam Girdwood 161101d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 161201d7584cSLiam Girdwood continue; 161301d7584cSLiam Girdwood 161401d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 161501d7584cSLiam Girdwood continue; 161601d7584cSLiam Girdwood 161701d7584cSLiam Girdwood soc_pcm_close(be_substream); 161801d7584cSLiam Girdwood be_substream->runtime = NULL; 161901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 162001d7584cSLiam Girdwood } 162101d7584cSLiam Girdwood } 162201d7584cSLiam Girdwood 162323607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 162401d7584cSLiam Girdwood { 162501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 162601d7584cSLiam Girdwood int err, count = 0; 162701d7584cSLiam Girdwood 162801d7584cSLiam Girdwood /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 16298d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 163001d7584cSLiam Girdwood 163101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 163201d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 163301d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 163401d7584cSLiam Girdwood 16352062b4c5SRussell King - ARM Linux if (!be_substream) { 16362062b4c5SRussell King - ARM Linux dev_err(be->dev, "ASoC: no backend %s stream\n", 16372062b4c5SRussell King - ARM Linux stream ? "capture" : "playback"); 16382062b4c5SRussell King - ARM Linux continue; 16392062b4c5SRussell King - ARM Linux } 16402062b4c5SRussell King - ARM Linux 164101d7584cSLiam Girdwood /* is this op for this BE ? */ 164201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 164301d7584cSLiam Girdwood continue; 164401d7584cSLiam Girdwood 164501d7584cSLiam Girdwood /* first time the dpcm is open ? */ 164601d7584cSLiam Girdwood if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1647103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: too many users %s at open %d\n", 164801d7584cSLiam Girdwood stream ? "capture" : "playback", 164901d7584cSLiam Girdwood be->dpcm[stream].state); 165001d7584cSLiam Girdwood 165101d7584cSLiam Girdwood if (be->dpcm[stream].users++ != 0) 165201d7584cSLiam Girdwood continue; 165301d7584cSLiam Girdwood 165401d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 165501d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 165601d7584cSLiam Girdwood continue; 165701d7584cSLiam Girdwood 16582062b4c5SRussell King - ARM Linux dev_dbg(be->dev, "ASoC: open %s BE %s\n", 16592062b4c5SRussell King - ARM Linux stream ? "capture" : "playback", be->dai_link->name); 166001d7584cSLiam Girdwood 166101d7584cSLiam Girdwood be_substream->runtime = be->dpcm[stream].runtime; 166201d7584cSLiam Girdwood err = soc_pcm_open(be_substream); 166301d7584cSLiam Girdwood if (err < 0) { 1664103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: BE open failed %d\n", err); 166501d7584cSLiam Girdwood be->dpcm[stream].users--; 166601d7584cSLiam Girdwood if (be->dpcm[stream].users < 0) 1667103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 166801d7584cSLiam Girdwood stream ? "capture" : "playback", 166901d7584cSLiam Girdwood be->dpcm[stream].state); 167001d7584cSLiam Girdwood 167101d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 167201d7584cSLiam Girdwood goto unwind; 167301d7584cSLiam Girdwood } 167401d7584cSLiam Girdwood 167501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 167601d7584cSLiam Girdwood count++; 167701d7584cSLiam Girdwood } 167801d7584cSLiam Girdwood 167901d7584cSLiam Girdwood return count; 168001d7584cSLiam Girdwood 168101d7584cSLiam Girdwood unwind: 168201d7584cSLiam Girdwood /* disable any enabled and non active backends */ 16838d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 168401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 168501d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 168601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 168701d7584cSLiam Girdwood 168801d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 168901d7584cSLiam Girdwood continue; 169001d7584cSLiam Girdwood 169101d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1692103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close %d\n", 169301d7584cSLiam Girdwood stream ? "capture" : "playback", 169401d7584cSLiam Girdwood be->dpcm[stream].state); 169501d7584cSLiam Girdwood 169601d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 169701d7584cSLiam Girdwood continue; 169801d7584cSLiam Girdwood 169901d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 170001d7584cSLiam Girdwood continue; 170101d7584cSLiam Girdwood 170201d7584cSLiam Girdwood soc_pcm_close(be_substream); 170301d7584cSLiam Girdwood be_substream->runtime = NULL; 170401d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 170501d7584cSLiam Girdwood } 170601d7584cSLiam Girdwood 170701d7584cSLiam Girdwood return err; 170801d7584cSLiam Girdwood } 170901d7584cSLiam Girdwood 171008ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1711435ffb76SJerome Brunet struct snd_soc_pcm_stream *stream) 171208ae9b45SLars-Peter Clausen { 171308ae9b45SLars-Peter Clausen runtime->hw.rate_min = stream->rate_min; 1714e33ffbd9SCharles Keepax runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); 171508ae9b45SLars-Peter Clausen runtime->hw.channels_min = stream->channels_min; 171608ae9b45SLars-Peter Clausen runtime->hw.channels_max = stream->channels_max; 1717002220a9SLars-Peter Clausen if (runtime->hw.formats) 1718435ffb76SJerome Brunet runtime->hw.formats &= stream->formats; 1719002220a9SLars-Peter Clausen else 1720435ffb76SJerome Brunet runtime->hw.formats = stream->formats; 172108ae9b45SLars-Peter Clausen runtime->hw.rates = stream->rates; 172208ae9b45SLars-Peter Clausen } 172308ae9b45SLars-Peter Clausen 1724435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, 1725435ffb76SJerome Brunet u64 *formats) 1726b073ed4eSKuninori Morimoto { 1727b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *fe = substream->private_data; 1728b073ed4eSKuninori Morimoto struct snd_soc_dpcm *dpcm; 17297afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1730b073ed4eSKuninori Morimoto int stream = substream->stream; 1731b073ed4eSKuninori Morimoto 1732b073ed4eSKuninori Morimoto if (!fe->dai_link->dpcm_merged_format) 1733435ffb76SJerome Brunet return; 1734b073ed4eSKuninori Morimoto 1735b073ed4eSKuninori Morimoto /* 1736b073ed4eSKuninori Morimoto * It returns merged BE codec format 1737b073ed4eSKuninori Morimoto * if FE want to use it (= dpcm_merged_format) 1738b073ed4eSKuninori Morimoto */ 1739b073ed4eSKuninori Morimoto 17408d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1741b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 1742b073ed4eSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream; 1743b073ed4eSKuninori Morimoto int i; 1744b073ed4eSKuninori Morimoto 17457afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 17464febced1SJerome Brunet /* 17474febced1SJerome Brunet * Skip CODECs which don't support the current stream 17484febced1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 17494febced1SJerome Brunet */ 17507afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 17514febced1SJerome Brunet continue; 17524febced1SJerome Brunet 1753acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1754b073ed4eSKuninori Morimoto 1755435ffb76SJerome Brunet *formats &= codec_stream->formats; 1756435ffb76SJerome Brunet } 1757b073ed4eSKuninori Morimoto } 1758b073ed4eSKuninori Morimoto } 1759b073ed4eSKuninori Morimoto 1760435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, 1761f4c277b8SJiada Wang unsigned int *channels_min, 1762f4c277b8SJiada Wang unsigned int *channels_max) 1763f4c277b8SJiada Wang { 1764f4c277b8SJiada Wang struct snd_soc_pcm_runtime *fe = substream->private_data; 1765f4c277b8SJiada Wang struct snd_soc_dpcm *dpcm; 1766f4c277b8SJiada Wang int stream = substream->stream; 1767f4c277b8SJiada Wang 1768f4c277b8SJiada Wang if (!fe->dai_link->dpcm_merged_chan) 1769f4c277b8SJiada Wang return; 1770f4c277b8SJiada Wang 1771f4c277b8SJiada Wang /* 1772f4c277b8SJiada Wang * It returns merged BE codec channel; 1773f4c277b8SJiada Wang * if FE want to use it (= dpcm_merged_chan) 1774f4c277b8SJiada Wang */ 1775f4c277b8SJiada Wang 17768d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1777f4c277b8SJiada Wang struct snd_soc_pcm_runtime *be = dpcm->be; 1778f4c277b8SJiada Wang struct snd_soc_pcm_stream *codec_stream; 17794f2bd18bSJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 1780f4c277b8SJiada Wang 1781acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); 17824f2bd18bSJerome Brunet 17834f2bd18bSJerome Brunet *channels_min = max(*channels_min, cpu_stream->channels_min); 17844f2bd18bSJerome Brunet *channels_max = min(*channels_max, cpu_stream->channels_max); 17854f2bd18bSJerome Brunet 17864f2bd18bSJerome Brunet /* 17874f2bd18bSJerome Brunet * chan min/max cannot be enforced if there are multiple CODEC 17884f2bd18bSJerome Brunet * DAIs connected to a single CPU DAI, use CPU DAI's directly 17894f2bd18bSJerome Brunet */ 17904f2bd18bSJerome Brunet if (be->num_codecs == 1) { 1791acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream); 1792f4c277b8SJiada Wang 1793f4c277b8SJiada Wang *channels_min = max(*channels_min, 1794f4c277b8SJiada Wang codec_stream->channels_min); 1795f4c277b8SJiada Wang *channels_max = min(*channels_max, 1796f4c277b8SJiada Wang codec_stream->channels_max); 1797f4c277b8SJiada Wang } 1798f4c277b8SJiada Wang } 1799f4c277b8SJiada Wang } 1800f4c277b8SJiada Wang 1801baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, 1802baacd8d1SJerome Brunet unsigned int *rates, 1803baacd8d1SJerome Brunet unsigned int *rate_min, 1804baacd8d1SJerome Brunet unsigned int *rate_max) 1805baacd8d1SJerome Brunet { 1806baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *fe = substream->private_data; 1807baacd8d1SJerome Brunet struct snd_soc_dpcm *dpcm; 1808baacd8d1SJerome Brunet int stream = substream->stream; 1809baacd8d1SJerome Brunet 1810baacd8d1SJerome Brunet if (!fe->dai_link->dpcm_merged_rate) 1811baacd8d1SJerome Brunet return; 1812baacd8d1SJerome Brunet 1813baacd8d1SJerome Brunet /* 1814baacd8d1SJerome Brunet * It returns merged BE codec channel; 1815baacd8d1SJerome Brunet * if FE want to use it (= dpcm_merged_chan) 1816baacd8d1SJerome Brunet */ 1817baacd8d1SJerome Brunet 18188d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1819baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *be = dpcm->be; 1820baacd8d1SJerome Brunet struct snd_soc_pcm_stream *codec_stream; 1821baacd8d1SJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 18227afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1823baacd8d1SJerome Brunet int i; 1824baacd8d1SJerome Brunet 1825acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); 1826baacd8d1SJerome Brunet 1827baacd8d1SJerome Brunet *rate_min = max(*rate_min, cpu_stream->rate_min); 1828baacd8d1SJerome Brunet *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max); 1829baacd8d1SJerome Brunet *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates); 1830baacd8d1SJerome Brunet 18317afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 1832baacd8d1SJerome Brunet /* 1833baacd8d1SJerome Brunet * Skip CODECs which don't support the current stream 1834baacd8d1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 1835baacd8d1SJerome Brunet */ 18367afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 1837baacd8d1SJerome Brunet continue; 1838baacd8d1SJerome Brunet 1839acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1840baacd8d1SJerome Brunet 1841baacd8d1SJerome Brunet *rate_min = max(*rate_min, codec_stream->rate_min); 1842baacd8d1SJerome Brunet *rate_max = min_not_zero(*rate_max, 1843baacd8d1SJerome Brunet codec_stream->rate_max); 1844baacd8d1SJerome Brunet *rates = snd_pcm_rate_mask_intersect(*rates, 1845baacd8d1SJerome Brunet codec_stream->rates); 1846baacd8d1SJerome Brunet } 1847baacd8d1SJerome Brunet } 1848baacd8d1SJerome Brunet } 1849baacd8d1SJerome Brunet 185045c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 185101d7584cSLiam Girdwood { 185201d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 185301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 185401d7584cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 185501d7584cSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 185601d7584cSLiam Girdwood 185708ae9b45SLars-Peter Clausen if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1858435ffb76SJerome Brunet dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); 185908ae9b45SLars-Peter Clausen else 1860435ffb76SJerome Brunet dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); 1861f4c277b8SJiada Wang 1862435ffb76SJerome Brunet dpcm_runtime_merge_format(substream, &runtime->hw.formats); 1863435ffb76SJerome Brunet dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, 1864435ffb76SJerome Brunet &runtime->hw.channels_max); 1865baacd8d1SJerome Brunet dpcm_runtime_merge_rate(substream, &runtime->hw.rates, 1866baacd8d1SJerome Brunet &runtime->hw.rate_min, &runtime->hw.rate_max); 186701d7584cSLiam Girdwood } 186801d7584cSLiam Girdwood 1869ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 1870ea9d0d77STakashi Iwai 1871ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock 1872ea9d0d77STakashi Iwai * for avoiding the race with trigger callback. 1873ea9d0d77STakashi Iwai * If the state is unset and a trigger is pending while the previous operation, 1874ea9d0d77STakashi Iwai * process the pending trigger action here. 1875ea9d0d77STakashi Iwai */ 1876ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 1877ea9d0d77STakashi Iwai int stream, enum snd_soc_dpcm_update state) 1878ea9d0d77STakashi Iwai { 1879ea9d0d77STakashi Iwai struct snd_pcm_substream *substream = 1880ea9d0d77STakashi Iwai snd_soc_dpcm_get_substream(fe, stream); 1881ea9d0d77STakashi Iwai 1882ea9d0d77STakashi Iwai snd_pcm_stream_lock_irq(substream); 1883ea9d0d77STakashi Iwai if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 1884ea9d0d77STakashi Iwai dpcm_fe_dai_do_trigger(substream, 1885ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending - 1); 1886ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = 0; 1887ea9d0d77STakashi Iwai } 1888ea9d0d77STakashi Iwai fe->dpcm[stream].runtime_update = state; 1889ea9d0d77STakashi Iwai snd_pcm_stream_unlock_irq(substream); 1890ea9d0d77STakashi Iwai } 1891ea9d0d77STakashi Iwai 1892906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 1893906c7d69SPC Liao int stream) 1894906c7d69SPC Liao { 1895906c7d69SPC Liao struct snd_soc_dpcm *dpcm; 1896906c7d69SPC Liao struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 1897906c7d69SPC Liao struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai; 1898906c7d69SPC Liao int err; 1899906c7d69SPC Liao 1900906c7d69SPC Liao /* apply symmetry for FE */ 1901906c7d69SPC Liao if (soc_pcm_has_symmetry(fe_substream)) 1902906c7d69SPC Liao fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1903906c7d69SPC Liao 1904906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1905906c7d69SPC Liao if (fe_cpu_dai->active) { 1906906c7d69SPC Liao err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 1907906c7d69SPC Liao if (err < 0) 1908906c7d69SPC Liao return err; 1909906c7d69SPC Liao } 1910906c7d69SPC Liao 1911906c7d69SPC Liao /* apply symmetry for BE */ 19128d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1913906c7d69SPC Liao struct snd_soc_pcm_runtime *be = dpcm->be; 1914906c7d69SPC Liao struct snd_pcm_substream *be_substream = 1915906c7d69SPC Liao snd_soc_dpcm_get_substream(be, stream); 19166246f283SJerome Brunet struct snd_soc_pcm_runtime *rtd; 19170b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 1918906c7d69SPC Liao int i; 1919906c7d69SPC Liao 19206246f283SJerome Brunet /* A backend may not have the requested substream */ 19216246f283SJerome Brunet if (!be_substream) 19226246f283SJerome Brunet continue; 19236246f283SJerome Brunet 19246246f283SJerome Brunet rtd = be_substream->private_data; 1925f1176614SJeeja KP if (rtd->dai_link->be_hw_params_fixup) 1926f1176614SJeeja KP continue; 1927f1176614SJeeja KP 1928906c7d69SPC Liao if (soc_pcm_has_symmetry(be_substream)) 1929906c7d69SPC Liao be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1930906c7d69SPC Liao 1931906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1932906c7d69SPC Liao if (rtd->cpu_dai->active) { 193399bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 193499bcedbdSKai Chieh Chuang rtd->cpu_dai); 1935906c7d69SPC Liao if (err < 0) 1936906c7d69SPC Liao return err; 1937906c7d69SPC Liao } 1938906c7d69SPC Liao 19390b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 19400b7990e3SKuninori Morimoto if (codec_dai->active) { 194199bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 19420b7990e3SKuninori Morimoto codec_dai); 1943906c7d69SPC Liao if (err < 0) 1944906c7d69SPC Liao return err; 1945906c7d69SPC Liao } 1946906c7d69SPC Liao } 1947906c7d69SPC Liao } 1948906c7d69SPC Liao 1949906c7d69SPC Liao return 0; 1950906c7d69SPC Liao } 1951906c7d69SPC Liao 195201d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 195301d7584cSLiam Girdwood { 195401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 195501d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = fe_substream->runtime; 195601d7584cSLiam Girdwood int stream = fe_substream->stream, ret = 0; 195701d7584cSLiam Girdwood 1958ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 195901d7584cSLiam Girdwood 196001d7584cSLiam Girdwood ret = dpcm_be_dai_startup(fe, fe_substream->stream); 196101d7584cSLiam Girdwood if (ret < 0) { 1962103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 196301d7584cSLiam Girdwood goto be_err; 196401d7584cSLiam Girdwood } 196501d7584cSLiam Girdwood 1966103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 196701d7584cSLiam Girdwood 196801d7584cSLiam Girdwood /* start the DAI frontend */ 196901d7584cSLiam Girdwood ret = soc_pcm_open(fe_substream); 197001d7584cSLiam Girdwood if (ret < 0) { 1971103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 197201d7584cSLiam Girdwood goto unwind; 197301d7584cSLiam Girdwood } 197401d7584cSLiam Girdwood 197501d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 197601d7584cSLiam Girdwood 197701d7584cSLiam Girdwood dpcm_set_fe_runtime(fe_substream); 197801d7584cSLiam Girdwood snd_pcm_limit_hw_rates(runtime); 197901d7584cSLiam Girdwood 1980906c7d69SPC Liao ret = dpcm_apply_symmetry(fe_substream, stream); 1981906c7d69SPC Liao if (ret < 0) { 1982906c7d69SPC Liao dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", 1983906c7d69SPC Liao ret); 1984906c7d69SPC Liao goto unwind; 1985906c7d69SPC Liao } 1986906c7d69SPC Liao 1987ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 198801d7584cSLiam Girdwood return 0; 198901d7584cSLiam Girdwood 199001d7584cSLiam Girdwood unwind: 199101d7584cSLiam Girdwood dpcm_be_dai_startup_unwind(fe, fe_substream->stream); 199201d7584cSLiam Girdwood be_err: 1993ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 199401d7584cSLiam Girdwood return ret; 199501d7584cSLiam Girdwood } 199601d7584cSLiam Girdwood 199723607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 199801d7584cSLiam Girdwood { 199901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 200001d7584cSLiam Girdwood 200101d7584cSLiam Girdwood /* only shutdown BEs that are either sinks or sources to this FE DAI */ 20028d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 200301d7584cSLiam Girdwood 200401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 200501d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 200601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 200701d7584cSLiam Girdwood 200801d7584cSLiam Girdwood /* is this op for this BE ? */ 200901d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 201001d7584cSLiam Girdwood continue; 201101d7584cSLiam Girdwood 201201d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 2013103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 201401d7584cSLiam Girdwood stream ? "capture" : "playback", 201501d7584cSLiam Girdwood be->dpcm[stream].state); 201601d7584cSLiam Girdwood 201701d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 201801d7584cSLiam Girdwood continue; 201901d7584cSLiam Girdwood 202001d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 20219c0ac70aSKai Chieh Chuang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { 20229c0ac70aSKai Chieh Chuang soc_pcm_hw_free(be_substream); 20239c0ac70aSKai Chieh Chuang be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 20249c0ac70aSKai Chieh Chuang } 202501d7584cSLiam Girdwood 2026103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: close BE %s\n", 202794d215ccS彭东林 be->dai_link->name); 202801d7584cSLiam Girdwood 202901d7584cSLiam Girdwood soc_pcm_close(be_substream); 203001d7584cSLiam Girdwood be_substream->runtime = NULL; 203101d7584cSLiam Girdwood 203201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 203301d7584cSLiam Girdwood } 203401d7584cSLiam Girdwood return 0; 203501d7584cSLiam Girdwood } 203601d7584cSLiam Girdwood 203701d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 203801d7584cSLiam Girdwood { 203901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 204001d7584cSLiam Girdwood int stream = substream->stream; 204101d7584cSLiam Girdwood 2042ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 204301d7584cSLiam Girdwood 204401d7584cSLiam Girdwood /* shutdown the BEs */ 204501d7584cSLiam Girdwood dpcm_be_dai_shutdown(fe, substream->stream); 204601d7584cSLiam Girdwood 2047103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 204801d7584cSLiam Girdwood 204901d7584cSLiam Girdwood /* now shutdown the frontend */ 205001d7584cSLiam Girdwood soc_pcm_close(substream); 205101d7584cSLiam Girdwood 205201d7584cSLiam Girdwood /* run the stream event for each BE */ 2053b0edff42SKuninori Morimoto snd_soc_dapm_stream_stop(fe, stream); 205401d7584cSLiam Girdwood 205501d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2056ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 205701d7584cSLiam Girdwood return 0; 205801d7584cSLiam Girdwood } 205901d7584cSLiam Girdwood 206023607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 206101d7584cSLiam Girdwood { 206201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 206301d7584cSLiam Girdwood 206401d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 206501d7584cSLiam Girdwood * to this frontend DAI */ 20668d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 206701d7584cSLiam Girdwood 206801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 206901d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 207001d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 207101d7584cSLiam Girdwood 207201d7584cSLiam Girdwood /* is this op for this BE ? */ 207301d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 207401d7584cSLiam Girdwood continue; 207501d7584cSLiam Girdwood 207601d7584cSLiam Girdwood /* only free hw when no longer used - check all FEs */ 207701d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 207801d7584cSLiam Girdwood continue; 207901d7584cSLiam Girdwood 208036fba62cSQiao Zhou /* do not free hw if this BE is used by other FE */ 208136fba62cSQiao Zhou if (be->dpcm[stream].users > 1) 208236fba62cSQiao Zhou continue; 208336fba62cSQiao Zhou 208401d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 208501d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 208601d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 208708b27848SPatrick Lai (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 20885e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 20895e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 209001d7584cSLiam Girdwood continue; 209101d7584cSLiam Girdwood 2092103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 209394d215ccS彭东林 be->dai_link->name); 209401d7584cSLiam Girdwood 209501d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 209601d7584cSLiam Girdwood 209701d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 209801d7584cSLiam Girdwood } 209901d7584cSLiam Girdwood 210001d7584cSLiam Girdwood return 0; 210101d7584cSLiam Girdwood } 210201d7584cSLiam Girdwood 210345c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 210401d7584cSLiam Girdwood { 210501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 210601d7584cSLiam Girdwood int err, stream = substream->stream; 210701d7584cSLiam Girdwood 210801d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2109ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 211001d7584cSLiam Girdwood 2111103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 211201d7584cSLiam Girdwood 211301d7584cSLiam Girdwood /* call hw_free on the frontend */ 211401d7584cSLiam Girdwood err = soc_pcm_hw_free(substream); 211501d7584cSLiam Girdwood if (err < 0) 2116103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 211701d7584cSLiam Girdwood fe->dai_link->name); 211801d7584cSLiam Girdwood 211901d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 212001d7584cSLiam Girdwood * to this frontend DAI */ 212101d7584cSLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 212201d7584cSLiam Girdwood 212301d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2124ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 212501d7584cSLiam Girdwood 212601d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 212701d7584cSLiam Girdwood return 0; 212801d7584cSLiam Girdwood } 212901d7584cSLiam Girdwood 213023607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 213101d7584cSLiam Girdwood { 213201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 213301d7584cSLiam Girdwood int ret; 213401d7584cSLiam Girdwood 21358d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 213601d7584cSLiam Girdwood 213701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 213801d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 213901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 214001d7584cSLiam Girdwood 214101d7584cSLiam Girdwood /* is this op for this BE ? */ 214201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 214301d7584cSLiam Girdwood continue; 214401d7584cSLiam Girdwood 214501d7584cSLiam Girdwood /* copy params for each dpcm */ 214601d7584cSLiam Girdwood memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 214701d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 214801d7584cSLiam Girdwood 214901d7584cSLiam Girdwood /* perform any hw_params fixups */ 215001d7584cSLiam Girdwood if (be->dai_link->be_hw_params_fixup) { 215101d7584cSLiam Girdwood ret = be->dai_link->be_hw_params_fixup(be, 215201d7584cSLiam Girdwood &dpcm->hw_params); 215301d7584cSLiam Girdwood if (ret < 0) { 215401d7584cSLiam Girdwood dev_err(be->dev, 2155103d84a3SLiam Girdwood "ASoC: hw_params BE fixup failed %d\n", 215601d7584cSLiam Girdwood ret); 215701d7584cSLiam Girdwood goto unwind; 215801d7584cSLiam Girdwood } 215901d7584cSLiam Girdwood } 216001d7584cSLiam Girdwood 2161ae061d2aSLibin Yang /* copy the fixed-up hw params for BE dai */ 2162ae061d2aSLibin Yang memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, 2163ae061d2aSLibin Yang sizeof(struct snd_pcm_hw_params)); 2164ae061d2aSLibin Yang 2165b0639bd2SKuninori Morimoto /* only allow hw_params() if no connected FEs are running */ 2166b0639bd2SKuninori Morimoto if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2167b0639bd2SKuninori Morimoto continue; 2168b0639bd2SKuninori Morimoto 2169b0639bd2SKuninori Morimoto if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2170b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2171b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2172b0639bd2SKuninori Morimoto continue; 2173b0639bd2SKuninori Morimoto 2174b0639bd2SKuninori Morimoto dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 217594d215ccS彭东林 be->dai_link->name); 2176b0639bd2SKuninori Morimoto 217701d7584cSLiam Girdwood ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 217801d7584cSLiam Girdwood if (ret < 0) { 217901d7584cSLiam Girdwood dev_err(dpcm->be->dev, 2180103d84a3SLiam Girdwood "ASoC: hw_params BE failed %d\n", ret); 218101d7584cSLiam Girdwood goto unwind; 218201d7584cSLiam Girdwood } 218301d7584cSLiam Girdwood 218401d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 218501d7584cSLiam Girdwood } 218601d7584cSLiam Girdwood return 0; 218701d7584cSLiam Girdwood 218801d7584cSLiam Girdwood unwind: 218901d7584cSLiam Girdwood /* disable any enabled and non active backends */ 21908d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 219101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 219201d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 219301d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 219401d7584cSLiam Girdwood 219501d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 219601d7584cSLiam Girdwood continue; 219701d7584cSLiam Girdwood 219801d7584cSLiam Girdwood /* only allow hw_free() if no connected FEs are running */ 219901d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 220001d7584cSLiam Girdwood continue; 220101d7584cSLiam Girdwood 220201d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 220301d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 220401d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 220501d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 220601d7584cSLiam Girdwood continue; 220701d7584cSLiam Girdwood 220801d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 220901d7584cSLiam Girdwood } 221001d7584cSLiam Girdwood 221101d7584cSLiam Girdwood return ret; 221201d7584cSLiam Girdwood } 221301d7584cSLiam Girdwood 221445c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 221501d7584cSLiam Girdwood struct snd_pcm_hw_params *params) 221601d7584cSLiam Girdwood { 221701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 221801d7584cSLiam Girdwood int ret, stream = substream->stream; 221901d7584cSLiam Girdwood 222001d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2221ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 222201d7584cSLiam Girdwood 222301d7584cSLiam Girdwood memcpy(&fe->dpcm[substream->stream].hw_params, params, 222401d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 222501d7584cSLiam Girdwood ret = dpcm_be_dai_hw_params(fe, substream->stream); 222601d7584cSLiam Girdwood if (ret < 0) { 2227103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 222801d7584cSLiam Girdwood goto out; 222901d7584cSLiam Girdwood } 223001d7584cSLiam Girdwood 2231103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 223201d7584cSLiam Girdwood fe->dai_link->name, params_rate(params), 223301d7584cSLiam Girdwood params_channels(params), params_format(params)); 223401d7584cSLiam Girdwood 223501d7584cSLiam Girdwood /* call hw_params on the frontend */ 223601d7584cSLiam Girdwood ret = soc_pcm_hw_params(substream, params); 223701d7584cSLiam Girdwood if (ret < 0) { 2238103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 223901d7584cSLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 224001d7584cSLiam Girdwood } else 224101d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 224201d7584cSLiam Girdwood 224301d7584cSLiam Girdwood out: 2244ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 224501d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 224601d7584cSLiam Girdwood return ret; 224701d7584cSLiam Girdwood } 224801d7584cSLiam Girdwood 224901d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 225001d7584cSLiam Girdwood struct snd_pcm_substream *substream, int cmd) 225101d7584cSLiam Girdwood { 225201d7584cSLiam Girdwood int ret; 225301d7584cSLiam Girdwood 2254103d84a3SLiam Girdwood dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 225594d215ccS彭东林 dpcm->be->dai_link->name, cmd); 225601d7584cSLiam Girdwood 225701d7584cSLiam Girdwood ret = soc_pcm_trigger(substream, cmd); 225801d7584cSLiam Girdwood if (ret < 0) 2259103d84a3SLiam Girdwood dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 226001d7584cSLiam Girdwood 226101d7584cSLiam Girdwood return ret; 226201d7584cSLiam Girdwood } 226301d7584cSLiam Girdwood 226423607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 226545c0a188SMark Brown int cmd) 226601d7584cSLiam Girdwood { 226701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 226801d7584cSLiam Girdwood int ret = 0; 226901d7584cSLiam Girdwood 22708d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 227101d7584cSLiam Girdwood 227201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 227301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 227401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 227501d7584cSLiam Girdwood 227601d7584cSLiam Girdwood /* is this op for this BE ? */ 227701d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 227801d7584cSLiam Girdwood continue; 227901d7584cSLiam Girdwood 228001d7584cSLiam Girdwood switch (cmd) { 228101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 228201d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 228301d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 228401d7584cSLiam Girdwood continue; 228501d7584cSLiam Girdwood 228601d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 228701d7584cSLiam Girdwood if (ret) 228801d7584cSLiam Girdwood return ret; 228901d7584cSLiam Girdwood 229001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 229101d7584cSLiam Girdwood break; 229201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 229301d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 229401d7584cSLiam Girdwood continue; 229501d7584cSLiam Girdwood 229601d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 229701d7584cSLiam Girdwood if (ret) 229801d7584cSLiam Girdwood return ret; 229901d7584cSLiam Girdwood 230001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 230101d7584cSLiam Girdwood break; 230201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 230301d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 230401d7584cSLiam Girdwood continue; 230501d7584cSLiam Girdwood 230601d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 230701d7584cSLiam Girdwood if (ret) 230801d7584cSLiam Girdwood return ret; 230901d7584cSLiam Girdwood 231001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 231101d7584cSLiam Girdwood break; 231201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 231301d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 231401d7584cSLiam Girdwood continue; 231501d7584cSLiam Girdwood 231601d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 231701d7584cSLiam Girdwood continue; 231801d7584cSLiam Girdwood 231901d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 232001d7584cSLiam Girdwood if (ret) 232101d7584cSLiam Girdwood return ret; 232201d7584cSLiam Girdwood 232301d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 232401d7584cSLiam Girdwood break; 232501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 2326868a6ca8SNicolin Chen if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 232701d7584cSLiam Girdwood continue; 232801d7584cSLiam Girdwood 232901d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 233001d7584cSLiam Girdwood continue; 233101d7584cSLiam Girdwood 233201d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 233301d7584cSLiam Girdwood if (ret) 233401d7584cSLiam Girdwood return ret; 233501d7584cSLiam Girdwood 233601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 233701d7584cSLiam Girdwood break; 233801d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 233901d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 234001d7584cSLiam Girdwood continue; 234101d7584cSLiam Girdwood 234201d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 234301d7584cSLiam Girdwood continue; 234401d7584cSLiam Girdwood 234501d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 234601d7584cSLiam Girdwood if (ret) 234701d7584cSLiam Girdwood return ret; 234801d7584cSLiam Girdwood 234901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 235001d7584cSLiam Girdwood break; 235101d7584cSLiam Girdwood } 235201d7584cSLiam Girdwood } 235301d7584cSLiam Girdwood 235401d7584cSLiam Girdwood return ret; 235501d7584cSLiam Girdwood } 235601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 235701d7584cSLiam Girdwood 2358acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, 2359acbf2774SRanjani Sridharan int cmd, bool fe_first) 2360acbf2774SRanjani Sridharan { 2361acbf2774SRanjani Sridharan struct snd_soc_pcm_runtime *fe = substream->private_data; 2362acbf2774SRanjani Sridharan int ret; 2363acbf2774SRanjani Sridharan 2364acbf2774SRanjani Sridharan /* call trigger on the frontend before the backend. */ 2365acbf2774SRanjani Sridharan if (fe_first) { 2366acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2367acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2368acbf2774SRanjani Sridharan 2369acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2370acbf2774SRanjani Sridharan if (ret < 0) 2371acbf2774SRanjani Sridharan return ret; 2372acbf2774SRanjani Sridharan 2373acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2374acbf2774SRanjani Sridharan return ret; 2375acbf2774SRanjani Sridharan } 2376acbf2774SRanjani Sridharan 2377acbf2774SRanjani Sridharan /* call trigger on the frontend after the backend. */ 2378acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2379acbf2774SRanjani Sridharan if (ret < 0) 2380acbf2774SRanjani Sridharan return ret; 2381acbf2774SRanjani Sridharan 2382acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2383acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2384acbf2774SRanjani Sridharan 2385acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2386acbf2774SRanjani Sridharan 2387acbf2774SRanjani Sridharan return ret; 2388acbf2774SRanjani Sridharan } 2389acbf2774SRanjani Sridharan 2390ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 239101d7584cSLiam Girdwood { 239201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 2393acbf2774SRanjani Sridharan int stream = substream->stream; 2394acbf2774SRanjani Sridharan int ret = 0; 239501d7584cSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 239601d7584cSLiam Girdwood 239701d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 239801d7584cSLiam Girdwood 239901d7584cSLiam Girdwood switch (trigger) { 240001d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_PRE: 2401acbf2774SRanjani Sridharan switch (cmd) { 2402acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2403acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2404acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2405acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2406acbf2774SRanjani Sridharan break; 2407acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2408acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2409acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2410acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2411acbf2774SRanjani Sridharan break; 2412acbf2774SRanjani Sridharan default: 2413acbf2774SRanjani Sridharan ret = -EINVAL; 2414acbf2774SRanjani Sridharan break; 241501d7584cSLiam Girdwood } 241601d7584cSLiam Girdwood break; 241701d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_POST: 2418acbf2774SRanjani Sridharan switch (cmd) { 2419acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2420acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2421acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2422acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2423acbf2774SRanjani Sridharan break; 2424acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2425acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2426acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2427acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2428acbf2774SRanjani Sridharan break; 2429acbf2774SRanjani Sridharan default: 2430acbf2774SRanjani Sridharan ret = -EINVAL; 2431acbf2774SRanjani Sridharan break; 243201d7584cSLiam Girdwood } 243301d7584cSLiam Girdwood break; 243407bf84aaSLiam Girdwood case SND_SOC_DPCM_TRIGGER_BESPOKE: 243507bf84aaSLiam Girdwood /* bespoke trigger() - handles both FE and BEs */ 243607bf84aaSLiam Girdwood 2437103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 243807bf84aaSLiam Girdwood fe->dai_link->name, cmd); 243907bf84aaSLiam Girdwood 244007bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, cmd); 244107bf84aaSLiam Girdwood break; 244201d7584cSLiam Girdwood default: 2443103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 244401d7584cSLiam Girdwood fe->dai_link->name); 244501d7584cSLiam Girdwood ret = -EINVAL; 244601d7584cSLiam Girdwood goto out; 244701d7584cSLiam Girdwood } 244801d7584cSLiam Girdwood 2449acbf2774SRanjani Sridharan if (ret < 0) { 2450acbf2774SRanjani Sridharan dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", 2451acbf2774SRanjani Sridharan cmd, ret); 2452acbf2774SRanjani Sridharan goto out; 2453acbf2774SRanjani Sridharan } 2454acbf2774SRanjani Sridharan 245501d7584cSLiam Girdwood switch (cmd) { 245601d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 245701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 245801d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 245901d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 246001d7584cSLiam Girdwood break; 246101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 246201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 246301d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 246401d7584cSLiam Girdwood break; 24659f169b9fSPatrick Lai case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 24669f169b9fSPatrick Lai fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 24679f169b9fSPatrick Lai break; 246801d7584cSLiam Girdwood } 246901d7584cSLiam Girdwood 247001d7584cSLiam Girdwood out: 247101d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 247201d7584cSLiam Girdwood return ret; 247301d7584cSLiam Girdwood } 247401d7584cSLiam Girdwood 2475ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2476ea9d0d77STakashi Iwai { 2477ea9d0d77STakashi Iwai struct snd_soc_pcm_runtime *fe = substream->private_data; 2478ea9d0d77STakashi Iwai int stream = substream->stream; 2479ea9d0d77STakashi Iwai 2480ea9d0d77STakashi Iwai /* if FE's runtime_update is already set, we're in race; 2481ea9d0d77STakashi Iwai * process this trigger later at exit 2482ea9d0d77STakashi Iwai */ 2483ea9d0d77STakashi Iwai if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2484ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = cmd + 1; 2485ea9d0d77STakashi Iwai return 0; /* delayed, assuming it's successful */ 2486ea9d0d77STakashi Iwai } 2487ea9d0d77STakashi Iwai 2488ea9d0d77STakashi Iwai /* we're alone, let's trigger */ 2489ea9d0d77STakashi Iwai return dpcm_fe_dai_do_trigger(substream, cmd); 2490ea9d0d77STakashi Iwai } 2491ea9d0d77STakashi Iwai 249223607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 249301d7584cSLiam Girdwood { 249401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 249501d7584cSLiam Girdwood int ret = 0; 249601d7584cSLiam Girdwood 24978d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 249801d7584cSLiam Girdwood 249901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 250001d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 250101d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 250201d7584cSLiam Girdwood 250301d7584cSLiam Girdwood /* is this op for this BE ? */ 250401d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 250501d7584cSLiam Girdwood continue; 250601d7584cSLiam Girdwood 250701d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 250895f444dcSKoro Chen (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 25095087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && 25105087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 251101d7584cSLiam Girdwood continue; 251201d7584cSLiam Girdwood 2513103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: prepare BE %s\n", 251494d215ccS彭东林 be->dai_link->name); 251501d7584cSLiam Girdwood 251601d7584cSLiam Girdwood ret = soc_pcm_prepare(be_substream); 251701d7584cSLiam Girdwood if (ret < 0) { 2518103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: backend prepare failed %d\n", 251901d7584cSLiam Girdwood ret); 252001d7584cSLiam Girdwood break; 252101d7584cSLiam Girdwood } 252201d7584cSLiam Girdwood 252301d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 252401d7584cSLiam Girdwood } 252501d7584cSLiam Girdwood return ret; 252601d7584cSLiam Girdwood } 252701d7584cSLiam Girdwood 252845c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 252901d7584cSLiam Girdwood { 253001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 253101d7584cSLiam Girdwood int stream = substream->stream, ret = 0; 253201d7584cSLiam Girdwood 253301d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 253401d7584cSLiam Girdwood 2535103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 253601d7584cSLiam Girdwood 2537ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 253801d7584cSLiam Girdwood 253901d7584cSLiam Girdwood /* there is no point preparing this FE if there are no BEs */ 254001d7584cSLiam Girdwood if (list_empty(&fe->dpcm[stream].be_clients)) { 2541103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 254201d7584cSLiam Girdwood fe->dai_link->name); 254301d7584cSLiam Girdwood ret = -EINVAL; 254401d7584cSLiam Girdwood goto out; 254501d7584cSLiam Girdwood } 254601d7584cSLiam Girdwood 254701d7584cSLiam Girdwood ret = dpcm_be_dai_prepare(fe, substream->stream); 254801d7584cSLiam Girdwood if (ret < 0) 254901d7584cSLiam Girdwood goto out; 255001d7584cSLiam Girdwood 255101d7584cSLiam Girdwood /* call prepare on the frontend */ 255201d7584cSLiam Girdwood ret = soc_pcm_prepare(substream); 255301d7584cSLiam Girdwood if (ret < 0) { 2554103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 255501d7584cSLiam Girdwood fe->dai_link->name); 255601d7584cSLiam Girdwood goto out; 255701d7584cSLiam Girdwood } 255801d7584cSLiam Girdwood 255901d7584cSLiam Girdwood /* run the stream event for each BE */ 256001d7584cSLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 256101d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 256201d7584cSLiam Girdwood 256301d7584cSLiam Girdwood out: 2564ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 256501d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 256601d7584cSLiam Girdwood 256701d7584cSLiam Girdwood return ret; 256801d7584cSLiam Girdwood } 256901d7584cSLiam Girdwood 2570618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2571618dae11SLiam Girdwood { 257207bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 257307bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 257407bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2575618dae11SLiam Girdwood int err; 257601d7584cSLiam Girdwood 2577103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2578618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2579618dae11SLiam Girdwood 258007bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 258107bf84aaSLiam Girdwood /* call bespoke trigger - FE takes care of all BE triggers */ 2582103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 258307bf84aaSLiam Girdwood fe->dai_link->name); 258407bf84aaSLiam Girdwood 258507bf84aaSLiam Girdwood err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 258607bf84aaSLiam Girdwood if (err < 0) 2587103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 258807bf84aaSLiam Girdwood } else { 2589103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 259007bf84aaSLiam Girdwood fe->dai_link->name); 259107bf84aaSLiam Girdwood 2592618dae11SLiam Girdwood err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2593618dae11SLiam Girdwood if (err < 0) 2594103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 259507bf84aaSLiam Girdwood } 2596618dae11SLiam Girdwood 2597618dae11SLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 2598618dae11SLiam Girdwood if (err < 0) 2599103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 2600618dae11SLiam Girdwood 2601618dae11SLiam Girdwood err = dpcm_be_dai_shutdown(fe, stream); 2602618dae11SLiam Girdwood if (err < 0) 2603103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 2604618dae11SLiam Girdwood 2605618dae11SLiam Girdwood /* run the stream event for each BE */ 2606618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2607618dae11SLiam Girdwood 2608618dae11SLiam Girdwood return 0; 2609618dae11SLiam Girdwood } 2610618dae11SLiam Girdwood 2611618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2612618dae11SLiam Girdwood { 261307bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 261407bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 2615618dae11SLiam Girdwood struct snd_soc_dpcm *dpcm; 261607bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2617618dae11SLiam Girdwood int ret; 2618a9764869SKaiChieh Chuang unsigned long flags; 2619618dae11SLiam Girdwood 2620103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2621618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2622618dae11SLiam Girdwood 2623618dae11SLiam Girdwood /* Only start the BE if the FE is ready */ 2624618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2625618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 2626618dae11SLiam Girdwood return -EINVAL; 2627618dae11SLiam Girdwood 2628618dae11SLiam Girdwood /* startup must always be called for new BEs */ 2629618dae11SLiam Girdwood ret = dpcm_be_dai_startup(fe, stream); 2630fffc0ca2SDan Carpenter if (ret < 0) 2631618dae11SLiam Girdwood goto disconnect; 2632618dae11SLiam Girdwood 2633618dae11SLiam Girdwood /* keep going if FE state is > open */ 2634618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2635618dae11SLiam Girdwood return 0; 2636618dae11SLiam Girdwood 2637618dae11SLiam Girdwood ret = dpcm_be_dai_hw_params(fe, stream); 2638fffc0ca2SDan Carpenter if (ret < 0) 2639618dae11SLiam Girdwood goto close; 2640618dae11SLiam Girdwood 2641618dae11SLiam Girdwood /* keep going if FE state is > hw_params */ 2642618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2643618dae11SLiam Girdwood return 0; 2644618dae11SLiam Girdwood 2645618dae11SLiam Girdwood 2646618dae11SLiam Girdwood ret = dpcm_be_dai_prepare(fe, stream); 2647fffc0ca2SDan Carpenter if (ret < 0) 2648618dae11SLiam Girdwood goto hw_free; 2649618dae11SLiam Girdwood 2650618dae11SLiam Girdwood /* run the stream event for each BE */ 2651618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2652618dae11SLiam Girdwood 2653618dae11SLiam Girdwood /* keep going if FE state is > prepare */ 2654618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2655618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2656618dae11SLiam Girdwood return 0; 2657618dae11SLiam Girdwood 265807bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 265907bf84aaSLiam Girdwood /* call trigger on the frontend - FE takes care of all BE triggers */ 2660103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 266107bf84aaSLiam Girdwood fe->dai_link->name); 266207bf84aaSLiam Girdwood 266307bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 266407bf84aaSLiam Girdwood if (ret < 0) { 2665103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 266607bf84aaSLiam Girdwood goto hw_free; 266707bf84aaSLiam Girdwood } 266807bf84aaSLiam Girdwood } else { 2669103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 2670618dae11SLiam Girdwood fe->dai_link->name); 2671618dae11SLiam Girdwood 2672618dae11SLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream, 2673618dae11SLiam Girdwood SNDRV_PCM_TRIGGER_START); 2674618dae11SLiam Girdwood if (ret < 0) { 2675103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2676618dae11SLiam Girdwood goto hw_free; 2677618dae11SLiam Girdwood } 267807bf84aaSLiam Girdwood } 2679618dae11SLiam Girdwood 2680618dae11SLiam Girdwood return 0; 2681618dae11SLiam Girdwood 2682618dae11SLiam Girdwood hw_free: 2683618dae11SLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 2684618dae11SLiam Girdwood close: 2685618dae11SLiam Girdwood dpcm_be_dai_shutdown(fe, stream); 2686618dae11SLiam Girdwood disconnect: 2687618dae11SLiam Girdwood /* disconnect any non started BEs */ 2688a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 26898d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2690618dae11SLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 2691618dae11SLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2692618dae11SLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2693618dae11SLiam Girdwood } 2694a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 2695618dae11SLiam Girdwood 2696618dae11SLiam Girdwood return ret; 2697618dae11SLiam Girdwood } 2698618dae11SLiam Girdwood 2699de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2700618dae11SLiam Girdwood { 2701618dae11SLiam Girdwood struct snd_soc_dapm_widget_list *list; 27027083f877SKuninori Morimoto int stream; 2703de15d7ffSJerome Brunet int count, paths; 2704580dff36SKuninori Morimoto int ret; 2705618dae11SLiam Girdwood 2706618dae11SLiam Girdwood if (!fe->dai_link->dynamic) 2707de15d7ffSJerome Brunet return 0; 2708618dae11SLiam Girdwood 2709618dae11SLiam Girdwood /* only check active links */ 2710618dae11SLiam Girdwood if (!fe->cpu_dai->active) 2711de15d7ffSJerome Brunet return 0; 2712618dae11SLiam Girdwood 2713618dae11SLiam Girdwood /* DAPM sync will call this to update DSP paths */ 2714de15d7ffSJerome Brunet dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2715de15d7ffSJerome Brunet new ? "new" : "old", fe->dai_link->name); 2716618dae11SLiam Girdwood 27177083f877SKuninori Morimoto for_each_pcm_streams(stream) { 2718075207d2SQiao Zhou 27197083f877SKuninori Morimoto /* skip if FE doesn't have playback/capture capability */ 27207083f877SKuninori Morimoto if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) || 27217083f877SKuninori Morimoto !snd_soc_dai_stream_valid(fe->codec_dai, stream)) 27227083f877SKuninori Morimoto continue; 2723618dae11SLiam Girdwood 27247083f877SKuninori Morimoto /* skip if FE isn't currently playing/capturing */ 27257083f877SKuninori Morimoto if (!fe->cpu_dai->stream_active[stream] || 27267083f877SKuninori Morimoto !fe->codec_dai->stream_active[stream]) 27277083f877SKuninori Morimoto continue; 27287083f877SKuninori Morimoto 27297083f877SKuninori Morimoto paths = dpcm_path_get(fe, stream, &list); 2730618dae11SLiam Girdwood if (paths < 0) { 2731103d84a3SLiam Girdwood dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 27327083f877SKuninori Morimoto fe->dai_link->name, 27337083f877SKuninori Morimoto stream == SNDRV_PCM_STREAM_PLAYBACK ? 27347083f877SKuninori Morimoto "playback" : "capture"); 2735618dae11SLiam Girdwood return paths; 2736618dae11SLiam Girdwood } 2737618dae11SLiam Girdwood 27387083f877SKuninori Morimoto /* update any playback/capture paths */ 27397083f877SKuninori Morimoto count = dpcm_process_paths(fe, stream, &list, new); 2740de15d7ffSJerome Brunet if (count) { 2741580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2742de15d7ffSJerome Brunet if (new) 2743580dff36SKuninori Morimoto ret = dpcm_run_update_startup(fe, stream); 2744de15d7ffSJerome Brunet else 2745580dff36SKuninori Morimoto ret = dpcm_run_update_shutdown(fe, stream); 2746580dff36SKuninori Morimoto if (ret < 0) 2747580dff36SKuninori Morimoto dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2748580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2749de15d7ffSJerome Brunet 27507083f877SKuninori Morimoto dpcm_clear_pending_state(fe, stream); 27517083f877SKuninori Morimoto dpcm_be_disconnect(fe, stream); 2752618dae11SLiam Girdwood } 2753618dae11SLiam Girdwood 27547ed9de76SQiao Zhou dpcm_path_put(&list); 2755618dae11SLiam Girdwood } 2756618dae11SLiam Girdwood 2757de15d7ffSJerome Brunet return 0; 2758618dae11SLiam Girdwood } 2759618dae11SLiam Girdwood 2760de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2761de15d7ffSJerome Brunet * any DAI links. 2762de15d7ffSJerome Brunet */ 2763de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card) 2764de15d7ffSJerome Brunet { 2765de15d7ffSJerome Brunet struct snd_soc_pcm_runtime *fe; 2766de15d7ffSJerome Brunet int ret = 0; 2767de15d7ffSJerome Brunet 2768de15d7ffSJerome Brunet mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2769de15d7ffSJerome Brunet /* shutdown all old paths first */ 2770bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2771de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 0); 2772de15d7ffSJerome Brunet if (ret) 2773de15d7ffSJerome Brunet goto out; 2774de15d7ffSJerome Brunet } 2775de15d7ffSJerome Brunet 2776de15d7ffSJerome Brunet /* bring new paths up */ 2777bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2778de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 1); 2779de15d7ffSJerome Brunet if (ret) 2780de15d7ffSJerome Brunet goto out; 2781de15d7ffSJerome Brunet } 2782de15d7ffSJerome Brunet 2783de15d7ffSJerome Brunet out: 2784618dae11SLiam Girdwood mutex_unlock(&card->mutex); 2785de15d7ffSJerome Brunet return ret; 2786618dae11SLiam Girdwood } 278701d7584cSLiam Girdwood 278845c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 278901d7584cSLiam Girdwood { 279001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 279101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 279201d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list; 279301d7584cSLiam Girdwood int ret; 279401d7584cSLiam Girdwood int stream = fe_substream->stream; 279501d7584cSLiam Girdwood 279601d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 279701d7584cSLiam Girdwood fe->dpcm[stream].runtime = fe_substream->runtime; 279801d7584cSLiam Girdwood 27998f70e515SQiao Zhou ret = dpcm_path_get(fe, stream, &list); 28008f70e515SQiao Zhou if (ret < 0) { 2801cae06eb9SKuninori Morimoto goto open_end; 28028f70e515SQiao Zhou } else if (ret == 0) { 2803103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 280401d7584cSLiam Girdwood fe->dai_link->name, stream ? "capture" : "playback"); 280501d7584cSLiam Girdwood } 280601d7584cSLiam Girdwood 280701d7584cSLiam Girdwood /* calculate valid and active FE <-> BE dpcms */ 280801d7584cSLiam Girdwood dpcm_process_paths(fe, stream, &list, 1); 280901d7584cSLiam Girdwood 281001d7584cSLiam Girdwood ret = dpcm_fe_dai_startup(fe_substream); 281101d7584cSLiam Girdwood if (ret < 0) { 281201d7584cSLiam Girdwood /* clean up all links */ 28138d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 281401d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 281501d7584cSLiam Girdwood 281601d7584cSLiam Girdwood dpcm_be_disconnect(fe, stream); 281701d7584cSLiam Girdwood fe->dpcm[stream].runtime = NULL; 281801d7584cSLiam Girdwood } 281901d7584cSLiam Girdwood 282001d7584cSLiam Girdwood dpcm_clear_pending_state(fe, stream); 282101d7584cSLiam Girdwood dpcm_path_put(&list); 2822cae06eb9SKuninori Morimoto open_end: 282301d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 282401d7584cSLiam Girdwood return ret; 282501d7584cSLiam Girdwood } 282601d7584cSLiam Girdwood 282745c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 282801d7584cSLiam Girdwood { 282901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 283001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 283101d7584cSLiam Girdwood int stream = fe_substream->stream, ret; 283201d7584cSLiam Girdwood 283301d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 283401d7584cSLiam Girdwood ret = dpcm_fe_dai_shutdown(fe_substream); 283501d7584cSLiam Girdwood 283601d7584cSLiam Girdwood /* mark FE's links ready to prune */ 28378d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 283801d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 283901d7584cSLiam Girdwood 284001d7584cSLiam Girdwood dpcm_be_disconnect(fe, stream); 284101d7584cSLiam Girdwood 284201d7584cSLiam Girdwood fe->dpcm[stream].runtime = NULL; 284301d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 284401d7584cSLiam Girdwood return ret; 284501d7584cSLiam Girdwood } 284601d7584cSLiam Girdwood 2847ddee627cSLiam Girdwood /* create a new pcm */ 2848ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2849ddee627cSLiam Girdwood { 28502e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 2851ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 28522b544dd7SKuninori Morimoto struct snd_soc_component *component; 2853ddee627cSLiam Girdwood struct snd_pcm *pcm; 2854ddee627cSLiam Girdwood char new_name[64]; 2855ddee627cSLiam Girdwood int ret = 0, playback = 0, capture = 0; 28562e5894d7SBenoit Cousson int i; 2857ddee627cSLiam Girdwood 285801d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 28591e9de42fSLiam Girdwood playback = rtd->dai_link->dpcm_playback; 28601e9de42fSLiam Girdwood capture = rtd->dai_link->dpcm_capture; 286101d7584cSLiam Girdwood } else { 2862a342031cSJerome Brunet /* Adapt stream for codec2codec links */ 2863a4877a6fSStephan Gerhold int cpu_capture = rtd->dai_link->params ? 2864a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 2865a4877a6fSStephan Gerhold int cpu_playback = rtd->dai_link->params ? 2866a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 2867a342031cSJerome Brunet 28680b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 2869467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 2870a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) 2871ddee627cSLiam Girdwood playback = 1; 2872467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 2873a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) 2874ddee627cSLiam Girdwood capture = 1; 287501d7584cSLiam Girdwood } 28762e5894d7SBenoit Cousson } 28772e5894d7SBenoit Cousson 2878d6bead02SFabio Estevam if (rtd->dai_link->playback_only) { 2879d6bead02SFabio Estevam playback = 1; 2880d6bead02SFabio Estevam capture = 0; 2881d6bead02SFabio Estevam } 2882d6bead02SFabio Estevam 2883d6bead02SFabio Estevam if (rtd->dai_link->capture_only) { 2884d6bead02SFabio Estevam playback = 0; 2885d6bead02SFabio Estevam capture = 1; 2886d6bead02SFabio Estevam } 2887d6bead02SFabio Estevam 288801d7584cSLiam Girdwood /* create the PCM */ 2889a342031cSJerome Brunet if (rtd->dai_link->params) { 2890a342031cSJerome Brunet snprintf(new_name, sizeof(new_name), "codec2codec(%s)", 2891a342031cSJerome Brunet rtd->dai_link->stream_name); 2892a342031cSJerome Brunet 2893a342031cSJerome Brunet ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 2894a342031cSJerome Brunet playback, capture, &pcm); 2895a342031cSJerome Brunet } else if (rtd->dai_link->no_pcm) { 289601d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)", 289701d7584cSLiam Girdwood rtd->dai_link->stream_name); 289801d7584cSLiam Girdwood 289901d7584cSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 290001d7584cSLiam Girdwood playback, capture, &pcm); 290101d7584cSLiam Girdwood } else { 290201d7584cSLiam Girdwood if (rtd->dai_link->dynamic) 290301d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s (*)", 290401d7584cSLiam Girdwood rtd->dai_link->stream_name); 290501d7584cSLiam Girdwood else 290601d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s %s-%d", 29072e5894d7SBenoit Cousson rtd->dai_link->stream_name, 29082e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? 29092e5894d7SBenoit Cousson "multicodec" : rtd->codec_dai->name, num); 291001d7584cSLiam Girdwood 291101d7584cSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 291201d7584cSLiam Girdwood capture, &pcm); 291301d7584cSLiam Girdwood } 2914ddee627cSLiam Girdwood if (ret < 0) { 2915103d84a3SLiam Girdwood dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 29165cb9b748SLiam Girdwood rtd->dai_link->name); 2917ddee627cSLiam Girdwood return ret; 2918ddee627cSLiam Girdwood } 2919103d84a3SLiam Girdwood dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 2920ddee627cSLiam Girdwood 2921ddee627cSLiam Girdwood /* DAPM dai link stream work */ 2922a342031cSJerome Brunet if (rtd->dai_link->params) 29234bf2e385SCurtis Malainey rtd->close_delayed_work_func = codec2codec_close_delayed_work; 2924a342031cSJerome Brunet else 292583f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work; 2926ddee627cSLiam Girdwood 292748c7699fSVinod Koul pcm->nonatomic = rtd->dai_link->nonatomic; 2928ddee627cSLiam Girdwood rtd->pcm = pcm; 2929ddee627cSLiam Girdwood pcm->private_data = rtd; 293001d7584cSLiam Girdwood 2931a342031cSJerome Brunet if (rtd->dai_link->no_pcm || rtd->dai_link->params) { 293201d7584cSLiam Girdwood if (playback) 293301d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 293401d7584cSLiam Girdwood if (capture) 293501d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 293601d7584cSLiam Girdwood goto out; 293701d7584cSLiam Girdwood } 293801d7584cSLiam Girdwood 293901d7584cSLiam Girdwood /* ASoC PCM operations */ 294001d7584cSLiam Girdwood if (rtd->dai_link->dynamic) { 294101d7584cSLiam Girdwood rtd->ops.open = dpcm_fe_dai_open; 294201d7584cSLiam Girdwood rtd->ops.hw_params = dpcm_fe_dai_hw_params; 294301d7584cSLiam Girdwood rtd->ops.prepare = dpcm_fe_dai_prepare; 294401d7584cSLiam Girdwood rtd->ops.trigger = dpcm_fe_dai_trigger; 294501d7584cSLiam Girdwood rtd->ops.hw_free = dpcm_fe_dai_hw_free; 294601d7584cSLiam Girdwood rtd->ops.close = dpcm_fe_dai_close; 294701d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 294801d7584cSLiam Girdwood } else { 294901d7584cSLiam Girdwood rtd->ops.open = soc_pcm_open; 295001d7584cSLiam Girdwood rtd->ops.hw_params = soc_pcm_hw_params; 295101d7584cSLiam Girdwood rtd->ops.prepare = soc_pcm_prepare; 295201d7584cSLiam Girdwood rtd->ops.trigger = soc_pcm_trigger; 295301d7584cSLiam Girdwood rtd->ops.hw_free = soc_pcm_hw_free; 295401d7584cSLiam Girdwood rtd->ops.close = soc_pcm_close; 295501d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 295601d7584cSLiam Girdwood } 295701d7584cSLiam Girdwood 2958613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 29592b544dd7SKuninori Morimoto const struct snd_soc_component_driver *drv = component->driver; 2960b8135864SKuninori Morimoto 29613b1c952cSTakashi Iwai if (drv->ioctl) 29623b1c952cSTakashi Iwai rtd->ops.ioctl = snd_soc_pcm_component_ioctl; 29631e5ddb6bSTakashi Iwai if (drv->sync_stop) 29641e5ddb6bSTakashi Iwai rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; 2965e9067bb5SKuninori Morimoto if (drv->copy_user) 296682d81f5cSKuninori Morimoto rtd->ops.copy_user = snd_soc_pcm_component_copy_user; 2967e9067bb5SKuninori Morimoto if (drv->page) 29689c712e4fSKuninori Morimoto rtd->ops.page = snd_soc_pcm_component_page; 2969e9067bb5SKuninori Morimoto if (drv->mmap) 2970205875e1SKuninori Morimoto rtd->ops.mmap = snd_soc_pcm_component_mmap; 2971b8135864SKuninori Morimoto } 2972b8135864SKuninori Morimoto 2973ddee627cSLiam Girdwood if (playback) 297401d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 2975ddee627cSLiam Girdwood 2976ddee627cSLiam Girdwood if (capture) 297701d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 2978ddee627cSLiam Girdwood 2979b2b2afbbSKuninori Morimoto ret = snd_soc_pcm_component_new(rtd); 2980ddee627cSLiam Girdwood if (ret < 0) { 29817484291eSKuninori Morimoto dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret); 2982ddee627cSLiam Girdwood return ret; 2983ddee627cSLiam Girdwood } 2984c641e5b2SJohan Hovold 29853d21ef0bSTakashi Iwai pcm->no_device_suspend = true; 298601d7584cSLiam Girdwood out: 29872e5894d7SBenoit Cousson dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", 29882e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, 2989ddee627cSLiam Girdwood cpu_dai->name); 2990ddee627cSLiam Girdwood return ret; 2991ddee627cSLiam Girdwood } 299201d7584cSLiam Girdwood 299301d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */ 299401d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 299501d7584cSLiam Girdwood { 299601d7584cSLiam Girdwood if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 299701d7584cSLiam Girdwood return 1; 299801d7584cSLiam Girdwood return 0; 299901d7584cSLiam Girdwood } 300001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 300101d7584cSLiam Girdwood 300201d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */ 300301d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 300401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 300501d7584cSLiam Girdwood { 300601d7584cSLiam Girdwood if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 300701d7584cSLiam Girdwood ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 300801d7584cSLiam Girdwood be->dpcm[stream].runtime_update)) 300901d7584cSLiam Girdwood return 1; 301001d7584cSLiam Girdwood return 0; 301101d7584cSLiam Girdwood } 301201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 301301d7584cSLiam Girdwood 301401d7584cSLiam Girdwood /* get the substream for this BE */ 301501d7584cSLiam Girdwood struct snd_pcm_substream * 301601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 301701d7584cSLiam Girdwood { 301801d7584cSLiam Girdwood return be->pcm->streams[stream].substream; 301901d7584cSLiam Girdwood } 302001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 302101d7584cSLiam Girdwood 3022085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, 3023085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, 3024085d22beSKuninori Morimoto int stream, 3025085d22beSKuninori Morimoto const enum snd_soc_dpcm_state *states, 3026085d22beSKuninori Morimoto int num_states) 302701d7584cSLiam Girdwood { 302801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 302901d7584cSLiam Girdwood int state; 3030a9764869SKaiChieh Chuang int ret = 1; 3031a9764869SKaiChieh Chuang unsigned long flags; 3032085d22beSKuninori Morimoto int i; 303301d7584cSLiam Girdwood 3034a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3035d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 303601d7584cSLiam Girdwood 303701d7584cSLiam Girdwood if (dpcm->fe == fe) 303801d7584cSLiam Girdwood continue; 303901d7584cSLiam Girdwood 304001d7584cSLiam Girdwood state = dpcm->fe->dpcm[stream].state; 3041085d22beSKuninori Morimoto for (i = 0; i < num_states; i++) { 3042085d22beSKuninori Morimoto if (state == states[i]) { 3043a9764869SKaiChieh Chuang ret = 0; 3044a9764869SKaiChieh Chuang break; 304501d7584cSLiam Girdwood } 3046a9764869SKaiChieh Chuang } 3047085d22beSKuninori Morimoto } 3048a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 304901d7584cSLiam Girdwood 3050085d22beSKuninori Morimoto /* it's safe to do this BE DAI */ 3051a9764869SKaiChieh Chuang return ret; 305201d7584cSLiam Girdwood } 3053085d22beSKuninori Morimoto 3054085d22beSKuninori Morimoto /* 3055085d22beSKuninori Morimoto * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 3056085d22beSKuninori Morimoto * are not running, paused or suspended for the specified stream direction. 3057085d22beSKuninori Morimoto */ 3058085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 3059085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, int stream) 3060085d22beSKuninori Morimoto { 3061085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3062085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3063085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3064085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3065085d22beSKuninori Morimoto }; 3066085d22beSKuninori Morimoto 3067085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 3068085d22beSKuninori Morimoto } 306901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 307001d7584cSLiam Girdwood 307101d7584cSLiam Girdwood /* 307201d7584cSLiam Girdwood * We can only change hw params a BE DAI if any of it's FE are not prepared, 307301d7584cSLiam Girdwood * running, paused or suspended for the specified stream direction. 307401d7584cSLiam Girdwood */ 307501d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 307601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 307701d7584cSLiam Girdwood { 3078085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3079085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3080085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3081085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3082085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PREPARE, 3083085d22beSKuninori Morimoto }; 308401d7584cSLiam Girdwood 3085085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 308601d7584cSLiam Girdwood } 308701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 3088