1ed517582SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2ed517582SKuninori Morimoto // 3ed517582SKuninori Morimoto // soc-pcm.c -- ALSA SoC PCM 4ed517582SKuninori Morimoto // 5ed517582SKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC. 6ed517582SKuninori Morimoto // Copyright 2005 Openedhand Ltd. 7ed517582SKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd. 8ed517582SKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc. 9ed517582SKuninori Morimoto // 10ed517582SKuninori Morimoto // Authors: Liam Girdwood <lrg@ti.com> 11ed517582SKuninori Morimoto // Mark Brown <broonie@opensource.wolfsonmicro.com> 12ddee627cSLiam Girdwood 13ddee627cSLiam Girdwood #include <linux/kernel.h> 14ddee627cSLiam Girdwood #include <linux/init.h> 15ddee627cSLiam Girdwood #include <linux/delay.h> 16988e8cc4SNicolin Chen #include <linux/pinctrl/consumer.h> 17d6652ef8SMark Brown #include <linux/pm_runtime.h> 18ddee627cSLiam Girdwood #include <linux/slab.h> 19ddee627cSLiam Girdwood #include <linux/workqueue.h> 2001d7584cSLiam Girdwood #include <linux/export.h> 21f86dcef8SLiam Girdwood #include <linux/debugfs.h> 22ddee627cSLiam Girdwood #include <sound/core.h> 23ddee627cSLiam Girdwood #include <sound/pcm.h> 24ddee627cSLiam Girdwood #include <sound/pcm_params.h> 25ddee627cSLiam Girdwood #include <sound/soc.h> 2601d7584cSLiam Girdwood #include <sound/soc-dpcm.h> 27ddee627cSLiam Girdwood #include <sound/initval.h> 28ddee627cSLiam Girdwood 2901d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS 8 3001d7584cSLiam Girdwood 31c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS 32c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state) 33c3212829SKuninori Morimoto { 34c3212829SKuninori Morimoto switch (state) { 35c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_NEW: 36c3212829SKuninori Morimoto return "new"; 37c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_OPEN: 38c3212829SKuninori Morimoto return "open"; 39c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_PARAMS: 40c3212829SKuninori Morimoto return "hw_params"; 41c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PREPARE: 42c3212829SKuninori Morimoto return "prepare"; 43c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_START: 44c3212829SKuninori Morimoto return "start"; 45c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_STOP: 46c3212829SKuninori Morimoto return "stop"; 47c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_SUSPEND: 48c3212829SKuninori Morimoto return "suspend"; 49c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PAUSED: 50c3212829SKuninori Morimoto return "paused"; 51c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_FREE: 52c3212829SKuninori Morimoto return "hw_free"; 53c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_CLOSE: 54c3212829SKuninori Morimoto return "close"; 55c3212829SKuninori Morimoto } 56c3212829SKuninori Morimoto 57c3212829SKuninori Morimoto return "unknown"; 58c3212829SKuninori Morimoto } 59c3212829SKuninori Morimoto 60c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 61c3212829SKuninori Morimoto int stream, char *buf, size_t size) 62c3212829SKuninori Morimoto { 63c3212829SKuninori Morimoto struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 64c3212829SKuninori Morimoto struct snd_soc_dpcm *dpcm; 65c3212829SKuninori Morimoto ssize_t offset = 0; 66c3212829SKuninori Morimoto unsigned long flags; 67c3212829SKuninori Morimoto 68c3212829SKuninori Morimoto /* FE state */ 69d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 70c3212829SKuninori Morimoto "[%s - %s]\n", fe->dai_link->name, 71c3212829SKuninori Morimoto stream ? "Capture" : "Playback"); 72c3212829SKuninori Morimoto 73d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "State: %s\n", 74c3212829SKuninori Morimoto dpcm_state_string(fe->dpcm[stream].state)); 75c3212829SKuninori Morimoto 76c3212829SKuninori Morimoto if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 77c3212829SKuninori Morimoto (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 78d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 79c3212829SKuninori Morimoto "Hardware Params: " 80c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 81c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 82c3212829SKuninori Morimoto params_channels(params), 83c3212829SKuninori Morimoto params_rate(params)); 84c3212829SKuninori Morimoto 85c3212829SKuninori Morimoto /* BEs state */ 86d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "Backends:\n"); 87c3212829SKuninori Morimoto 88c3212829SKuninori Morimoto if (list_empty(&fe->dpcm[stream].be_clients)) { 89d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 90c3212829SKuninori Morimoto " No active DSP links\n"); 91c3212829SKuninori Morimoto goto out; 92c3212829SKuninori Morimoto } 93c3212829SKuninori Morimoto 94c3212829SKuninori Morimoto spin_lock_irqsave(&fe->card->dpcm_lock, flags); 95c3212829SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 96c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 97c3212829SKuninori Morimoto params = &dpcm->hw_params; 98c3212829SKuninori Morimoto 99d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 100c3212829SKuninori Morimoto "- %s\n", be->dai_link->name); 101c3212829SKuninori Morimoto 102d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 103c3212829SKuninori Morimoto " State: %s\n", 104c3212829SKuninori Morimoto dpcm_state_string(be->dpcm[stream].state)); 105c3212829SKuninori Morimoto 106c3212829SKuninori Morimoto if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 107c3212829SKuninori Morimoto (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 108d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, 109c3212829SKuninori Morimoto " Hardware Params: " 110c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 111c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 112c3212829SKuninori Morimoto params_channels(params), 113c3212829SKuninori Morimoto params_rate(params)); 114c3212829SKuninori Morimoto } 115c3212829SKuninori Morimoto spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 116c3212829SKuninori Morimoto out: 117c3212829SKuninori Morimoto return offset; 118c3212829SKuninori Morimoto } 119c3212829SKuninori Morimoto 120c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 121c3212829SKuninori Morimoto size_t count, loff_t *ppos) 122c3212829SKuninori Morimoto { 123c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *fe = file->private_data; 124c3212829SKuninori Morimoto ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 125c3212829SKuninori Morimoto int stream; 126c3212829SKuninori Morimoto char *buf; 127c3212829SKuninori Morimoto 1286e1276a5SBard Liao if (fe->num_cpus > 1) { 1296e1276a5SBard Liao dev_err(fe->dev, 1306e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 1316e1276a5SBard Liao return -EINVAL; 1326e1276a5SBard Liao } 1336e1276a5SBard Liao 134c3212829SKuninori Morimoto buf = kmalloc(out_count, GFP_KERNEL); 135c3212829SKuninori Morimoto if (!buf) 136c3212829SKuninori Morimoto return -ENOMEM; 137c3212829SKuninori Morimoto 138c3212829SKuninori Morimoto for_each_pcm_streams(stream) 139c2233a26SKuninori Morimoto if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream)) 140c3212829SKuninori Morimoto offset += dpcm_show_state(fe, stream, 141c3212829SKuninori Morimoto buf + offset, 142c3212829SKuninori Morimoto out_count - offset); 143c3212829SKuninori Morimoto 144c3212829SKuninori Morimoto ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 145c3212829SKuninori Morimoto 146c3212829SKuninori Morimoto kfree(buf); 147c3212829SKuninori Morimoto return ret; 148c3212829SKuninori Morimoto } 149c3212829SKuninori Morimoto 150c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = { 151c3212829SKuninori Morimoto .open = simple_open, 152c3212829SKuninori Morimoto .read = dpcm_state_read_file, 153c3212829SKuninori Morimoto .llseek = default_llseek, 154c3212829SKuninori Morimoto }; 155c3212829SKuninori Morimoto 156c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 157c3212829SKuninori Morimoto { 158c3212829SKuninori Morimoto if (!rtd->dai_link) 159c3212829SKuninori Morimoto return; 160c3212829SKuninori Morimoto 161c3212829SKuninori Morimoto if (!rtd->dai_link->dynamic) 162c3212829SKuninori Morimoto return; 163c3212829SKuninori Morimoto 164c3212829SKuninori Morimoto if (!rtd->card->debugfs_card_root) 165c3212829SKuninori Morimoto return; 166c3212829SKuninori Morimoto 167c3212829SKuninori Morimoto rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 168c3212829SKuninori Morimoto rtd->card->debugfs_card_root); 169c3212829SKuninori Morimoto 170c3212829SKuninori Morimoto debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 171c3212829SKuninori Morimoto rtd, &dpcm_state_fops); 172c3212829SKuninori Morimoto } 173154dae87SKuninori Morimoto 174154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream) 175154dae87SKuninori Morimoto { 176154dae87SKuninori Morimoto char *name; 177154dae87SKuninori Morimoto 178154dae87SKuninori Morimoto name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name, 179154dae87SKuninori Morimoto stream ? "capture" : "playback"); 180154dae87SKuninori Morimoto if (name) { 181154dae87SKuninori Morimoto dpcm->debugfs_state = debugfs_create_dir( 182154dae87SKuninori Morimoto name, dpcm->fe->debugfs_dpcm_root); 183154dae87SKuninori Morimoto debugfs_create_u32("state", 0644, dpcm->debugfs_state, 184154dae87SKuninori Morimoto &dpcm->state); 185154dae87SKuninori Morimoto kfree(name); 186154dae87SKuninori Morimoto } 187154dae87SKuninori Morimoto } 188154dae87SKuninori Morimoto 189154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 190154dae87SKuninori Morimoto { 191154dae87SKuninori Morimoto debugfs_remove_recursive(dpcm->debugfs_state); 192154dae87SKuninori Morimoto } 193154dae87SKuninori Morimoto 194154dae87SKuninori Morimoto #else 195154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, 196154dae87SKuninori Morimoto int stream) 197154dae87SKuninori Morimoto { 198154dae87SKuninori Morimoto } 199154dae87SKuninori Morimoto 200154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm) 201154dae87SKuninori Morimoto { 202154dae87SKuninori Morimoto } 203c3212829SKuninori Morimoto #endif 204c3212829SKuninori Morimoto 205f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd, 206f183f927SKuninori Morimoto struct snd_pcm_substream *substream) 207f183f927SKuninori Morimoto { 208f183f927SKuninori Morimoto if (rtd->dai_link->ops && 209f183f927SKuninori Morimoto rtd->dai_link->ops->startup) 210f183f927SKuninori Morimoto return rtd->dai_link->ops->startup(substream); 211f183f927SKuninori Morimoto return 0; 212f183f927SKuninori Morimoto } 213f183f927SKuninori Morimoto 2140be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd, 2150be429f9SKuninori Morimoto struct snd_pcm_substream *substream) 2160be429f9SKuninori Morimoto { 2170be429f9SKuninori Morimoto if (rtd->dai_link->ops && 2180be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown) 2190be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown(substream); 2200be429f9SKuninori Morimoto } 2210be429f9SKuninori Morimoto 22244c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd, 22344c1a75bSKuninori Morimoto struct snd_pcm_substream *substream) 22444c1a75bSKuninori Morimoto { 22544c1a75bSKuninori Morimoto if (rtd->dai_link->ops && 22644c1a75bSKuninori Morimoto rtd->dai_link->ops->prepare) 22744c1a75bSKuninori Morimoto return rtd->dai_link->ops->prepare(substream); 22844c1a75bSKuninori Morimoto return 0; 22944c1a75bSKuninori Morimoto } 23044c1a75bSKuninori Morimoto 231de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd, 232de9ad990SKuninori Morimoto struct snd_pcm_substream *substream, 233de9ad990SKuninori Morimoto struct snd_pcm_hw_params *params) 234de9ad990SKuninori Morimoto { 235de9ad990SKuninori Morimoto if (rtd->dai_link->ops && 236de9ad990SKuninori Morimoto rtd->dai_link->ops->hw_params) 237de9ad990SKuninori Morimoto return rtd->dai_link->ops->hw_params(substream, params); 238de9ad990SKuninori Morimoto return 0; 239de9ad990SKuninori Morimoto } 240de9ad990SKuninori Morimoto 24149f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd, 24249f020e5SKuninori Morimoto struct snd_pcm_substream *substream) 24349f020e5SKuninori Morimoto { 24449f020e5SKuninori Morimoto if (rtd->dai_link->ops && 24549f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free) 24649f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free(substream); 24749f020e5SKuninori Morimoto } 24849f020e5SKuninori Morimoto 249ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd, 250ad2bf9f2SKuninori Morimoto struct snd_pcm_substream *substream, 251ad2bf9f2SKuninori Morimoto int cmd) 252ad2bf9f2SKuninori Morimoto { 253ad2bf9f2SKuninori Morimoto if (rtd->dai_link->ops && 254ad2bf9f2SKuninori Morimoto rtd->dai_link->ops->trigger) 255ad2bf9f2SKuninori Morimoto return rtd->dai_link->ops->trigger(substream, cmd); 256ad2bf9f2SKuninori Morimoto return 0; 257ad2bf9f2SKuninori Morimoto } 258ad2bf9f2SKuninori Morimoto 259d9051d86SKuninori Morimoto /** 260d9051d86SKuninori Morimoto * snd_soc_runtime_action() - Increment/Decrement active count for 261d9051d86SKuninori Morimoto * PCM runtime components 262d9051d86SKuninori Morimoto * @rtd: ASoC PCM runtime that is activated 263d9051d86SKuninori Morimoto * @stream: Direction of the PCM stream 264d9051d86SKuninori Morimoto * 265d9051d86SKuninori Morimoto * Increments/Decrements the active count for all the DAIs and components 266d9051d86SKuninori Morimoto * attached to a PCM runtime. 267d9051d86SKuninori Morimoto * Should typically be called when a stream is opened. 268d9051d86SKuninori Morimoto * 269d9051d86SKuninori Morimoto * Must be called with the rtd->card->pcm_mutex being held 270d9051d86SKuninori Morimoto */ 271d9051d86SKuninori Morimoto void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, 2727a5aaba4SKuninori Morimoto int stream, int action) 2737a5aaba4SKuninori Morimoto { 274c840f769SKuninori Morimoto struct snd_soc_dai *dai; 2757a5aaba4SKuninori Morimoto int i; 2767a5aaba4SKuninori Morimoto 2777a5aaba4SKuninori Morimoto lockdep_assert_held(&rtd->card->pcm_mutex); 2787a5aaba4SKuninori Morimoto 279dc829106SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 280dc829106SKuninori Morimoto snd_soc_dai_action(dai, stream, action); 2817a5aaba4SKuninori Morimoto } 282d9051d86SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_action); 28324894b76SLars-Peter Clausen 28424894b76SLars-Peter Clausen /** 285208a1589SLars-Peter Clausen * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 286208a1589SLars-Peter Clausen * @rtd: The ASoC PCM runtime that should be checked. 287208a1589SLars-Peter Clausen * 288208a1589SLars-Peter Clausen * This function checks whether the power down delay should be ignored for a 289208a1589SLars-Peter Clausen * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 290208a1589SLars-Peter Clausen * been configured to ignore the delay, or if none of the components benefits 291208a1589SLars-Peter Clausen * from having the delay. 292208a1589SLars-Peter Clausen */ 293208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 294208a1589SLars-Peter Clausen { 295fbb16563SKuninori Morimoto struct snd_soc_component *component; 2962e5894d7SBenoit Cousson bool ignore = true; 297613fb500SKuninori Morimoto int i; 2982e5894d7SBenoit Cousson 299208a1589SLars-Peter Clausen if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 300208a1589SLars-Peter Clausen return true; 301208a1589SLars-Peter Clausen 302613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 30372c38184SKuninori Morimoto ignore &= !component->driver->use_pmdown_time; 304fbb16563SKuninori Morimoto 305fbb16563SKuninori Morimoto return ignore; 306208a1589SLars-Peter Clausen } 307208a1589SLars-Peter Clausen 308208a1589SLars-Peter Clausen /** 30990996f43SLars-Peter Clausen * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 31090996f43SLars-Peter Clausen * @substream: the pcm substream 31190996f43SLars-Peter Clausen * @hw: the hardware parameters 31290996f43SLars-Peter Clausen * 31390996f43SLars-Peter Clausen * Sets the substream runtime hardware parameters. 31490996f43SLars-Peter Clausen */ 31590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 31690996f43SLars-Peter Clausen const struct snd_pcm_hardware *hw) 31790996f43SLars-Peter Clausen { 31890996f43SLars-Peter Clausen struct snd_pcm_runtime *runtime = substream->runtime; 31990996f43SLars-Peter Clausen runtime->hw.info = hw->info; 32090996f43SLars-Peter Clausen runtime->hw.formats = hw->formats; 32190996f43SLars-Peter Clausen runtime->hw.period_bytes_min = hw->period_bytes_min; 32290996f43SLars-Peter Clausen runtime->hw.period_bytes_max = hw->period_bytes_max; 32390996f43SLars-Peter Clausen runtime->hw.periods_min = hw->periods_min; 32490996f43SLars-Peter Clausen runtime->hw.periods_max = hw->periods_max; 32590996f43SLars-Peter Clausen runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 32690996f43SLars-Peter Clausen runtime->hw.fifo_size = hw->fifo_size; 32790996f43SLars-Peter Clausen return 0; 32890996f43SLars-Peter Clausen } 32990996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 33090996f43SLars-Peter Clausen 33101d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */ 33223607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 33301d7584cSLiam Girdwood int event) 33401d7584cSLiam Girdwood { 33501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 33601d7584cSLiam Girdwood 3378d6258a4SKuninori Morimoto for_each_dpcm_be(fe, dir, dpcm) { 33801d7584cSLiam Girdwood 33901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 34001d7584cSLiam Girdwood 341103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 34201d7584cSLiam Girdwood be->dai_link->name, event, dir); 34301d7584cSLiam Girdwood 344b1cd2e34SBanajit Goswami if ((event == SND_SOC_DAPM_STREAM_STOP) && 345b1cd2e34SBanajit Goswami (be->dpcm[dir].users >= 1)) 346b1cd2e34SBanajit Goswami continue; 347b1cd2e34SBanajit Goswami 34801d7584cSLiam Girdwood snd_soc_dapm_stream_event(be, dir, event); 34901d7584cSLiam Girdwood } 35001d7584cSLiam Girdwood 35101d7584cSLiam Girdwood snd_soc_dapm_stream_event(fe, dir, event); 35201d7584cSLiam Girdwood 35301d7584cSLiam Girdwood return 0; 35401d7584cSLiam Girdwood } 35501d7584cSLiam Girdwood 35617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 35717841020SDong Aisheng struct snd_soc_dai *soc_dai) 358ddee627cSLiam Girdwood { 359ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 360ddee627cSLiam Girdwood int ret; 361ddee627cSLiam Girdwood 3623635bf09SNicolin Chen if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 3633635bf09SNicolin Chen rtd->dai_link->symmetric_rates)) { 3643635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 3653635bf09SNicolin Chen soc_dai->rate); 366ddee627cSLiam Girdwood 3674dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 368ddee627cSLiam Girdwood SNDRV_PCM_HW_PARAM_RATE, 3694dcdd43bSLars-Peter Clausen soc_dai->rate); 370ddee627cSLiam Girdwood if (ret < 0) { 37117841020SDong Aisheng dev_err(soc_dai->dev, 3723635bf09SNicolin Chen "ASoC: Unable to apply rate constraint: %d\n", 373103d84a3SLiam Girdwood ret); 374ddee627cSLiam Girdwood return ret; 375ddee627cSLiam Girdwood } 3763635bf09SNicolin Chen } 3773635bf09SNicolin Chen 3783635bf09SNicolin Chen if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 3793635bf09SNicolin Chen rtd->dai_link->symmetric_channels)) { 3803635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 3813635bf09SNicolin Chen soc_dai->channels); 3823635bf09SNicolin Chen 3834dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 3843635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_CHANNELS, 3853635bf09SNicolin Chen soc_dai->channels); 3863635bf09SNicolin Chen if (ret < 0) { 3873635bf09SNicolin Chen dev_err(soc_dai->dev, 3883635bf09SNicolin Chen "ASoC: Unable to apply channel symmetry constraint: %d\n", 3893635bf09SNicolin Chen ret); 3903635bf09SNicolin Chen return ret; 3913635bf09SNicolin Chen } 3923635bf09SNicolin Chen } 3933635bf09SNicolin Chen 3943635bf09SNicolin Chen if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 3953635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits)) { 3963635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 3973635bf09SNicolin Chen soc_dai->sample_bits); 3983635bf09SNicolin Chen 3994dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 4003635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 4013635bf09SNicolin Chen soc_dai->sample_bits); 4023635bf09SNicolin Chen if (ret < 0) { 4033635bf09SNicolin Chen dev_err(soc_dai->dev, 4043635bf09SNicolin Chen "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 4053635bf09SNicolin Chen ret); 4063635bf09SNicolin Chen return ret; 4073635bf09SNicolin Chen } 4083635bf09SNicolin Chen } 409ddee627cSLiam Girdwood 410ddee627cSLiam Girdwood return 0; 411ddee627cSLiam Girdwood } 412ddee627cSLiam Girdwood 4133635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 4143635bf09SNicolin Chen struct snd_pcm_hw_params *params) 4153635bf09SNicolin Chen { 4163635bf09SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 417c840f769SKuninori Morimoto struct snd_soc_dai *dai; 41819bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 4192e5894d7SBenoit Cousson unsigned int rate, channels, sample_bits, symmetry, i; 4203635bf09SNicolin Chen 4213635bf09SNicolin Chen rate = params_rate(params); 4223635bf09SNicolin Chen channels = params_channels(params); 4233635bf09SNicolin Chen sample_bits = snd_pcm_format_physical_width(params_format(params)); 4243635bf09SNicolin Chen 4253635bf09SNicolin Chen /* reject unmatched parameters when applying symmetry */ 42619bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_rates; 42719bdcc7aSShreyas NC 428c840f769SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, dai) 429c840f769SKuninori Morimoto symmetry |= dai->driver->symmetric_rates; 4302e5894d7SBenoit Cousson 43119bdcc7aSShreyas NC if (symmetry) { 432a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 43319bdcc7aSShreyas NC if (cpu_dai->rate && cpu_dai->rate != rate) { 4343635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 4353635bf09SNicolin Chen cpu_dai->rate, rate); 4363635bf09SNicolin Chen return -EINVAL; 4373635bf09SNicolin Chen } 43819bdcc7aSShreyas NC } 43919bdcc7aSShreyas NC } 4403635bf09SNicolin Chen 44119bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_channels; 44219bdcc7aSShreyas NC 443c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 444c840f769SKuninori Morimoto symmetry |= dai->driver->symmetric_channels; 4452e5894d7SBenoit Cousson 44619bdcc7aSShreyas NC if (symmetry) { 447a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 44819bdcc7aSShreyas NC if (cpu_dai->channels && 44919bdcc7aSShreyas NC cpu_dai->channels != channels) { 4503635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 4513635bf09SNicolin Chen cpu_dai->channels, channels); 4523635bf09SNicolin Chen return -EINVAL; 4533635bf09SNicolin Chen } 45419bdcc7aSShreyas NC } 45519bdcc7aSShreyas NC } 4563635bf09SNicolin Chen 45719bdcc7aSShreyas NC symmetry = rtd->dai_link->symmetric_samplebits; 45819bdcc7aSShreyas NC 459c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 460c840f769SKuninori Morimoto symmetry |= dai->driver->symmetric_samplebits; 4612e5894d7SBenoit Cousson 46219bdcc7aSShreyas NC if (symmetry) { 463a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 46419bdcc7aSShreyas NC if (cpu_dai->sample_bits && 46519bdcc7aSShreyas NC cpu_dai->sample_bits != sample_bits) { 4663635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 4673635bf09SNicolin Chen cpu_dai->sample_bits, sample_bits); 4683635bf09SNicolin Chen return -EINVAL; 4693635bf09SNicolin Chen } 47019bdcc7aSShreyas NC } 47119bdcc7aSShreyas NC } 472ddee627cSLiam Girdwood 473ddee627cSLiam Girdwood return 0; 474ddee627cSLiam Girdwood } 475ddee627cSLiam Girdwood 47662e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 47762e5f676SLars-Peter Clausen { 47862e5f676SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = substream->private_data; 47962e5f676SLars-Peter Clausen struct snd_soc_dai_link *link = rtd->dai_link; 480c840f769SKuninori Morimoto struct snd_soc_dai *dai; 4812e5894d7SBenoit Cousson unsigned int symmetry, i; 48262e5f676SLars-Peter Clausen 48319bdcc7aSShreyas NC symmetry = link->symmetric_rates || 48419bdcc7aSShreyas NC link->symmetric_channels || 48519bdcc7aSShreyas NC link->symmetric_samplebits; 48619bdcc7aSShreyas NC 487c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 48819bdcc7aSShreyas NC symmetry = symmetry || 489c840f769SKuninori Morimoto dai->driver->symmetric_rates || 490c840f769SKuninori Morimoto dai->driver->symmetric_channels || 491c840f769SKuninori Morimoto dai->driver->symmetric_samplebits; 4922e5894d7SBenoit Cousson 4932e5894d7SBenoit Cousson return symmetry; 49462e5f676SLars-Peter Clausen } 49562e5f676SLars-Peter Clausen 4962e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 49758ba9b25SMark Brown { 4982e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 499c6068d3aSTakashi Iwai int ret; 50058ba9b25SMark Brown 50158ba9b25SMark Brown if (!bits) 50258ba9b25SMark Brown return; 50358ba9b25SMark Brown 5040e2a3751SLars-Peter Clausen ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 50558ba9b25SMark Brown if (ret != 0) 5060e2a3751SLars-Peter Clausen dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 5070e2a3751SLars-Peter Clausen bits, ret); 50858ba9b25SMark Brown } 50958ba9b25SMark Brown 510c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 511bd477c31SLars-Peter Clausen { 512c8dd1fecSBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 51319bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 5142e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 51557be9206SKuninori Morimoto struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; 51657be9206SKuninori Morimoto int stream = substream->stream; 5172e5894d7SBenoit Cousson int i; 51819bdcc7aSShreyas NC unsigned int bits = 0, cpu_bits = 0; 519c8dd1fecSBenoit Cousson 520a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 52157be9206SKuninori Morimoto pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); 52257be9206SKuninori Morimoto 52357be9206SKuninori Morimoto if (pcm_codec->sig_bits == 0) { 5242e5894d7SBenoit Cousson bits = 0; 5252e5894d7SBenoit Cousson break; 5262e5894d7SBenoit Cousson } 52757be9206SKuninori Morimoto bits = max(pcm_codec->sig_bits, bits); 5282e5894d7SBenoit Cousson } 52957be9206SKuninori Morimoto 530a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 53157be9206SKuninori Morimoto pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 53219bdcc7aSShreyas NC 53319bdcc7aSShreyas NC if (pcm_cpu->sig_bits == 0) { 53419bdcc7aSShreyas NC cpu_bits = 0; 53519bdcc7aSShreyas NC break; 53619bdcc7aSShreyas NC } 53719bdcc7aSShreyas NC cpu_bits = max(pcm_cpu->sig_bits, cpu_bits); 53819bdcc7aSShreyas NC } 539c8dd1fecSBenoit Cousson 5402e5894d7SBenoit Cousson soc_pcm_set_msb(substream, bits); 5412e5894d7SBenoit Cousson soc_pcm_set_msb(substream, cpu_bits); 542c8dd1fecSBenoit Cousson } 543c8dd1fecSBenoit Cousson 5445854a464SSamuel Holland /** 5455854a464SSamuel Holland * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream 5465854a464SSamuel Holland * @rtd: ASoC PCM runtime 5475854a464SSamuel Holland * @hw: PCM hardware parameters (output) 5485854a464SSamuel Holland * @stream: Direction of the PCM stream 5495854a464SSamuel Holland * 5505854a464SSamuel Holland * Calculates the subset of stream parameters supported by all DAIs 5515854a464SSamuel Holland * associated with the PCM stream. 5525854a464SSamuel Holland */ 5535854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd, 5545854a464SSamuel Holland struct snd_pcm_hardware *hw, int stream) 555bd477c31SLars-Peter Clausen { 5560b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 55719bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 5582e5894d7SBenoit Cousson struct snd_soc_pcm_stream *codec_stream; 5592e5894d7SBenoit Cousson struct snd_soc_pcm_stream *cpu_stream; 5602e5894d7SBenoit Cousson unsigned int chan_min = 0, chan_max = UINT_MAX; 56119bdcc7aSShreyas NC unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX; 5622e5894d7SBenoit Cousson unsigned int rate_min = 0, rate_max = UINT_MAX; 56319bdcc7aSShreyas NC unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX; 56419bdcc7aSShreyas NC unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX; 5652e5894d7SBenoit Cousson u64 formats = ULLONG_MAX; 5662e5894d7SBenoit Cousson int i; 56778e45c99SLars-Peter Clausen 56819bdcc7aSShreyas NC /* first calculate min/max only for CPUs in the DAI link */ 569a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 5700e9cf4c4SBard Liao 5710e9cf4c4SBard Liao /* 5720e9cf4c4SBard Liao * Skip CPUs which don't support the current stream type. 5730e9cf4c4SBard Liao * Otherwise, since the rate, channel, and format values will 5740e9cf4c4SBard Liao * zero in that case, we would have no usable settings left, 5750e9cf4c4SBard Liao * causing the resulting setup to fail. 5760e9cf4c4SBard Liao */ 5775854a464SSamuel Holland if (!snd_soc_dai_stream_valid(cpu_dai, stream)) 5780e9cf4c4SBard Liao continue; 5790e9cf4c4SBard Liao 58019bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 58178e45c99SLars-Peter Clausen 58219bdcc7aSShreyas NC cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min); 58319bdcc7aSShreyas NC cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max); 58419bdcc7aSShreyas NC cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min); 58519bdcc7aSShreyas NC cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max); 58619bdcc7aSShreyas NC formats &= cpu_stream->formats; 58719bdcc7aSShreyas NC cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates, 58819bdcc7aSShreyas NC cpu_rates); 58919bdcc7aSShreyas NC } 59019bdcc7aSShreyas NC 59119bdcc7aSShreyas NC /* second calculate min/max only for CODECs in the DAI link */ 592a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 593cde79035SRicard Wanderlof 594cde79035SRicard Wanderlof /* 595cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type. 596cde79035SRicard Wanderlof * Otherwise, since the rate, channel, and format values will 597cde79035SRicard Wanderlof * zero in that case, we would have no usable settings left, 598cde79035SRicard Wanderlof * causing the resulting setup to fail. 599cde79035SRicard Wanderlof */ 60025c2f515SKuninori Morimoto if (!snd_soc_dai_stream_valid(codec_dai, stream)) 601cde79035SRicard Wanderlof continue; 602cde79035SRicard Wanderlof 603acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); 604acf253c1SKuninori Morimoto 6052e5894d7SBenoit Cousson chan_min = max(chan_min, codec_stream->channels_min); 6062e5894d7SBenoit Cousson chan_max = min(chan_max, codec_stream->channels_max); 6072e5894d7SBenoit Cousson rate_min = max(rate_min, codec_stream->rate_min); 6082e5894d7SBenoit Cousson rate_max = min_not_zero(rate_max, codec_stream->rate_max); 6092e5894d7SBenoit Cousson formats &= codec_stream->formats; 6102e5894d7SBenoit Cousson rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); 6112e5894d7SBenoit Cousson } 6122e5894d7SBenoit Cousson 6135854a464SSamuel Holland /* Verify both a valid CPU DAI and a valid CODEC DAI were found */ 6145854a464SSamuel Holland if (!chan_min || !cpu_chan_min) 6155854a464SSamuel Holland return -EINVAL; 6165854a464SSamuel Holland 6172e5894d7SBenoit Cousson /* 6182e5894d7SBenoit Cousson * chan min/max cannot be enforced if there are multiple CODEC DAIs 61919bdcc7aSShreyas NC * connected to CPU DAI(s), use CPU DAI's directly and let 6202e5894d7SBenoit Cousson * channel allocation be fixed up later 6212e5894d7SBenoit Cousson */ 6222e5894d7SBenoit Cousson if (rtd->num_codecs > 1) { 62319bdcc7aSShreyas NC chan_min = cpu_chan_min; 62419bdcc7aSShreyas NC chan_max = cpu_chan_max; 6252e5894d7SBenoit Cousson } 6262e5894d7SBenoit Cousson 62719bdcc7aSShreyas NC /* finally find a intersection between CODECs and CPUs */ 62819bdcc7aSShreyas NC hw->channels_min = max(chan_min, cpu_chan_min); 62919bdcc7aSShreyas NC hw->channels_max = min(chan_max, cpu_chan_max); 63019bdcc7aSShreyas NC hw->formats = formats; 63119bdcc7aSShreyas NC hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates); 63278e45c99SLars-Peter Clausen 6335854a464SSamuel Holland snd_pcm_hw_limit_rates(hw); 63478e45c99SLars-Peter Clausen 63519bdcc7aSShreyas NC hw->rate_min = max(hw->rate_min, cpu_rate_min); 6362e5894d7SBenoit Cousson hw->rate_min = max(hw->rate_min, rate_min); 63719bdcc7aSShreyas NC hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max); 6382e5894d7SBenoit Cousson hw->rate_max = min_not_zero(hw->rate_max, rate_max); 6395854a464SSamuel Holland 6405854a464SSamuel Holland return 0; 6415854a464SSamuel Holland } 6425854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw); 6435854a464SSamuel Holland 6445854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 6455854a464SSamuel Holland { 6465854a464SSamuel Holland struct snd_pcm_hardware *hw = &substream->runtime->hw; 6475854a464SSamuel Holland struct snd_soc_pcm_runtime *rtd = substream->private_data; 6485854a464SSamuel Holland u64 formats = hw->formats; 6495854a464SSamuel Holland 6505854a464SSamuel Holland /* 6515854a464SSamuel Holland * At least one CPU and one CODEC should match. Otherwise, we should 6525854a464SSamuel Holland * have bailed out on a higher level, since there would be no CPU or 6535854a464SSamuel Holland * CODEC to support the transfer direction in that case. 6545854a464SSamuel Holland */ 6555854a464SSamuel Holland snd_soc_runtime_calc_hw(rtd, hw, substream->stream); 6565854a464SSamuel Holland 6575854a464SSamuel Holland if (formats) 6585854a464SSamuel Holland hw->formats &= formats; 659bd477c31SLars-Peter Clausen } 660bd477c31SLars-Peter Clausen 661dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream) 662e7ecfdb7SKuninori Morimoto { 663e7ecfdb7SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 664d2aaa8d8SKai Vehmanen struct snd_soc_component *last = NULL; 665e7ecfdb7SKuninori Morimoto struct snd_soc_component *component; 666613fb500SKuninori Morimoto int i, ret = 0; 667e7ecfdb7SKuninori Morimoto 668613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 669d2aaa8d8SKai Vehmanen last = component; 670d2aaa8d8SKai Vehmanen 6714a81e8f3SKuninori Morimoto ret = snd_soc_component_module_get_when_open(component); 6724a81e8f3SKuninori Morimoto if (ret < 0) { 673e7ecfdb7SKuninori Morimoto dev_err(component->dev, 674e7ecfdb7SKuninori Morimoto "ASoC: can't get module %s\n", 675e7ecfdb7SKuninori Morimoto component->name); 676d2aaa8d8SKai Vehmanen break; 677e7ecfdb7SKuninori Morimoto } 678e7ecfdb7SKuninori Morimoto 679ae2f4849SKuninori Morimoto ret = snd_soc_component_open(component, substream); 680e7ecfdb7SKuninori Morimoto if (ret < 0) { 681d2aaa8d8SKai Vehmanen snd_soc_component_module_put_when_close(component); 682e7ecfdb7SKuninori Morimoto dev_err(component->dev, 683e7ecfdb7SKuninori Morimoto "ASoC: can't open component %s: %d\n", 684e7ecfdb7SKuninori Morimoto component->name, ret); 685d2aaa8d8SKai Vehmanen break; 686e7ecfdb7SKuninori Morimoto } 687e7ecfdb7SKuninori Morimoto } 688dd03907bSKuninori Morimoto 689d2aaa8d8SKai Vehmanen if (ret < 0) { 690d2aaa8d8SKai Vehmanen /* rollback on error */ 691d2aaa8d8SKai Vehmanen for_each_rtd_components(rtd, i, component) { 692d2aaa8d8SKai Vehmanen if (component == last) 693d2aaa8d8SKai Vehmanen break; 694d2aaa8d8SKai Vehmanen 695d2aaa8d8SKai Vehmanen snd_soc_component_close(component, substream); 696d2aaa8d8SKai Vehmanen snd_soc_component_module_put_when_close(component); 697d2aaa8d8SKai Vehmanen } 698d2aaa8d8SKai Vehmanen } 699d2aaa8d8SKai Vehmanen 700d2aaa8d8SKai Vehmanen return ret; 701e7ecfdb7SKuninori Morimoto } 702e7ecfdb7SKuninori Morimoto 703dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream) 704244e2936SCharles Keepax { 705244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 706244e2936SCharles Keepax struct snd_soc_component *component; 707e82ebffcSKuninori Morimoto int i, r, ret = 0; 708244e2936SCharles Keepax 709613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 710e82ebffcSKuninori Morimoto r = snd_soc_component_close(component, substream); 711e82ebffcSKuninori Morimoto if (r < 0) 712e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 713e82ebffcSKuninori Morimoto 7144a81e8f3SKuninori Morimoto snd_soc_component_module_put_when_close(component); 715244e2936SCharles Keepax } 716244e2936SCharles Keepax 7173672beb8SKuninori Morimoto return ret; 718244e2936SCharles Keepax } 719244e2936SCharles Keepax 72058ba9b25SMark Brown /* 72162c86d1dSKuninori Morimoto * Called by ALSA when a PCM substream is closed. Private data can be 72262c86d1dSKuninori Morimoto * freed here. The cpu DAI, codec DAI, machine and components are also 72362c86d1dSKuninori Morimoto * shutdown. 72462c86d1dSKuninori Morimoto */ 72562c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream) 72662c86d1dSKuninori Morimoto { 72762c86d1dSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 72862c86d1dSKuninori Morimoto struct snd_soc_component *component; 729c840f769SKuninori Morimoto struct snd_soc_dai *dai; 73062c86d1dSKuninori Morimoto int i; 73162c86d1dSKuninori Morimoto 73262c86d1dSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 73362c86d1dSKuninori Morimoto 73462c86d1dSKuninori Morimoto snd_soc_runtime_deactivate(rtd, substream->stream); 73562c86d1dSKuninori Morimoto 736c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 737c840f769SKuninori Morimoto snd_soc_dai_shutdown(dai, substream); 73862c86d1dSKuninori Morimoto 73962c86d1dSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 74062c86d1dSKuninori Morimoto 74162c86d1dSKuninori Morimoto soc_pcm_components_close(substream); 74262c86d1dSKuninori Morimoto 74362c86d1dSKuninori Morimoto snd_soc_dapm_stream_stop(rtd, substream->stream); 74462c86d1dSKuninori Morimoto 74562c86d1dSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 74662c86d1dSKuninori Morimoto 74762c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 74862c86d1dSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 74962c86d1dSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 75062c86d1dSKuninori Morimoto } 75162c86d1dSKuninori Morimoto 75262c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) 753*b3dea624SKuninori Morimoto if (!snd_soc_component_active(component)) 75462c86d1dSKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 75562c86d1dSKuninori Morimoto 75662c86d1dSKuninori Morimoto return 0; 75762c86d1dSKuninori Morimoto } 75862c86d1dSKuninori Morimoto 75962c86d1dSKuninori Morimoto /* 760ddee627cSLiam Girdwood * Called by ALSA when a PCM substream is opened, the runtime->hw record is 761ddee627cSLiam Girdwood * then initialized and any private data can be allocated. This also calls 762ef050becSCharles Keepax * startup for the cpu DAI, component, machine and codec DAI. 763ddee627cSLiam Girdwood */ 764ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream) 765ddee627cSLiam Girdwood { 766ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 767ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 76890be711eSKuninori Morimoto struct snd_soc_component *component; 769c840f769SKuninori Morimoto struct snd_soc_dai *dai; 7702e5894d7SBenoit Cousson const char *codec_dai_name = "multicodec"; 77119bdcc7aSShreyas NC const char *cpu_dai_name = "multicpu"; 772244e2936SCharles Keepax int i, ret = 0; 773ddee627cSLiam Girdwood 77476c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 77576c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 77690be711eSKuninori Morimoto 777613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 77890be711eSKuninori Morimoto pm_runtime_get_sync(component->dev); 779d6652ef8SMark Brown 78072b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 781ddee627cSLiam Girdwood 7825d9fa03eSKuninori Morimoto ret = soc_pcm_components_open(substream); 7835d9fa03eSKuninori Morimoto if (ret < 0) 7845d9fa03eSKuninori Morimoto goto component_err; 7855d9fa03eSKuninori Morimoto 7865d9fa03eSKuninori Morimoto ret = soc_rtd_startup(rtd, substream); 7875d9fa03eSKuninori Morimoto if (ret < 0) { 7885d9fa03eSKuninori Morimoto pr_err("ASoC: %s startup failed: %d\n", 7895d9fa03eSKuninori Morimoto rtd->dai_link->name, ret); 790d2aaa8d8SKai Vehmanen goto rtd_startup_err; 7915d9fa03eSKuninori Morimoto } 7925d9fa03eSKuninori Morimoto 793ddee627cSLiam Girdwood /* startup the audio subsystem */ 794c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) { 795c840f769SKuninori Morimoto ret = snd_soc_dai_startup(dai, substream); 796ddee627cSLiam Girdwood if (ret < 0) { 797c840f769SKuninori Morimoto dev_err(dai->dev, 798c840f769SKuninori Morimoto "ASoC: can't open DAI %s: %d\n", 799c840f769SKuninori Morimoto dai->name, ret); 8005d9fa03eSKuninori Morimoto goto config_err; 801ddee627cSLiam Girdwood } 802ddee627cSLiam Girdwood 8032e5894d7SBenoit Cousson if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 804c840f769SKuninori Morimoto dai->tx_mask = 0; 8052e5894d7SBenoit Cousson else 806c840f769SKuninori Morimoto dai->rx_mask = 0; 8072e5894d7SBenoit Cousson } 8082e5894d7SBenoit Cousson 80901d7584cSLiam Girdwood /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 81001d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 81101d7584cSLiam Girdwood goto dynamic; 81201d7584cSLiam Girdwood 813ddee627cSLiam Girdwood /* Check that the codec and cpu DAIs are compatible */ 8142e5894d7SBenoit Cousson soc_pcm_init_runtime_hw(substream); 8152e5894d7SBenoit Cousson 8162e5894d7SBenoit Cousson if (rtd->num_codecs == 1) 817c2233a26SKuninori Morimoto codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name; 818ddee627cSLiam Girdwood 81919bdcc7aSShreyas NC if (rtd->num_cpus == 1) 820c2233a26SKuninori Morimoto cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name; 82119bdcc7aSShreyas NC 82262e5f676SLars-Peter Clausen if (soc_pcm_has_symmetry(substream)) 82362e5f676SLars-Peter Clausen runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 82462e5f676SLars-Peter Clausen 825ddee627cSLiam Girdwood ret = -EINVAL; 826ddee627cSLiam Girdwood if (!runtime->hw.rates) { 827103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 82819bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 829ddee627cSLiam Girdwood goto config_err; 830ddee627cSLiam Girdwood } 831ddee627cSLiam Girdwood if (!runtime->hw.formats) { 832103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 83319bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 834ddee627cSLiam Girdwood goto config_err; 835ddee627cSLiam Girdwood } 836ddee627cSLiam Girdwood if (!runtime->hw.channels_min || !runtime->hw.channels_max || 837ddee627cSLiam Girdwood runtime->hw.channels_min > runtime->hw.channels_max) { 838103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 83919bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 840ddee627cSLiam Girdwood goto config_err; 841ddee627cSLiam Girdwood } 842ddee627cSLiam Girdwood 843c8dd1fecSBenoit Cousson soc_pcm_apply_msb(substream); 84458ba9b25SMark Brown 845ddee627cSLiam Girdwood /* Symmetry only applies if we've already got an active stream. */ 846c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) { 847*b3dea624SKuninori Morimoto if (snd_soc_dai_active(dai)) { 848c840f769SKuninori Morimoto ret = soc_pcm_apply_symmetry(substream, dai); 849ddee627cSLiam Girdwood if (ret != 0) 850ddee627cSLiam Girdwood goto config_err; 851ddee627cSLiam Girdwood } 8522e5894d7SBenoit Cousson } 853ddee627cSLiam Girdwood 854103d84a3SLiam Girdwood pr_debug("ASoC: %s <-> %s info:\n", 85519bdcc7aSShreyas NC codec_dai_name, cpu_dai_name); 856103d84a3SLiam Girdwood pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 857103d84a3SLiam Girdwood pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 858ddee627cSLiam Girdwood runtime->hw.channels_max); 859103d84a3SLiam Girdwood pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 860ddee627cSLiam Girdwood runtime->hw.rate_max); 861ddee627cSLiam Girdwood 86201d7584cSLiam Girdwood dynamic: 86324894b76SLars-Peter Clausen 86424894b76SLars-Peter Clausen snd_soc_runtime_activate(rtd, substream->stream); 86524894b76SLars-Peter Clausen 86672b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 867ddee627cSLiam Girdwood return 0; 868ddee627cSLiam Girdwood 869ddee627cSLiam Girdwood config_err: 870c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 871c840f769SKuninori Morimoto snd_soc_dai_shutdown(dai, substream); 8722e5894d7SBenoit Cousson 8735d9fa03eSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 874d2aaa8d8SKai Vehmanen rtd_startup_err: 875dd03907bSKuninori Morimoto soc_pcm_components_close(substream); 876d2aaa8d8SKai Vehmanen component_err: 87772b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 878d6652ef8SMark Brown 879613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 88090be711eSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 88190be711eSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 8823f809783SSanyog Kale } 8833f809783SSanyog Kale 88476c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 885*b3dea624SKuninori Morimoto if (!snd_soc_component_active(component)) 88676c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 887d6652ef8SMark Brown 888ddee627cSLiam Girdwood return ret; 889ddee627cSLiam Girdwood } 890ddee627cSLiam Girdwood 8914bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 892a342031cSJerome Brunet { 893a342031cSJerome Brunet /* 894a342031cSJerome Brunet * Currently nothing to do for c2c links 895a342031cSJerome Brunet * Since c2c links are internal nodes in the DAPM graph and 896a342031cSJerome Brunet * don't interface with the outside world or application layer 897a342031cSJerome Brunet * we don't have to do any special handling on close. 898a342031cSJerome Brunet */ 899a342031cSJerome Brunet } 900a342031cSJerome Brunet 901ddee627cSLiam Girdwood /* 902ddee627cSLiam Girdwood * Called by ALSA when the PCM substream is prepared, can set format, sample 903ddee627cSLiam Girdwood * rate, etc. This function is non atomic and can be called multiple times, 904ddee627cSLiam Girdwood * it can refer to the runtime info. 905ddee627cSLiam Girdwood */ 906ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream) 907ddee627cSLiam Girdwood { 908ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 909b8135864SKuninori Morimoto struct snd_soc_component *component; 910c840f769SKuninori Morimoto struct snd_soc_dai *dai; 9112e5894d7SBenoit Cousson int i, ret = 0; 912ddee627cSLiam Girdwood 91372b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 914ddee627cSLiam Girdwood 91544c1a75bSKuninori Morimoto ret = soc_rtd_prepare(rtd, substream); 916ddee627cSLiam Girdwood if (ret < 0) { 91744c1a75bSKuninori Morimoto dev_err(rtd->card->dev, 91844c1a75bSKuninori Morimoto "ASoC: machine prepare error: %d\n", ret); 919ddee627cSLiam Girdwood goto out; 920ddee627cSLiam Girdwood } 921ddee627cSLiam Girdwood 922613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 9236d537233SKuninori Morimoto ret = snd_soc_component_prepare(component, substream); 924b8135864SKuninori Morimoto if (ret < 0) { 925b8135864SKuninori Morimoto dev_err(component->dev, 926b8135864SKuninori Morimoto "ASoC: platform prepare error: %d\n", ret); 927b8135864SKuninori Morimoto goto out; 928b8135864SKuninori Morimoto } 929b8135864SKuninori Morimoto } 930b8135864SKuninori Morimoto 931d108c7fdSKuninori Morimoto ret = snd_soc_pcm_dai_prepare(substream); 932ddee627cSLiam Girdwood if (ret < 0) { 933d108c7fdSKuninori Morimoto dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret); 934ddee627cSLiam Girdwood goto out; 935ddee627cSLiam Girdwood } 936ddee627cSLiam Girdwood 937ddee627cSLiam Girdwood /* cancel any delayed stream shutdown that is pending */ 938ddee627cSLiam Girdwood if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 9399bffb1fbSMisael Lopez Cruz rtd->pop_wait) { 9409bffb1fbSMisael Lopez Cruz rtd->pop_wait = 0; 941ddee627cSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 942ddee627cSLiam Girdwood } 943ddee627cSLiam Girdwood 944d9b0951bSLiam Girdwood snd_soc_dapm_stream_event(rtd, substream->stream, 945ddee627cSLiam Girdwood SND_SOC_DAPM_STREAM_START); 946ddee627cSLiam Girdwood 947c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) 948c840f769SKuninori Morimoto snd_soc_dai_digital_mute(dai, 0, substream->stream); 949ddee627cSLiam Girdwood 950ddee627cSLiam Girdwood out: 95172b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 952ddee627cSLiam Girdwood return ret; 953ddee627cSLiam Girdwood } 954ddee627cSLiam Girdwood 9552e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 9562e5894d7SBenoit Cousson unsigned int mask) 9572e5894d7SBenoit Cousson { 9582e5894d7SBenoit Cousson struct snd_interval *interval; 9592e5894d7SBenoit Cousson int channels = hweight_long(mask); 9602e5894d7SBenoit Cousson 9612e5894d7SBenoit Cousson interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 9622e5894d7SBenoit Cousson interval->min = channels; 9632e5894d7SBenoit Cousson interval->max = channels; 9642e5894d7SBenoit Cousson } 9652e5894d7SBenoit Cousson 966244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, 967244e2936SCharles Keepax struct snd_soc_component *last) 968244e2936SCharles Keepax { 969244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 970244e2936SCharles Keepax struct snd_soc_component *component; 971e82ebffcSKuninori Morimoto int i, r, ret = 0; 972244e2936SCharles Keepax 973613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 974244e2936SCharles Keepax if (component == last) 975244e2936SCharles Keepax break; 976244e2936SCharles Keepax 977e82ebffcSKuninori Morimoto r = snd_soc_component_hw_free(component, substream); 978e82ebffcSKuninori Morimoto if (r < 0) 979e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 980244e2936SCharles Keepax } 981244e2936SCharles Keepax 982eae7136aSKuninori Morimoto return ret; 983244e2936SCharles Keepax } 984244e2936SCharles Keepax 985ddee627cSLiam Girdwood /* 986ddee627cSLiam Girdwood * Called by ALSA when the hardware params are set by application. This 987ddee627cSLiam Girdwood * function can also be called multiple times and can allocate buffers 988ddee627cSLiam Girdwood * (using snd_pcm_lib_* ). It's non-atomic. 989ddee627cSLiam Girdwood */ 990ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 991ddee627cSLiam Girdwood struct snd_pcm_hw_params *params) 992ddee627cSLiam Girdwood { 993ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 994b8135864SKuninori Morimoto struct snd_soc_component *component; 99519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 9960b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 997244e2936SCharles Keepax int i, ret = 0; 998ddee627cSLiam Girdwood 99972b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 10005cca5951SShengjiu Wang 10015cca5951SShengjiu Wang ret = soc_pcm_params_symmetry(substream, params); 10025cca5951SShengjiu Wang if (ret) 10035cca5951SShengjiu Wang goto out; 10045cca5951SShengjiu Wang 1005de9ad990SKuninori Morimoto ret = soc_rtd_hw_params(rtd, substream, params); 1006ddee627cSLiam Girdwood if (ret < 0) { 1007de9ad990SKuninori Morimoto dev_err(rtd->card->dev, 1008de9ad990SKuninori Morimoto "ASoC: machine hw_params failed: %d\n", ret); 1009ddee627cSLiam Girdwood goto out; 1010ddee627cSLiam Girdwood } 1011ddee627cSLiam Girdwood 1012a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 10132e5894d7SBenoit Cousson struct snd_pcm_hw_params codec_params; 10142e5894d7SBenoit Cousson 1015cde79035SRicard Wanderlof /* 1016cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type, 1017cde79035SRicard Wanderlof * the idea being that if a CODEC is not used for the currently 1018cde79035SRicard Wanderlof * set up transfer direction, it should not need to be 1019cde79035SRicard Wanderlof * configured, especially since the configuration used might 1020cde79035SRicard Wanderlof * not even be supported by that CODEC. There may be cases 1021cde79035SRicard Wanderlof * however where a CODEC needs to be set up although it is 1022cde79035SRicard Wanderlof * actually not being used for the transfer, e.g. if a 1023cde79035SRicard Wanderlof * capture-only CODEC is acting as an LRCLK and/or BCLK master 1024cde79035SRicard Wanderlof * for the DAI link including a playback-only CODEC. 1025cde79035SRicard Wanderlof * If this becomes necessary, we will have to augment the 1026cde79035SRicard Wanderlof * machine driver setup with information on how to act, so 1027cde79035SRicard Wanderlof * we can do the right thing here. 1028cde79035SRicard Wanderlof */ 1029cde79035SRicard Wanderlof if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1030cde79035SRicard Wanderlof continue; 1031cde79035SRicard Wanderlof 10322e5894d7SBenoit Cousson /* copy params for each codec */ 10332e5894d7SBenoit Cousson codec_params = *params; 10342e5894d7SBenoit Cousson 10352e5894d7SBenoit Cousson /* fixup params based on TDM slot masks */ 1036570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 1037570f18b6SRander Wang codec_dai->tx_mask) 10382e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 10392e5894d7SBenoit Cousson codec_dai->tx_mask); 1040570f18b6SRander Wang 1041570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 1042570f18b6SRander Wang codec_dai->rx_mask) 10432e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 10442e5894d7SBenoit Cousson codec_dai->rx_mask); 10452e5894d7SBenoit Cousson 1046aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(codec_dai, substream, 1047aa6166c2SKuninori Morimoto &codec_params); 104893e6958aSBenoit Cousson if(ret < 0) 1049ddee627cSLiam Girdwood goto codec_err; 1050ddee627cSLiam Girdwood 10512e5894d7SBenoit Cousson codec_dai->rate = params_rate(&codec_params); 10522e5894d7SBenoit Cousson codec_dai->channels = params_channels(&codec_params); 10532e5894d7SBenoit Cousson codec_dai->sample_bits = snd_pcm_format_physical_width( 10542e5894d7SBenoit Cousson params_format(&codec_params)); 1055078a85f2SCharles Keepax 1056078a85f2SCharles Keepax snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); 1057ddee627cSLiam Girdwood } 1058ddee627cSLiam Girdwood 1059a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 10600e9cf4c4SBard Liao /* 10610e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 10620e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 10630e9cf4c4SBard Liao */ 10640e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 10650e9cf4c4SBard Liao continue; 10660e9cf4c4SBard Liao 1067aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(cpu_dai, substream, params); 106893e6958aSBenoit Cousson if (ret < 0) 1069ddee627cSLiam Girdwood goto interface_err; 1070ddee627cSLiam Girdwood 107119bdcc7aSShreyas NC /* store the parameters for each DAI */ 1072ca58221dSKuninori Morimoto cpu_dai->rate = params_rate(params); 1073ca58221dSKuninori Morimoto cpu_dai->channels = params_channels(params); 1074ca58221dSKuninori Morimoto cpu_dai->sample_bits = 1075ca58221dSKuninori Morimoto snd_pcm_format_physical_width(params_format(params)); 1076ca58221dSKuninori Morimoto 1077ca58221dSKuninori Morimoto snd_soc_dapm_update_dai(substream, params, cpu_dai); 107819bdcc7aSShreyas NC } 1079ca58221dSKuninori Morimoto 1080613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1081245c539aSKuninori Morimoto ret = snd_soc_component_hw_params(component, substream, params); 1082244e2936SCharles Keepax if (ret < 0) { 1083b8135864SKuninori Morimoto dev_err(component->dev, 1084b8135864SKuninori Morimoto "ASoC: %s hw params failed: %d\n", 1085244e2936SCharles Keepax component->name, ret); 1086b8135864SKuninori Morimoto goto component_err; 1087244e2936SCharles Keepax } 1088244e2936SCharles Keepax } 1089244e2936SCharles Keepax component = NULL; 1090b8135864SKuninori Morimoto 1091ddee627cSLiam Girdwood out: 109272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1093ddee627cSLiam Girdwood return ret; 1094ddee627cSLiam Girdwood 1095b8135864SKuninori Morimoto component_err: 1096244e2936SCharles Keepax soc_pcm_components_hw_free(substream, component); 1097b8135864SKuninori Morimoto 109819bdcc7aSShreyas NC i = rtd->num_cpus; 1099ddee627cSLiam Girdwood 1100ddee627cSLiam Girdwood interface_err: 1101a4be4187SKuninori Morimoto for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) { 11020e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 11030e9cf4c4SBard Liao continue; 11040e9cf4c4SBard Liao 110519bdcc7aSShreyas NC snd_soc_dai_hw_free(cpu_dai, substream); 110619bdcc7aSShreyas NC cpu_dai->rate = 0; 110719bdcc7aSShreyas NC } 110819bdcc7aSShreyas NC 11092e5894d7SBenoit Cousson i = rtd->num_codecs; 1110ddee627cSLiam Girdwood 1111ddee627cSLiam Girdwood codec_err: 1112a4be4187SKuninori Morimoto for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) { 1113f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1114f47b9ad9SJerome Brunet continue; 1115f47b9ad9SJerome Brunet 1116846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 11172e5894d7SBenoit Cousson codec_dai->rate = 0; 11182e5894d7SBenoit Cousson } 11192e5894d7SBenoit Cousson 112049f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1121ddee627cSLiam Girdwood 112272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1123ddee627cSLiam Girdwood return ret; 1124ddee627cSLiam Girdwood } 1125ddee627cSLiam Girdwood 1126ddee627cSLiam Girdwood /* 1127ddee627cSLiam Girdwood * Frees resources allocated by hw_params, can be called multiple times 1128ddee627cSLiam Girdwood */ 1129ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1130ddee627cSLiam Girdwood { 1131ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1132c840f769SKuninori Morimoto struct snd_soc_dai *dai; 11332e5894d7SBenoit Cousson int i; 1134ddee627cSLiam Girdwood 113572b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 1136ddee627cSLiam Girdwood 1137d3383420SNicolin Chen /* clear the corresponding DAIs parameters when going to be inactive */ 1138c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) { 1139*b3dea624SKuninori Morimoto int active = snd_soc_dai_stream_active(dai, substream->stream); 1140d3383420SNicolin Chen 1141*b3dea624SKuninori Morimoto if (snd_soc_dai_active(dai) == 1) { 1142c840f769SKuninori Morimoto dai->rate = 0; 1143c840f769SKuninori Morimoto dai->channels = 0; 1144c840f769SKuninori Morimoto dai->sample_bits = 0; 1145d3383420SNicolin Chen } 11460f6011fdSKuninori Morimoto 114767ad8777SKuninori Morimoto if (active == 1) 1148c840f769SKuninori Morimoto snd_soc_dai_digital_mute(dai, 1, substream->stream); 1149a9ee331bSKuninori Morimoto } 1150a9ee331bSKuninori Morimoto 1151ddee627cSLiam Girdwood /* free any machine hw params */ 115249f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1153ddee627cSLiam Girdwood 1154b8135864SKuninori Morimoto /* free any component resources */ 1155244e2936SCharles Keepax soc_pcm_components_hw_free(substream, NULL); 1156b8135864SKuninori Morimoto 1157ddee627cSLiam Girdwood /* now free hw params for the DAIs */ 1158c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) { 1159c840f769SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, substream->stream)) 1160f47b9ad9SJerome Brunet continue; 1161f47b9ad9SJerome Brunet 1162c840f769SKuninori Morimoto snd_soc_dai_hw_free(dai, substream); 11630e9cf4c4SBard Liao } 1164ddee627cSLiam Girdwood 116572b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1166ddee627cSLiam Girdwood return 0; 1167ddee627cSLiam Girdwood } 1168ddee627cSLiam Girdwood 11694378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd) 1170ddee627cSLiam Girdwood { 1171ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1172b8135864SKuninori Morimoto struct snd_soc_component *component; 11732e5894d7SBenoit Cousson int i, ret; 1174ddee627cSLiam Girdwood 1175ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 1176ddee627cSLiam Girdwood if (ret < 0) 1177ddee627cSLiam Girdwood return ret; 1178ddee627cSLiam Girdwood 1179613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11805693d50cSKuninori Morimoto ret = snd_soc_component_trigger(component, substream, cmd); 1181b8135864SKuninori Morimoto if (ret < 0) 1182b8135864SKuninori Morimoto return ret; 1183b8135864SKuninori Morimoto } 1184b8135864SKuninori Morimoto 118542f2472dSKuninori Morimoto return snd_soc_pcm_dai_trigger(substream, cmd); 11864378f1fbSPeter Ujfalusi } 11874378f1fbSPeter Ujfalusi 11884378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd) 11894378f1fbSPeter Ujfalusi { 11904378f1fbSPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 11914378f1fbSPeter Ujfalusi struct snd_soc_component *component; 11924378f1fbSPeter Ujfalusi int i, ret; 11934378f1fbSPeter Ujfalusi 119442f2472dSKuninori Morimoto ret = snd_soc_pcm_dai_trigger(substream, cmd); 11954378f1fbSPeter Ujfalusi if (ret < 0) 11964378f1fbSPeter Ujfalusi return ret; 11974378f1fbSPeter Ujfalusi 1198613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11994378f1fbSPeter Ujfalusi ret = snd_soc_component_trigger(component, substream, cmd); 12004378f1fbSPeter Ujfalusi if (ret < 0) 12014378f1fbSPeter Ujfalusi return ret; 12024378f1fbSPeter Ujfalusi } 12034378f1fbSPeter Ujfalusi 1204ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 12054792b0dbSJarkko Nikula if (ret < 0) 12064792b0dbSJarkko Nikula return ret; 12074792b0dbSJarkko Nikula 1208ddee627cSLiam Girdwood return 0; 1209ddee627cSLiam Girdwood } 1210ddee627cSLiam Girdwood 12114378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 12124378f1fbSPeter Ujfalusi { 12134378f1fbSPeter Ujfalusi int ret; 12144378f1fbSPeter Ujfalusi 12154378f1fbSPeter Ujfalusi switch (cmd) { 12164378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_START: 12174378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_RESUME: 12184378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 12194378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_start(substream, cmd); 12204378f1fbSPeter Ujfalusi break; 12214378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_STOP: 12224378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_SUSPEND: 12234378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 12244378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_stop(substream, cmd); 12254378f1fbSPeter Ujfalusi break; 12264378f1fbSPeter Ujfalusi default: 12274378f1fbSPeter Ujfalusi return -EINVAL; 12284378f1fbSPeter Ujfalusi } 12294378f1fbSPeter Ujfalusi 12304378f1fbSPeter Ujfalusi return ret; 12314378f1fbSPeter Ujfalusi } 12324378f1fbSPeter Ujfalusi 1233ddee627cSLiam Girdwood /* 1234ddee627cSLiam Girdwood * soc level wrapper for pointer callback 1235ef050becSCharles Keepax * If cpu_dai, codec_dai, component driver has the delay callback, then 1236ddee627cSLiam Girdwood * the runtime->delay will be updated accordingly. 1237ddee627cSLiam Girdwood */ 1238ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1239ddee627cSLiam Girdwood { 1240ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 124119bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 12422e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 1243ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 1244ddee627cSLiam Girdwood snd_pcm_uframes_t offset = 0; 1245ddee627cSLiam Girdwood snd_pcm_sframes_t delay = 0; 12462e5894d7SBenoit Cousson snd_pcm_sframes_t codec_delay = 0; 124719bdcc7aSShreyas NC snd_pcm_sframes_t cpu_delay = 0; 12482e5894d7SBenoit Cousson int i; 1249ddee627cSLiam Girdwood 12509fb4c2bfSAkshu Agrawal /* clearing the previous total delay */ 12519fb4c2bfSAkshu Agrawal runtime->delay = 0; 12529fb4c2bfSAkshu Agrawal 12530035e256SKuninori Morimoto offset = snd_soc_pcm_component_pointer(substream); 1254b8135864SKuninori Morimoto 12559fb4c2bfSAkshu Agrawal /* base delay if assigned in pointer callback */ 12569fb4c2bfSAkshu Agrawal delay = runtime->delay; 1257b8135864SKuninori Morimoto 1258a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 125919bdcc7aSShreyas NC cpu_delay = max(cpu_delay, 126019bdcc7aSShreyas NC snd_soc_dai_delay(cpu_dai, substream)); 126119bdcc7aSShreyas NC } 126219bdcc7aSShreyas NC delay += cpu_delay; 1263ddee627cSLiam Girdwood 1264a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 12652e5894d7SBenoit Cousson codec_delay = max(codec_delay, 12661dea80d4SKuninori Morimoto snd_soc_dai_delay(codec_dai, substream)); 12672e5894d7SBenoit Cousson } 12682e5894d7SBenoit Cousson delay += codec_delay; 1269ddee627cSLiam Girdwood 1270ddee627cSLiam Girdwood runtime->delay = delay; 1271ddee627cSLiam Girdwood 1272ddee627cSLiam Girdwood return offset; 1273ddee627cSLiam Girdwood } 1274ddee627cSLiam Girdwood 127501d7584cSLiam Girdwood /* connect a FE and BE */ 127601d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 127701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 127801d7584cSLiam Girdwood { 127901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1280a9764869SKaiChieh Chuang unsigned long flags; 128101d7584cSLiam Girdwood 128201d7584cSLiam Girdwood /* only add new dpcms */ 12838d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 128401d7584cSLiam Girdwood if (dpcm->be == be && dpcm->fe == fe) 128501d7584cSLiam Girdwood return 0; 128601d7584cSLiam Girdwood } 128701d7584cSLiam Girdwood 128801d7584cSLiam Girdwood dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 128901d7584cSLiam Girdwood if (!dpcm) 129001d7584cSLiam Girdwood return -ENOMEM; 129101d7584cSLiam Girdwood 129201d7584cSLiam Girdwood dpcm->be = be; 129301d7584cSLiam Girdwood dpcm->fe = fe; 129401d7584cSLiam Girdwood be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 129501d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1296a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 129701d7584cSLiam Girdwood list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 129801d7584cSLiam Girdwood list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1299a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 130001d7584cSLiam Girdwood 130101d7584cSLiam Girdwood dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 130201d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 130301d7584cSLiam Girdwood stream ? "<-" : "->", be->dai_link->name); 130401d7584cSLiam Girdwood 1305154dae87SKuninori Morimoto dpcm_create_debugfs_state(dpcm, stream); 1306154dae87SKuninori Morimoto 130701d7584cSLiam Girdwood return 1; 130801d7584cSLiam Girdwood } 130901d7584cSLiam Girdwood 131001d7584cSLiam Girdwood /* reparent a BE onto another FE */ 131101d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 131201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 131301d7584cSLiam Girdwood { 131401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 131501d7584cSLiam Girdwood struct snd_pcm_substream *fe_substream, *be_substream; 131601d7584cSLiam Girdwood 131701d7584cSLiam Girdwood /* reparent if BE is connected to other FEs */ 131801d7584cSLiam Girdwood if (!be->dpcm[stream].users) 131901d7584cSLiam Girdwood return; 132001d7584cSLiam Girdwood 132101d7584cSLiam Girdwood be_substream = snd_soc_dpcm_get_substream(be, stream); 132201d7584cSLiam Girdwood 1323d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 132401d7584cSLiam Girdwood if (dpcm->fe == fe) 132501d7584cSLiam Girdwood continue; 132601d7584cSLiam Girdwood 132701d7584cSLiam Girdwood dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 132801d7584cSLiam Girdwood stream ? "capture" : "playback", 132901d7584cSLiam Girdwood dpcm->fe->dai_link->name, 133001d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 133101d7584cSLiam Girdwood 133201d7584cSLiam Girdwood fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 133301d7584cSLiam Girdwood be_substream->runtime = fe_substream->runtime; 133401d7584cSLiam Girdwood break; 133501d7584cSLiam Girdwood } 133601d7584cSLiam Girdwood } 133701d7584cSLiam Girdwood 133801d7584cSLiam Girdwood /* disconnect a BE and FE */ 133923607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 134001d7584cSLiam Girdwood { 134101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm, *d; 1342a9764869SKaiChieh Chuang unsigned long flags; 134301d7584cSLiam Girdwood 13448d6258a4SKuninori Morimoto for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1345103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 134601d7584cSLiam Girdwood stream ? "capture" : "playback", 134701d7584cSLiam Girdwood dpcm->be->dai_link->name); 134801d7584cSLiam Girdwood 134901d7584cSLiam Girdwood if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 135001d7584cSLiam Girdwood continue; 135101d7584cSLiam Girdwood 135201d7584cSLiam Girdwood dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 135301d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 135401d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 135501d7584cSLiam Girdwood 135601d7584cSLiam Girdwood /* BEs still alive need new FE */ 135701d7584cSLiam Girdwood dpcm_be_reparent(fe, dpcm->be, stream); 135801d7584cSLiam Girdwood 1359154dae87SKuninori Morimoto dpcm_remove_debugfs_state(dpcm); 1360154dae87SKuninori Morimoto 1361a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 136201d7584cSLiam Girdwood list_del(&dpcm->list_be); 136301d7584cSLiam Girdwood list_del(&dpcm->list_fe); 1364a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 136501d7584cSLiam Girdwood kfree(dpcm); 136601d7584cSLiam Girdwood } 136701d7584cSLiam Girdwood } 136801d7584cSLiam Girdwood 136901d7584cSLiam Girdwood /* get BE for DAI widget and stream */ 137001d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 137101d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget, int stream) 137201d7584cSLiam Girdwood { 137301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 137493597faeSKuninori Morimoto struct snd_soc_dapm_widget *w; 13757afecb30SKuninori Morimoto struct snd_soc_dai *dai; 13761a497983SMengdong Lin int i; 137701d7584cSLiam Girdwood 13783c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 13793c146465SLiam Girdwood 1380bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, be) { 138101d7584cSLiam Girdwood 138235ea0655SLiam Girdwood if (!be->dai_link->no_pcm) 138335ea0655SLiam Girdwood continue; 138435ea0655SLiam Girdwood 1385c840f769SKuninori Morimoto for_each_rtd_dais(be, i, dai) { 138619bdcc7aSShreyas NC w = snd_soc_dai_get_widget(dai, stream); 138793597faeSKuninori Morimoto 13883c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: try BE : %s\n", 138993597faeSKuninori Morimoto w ? w->name : "(not set)"); 13903c146465SLiam Girdwood 139193597faeSKuninori Morimoto if (w == widget) 139201d7584cSLiam Girdwood return be; 139319bdcc7aSShreyas NC } 139401d7584cSLiam Girdwood } 139501d7584cSLiam Girdwood 13969d6ee365SJerome Brunet /* Widget provided is not a BE */ 139701d7584cSLiam Girdwood return NULL; 139801d7584cSLiam Girdwood } 139901d7584cSLiam Girdwood 140001d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list, 140101d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget) 140201d7584cSLiam Girdwood { 140309e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *w; 140401d7584cSLiam Girdwood int i; 140501d7584cSLiam Girdwood 140609e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, w) 140709e88f8aSKuninori Morimoto if (widget == w) 140801d7584cSLiam Girdwood return 1; 140901d7584cSLiam Girdwood 141001d7584cSLiam Girdwood return 0; 141101d7584cSLiam Girdwood } 141201d7584cSLiam Girdwood 14135fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, 14145fdd022cSPiotr Stankiewicz enum snd_soc_dapm_direction dir) 14155fdd022cSPiotr Stankiewicz { 14165fdd022cSPiotr Stankiewicz struct snd_soc_card *card = widget->dapm->card; 14175fdd022cSPiotr Stankiewicz struct snd_soc_pcm_runtime *rtd; 1418c2cd8216SKuninori Morimoto int stream; 14195fdd022cSPiotr Stankiewicz 1420c2cd8216SKuninori Morimoto /* adjust dir to stream */ 1421c2cd8216SKuninori Morimoto if (dir == SND_SOC_DAPM_DIR_OUT) 1422c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK; 1423c2cd8216SKuninori Morimoto else 1424c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE; 1425c2cd8216SKuninori Morimoto 1426027a4838SKuninori Morimoto rtd = dpcm_get_be(card, widget, stream); 1427027a4838SKuninori Morimoto if (rtd) 14285fdd022cSPiotr Stankiewicz return true; 14295fdd022cSPiotr Stankiewicz 14305fdd022cSPiotr Stankiewicz return false; 14315fdd022cSPiotr Stankiewicz } 14325fdd022cSPiotr Stankiewicz 143323607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 14341ce43acfSLars-Peter Clausen int stream, struct snd_soc_dapm_widget_list **list) 143501d7584cSLiam Girdwood { 1436c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); 143701d7584cSLiam Girdwood int paths; 143801d7584cSLiam Girdwood 14396e1276a5SBard Liao if (fe->num_cpus > 1) { 14406e1276a5SBard Liao dev_err(fe->dev, 14416e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 14426e1276a5SBard Liao return -EINVAL; 14436e1276a5SBard Liao } 14446e1276a5SBard Liao 144501d7584cSLiam Girdwood /* get number of valid DAI paths and their widgets */ 14466742064aSPiotr Stankiewicz paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 14475fdd022cSPiotr Stankiewicz dpcm_end_walk_at_be); 144801d7584cSLiam Girdwood 1449103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 145001d7584cSLiam Girdwood stream ? "capture" : "playback"); 145101d7584cSLiam Girdwood 145201d7584cSLiam Girdwood return paths; 145301d7584cSLiam Girdwood } 145401d7584cSLiam Girdwood 145552645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list) 145652645e33SKuninori Morimoto { 145752645e33SKuninori Morimoto snd_soc_dapm_dai_free_widgets(list); 145852645e33SKuninori Morimoto } 145952645e33SKuninori Morimoto 14608cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream, 14618cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list *list) 146201d7584cSLiam Girdwood { 146301d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget; 14647afecb30SKuninori Morimoto struct snd_soc_dai *dai; 14652e5894d7SBenoit Cousson unsigned int i; 146601d7584cSLiam Girdwood 1467c840f769SKuninori Morimoto /* is there a valid DAI widget for this BE */ 1468c840f769SKuninori Morimoto for_each_rtd_dais(dpcm->be, i, dai) { 146919bdcc7aSShreyas NC widget = snd_soc_dai_get_widget(dai, stream); 147001d7584cSLiam Girdwood 147119bdcc7aSShreyas NC /* 1472c840f769SKuninori Morimoto * The BE is pruned only if none of the dai 147319bdcc7aSShreyas NC * widgets are in the active list. 147419bdcc7aSShreyas NC */ 147501d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 14768cce6569SGuennadi Liakhovetski return true; 147719bdcc7aSShreyas NC } 147801d7584cSLiam Girdwood 14798cce6569SGuennadi Liakhovetski return false; 14808cce6569SGuennadi Liakhovetski } 14818cce6569SGuennadi Liakhovetski 14828cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 14838cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list **list_) 14848cce6569SGuennadi Liakhovetski { 14858cce6569SGuennadi Liakhovetski struct snd_soc_dpcm *dpcm; 14868cce6569SGuennadi Liakhovetski int prune = 0; 14878cce6569SGuennadi Liakhovetski 14888cce6569SGuennadi Liakhovetski /* Destroy any old FE <--> BE connections */ 14898cce6569SGuennadi Liakhovetski for_each_dpcm_be(fe, stream, dpcm) { 14908cce6569SGuennadi Liakhovetski if (dpcm_be_is_active(dpcm, stream, *list_)) 1491bed646dcSKuninori Morimoto continue; 149201d7584cSLiam Girdwood 1493103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 149401d7584cSLiam Girdwood stream ? "capture" : "playback", 149501d7584cSLiam Girdwood dpcm->be->dai_link->name, fe->dai_link->name); 149601d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 149701d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 149801d7584cSLiam Girdwood prune++; 149901d7584cSLiam Girdwood } 150001d7584cSLiam Girdwood 1501103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 150201d7584cSLiam Girdwood return prune; 150301d7584cSLiam Girdwood } 150401d7584cSLiam Girdwood 150501d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 150601d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 150701d7584cSLiam Girdwood { 150801d7584cSLiam Girdwood struct snd_soc_card *card = fe->card; 150901d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 151001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 151109e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *widget; 151201d7584cSLiam Girdwood int i, new = 0, err; 151301d7584cSLiam Girdwood 151401d7584cSLiam Girdwood /* Create any new FE <--> BE connections */ 151509e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, widget) { 151601d7584cSLiam Girdwood 151709e88f8aSKuninori Morimoto switch (widget->id) { 15184616274dSMark Brown case snd_soc_dapm_dai_in: 1519c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1520c5b8540dSKoro Chen continue; 1521c5b8540dSKoro Chen break; 15224616274dSMark Brown case snd_soc_dapm_dai_out: 1523c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_CAPTURE) 1524c5b8540dSKoro Chen continue; 15254616274dSMark Brown break; 15264616274dSMark Brown default: 152701d7584cSLiam Girdwood continue; 15284616274dSMark Brown } 152901d7584cSLiam Girdwood 153001d7584cSLiam Girdwood /* is there a valid BE rtd for this widget */ 153109e88f8aSKuninori Morimoto be = dpcm_get_be(card, widget, stream); 153201d7584cSLiam Girdwood if (!be) { 1533103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no BE found for %s\n", 153409e88f8aSKuninori Morimoto widget->name); 153501d7584cSLiam Girdwood continue; 153601d7584cSLiam Girdwood } 153701d7584cSLiam Girdwood 153801d7584cSLiam Girdwood /* don't connect if FE is not running */ 153923607025SLiam Girdwood if (!fe->dpcm[stream].runtime && !fe->fe_compr) 154001d7584cSLiam Girdwood continue; 154101d7584cSLiam Girdwood 154201d7584cSLiam Girdwood /* newly connected FE and BE */ 154301d7584cSLiam Girdwood err = dpcm_be_connect(fe, be, stream); 154401d7584cSLiam Girdwood if (err < 0) { 1545103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: can't connect %s\n", 154609e88f8aSKuninori Morimoto widget->name); 154701d7584cSLiam Girdwood break; 154801d7584cSLiam Girdwood } else if (err == 0) /* already connected */ 154901d7584cSLiam Girdwood continue; 155001d7584cSLiam Girdwood 155101d7584cSLiam Girdwood /* new */ 155201d7584cSLiam Girdwood be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 155301d7584cSLiam Girdwood new++; 155401d7584cSLiam Girdwood } 155501d7584cSLiam Girdwood 1556103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 155701d7584cSLiam Girdwood return new; 155801d7584cSLiam Girdwood } 155901d7584cSLiam Girdwood 156001d7584cSLiam Girdwood /* 156101d7584cSLiam Girdwood * Find the corresponding BE DAIs that source or sink audio to this 156201d7584cSLiam Girdwood * FE substream. 156301d7584cSLiam Girdwood */ 156423607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 156501d7584cSLiam Girdwood int stream, struct snd_soc_dapm_widget_list **list, int new) 156601d7584cSLiam Girdwood { 156701d7584cSLiam Girdwood if (new) 156801d7584cSLiam Girdwood return dpcm_add_paths(fe, stream, list); 156901d7584cSLiam Girdwood else 157001d7584cSLiam Girdwood return dpcm_prune_paths(fe, stream, list); 157101d7584cSLiam Girdwood } 157201d7584cSLiam Girdwood 157323607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 157401d7584cSLiam Girdwood { 157501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1576a9764869SKaiChieh Chuang unsigned long flags; 157701d7584cSLiam Girdwood 1578a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 15798d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 158001d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = 158101d7584cSLiam Girdwood SND_SOC_DPCM_UPDATE_NO; 1582a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 158301d7584cSLiam Girdwood } 158401d7584cSLiam Girdwood 158501d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 158601d7584cSLiam Girdwood int stream) 158701d7584cSLiam Girdwood { 158801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 158901d7584cSLiam Girdwood 159001d7584cSLiam Girdwood /* disable any enabled and non active backends */ 15918d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 159201d7584cSLiam Girdwood 159301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 159401d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 159501d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 159601d7584cSLiam Girdwood 159701d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1598103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 159901d7584cSLiam Girdwood stream ? "capture" : "playback", 160001d7584cSLiam Girdwood be->dpcm[stream].state); 160101d7584cSLiam Girdwood 160201d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 160301d7584cSLiam Girdwood continue; 160401d7584cSLiam Girdwood 160501d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 160601d7584cSLiam Girdwood continue; 160701d7584cSLiam Girdwood 160801d7584cSLiam Girdwood soc_pcm_close(be_substream); 160901d7584cSLiam Girdwood be_substream->runtime = NULL; 161001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 161101d7584cSLiam Girdwood } 161201d7584cSLiam Girdwood } 161301d7584cSLiam Girdwood 161423607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 161501d7584cSLiam Girdwood { 161601d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 161701d7584cSLiam Girdwood int err, count = 0; 161801d7584cSLiam Girdwood 161901d7584cSLiam Girdwood /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 16208d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 162101d7584cSLiam Girdwood 162201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 162301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 162401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 162501d7584cSLiam Girdwood 16262062b4c5SRussell King - ARM Linux if (!be_substream) { 16272062b4c5SRussell King - ARM Linux dev_err(be->dev, "ASoC: no backend %s stream\n", 16282062b4c5SRussell King - ARM Linux stream ? "capture" : "playback"); 16292062b4c5SRussell King - ARM Linux continue; 16302062b4c5SRussell King - ARM Linux } 16312062b4c5SRussell King - ARM Linux 163201d7584cSLiam Girdwood /* is this op for this BE ? */ 163301d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 163401d7584cSLiam Girdwood continue; 163501d7584cSLiam Girdwood 163601d7584cSLiam Girdwood /* first time the dpcm is open ? */ 163701d7584cSLiam Girdwood if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1638103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: too many users %s at open %d\n", 163901d7584cSLiam Girdwood stream ? "capture" : "playback", 164001d7584cSLiam Girdwood be->dpcm[stream].state); 164101d7584cSLiam Girdwood 164201d7584cSLiam Girdwood if (be->dpcm[stream].users++ != 0) 164301d7584cSLiam Girdwood continue; 164401d7584cSLiam Girdwood 164501d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 164601d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 164701d7584cSLiam Girdwood continue; 164801d7584cSLiam Girdwood 16492062b4c5SRussell King - ARM Linux dev_dbg(be->dev, "ASoC: open %s BE %s\n", 16502062b4c5SRussell King - ARM Linux stream ? "capture" : "playback", be->dai_link->name); 165101d7584cSLiam Girdwood 165201d7584cSLiam Girdwood be_substream->runtime = be->dpcm[stream].runtime; 165301d7584cSLiam Girdwood err = soc_pcm_open(be_substream); 165401d7584cSLiam Girdwood if (err < 0) { 1655103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: BE open failed %d\n", err); 165601d7584cSLiam Girdwood be->dpcm[stream].users--; 165701d7584cSLiam Girdwood if (be->dpcm[stream].users < 0) 1658103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 165901d7584cSLiam Girdwood stream ? "capture" : "playback", 166001d7584cSLiam Girdwood be->dpcm[stream].state); 166101d7584cSLiam Girdwood 166201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 166301d7584cSLiam Girdwood goto unwind; 166401d7584cSLiam Girdwood } 166501d7584cSLiam Girdwood 166601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 166701d7584cSLiam Girdwood count++; 166801d7584cSLiam Girdwood } 166901d7584cSLiam Girdwood 167001d7584cSLiam Girdwood return count; 167101d7584cSLiam Girdwood 167201d7584cSLiam Girdwood unwind: 167301d7584cSLiam Girdwood /* disable any enabled and non active backends */ 16748d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 167501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 167601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 167701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 167801d7584cSLiam Girdwood 167901d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 168001d7584cSLiam Girdwood continue; 168101d7584cSLiam Girdwood 168201d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1683103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close %d\n", 168401d7584cSLiam Girdwood stream ? "capture" : "playback", 168501d7584cSLiam Girdwood be->dpcm[stream].state); 168601d7584cSLiam Girdwood 168701d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 168801d7584cSLiam Girdwood continue; 168901d7584cSLiam Girdwood 169001d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 169101d7584cSLiam Girdwood continue; 169201d7584cSLiam Girdwood 169301d7584cSLiam Girdwood soc_pcm_close(be_substream); 169401d7584cSLiam Girdwood be_substream->runtime = NULL; 169501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 169601d7584cSLiam Girdwood } 169701d7584cSLiam Girdwood 169801d7584cSLiam Girdwood return err; 169901d7584cSLiam Girdwood } 170001d7584cSLiam Girdwood 170108ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1702435ffb76SJerome Brunet struct snd_soc_pcm_stream *stream) 170308ae9b45SLars-Peter Clausen { 170408ae9b45SLars-Peter Clausen runtime->hw.rate_min = stream->rate_min; 1705e33ffbd9SCharles Keepax runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); 170608ae9b45SLars-Peter Clausen runtime->hw.channels_min = stream->channels_min; 170708ae9b45SLars-Peter Clausen runtime->hw.channels_max = stream->channels_max; 1708002220a9SLars-Peter Clausen if (runtime->hw.formats) 1709435ffb76SJerome Brunet runtime->hw.formats &= stream->formats; 1710002220a9SLars-Peter Clausen else 1711435ffb76SJerome Brunet runtime->hw.formats = stream->formats; 171208ae9b45SLars-Peter Clausen runtime->hw.rates = stream->rates; 171308ae9b45SLars-Peter Clausen } 171408ae9b45SLars-Peter Clausen 1715435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, 1716435ffb76SJerome Brunet u64 *formats) 1717b073ed4eSKuninori Morimoto { 1718b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *fe = substream->private_data; 1719b073ed4eSKuninori Morimoto struct snd_soc_dpcm *dpcm; 17207afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1721b073ed4eSKuninori Morimoto int stream = substream->stream; 1722b073ed4eSKuninori Morimoto 1723b073ed4eSKuninori Morimoto if (!fe->dai_link->dpcm_merged_format) 1724435ffb76SJerome Brunet return; 1725b073ed4eSKuninori Morimoto 1726b073ed4eSKuninori Morimoto /* 1727b073ed4eSKuninori Morimoto * It returns merged BE codec format 1728b073ed4eSKuninori Morimoto * if FE want to use it (= dpcm_merged_format) 1729b073ed4eSKuninori Morimoto */ 1730b073ed4eSKuninori Morimoto 17318d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1732b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 1733b073ed4eSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream; 1734b073ed4eSKuninori Morimoto int i; 1735b073ed4eSKuninori Morimoto 1736a4be4187SKuninori Morimoto for_each_rtd_codec_dais(be, i, dai) { 17374febced1SJerome Brunet /* 17384febced1SJerome Brunet * Skip CODECs which don't support the current stream 17394febced1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 17404febced1SJerome Brunet */ 17417afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 17424febced1SJerome Brunet continue; 17434febced1SJerome Brunet 1744acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1745b073ed4eSKuninori Morimoto 1746435ffb76SJerome Brunet *formats &= codec_stream->formats; 1747435ffb76SJerome Brunet } 1748b073ed4eSKuninori Morimoto } 1749b073ed4eSKuninori Morimoto } 1750b073ed4eSKuninori Morimoto 1751435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, 1752f4c277b8SJiada Wang unsigned int *channels_min, 1753f4c277b8SJiada Wang unsigned int *channels_max) 1754f4c277b8SJiada Wang { 1755f4c277b8SJiada Wang struct snd_soc_pcm_runtime *fe = substream->private_data; 1756f4c277b8SJiada Wang struct snd_soc_dpcm *dpcm; 1757f4c277b8SJiada Wang int stream = substream->stream; 1758f4c277b8SJiada Wang 1759f4c277b8SJiada Wang if (!fe->dai_link->dpcm_merged_chan) 1760f4c277b8SJiada Wang return; 1761f4c277b8SJiada Wang 1762f4c277b8SJiada Wang /* 1763f4c277b8SJiada Wang * It returns merged BE codec channel; 1764f4c277b8SJiada Wang * if FE want to use it (= dpcm_merged_chan) 1765f4c277b8SJiada Wang */ 1766f4c277b8SJiada Wang 17678d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1768f4c277b8SJiada Wang struct snd_soc_pcm_runtime *be = dpcm->be; 1769f4c277b8SJiada Wang struct snd_soc_pcm_stream *codec_stream; 17704f2bd18bSJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 177119bdcc7aSShreyas NC struct snd_soc_dai *dai; 177219bdcc7aSShreyas NC int i; 1773f4c277b8SJiada Wang 1774a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(be, i, dai) { 17750e9cf4c4SBard Liao /* 17760e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 17770e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 17780e9cf4c4SBard Liao */ 17790e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream)) 17800e9cf4c4SBard Liao continue; 17810e9cf4c4SBard Liao 178219bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream); 17834f2bd18bSJerome Brunet 178419bdcc7aSShreyas NC *channels_min = max(*channels_min, 178519bdcc7aSShreyas NC cpu_stream->channels_min); 178619bdcc7aSShreyas NC *channels_max = min(*channels_max, 178719bdcc7aSShreyas NC cpu_stream->channels_max); 178819bdcc7aSShreyas NC } 17894f2bd18bSJerome Brunet 17904f2bd18bSJerome Brunet /* 17914f2bd18bSJerome Brunet * chan min/max cannot be enforced if there are multiple CODEC 17924f2bd18bSJerome Brunet * DAIs connected to a single CPU DAI, use CPU DAI's directly 17934f2bd18bSJerome Brunet */ 17944f2bd18bSJerome Brunet if (be->num_codecs == 1) { 1795c2233a26SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream); 1796f4c277b8SJiada Wang 1797f4c277b8SJiada Wang *channels_min = max(*channels_min, 1798f4c277b8SJiada Wang codec_stream->channels_min); 1799f4c277b8SJiada Wang *channels_max = min(*channels_max, 1800f4c277b8SJiada Wang codec_stream->channels_max); 1801f4c277b8SJiada Wang } 1802f4c277b8SJiada Wang } 1803f4c277b8SJiada Wang } 1804f4c277b8SJiada Wang 1805baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, 1806baacd8d1SJerome Brunet unsigned int *rates, 1807baacd8d1SJerome Brunet unsigned int *rate_min, 1808baacd8d1SJerome Brunet unsigned int *rate_max) 1809baacd8d1SJerome Brunet { 1810baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *fe = substream->private_data; 1811baacd8d1SJerome Brunet struct snd_soc_dpcm *dpcm; 1812baacd8d1SJerome Brunet int stream = substream->stream; 1813baacd8d1SJerome Brunet 1814baacd8d1SJerome Brunet if (!fe->dai_link->dpcm_merged_rate) 1815baacd8d1SJerome Brunet return; 1816baacd8d1SJerome Brunet 1817baacd8d1SJerome Brunet /* 1818baacd8d1SJerome Brunet * It returns merged BE codec channel; 1819baacd8d1SJerome Brunet * if FE want to use it (= dpcm_merged_chan) 1820baacd8d1SJerome Brunet */ 1821baacd8d1SJerome Brunet 18228d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1823baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *be = dpcm->be; 1824c840f769SKuninori Morimoto struct snd_soc_pcm_stream *pcm; 18257afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1826baacd8d1SJerome Brunet int i; 1827baacd8d1SJerome Brunet 1828c840f769SKuninori Morimoto for_each_rtd_dais(be, i, dai) { 18290e9cf4c4SBard Liao /* 1830c840f769SKuninori Morimoto * Skip DAIs which don't support the current stream 18310e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 18320e9cf4c4SBard Liao */ 18330e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream)) 18340e9cf4c4SBard Liao continue; 18350e9cf4c4SBard Liao 1836c840f769SKuninori Morimoto pcm = snd_soc_dai_get_pcm_stream(dai, stream); 1837baacd8d1SJerome Brunet 1838c840f769SKuninori Morimoto *rate_min = max(*rate_min, pcm->rate_min); 1839c840f769SKuninori Morimoto *rate_max = min_not_zero(*rate_max, pcm->rate_max); 1840c840f769SKuninori Morimoto *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates); 1841baacd8d1SJerome Brunet } 1842baacd8d1SJerome Brunet } 1843baacd8d1SJerome Brunet } 1844baacd8d1SJerome Brunet 184545c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 184601d7584cSLiam Girdwood { 184701d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 184801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 184919bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 185019bdcc7aSShreyas NC int i; 185101d7584cSLiam Girdwood 1852a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 18530e9cf4c4SBard Liao /* 18540e9cf4c4SBard Liao * Skip CPUs which don't support the current stream 18550e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details 18560e9cf4c4SBard Liao */ 18570e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream)) 18580e9cf4c4SBard Liao continue; 18590e9cf4c4SBard Liao 18600c9ba720SKuninori Morimoto dpcm_init_runtime_hw(runtime, 18610c9ba720SKuninori Morimoto snd_soc_dai_get_pcm_stream(cpu_dai, 18620c9ba720SKuninori Morimoto substream->stream)); 186319bdcc7aSShreyas NC } 1864f4c277b8SJiada Wang 1865435ffb76SJerome Brunet dpcm_runtime_merge_format(substream, &runtime->hw.formats); 1866435ffb76SJerome Brunet dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, 1867435ffb76SJerome Brunet &runtime->hw.channels_max); 1868baacd8d1SJerome Brunet dpcm_runtime_merge_rate(substream, &runtime->hw.rates, 1869baacd8d1SJerome Brunet &runtime->hw.rate_min, &runtime->hw.rate_max); 187001d7584cSLiam Girdwood } 187101d7584cSLiam Girdwood 1872ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 1873ea9d0d77STakashi Iwai 1874ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock 1875ea9d0d77STakashi Iwai * for avoiding the race with trigger callback. 1876ea9d0d77STakashi Iwai * If the state is unset and a trigger is pending while the previous operation, 1877ea9d0d77STakashi Iwai * process the pending trigger action here. 1878ea9d0d77STakashi Iwai */ 1879ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 1880ea9d0d77STakashi Iwai int stream, enum snd_soc_dpcm_update state) 1881ea9d0d77STakashi Iwai { 1882ea9d0d77STakashi Iwai struct snd_pcm_substream *substream = 1883ea9d0d77STakashi Iwai snd_soc_dpcm_get_substream(fe, stream); 1884ea9d0d77STakashi Iwai 1885ea9d0d77STakashi Iwai snd_pcm_stream_lock_irq(substream); 1886ea9d0d77STakashi Iwai if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 1887ea9d0d77STakashi Iwai dpcm_fe_dai_do_trigger(substream, 1888ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending - 1); 1889ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = 0; 1890ea9d0d77STakashi Iwai } 1891ea9d0d77STakashi Iwai fe->dpcm[stream].runtime_update = state; 1892ea9d0d77STakashi Iwai snd_pcm_stream_unlock_irq(substream); 1893ea9d0d77STakashi Iwai } 1894ea9d0d77STakashi Iwai 1895906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 1896906c7d69SPC Liao int stream) 1897906c7d69SPC Liao { 1898906c7d69SPC Liao struct snd_soc_dpcm *dpcm; 1899906c7d69SPC Liao struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 190019bdcc7aSShreyas NC struct snd_soc_dai *fe_cpu_dai; 1901906c7d69SPC Liao int err; 190219bdcc7aSShreyas NC int i; 1903906c7d69SPC Liao 1904906c7d69SPC Liao /* apply symmetry for FE */ 1905906c7d69SPC Liao if (soc_pcm_has_symmetry(fe_substream)) 1906906c7d69SPC Liao fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1907906c7d69SPC Liao 1908a4be4187SKuninori Morimoto for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) { 1909906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1910*b3dea624SKuninori Morimoto if (snd_soc_dai_active(fe_cpu_dai)) { 1911906c7d69SPC Liao err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 1912906c7d69SPC Liao if (err < 0) 1913906c7d69SPC Liao return err; 1914906c7d69SPC Liao } 191519bdcc7aSShreyas NC } 1916906c7d69SPC Liao 1917906c7d69SPC Liao /* apply symmetry for BE */ 19188d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1919906c7d69SPC Liao struct snd_soc_pcm_runtime *be = dpcm->be; 1920906c7d69SPC Liao struct snd_pcm_substream *be_substream = 1921906c7d69SPC Liao snd_soc_dpcm_get_substream(be, stream); 19226246f283SJerome Brunet struct snd_soc_pcm_runtime *rtd; 1923c840f769SKuninori Morimoto struct snd_soc_dai *dai; 1924906c7d69SPC Liao int i; 1925906c7d69SPC Liao 19266246f283SJerome Brunet /* A backend may not have the requested substream */ 19276246f283SJerome Brunet if (!be_substream) 19286246f283SJerome Brunet continue; 19296246f283SJerome Brunet 19306246f283SJerome Brunet rtd = be_substream->private_data; 1931f1176614SJeeja KP if (rtd->dai_link->be_hw_params_fixup) 1932f1176614SJeeja KP continue; 1933f1176614SJeeja KP 1934906c7d69SPC Liao if (soc_pcm_has_symmetry(be_substream)) 1935906c7d69SPC Liao be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1936906c7d69SPC Liao 1937906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1938c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) { 1939*b3dea624SKuninori Morimoto if (snd_soc_dai_active(dai)) { 1940c840f769SKuninori Morimoto err = soc_pcm_apply_symmetry(fe_substream, dai); 1941906c7d69SPC Liao if (err < 0) 1942906c7d69SPC Liao return err; 1943906c7d69SPC Liao } 1944906c7d69SPC Liao } 1945906c7d69SPC Liao } 1946906c7d69SPC Liao 1947906c7d69SPC Liao return 0; 1948906c7d69SPC Liao } 1949906c7d69SPC Liao 195001d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 195101d7584cSLiam Girdwood { 195201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 195301d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = fe_substream->runtime; 195401d7584cSLiam Girdwood int stream = fe_substream->stream, ret = 0; 195501d7584cSLiam Girdwood 1956ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 195701d7584cSLiam Girdwood 195825c2f515SKuninori Morimoto ret = dpcm_be_dai_startup(fe, stream); 195901d7584cSLiam Girdwood if (ret < 0) { 1960103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 196101d7584cSLiam Girdwood goto be_err; 196201d7584cSLiam Girdwood } 196301d7584cSLiam Girdwood 1964103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 196501d7584cSLiam Girdwood 196601d7584cSLiam Girdwood /* start the DAI frontend */ 196701d7584cSLiam Girdwood ret = soc_pcm_open(fe_substream); 196801d7584cSLiam Girdwood if (ret < 0) { 1969103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 197001d7584cSLiam Girdwood goto unwind; 197101d7584cSLiam Girdwood } 197201d7584cSLiam Girdwood 197301d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 197401d7584cSLiam Girdwood 197501d7584cSLiam Girdwood dpcm_set_fe_runtime(fe_substream); 197601d7584cSLiam Girdwood snd_pcm_limit_hw_rates(runtime); 197701d7584cSLiam Girdwood 1978906c7d69SPC Liao ret = dpcm_apply_symmetry(fe_substream, stream); 19798a01fbf0SKuninori Morimoto if (ret < 0) 1980906c7d69SPC Liao dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", 1981906c7d69SPC Liao ret); 198201d7584cSLiam Girdwood 198301d7584cSLiam Girdwood unwind: 19848a01fbf0SKuninori Morimoto if (ret < 0) 198525c2f515SKuninori Morimoto dpcm_be_dai_startup_unwind(fe, stream); 198601d7584cSLiam Girdwood be_err: 1987ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 198801d7584cSLiam Girdwood return ret; 198901d7584cSLiam Girdwood } 199001d7584cSLiam Girdwood 199123607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 199201d7584cSLiam Girdwood { 199301d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 199401d7584cSLiam Girdwood 199501d7584cSLiam Girdwood /* only shutdown BEs that are either sinks or sources to this FE DAI */ 19968d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 199701d7584cSLiam Girdwood 199801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 199901d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 200001d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 200101d7584cSLiam Girdwood 200201d7584cSLiam Girdwood /* is this op for this BE ? */ 200301d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 200401d7584cSLiam Girdwood continue; 200501d7584cSLiam Girdwood 200601d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 2007103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 200801d7584cSLiam Girdwood stream ? "capture" : "playback", 200901d7584cSLiam Girdwood be->dpcm[stream].state); 201001d7584cSLiam Girdwood 201101d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 201201d7584cSLiam Girdwood continue; 201301d7584cSLiam Girdwood 201401d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 20159c0ac70aSKai Chieh Chuang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { 20169c0ac70aSKai Chieh Chuang soc_pcm_hw_free(be_substream); 20179c0ac70aSKai Chieh Chuang be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 20189c0ac70aSKai Chieh Chuang } 201901d7584cSLiam Girdwood 2020103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: close BE %s\n", 202194d215ccS彭东林 be->dai_link->name); 202201d7584cSLiam Girdwood 202301d7584cSLiam Girdwood soc_pcm_close(be_substream); 202401d7584cSLiam Girdwood be_substream->runtime = NULL; 202501d7584cSLiam Girdwood 202601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 202701d7584cSLiam Girdwood } 202801d7584cSLiam Girdwood return 0; 202901d7584cSLiam Girdwood } 203001d7584cSLiam Girdwood 203101d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 203201d7584cSLiam Girdwood { 203301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 203401d7584cSLiam Girdwood int stream = substream->stream; 203501d7584cSLiam Girdwood 2036ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 203701d7584cSLiam Girdwood 203801d7584cSLiam Girdwood /* shutdown the BEs */ 203925c2f515SKuninori Morimoto dpcm_be_dai_shutdown(fe, stream); 204001d7584cSLiam Girdwood 2041103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 204201d7584cSLiam Girdwood 204301d7584cSLiam Girdwood /* now shutdown the frontend */ 204401d7584cSLiam Girdwood soc_pcm_close(substream); 204501d7584cSLiam Girdwood 204601d7584cSLiam Girdwood /* run the stream event for each BE */ 20471c531230SKuninori Morimoto dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 204801d7584cSLiam Girdwood 204901d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2050ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 205101d7584cSLiam Girdwood return 0; 205201d7584cSLiam Girdwood } 205301d7584cSLiam Girdwood 205423607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 205501d7584cSLiam Girdwood { 205601d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 205701d7584cSLiam Girdwood 205801d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 205901d7584cSLiam Girdwood * to this frontend DAI */ 20608d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 206101d7584cSLiam Girdwood 206201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 206301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 206401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 206501d7584cSLiam Girdwood 206601d7584cSLiam Girdwood /* is this op for this BE ? */ 206701d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 206801d7584cSLiam Girdwood continue; 206901d7584cSLiam Girdwood 207001d7584cSLiam Girdwood /* only free hw when no longer used - check all FEs */ 207101d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 207201d7584cSLiam Girdwood continue; 207301d7584cSLiam Girdwood 207436fba62cSQiao Zhou /* do not free hw if this BE is used by other FE */ 207536fba62cSQiao Zhou if (be->dpcm[stream].users > 1) 207636fba62cSQiao Zhou continue; 207736fba62cSQiao Zhou 207801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 207901d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 208001d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 208108b27848SPatrick Lai (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 20825e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 20835e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 208401d7584cSLiam Girdwood continue; 208501d7584cSLiam Girdwood 2086103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 208794d215ccS彭东林 be->dai_link->name); 208801d7584cSLiam Girdwood 208901d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 209001d7584cSLiam Girdwood 209101d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 209201d7584cSLiam Girdwood } 209301d7584cSLiam Girdwood 209401d7584cSLiam Girdwood return 0; 209501d7584cSLiam Girdwood } 209601d7584cSLiam Girdwood 209745c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 209801d7584cSLiam Girdwood { 209901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 210001d7584cSLiam Girdwood int err, stream = substream->stream; 210101d7584cSLiam Girdwood 210201d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2103ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 210401d7584cSLiam Girdwood 2105103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 210601d7584cSLiam Girdwood 210701d7584cSLiam Girdwood /* call hw_free on the frontend */ 210801d7584cSLiam Girdwood err = soc_pcm_hw_free(substream); 210901d7584cSLiam Girdwood if (err < 0) 2110103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 211101d7584cSLiam Girdwood fe->dai_link->name); 211201d7584cSLiam Girdwood 211301d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 211401d7584cSLiam Girdwood * to this frontend DAI */ 211501d7584cSLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 211601d7584cSLiam Girdwood 211701d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2118ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 211901d7584cSLiam Girdwood 212001d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 212101d7584cSLiam Girdwood return 0; 212201d7584cSLiam Girdwood } 212301d7584cSLiam Girdwood 212423607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 212501d7584cSLiam Girdwood { 212601d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 212701d7584cSLiam Girdwood int ret; 212801d7584cSLiam Girdwood 21298d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 213001d7584cSLiam Girdwood 213101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 213201d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 213301d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 213401d7584cSLiam Girdwood 213501d7584cSLiam Girdwood /* is this op for this BE ? */ 213601d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 213701d7584cSLiam Girdwood continue; 213801d7584cSLiam Girdwood 213901d7584cSLiam Girdwood /* copy params for each dpcm */ 214001d7584cSLiam Girdwood memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 214101d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 214201d7584cSLiam Girdwood 214301d7584cSLiam Girdwood /* perform any hw_params fixups */ 214401d7584cSLiam Girdwood if (be->dai_link->be_hw_params_fixup) { 214501d7584cSLiam Girdwood ret = be->dai_link->be_hw_params_fixup(be, 214601d7584cSLiam Girdwood &dpcm->hw_params); 214701d7584cSLiam Girdwood if (ret < 0) { 214801d7584cSLiam Girdwood dev_err(be->dev, 2149103d84a3SLiam Girdwood "ASoC: hw_params BE fixup failed %d\n", 215001d7584cSLiam Girdwood ret); 215101d7584cSLiam Girdwood goto unwind; 215201d7584cSLiam Girdwood } 215301d7584cSLiam Girdwood } 215401d7584cSLiam Girdwood 2155ae061d2aSLibin Yang /* copy the fixed-up hw params for BE dai */ 2156ae061d2aSLibin Yang memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, 2157ae061d2aSLibin Yang sizeof(struct snd_pcm_hw_params)); 2158ae061d2aSLibin Yang 2159b0639bd2SKuninori Morimoto /* only allow hw_params() if no connected FEs are running */ 2160b0639bd2SKuninori Morimoto if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2161b0639bd2SKuninori Morimoto continue; 2162b0639bd2SKuninori Morimoto 2163b0639bd2SKuninori Morimoto if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2164b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2165b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2166b0639bd2SKuninori Morimoto continue; 2167b0639bd2SKuninori Morimoto 2168b0639bd2SKuninori Morimoto dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 216994d215ccS彭东林 be->dai_link->name); 2170b0639bd2SKuninori Morimoto 217101d7584cSLiam Girdwood ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 217201d7584cSLiam Girdwood if (ret < 0) { 217301d7584cSLiam Girdwood dev_err(dpcm->be->dev, 2174103d84a3SLiam Girdwood "ASoC: hw_params BE failed %d\n", ret); 217501d7584cSLiam Girdwood goto unwind; 217601d7584cSLiam Girdwood } 217701d7584cSLiam Girdwood 217801d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 217901d7584cSLiam Girdwood } 218001d7584cSLiam Girdwood return 0; 218101d7584cSLiam Girdwood 218201d7584cSLiam Girdwood unwind: 218301d7584cSLiam Girdwood /* disable any enabled and non active backends */ 21848d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 218501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 218601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 218701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 218801d7584cSLiam Girdwood 218901d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 219001d7584cSLiam Girdwood continue; 219101d7584cSLiam Girdwood 219201d7584cSLiam Girdwood /* only allow hw_free() if no connected FEs are running */ 219301d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 219401d7584cSLiam Girdwood continue; 219501d7584cSLiam Girdwood 219601d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 219701d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 219801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 219901d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 220001d7584cSLiam Girdwood continue; 220101d7584cSLiam Girdwood 220201d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 220301d7584cSLiam Girdwood } 220401d7584cSLiam Girdwood 220501d7584cSLiam Girdwood return ret; 220601d7584cSLiam Girdwood } 220701d7584cSLiam Girdwood 220845c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 220901d7584cSLiam Girdwood struct snd_pcm_hw_params *params) 221001d7584cSLiam Girdwood { 221101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 221201d7584cSLiam Girdwood int ret, stream = substream->stream; 221301d7584cSLiam Girdwood 221401d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2215ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 221601d7584cSLiam Girdwood 221725c2f515SKuninori Morimoto memcpy(&fe->dpcm[stream].hw_params, params, 221801d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 221925c2f515SKuninori Morimoto ret = dpcm_be_dai_hw_params(fe, stream); 222001d7584cSLiam Girdwood if (ret < 0) { 2221103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 222201d7584cSLiam Girdwood goto out; 222301d7584cSLiam Girdwood } 222401d7584cSLiam Girdwood 2225103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 222601d7584cSLiam Girdwood fe->dai_link->name, params_rate(params), 222701d7584cSLiam Girdwood params_channels(params), params_format(params)); 222801d7584cSLiam Girdwood 222901d7584cSLiam Girdwood /* call hw_params on the frontend */ 223001d7584cSLiam Girdwood ret = soc_pcm_hw_params(substream, params); 223101d7584cSLiam Girdwood if (ret < 0) { 2232103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 223301d7584cSLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 223401d7584cSLiam Girdwood } else 223501d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 223601d7584cSLiam Girdwood 223701d7584cSLiam Girdwood out: 2238ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 223901d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 224001d7584cSLiam Girdwood return ret; 224101d7584cSLiam Girdwood } 224201d7584cSLiam Girdwood 224301d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 224401d7584cSLiam Girdwood struct snd_pcm_substream *substream, int cmd) 224501d7584cSLiam Girdwood { 224601d7584cSLiam Girdwood int ret; 224701d7584cSLiam Girdwood 2248103d84a3SLiam Girdwood dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 224994d215ccS彭东林 dpcm->be->dai_link->name, cmd); 225001d7584cSLiam Girdwood 225101d7584cSLiam Girdwood ret = soc_pcm_trigger(substream, cmd); 225201d7584cSLiam Girdwood if (ret < 0) 2253103d84a3SLiam Girdwood dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 225401d7584cSLiam Girdwood 225501d7584cSLiam Girdwood return ret; 225601d7584cSLiam Girdwood } 225701d7584cSLiam Girdwood 225823607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 225945c0a188SMark Brown int cmd) 226001d7584cSLiam Girdwood { 226101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 226201d7584cSLiam Girdwood int ret = 0; 226301d7584cSLiam Girdwood 22648d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 226501d7584cSLiam Girdwood 226601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 226701d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 226801d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 226901d7584cSLiam Girdwood 227001d7584cSLiam Girdwood /* is this op for this BE ? */ 227101d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 227201d7584cSLiam Girdwood continue; 227301d7584cSLiam Girdwood 227401d7584cSLiam Girdwood switch (cmd) { 227501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 227601d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 227721fca8bdS이경택 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 227821fca8bdS이경택 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 227901d7584cSLiam Girdwood continue; 228001d7584cSLiam Girdwood 228101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 228201d7584cSLiam Girdwood if (ret) 228301d7584cSLiam Girdwood return ret; 228401d7584cSLiam Girdwood 228501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 228601d7584cSLiam Girdwood break; 228701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 228801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 228901d7584cSLiam Girdwood continue; 229001d7584cSLiam Girdwood 229101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 229201d7584cSLiam Girdwood if (ret) 229301d7584cSLiam Girdwood return ret; 229401d7584cSLiam Girdwood 229501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 229601d7584cSLiam Girdwood break; 229701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 229801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 229901d7584cSLiam Girdwood continue; 230001d7584cSLiam Girdwood 230101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 230201d7584cSLiam Girdwood if (ret) 230301d7584cSLiam Girdwood return ret; 230401d7584cSLiam Girdwood 230501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 230601d7584cSLiam Girdwood break; 230701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 230821fca8bdS이경택 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) && 230921fca8bdS이경택 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 231001d7584cSLiam Girdwood continue; 231101d7584cSLiam Girdwood 231201d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 231301d7584cSLiam Girdwood continue; 231401d7584cSLiam Girdwood 231501d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 231601d7584cSLiam Girdwood if (ret) 231701d7584cSLiam Girdwood return ret; 231801d7584cSLiam Girdwood 231901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 232001d7584cSLiam Girdwood break; 232101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 2322868a6ca8SNicolin Chen if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 232301d7584cSLiam Girdwood continue; 232401d7584cSLiam Girdwood 232501d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 232601d7584cSLiam Girdwood continue; 232701d7584cSLiam Girdwood 232801d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 232901d7584cSLiam Girdwood if (ret) 233001d7584cSLiam Girdwood return ret; 233101d7584cSLiam Girdwood 233201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND; 233301d7584cSLiam Girdwood break; 233401d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 233501d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 233601d7584cSLiam Girdwood continue; 233701d7584cSLiam Girdwood 233801d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 233901d7584cSLiam Girdwood continue; 234001d7584cSLiam Girdwood 234101d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 234201d7584cSLiam Girdwood if (ret) 234301d7584cSLiam Girdwood return ret; 234401d7584cSLiam Girdwood 234501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 234601d7584cSLiam Girdwood break; 234701d7584cSLiam Girdwood } 234801d7584cSLiam Girdwood } 234901d7584cSLiam Girdwood 235001d7584cSLiam Girdwood return ret; 235101d7584cSLiam Girdwood } 235201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 235301d7584cSLiam Girdwood 2354acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, 2355acbf2774SRanjani Sridharan int cmd, bool fe_first) 2356acbf2774SRanjani Sridharan { 2357acbf2774SRanjani Sridharan struct snd_soc_pcm_runtime *fe = substream->private_data; 2358acbf2774SRanjani Sridharan int ret; 2359acbf2774SRanjani Sridharan 2360acbf2774SRanjani Sridharan /* call trigger on the frontend before the backend. */ 2361acbf2774SRanjani Sridharan if (fe_first) { 2362acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2363acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2364acbf2774SRanjani Sridharan 2365acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2366acbf2774SRanjani Sridharan if (ret < 0) 2367acbf2774SRanjani Sridharan return ret; 2368acbf2774SRanjani Sridharan 2369acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2370acbf2774SRanjani Sridharan return ret; 2371acbf2774SRanjani Sridharan } 2372acbf2774SRanjani Sridharan 2373acbf2774SRanjani Sridharan /* call trigger on the frontend after the backend. */ 2374acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2375acbf2774SRanjani Sridharan if (ret < 0) 2376acbf2774SRanjani Sridharan return ret; 2377acbf2774SRanjani Sridharan 2378acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2379acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2380acbf2774SRanjani Sridharan 2381acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2382acbf2774SRanjani Sridharan 2383acbf2774SRanjani Sridharan return ret; 2384acbf2774SRanjani Sridharan } 2385acbf2774SRanjani Sridharan 2386ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 238701d7584cSLiam Girdwood { 238801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 2389acbf2774SRanjani Sridharan int stream = substream->stream; 2390acbf2774SRanjani Sridharan int ret = 0; 239101d7584cSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 239201d7584cSLiam Girdwood 239301d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 239401d7584cSLiam Girdwood 239501d7584cSLiam Girdwood switch (trigger) { 239601d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_PRE: 2397acbf2774SRanjani Sridharan switch (cmd) { 2398acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2399acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2400acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2401acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2402acbf2774SRanjani Sridharan break; 2403acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2404acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2405acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2406acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2407acbf2774SRanjani Sridharan break; 2408acbf2774SRanjani Sridharan default: 2409acbf2774SRanjani Sridharan ret = -EINVAL; 2410acbf2774SRanjani Sridharan break; 241101d7584cSLiam Girdwood } 241201d7584cSLiam Girdwood break; 241301d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_POST: 2414acbf2774SRanjani Sridharan switch (cmd) { 2415acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2416acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2417acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2418acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2419acbf2774SRanjani Sridharan break; 2420acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2421acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2422acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2423acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2424acbf2774SRanjani Sridharan break; 2425acbf2774SRanjani Sridharan default: 2426acbf2774SRanjani Sridharan ret = -EINVAL; 2427acbf2774SRanjani Sridharan break; 242801d7584cSLiam Girdwood } 242901d7584cSLiam Girdwood break; 243007bf84aaSLiam Girdwood case SND_SOC_DPCM_TRIGGER_BESPOKE: 243107bf84aaSLiam Girdwood /* bespoke trigger() - handles both FE and BEs */ 243207bf84aaSLiam Girdwood 2433103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 243407bf84aaSLiam Girdwood fe->dai_link->name, cmd); 243507bf84aaSLiam Girdwood 243630819358SKuninori Morimoto ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd); 243707bf84aaSLiam Girdwood break; 243801d7584cSLiam Girdwood default: 2439103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 244001d7584cSLiam Girdwood fe->dai_link->name); 244101d7584cSLiam Girdwood ret = -EINVAL; 244201d7584cSLiam Girdwood goto out; 244301d7584cSLiam Girdwood } 244401d7584cSLiam Girdwood 2445acbf2774SRanjani Sridharan if (ret < 0) { 2446acbf2774SRanjani Sridharan dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", 2447acbf2774SRanjani Sridharan cmd, ret); 2448acbf2774SRanjani Sridharan goto out; 2449acbf2774SRanjani Sridharan } 2450acbf2774SRanjani Sridharan 245101d7584cSLiam Girdwood switch (cmd) { 245201d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 245301d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 245401d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 245501d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 245601d7584cSLiam Girdwood break; 245701d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 245801d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 245901d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 246001d7584cSLiam Girdwood break; 24619f169b9fSPatrick Lai case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 24629f169b9fSPatrick Lai fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 24639f169b9fSPatrick Lai break; 246401d7584cSLiam Girdwood } 246501d7584cSLiam Girdwood 246601d7584cSLiam Girdwood out: 246701d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 246801d7584cSLiam Girdwood return ret; 246901d7584cSLiam Girdwood } 247001d7584cSLiam Girdwood 2471ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2472ea9d0d77STakashi Iwai { 2473ea9d0d77STakashi Iwai struct snd_soc_pcm_runtime *fe = substream->private_data; 2474ea9d0d77STakashi Iwai int stream = substream->stream; 2475ea9d0d77STakashi Iwai 2476ea9d0d77STakashi Iwai /* if FE's runtime_update is already set, we're in race; 2477ea9d0d77STakashi Iwai * process this trigger later at exit 2478ea9d0d77STakashi Iwai */ 2479ea9d0d77STakashi Iwai if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2480ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = cmd + 1; 2481ea9d0d77STakashi Iwai return 0; /* delayed, assuming it's successful */ 2482ea9d0d77STakashi Iwai } 2483ea9d0d77STakashi Iwai 2484ea9d0d77STakashi Iwai /* we're alone, let's trigger */ 2485ea9d0d77STakashi Iwai return dpcm_fe_dai_do_trigger(substream, cmd); 2486ea9d0d77STakashi Iwai } 2487ea9d0d77STakashi Iwai 248823607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 248901d7584cSLiam Girdwood { 249001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 249101d7584cSLiam Girdwood int ret = 0; 249201d7584cSLiam Girdwood 24938d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 249401d7584cSLiam Girdwood 249501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 249601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 249701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 249801d7584cSLiam Girdwood 249901d7584cSLiam Girdwood /* is this op for this BE ? */ 250001d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 250101d7584cSLiam Girdwood continue; 250201d7584cSLiam Girdwood 250301d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 250495f444dcSKoro Chen (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 25055087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && 25065087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 250701d7584cSLiam Girdwood continue; 250801d7584cSLiam Girdwood 2509103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: prepare BE %s\n", 251094d215ccS彭东林 be->dai_link->name); 251101d7584cSLiam Girdwood 251201d7584cSLiam Girdwood ret = soc_pcm_prepare(be_substream); 251301d7584cSLiam Girdwood if (ret < 0) { 2514103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: backend prepare failed %d\n", 251501d7584cSLiam Girdwood ret); 251601d7584cSLiam Girdwood break; 251701d7584cSLiam Girdwood } 251801d7584cSLiam Girdwood 251901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 252001d7584cSLiam Girdwood } 252101d7584cSLiam Girdwood return ret; 252201d7584cSLiam Girdwood } 252301d7584cSLiam Girdwood 252445c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 252501d7584cSLiam Girdwood { 252601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 252701d7584cSLiam Girdwood int stream = substream->stream, ret = 0; 252801d7584cSLiam Girdwood 252901d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 253001d7584cSLiam Girdwood 2531103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 253201d7584cSLiam Girdwood 2533ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 253401d7584cSLiam Girdwood 253501d7584cSLiam Girdwood /* there is no point preparing this FE if there are no BEs */ 253601d7584cSLiam Girdwood if (list_empty(&fe->dpcm[stream].be_clients)) { 2537103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 253801d7584cSLiam Girdwood fe->dai_link->name); 253901d7584cSLiam Girdwood ret = -EINVAL; 254001d7584cSLiam Girdwood goto out; 254101d7584cSLiam Girdwood } 254201d7584cSLiam Girdwood 254325c2f515SKuninori Morimoto ret = dpcm_be_dai_prepare(fe, stream); 254401d7584cSLiam Girdwood if (ret < 0) 254501d7584cSLiam Girdwood goto out; 254601d7584cSLiam Girdwood 254701d7584cSLiam Girdwood /* call prepare on the frontend */ 254801d7584cSLiam Girdwood ret = soc_pcm_prepare(substream); 254901d7584cSLiam Girdwood if (ret < 0) { 2550103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 255101d7584cSLiam Girdwood fe->dai_link->name); 255201d7584cSLiam Girdwood goto out; 255301d7584cSLiam Girdwood } 255401d7584cSLiam Girdwood 255501d7584cSLiam Girdwood /* run the stream event for each BE */ 255601d7584cSLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 255701d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 255801d7584cSLiam Girdwood 255901d7584cSLiam Girdwood out: 2560ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 256101d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 256201d7584cSLiam Girdwood 256301d7584cSLiam Girdwood return ret; 256401d7584cSLiam Girdwood } 256501d7584cSLiam Girdwood 2566618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2567618dae11SLiam Girdwood { 256807bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 256907bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 257007bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2571618dae11SLiam Girdwood int err; 257201d7584cSLiam Girdwood 2573103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2574618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2575618dae11SLiam Girdwood 257607bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 257707bf84aaSLiam Girdwood /* call bespoke trigger - FE takes care of all BE triggers */ 2578103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 257907bf84aaSLiam Girdwood fe->dai_link->name); 258007bf84aaSLiam Girdwood 258130819358SKuninori Morimoto err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 258207bf84aaSLiam Girdwood if (err < 0) 2583103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 258407bf84aaSLiam Girdwood } else { 2585103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 258607bf84aaSLiam Girdwood fe->dai_link->name); 258707bf84aaSLiam Girdwood 2588618dae11SLiam Girdwood err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2589618dae11SLiam Girdwood if (err < 0) 2590103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 259107bf84aaSLiam Girdwood } 2592618dae11SLiam Girdwood 2593618dae11SLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 2594618dae11SLiam Girdwood if (err < 0) 2595103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 2596618dae11SLiam Girdwood 2597618dae11SLiam Girdwood err = dpcm_be_dai_shutdown(fe, stream); 2598618dae11SLiam Girdwood if (err < 0) 2599103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 2600618dae11SLiam Girdwood 2601618dae11SLiam Girdwood /* run the stream event for each BE */ 2602618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2603618dae11SLiam Girdwood 2604618dae11SLiam Girdwood return 0; 2605618dae11SLiam Girdwood } 2606618dae11SLiam Girdwood 2607618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2608618dae11SLiam Girdwood { 260907bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 261007bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 2611618dae11SLiam Girdwood struct snd_soc_dpcm *dpcm; 261207bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2613618dae11SLiam Girdwood int ret; 2614a9764869SKaiChieh Chuang unsigned long flags; 2615618dae11SLiam Girdwood 2616103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2617618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2618618dae11SLiam Girdwood 2619618dae11SLiam Girdwood /* Only start the BE if the FE is ready */ 2620618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2621618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 2622618dae11SLiam Girdwood return -EINVAL; 2623618dae11SLiam Girdwood 2624618dae11SLiam Girdwood /* startup must always be called for new BEs */ 2625618dae11SLiam Girdwood ret = dpcm_be_dai_startup(fe, stream); 2626fffc0ca2SDan Carpenter if (ret < 0) 2627618dae11SLiam Girdwood goto disconnect; 2628618dae11SLiam Girdwood 2629618dae11SLiam Girdwood /* keep going if FE state is > open */ 2630618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2631618dae11SLiam Girdwood return 0; 2632618dae11SLiam Girdwood 2633618dae11SLiam Girdwood ret = dpcm_be_dai_hw_params(fe, stream); 2634fffc0ca2SDan Carpenter if (ret < 0) 2635618dae11SLiam Girdwood goto close; 2636618dae11SLiam Girdwood 2637618dae11SLiam Girdwood /* keep going if FE state is > hw_params */ 2638618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2639618dae11SLiam Girdwood return 0; 2640618dae11SLiam Girdwood 2641618dae11SLiam Girdwood 2642618dae11SLiam Girdwood ret = dpcm_be_dai_prepare(fe, stream); 2643fffc0ca2SDan Carpenter if (ret < 0) 2644618dae11SLiam Girdwood goto hw_free; 2645618dae11SLiam Girdwood 2646618dae11SLiam Girdwood /* run the stream event for each BE */ 2647618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2648618dae11SLiam Girdwood 2649618dae11SLiam Girdwood /* keep going if FE state is > prepare */ 2650618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2651618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2652618dae11SLiam Girdwood return 0; 2653618dae11SLiam Girdwood 265407bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 265507bf84aaSLiam Girdwood /* call trigger on the frontend - FE takes care of all BE triggers */ 2656103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 265707bf84aaSLiam Girdwood fe->dai_link->name); 265807bf84aaSLiam Girdwood 265930819358SKuninori Morimoto ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 266007bf84aaSLiam Girdwood if (ret < 0) { 2661103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 266207bf84aaSLiam Girdwood goto hw_free; 266307bf84aaSLiam Girdwood } 266407bf84aaSLiam Girdwood } else { 2665103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 2666618dae11SLiam Girdwood fe->dai_link->name); 2667618dae11SLiam Girdwood 2668618dae11SLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream, 2669618dae11SLiam Girdwood SNDRV_PCM_TRIGGER_START); 2670618dae11SLiam Girdwood if (ret < 0) { 2671103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2672618dae11SLiam Girdwood goto hw_free; 2673618dae11SLiam Girdwood } 267407bf84aaSLiam Girdwood } 2675618dae11SLiam Girdwood 2676618dae11SLiam Girdwood return 0; 2677618dae11SLiam Girdwood 2678618dae11SLiam Girdwood hw_free: 2679618dae11SLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 2680618dae11SLiam Girdwood close: 2681618dae11SLiam Girdwood dpcm_be_dai_shutdown(fe, stream); 2682618dae11SLiam Girdwood disconnect: 2683618dae11SLiam Girdwood /* disconnect any non started BEs */ 2684a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 26858d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2686618dae11SLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 2687618dae11SLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2688618dae11SLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2689618dae11SLiam Girdwood } 2690a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 2691618dae11SLiam Girdwood 2692618dae11SLiam Girdwood return ret; 2693618dae11SLiam Girdwood } 2694618dae11SLiam Girdwood 2695de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2696618dae11SLiam Girdwood { 2697618dae11SLiam Girdwood struct snd_soc_dapm_widget_list *list; 26987083f877SKuninori Morimoto int stream; 2699de15d7ffSJerome Brunet int count, paths; 2700580dff36SKuninori Morimoto int ret; 2701618dae11SLiam Girdwood 27026e1276a5SBard Liao if (fe->num_cpus > 1) { 27036e1276a5SBard Liao dev_err(fe->dev, 27046e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__); 27056e1276a5SBard Liao return -EINVAL; 27066e1276a5SBard Liao } 27076e1276a5SBard Liao 2708618dae11SLiam Girdwood if (!fe->dai_link->dynamic) 2709de15d7ffSJerome Brunet return 0; 2710618dae11SLiam Girdwood 2711618dae11SLiam Girdwood /* only check active links */ 2712*b3dea624SKuninori Morimoto if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0))) 2713de15d7ffSJerome Brunet return 0; 2714618dae11SLiam Girdwood 2715618dae11SLiam Girdwood /* DAPM sync will call this to update DSP paths */ 2716de15d7ffSJerome Brunet dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2717de15d7ffSJerome Brunet new ? "new" : "old", fe->dai_link->name); 2718618dae11SLiam Girdwood 27197083f877SKuninori Morimoto for_each_pcm_streams(stream) { 2720075207d2SQiao Zhou 27217083f877SKuninori Morimoto /* skip if FE doesn't have playback/capture capability */ 2722c2233a26SKuninori Morimoto if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) || 2723c2233a26SKuninori Morimoto !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream)) 27247083f877SKuninori Morimoto continue; 2725618dae11SLiam Girdwood 27267083f877SKuninori Morimoto /* skip if FE isn't currently playing/capturing */ 2727*b3dea624SKuninori Morimoto if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) || 2728*b3dea624SKuninori Morimoto !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream)) 27297083f877SKuninori Morimoto continue; 27307083f877SKuninori Morimoto 27317083f877SKuninori Morimoto paths = dpcm_path_get(fe, stream, &list); 2732618dae11SLiam Girdwood if (paths < 0) { 2733103d84a3SLiam Girdwood dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 27347083f877SKuninori Morimoto fe->dai_link->name, 27357083f877SKuninori Morimoto stream == SNDRV_PCM_STREAM_PLAYBACK ? 27367083f877SKuninori Morimoto "playback" : "capture"); 2737618dae11SLiam Girdwood return paths; 2738618dae11SLiam Girdwood } 2739618dae11SLiam Girdwood 27407083f877SKuninori Morimoto /* update any playback/capture paths */ 27417083f877SKuninori Morimoto count = dpcm_process_paths(fe, stream, &list, new); 2742de15d7ffSJerome Brunet if (count) { 2743580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2744de15d7ffSJerome Brunet if (new) 2745580dff36SKuninori Morimoto ret = dpcm_run_update_startup(fe, stream); 2746de15d7ffSJerome Brunet else 2747580dff36SKuninori Morimoto ret = dpcm_run_update_shutdown(fe, stream); 2748580dff36SKuninori Morimoto if (ret < 0) 2749580dff36SKuninori Morimoto dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2750580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2751de15d7ffSJerome Brunet 27527083f877SKuninori Morimoto dpcm_clear_pending_state(fe, stream); 27537083f877SKuninori Morimoto dpcm_be_disconnect(fe, stream); 2754618dae11SLiam Girdwood } 2755618dae11SLiam Girdwood 27567ed9de76SQiao Zhou dpcm_path_put(&list); 2757618dae11SLiam Girdwood } 2758618dae11SLiam Girdwood 2759de15d7ffSJerome Brunet return 0; 2760618dae11SLiam Girdwood } 2761618dae11SLiam Girdwood 2762de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2763de15d7ffSJerome Brunet * any DAI links. 2764de15d7ffSJerome Brunet */ 2765f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card) 2766de15d7ffSJerome Brunet { 2767de15d7ffSJerome Brunet struct snd_soc_pcm_runtime *fe; 2768de15d7ffSJerome Brunet int ret = 0; 2769de15d7ffSJerome Brunet 2770de15d7ffSJerome Brunet mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2771de15d7ffSJerome Brunet /* shutdown all old paths first */ 2772bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2773de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 0); 2774de15d7ffSJerome Brunet if (ret) 2775de15d7ffSJerome Brunet goto out; 2776de15d7ffSJerome Brunet } 2777de15d7ffSJerome Brunet 2778de15d7ffSJerome Brunet /* bring new paths up */ 2779bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2780de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 1); 2781de15d7ffSJerome Brunet if (ret) 2782de15d7ffSJerome Brunet goto out; 2783de15d7ffSJerome Brunet } 2784de15d7ffSJerome Brunet 2785de15d7ffSJerome Brunet out: 2786618dae11SLiam Girdwood mutex_unlock(&card->mutex); 2787de15d7ffSJerome Brunet return ret; 2788618dae11SLiam Girdwood } 2789f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update); 279001d7584cSLiam Girdwood 2791265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream) 279201d7584cSLiam Girdwood { 279301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 279401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 2795265694b6SKuninori Morimoto int stream = fe_substream->stream; 279630fca26fSKuninori Morimoto 279730fca26fSKuninori Morimoto /* mark FE's links ready to prune */ 279830fca26fSKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 279930fca26fSKuninori Morimoto dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 280030fca26fSKuninori Morimoto 280130fca26fSKuninori Morimoto dpcm_be_disconnect(fe, stream); 280230fca26fSKuninori Morimoto 280330fca26fSKuninori Morimoto fe->dpcm[stream].runtime = NULL; 2804265694b6SKuninori Morimoto } 2805265694b6SKuninori Morimoto 2806265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 2807265694b6SKuninori Morimoto { 2808265694b6SKuninori Morimoto struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 2809265694b6SKuninori Morimoto int ret; 2810265694b6SKuninori Morimoto 2811265694b6SKuninori Morimoto mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2812265694b6SKuninori Morimoto ret = dpcm_fe_dai_shutdown(fe_substream); 2813265694b6SKuninori Morimoto 2814265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream); 2815265694b6SKuninori Morimoto 281630fca26fSKuninori Morimoto mutex_unlock(&fe->card->mutex); 281730fca26fSKuninori Morimoto return ret; 281830fca26fSKuninori Morimoto } 281930fca26fSKuninori Morimoto 282001d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 282101d7584cSLiam Girdwood { 282201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 282301d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list; 282401d7584cSLiam Girdwood int ret; 282501d7584cSLiam Girdwood int stream = fe_substream->stream; 282601d7584cSLiam Girdwood 282701d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 282801d7584cSLiam Girdwood fe->dpcm[stream].runtime = fe_substream->runtime; 282901d7584cSLiam Girdwood 28308f70e515SQiao Zhou ret = dpcm_path_get(fe, stream, &list); 28318f70e515SQiao Zhou if (ret < 0) { 2832cae06eb9SKuninori Morimoto goto open_end; 28338f70e515SQiao Zhou } else if (ret == 0) { 2834103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 283501d7584cSLiam Girdwood fe->dai_link->name, stream ? "capture" : "playback"); 283601d7584cSLiam Girdwood } 283701d7584cSLiam Girdwood 283801d7584cSLiam Girdwood /* calculate valid and active FE <-> BE dpcms */ 283901d7584cSLiam Girdwood dpcm_process_paths(fe, stream, &list, 1); 284001d7584cSLiam Girdwood 284101d7584cSLiam Girdwood ret = dpcm_fe_dai_startup(fe_substream); 2842265694b6SKuninori Morimoto if (ret < 0) 2843265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream); 284401d7584cSLiam Girdwood 284501d7584cSLiam Girdwood dpcm_clear_pending_state(fe, stream); 284601d7584cSLiam Girdwood dpcm_path_put(&list); 2847cae06eb9SKuninori Morimoto open_end: 284801d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 284901d7584cSLiam Girdwood return ret; 285001d7584cSLiam Girdwood } 285101d7584cSLiam Girdwood 2852ddee627cSLiam Girdwood /* create a new pcm */ 2853ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2854ddee627cSLiam Girdwood { 28552e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 285619bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai; 28572b544dd7SKuninori Morimoto struct snd_soc_component *component; 2858ddee627cSLiam Girdwood struct snd_pcm *pcm; 2859ddee627cSLiam Girdwood char new_name[64]; 2860ddee627cSLiam Girdwood int ret = 0, playback = 0, capture = 0; 28612e5894d7SBenoit Cousson int i; 2862ddee627cSLiam Girdwood 286301d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 28649b5db059SStephan Gerhold cpu_dai = asoc_rtd_to_cpu(rtd, 0); 28659b5db059SStephan Gerhold if (rtd->num_cpus > 1) { 28669b5db059SStephan Gerhold dev_err(rtd->dev, 28679b5db059SStephan Gerhold "DPCM doesn't support Multi CPU yet\n"); 28689b5db059SStephan Gerhold return -EINVAL; 28699b5db059SStephan Gerhold } 28709b5db059SStephan Gerhold 28719b5db059SStephan Gerhold playback = rtd->dai_link->dpcm_playback && 28729b5db059SStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK); 28739b5db059SStephan Gerhold capture = rtd->dai_link->dpcm_capture && 28749b5db059SStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE); 287501d7584cSLiam Girdwood } else { 2876a342031cSJerome Brunet /* Adapt stream for codec2codec links */ 2877a4877a6fSStephan Gerhold int cpu_capture = rtd->dai_link->params ? 2878a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 2879a4877a6fSStephan Gerhold int cpu_playback = rtd->dai_link->params ? 2880a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 2881a342031cSJerome Brunet 2882a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) { 288319bdcc7aSShreyas NC if (rtd->num_cpus == 1) { 2884c2233a26SKuninori Morimoto cpu_dai = asoc_rtd_to_cpu(rtd, 0); 288519bdcc7aSShreyas NC } else if (rtd->num_cpus == rtd->num_codecs) { 2886c2233a26SKuninori Morimoto cpu_dai = asoc_rtd_to_cpu(rtd, i); 288719bdcc7aSShreyas NC } else { 288819bdcc7aSShreyas NC dev_err(rtd->card->dev, 288919bdcc7aSShreyas NC "N cpus to M codecs link is not supported yet\n"); 289019bdcc7aSShreyas NC return -EINVAL; 289119bdcc7aSShreyas NC } 289219bdcc7aSShreyas NC 2893467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 2894a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) 2895ddee627cSLiam Girdwood playback = 1; 2896467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 2897a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) 2898ddee627cSLiam Girdwood capture = 1; 289901d7584cSLiam Girdwood } 29002e5894d7SBenoit Cousson } 29012e5894d7SBenoit Cousson 2902d6bead02SFabio Estevam if (rtd->dai_link->playback_only) { 2903d6bead02SFabio Estevam playback = 1; 2904d6bead02SFabio Estevam capture = 0; 2905d6bead02SFabio Estevam } 2906d6bead02SFabio Estevam 2907d6bead02SFabio Estevam if (rtd->dai_link->capture_only) { 2908d6bead02SFabio Estevam playback = 0; 2909d6bead02SFabio Estevam capture = 1; 2910d6bead02SFabio Estevam } 2911d6bead02SFabio Estevam 291201d7584cSLiam Girdwood /* create the PCM */ 2913a342031cSJerome Brunet if (rtd->dai_link->params) { 2914a342031cSJerome Brunet snprintf(new_name, sizeof(new_name), "codec2codec(%s)", 2915a342031cSJerome Brunet rtd->dai_link->stream_name); 2916a342031cSJerome Brunet 2917a342031cSJerome Brunet ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 2918a342031cSJerome Brunet playback, capture, &pcm); 2919a342031cSJerome Brunet } else if (rtd->dai_link->no_pcm) { 292001d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)", 292101d7584cSLiam Girdwood rtd->dai_link->stream_name); 292201d7584cSLiam Girdwood 292301d7584cSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 292401d7584cSLiam Girdwood playback, capture, &pcm); 292501d7584cSLiam Girdwood } else { 292601d7584cSLiam Girdwood if (rtd->dai_link->dynamic) 292701d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s (*)", 292801d7584cSLiam Girdwood rtd->dai_link->stream_name); 292901d7584cSLiam Girdwood else 293001d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s %s-%d", 29312e5894d7SBenoit Cousson rtd->dai_link->stream_name, 29322e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? 2933c2233a26SKuninori Morimoto "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num); 293401d7584cSLiam Girdwood 293501d7584cSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 293601d7584cSLiam Girdwood capture, &pcm); 293701d7584cSLiam Girdwood } 2938ddee627cSLiam Girdwood if (ret < 0) { 2939103d84a3SLiam Girdwood dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 29405cb9b748SLiam Girdwood rtd->dai_link->name); 2941ddee627cSLiam Girdwood return ret; 2942ddee627cSLiam Girdwood } 2943103d84a3SLiam Girdwood dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 2944ddee627cSLiam Girdwood 2945ddee627cSLiam Girdwood /* DAPM dai link stream work */ 2946a342031cSJerome Brunet if (rtd->dai_link->params) 29474bf2e385SCurtis Malainey rtd->close_delayed_work_func = codec2codec_close_delayed_work; 2948a342031cSJerome Brunet else 294983f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work; 2950ddee627cSLiam Girdwood 295148c7699fSVinod Koul pcm->nonatomic = rtd->dai_link->nonatomic; 2952ddee627cSLiam Girdwood rtd->pcm = pcm; 2953ddee627cSLiam Girdwood pcm->private_data = rtd; 295401d7584cSLiam Girdwood 2955a342031cSJerome Brunet if (rtd->dai_link->no_pcm || rtd->dai_link->params) { 295601d7584cSLiam Girdwood if (playback) 295701d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 295801d7584cSLiam Girdwood if (capture) 295901d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 296001d7584cSLiam Girdwood goto out; 296101d7584cSLiam Girdwood } 296201d7584cSLiam Girdwood 296301d7584cSLiam Girdwood /* ASoC PCM operations */ 296401d7584cSLiam Girdwood if (rtd->dai_link->dynamic) { 296501d7584cSLiam Girdwood rtd->ops.open = dpcm_fe_dai_open; 296601d7584cSLiam Girdwood rtd->ops.hw_params = dpcm_fe_dai_hw_params; 296701d7584cSLiam Girdwood rtd->ops.prepare = dpcm_fe_dai_prepare; 296801d7584cSLiam Girdwood rtd->ops.trigger = dpcm_fe_dai_trigger; 296901d7584cSLiam Girdwood rtd->ops.hw_free = dpcm_fe_dai_hw_free; 297001d7584cSLiam Girdwood rtd->ops.close = dpcm_fe_dai_close; 297101d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 297201d7584cSLiam Girdwood } else { 297301d7584cSLiam Girdwood rtd->ops.open = soc_pcm_open; 297401d7584cSLiam Girdwood rtd->ops.hw_params = soc_pcm_hw_params; 297501d7584cSLiam Girdwood rtd->ops.prepare = soc_pcm_prepare; 297601d7584cSLiam Girdwood rtd->ops.trigger = soc_pcm_trigger; 297701d7584cSLiam Girdwood rtd->ops.hw_free = soc_pcm_hw_free; 297801d7584cSLiam Girdwood rtd->ops.close = soc_pcm_close; 297901d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 298001d7584cSLiam Girdwood } 298101d7584cSLiam Girdwood 2982613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 29832b544dd7SKuninori Morimoto const struct snd_soc_component_driver *drv = component->driver; 2984b8135864SKuninori Morimoto 29853b1c952cSTakashi Iwai if (drv->ioctl) 29863b1c952cSTakashi Iwai rtd->ops.ioctl = snd_soc_pcm_component_ioctl; 29871e5ddb6bSTakashi Iwai if (drv->sync_stop) 29881e5ddb6bSTakashi Iwai rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; 2989e9067bb5SKuninori Morimoto if (drv->copy_user) 299082d81f5cSKuninori Morimoto rtd->ops.copy_user = snd_soc_pcm_component_copy_user; 2991e9067bb5SKuninori Morimoto if (drv->page) 29929c712e4fSKuninori Morimoto rtd->ops.page = snd_soc_pcm_component_page; 2993e9067bb5SKuninori Morimoto if (drv->mmap) 2994205875e1SKuninori Morimoto rtd->ops.mmap = snd_soc_pcm_component_mmap; 2995b8135864SKuninori Morimoto } 2996b8135864SKuninori Morimoto 2997ddee627cSLiam Girdwood if (playback) 299801d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 2999ddee627cSLiam Girdwood 3000ddee627cSLiam Girdwood if (capture) 300101d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 3002ddee627cSLiam Girdwood 3003b2b2afbbSKuninori Morimoto ret = snd_soc_pcm_component_new(rtd); 3004ddee627cSLiam Girdwood if (ret < 0) { 30057484291eSKuninori Morimoto dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret); 3006ddee627cSLiam Girdwood return ret; 3007ddee627cSLiam Girdwood } 3008c641e5b2SJohan Hovold 30093d21ef0bSTakashi Iwai pcm->no_device_suspend = true; 301001d7584cSLiam Girdwood out: 30112e5894d7SBenoit Cousson dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", 3012c2233a26SKuninori Morimoto (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, 3013c2233a26SKuninori Morimoto (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name); 3014ddee627cSLiam Girdwood return ret; 3015ddee627cSLiam Girdwood } 301601d7584cSLiam Girdwood 301701d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */ 301801d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 301901d7584cSLiam Girdwood { 302001d7584cSLiam Girdwood if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 302101d7584cSLiam Girdwood return 1; 302201d7584cSLiam Girdwood return 0; 302301d7584cSLiam Girdwood } 302401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 302501d7584cSLiam Girdwood 302601d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */ 302701d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 302801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 302901d7584cSLiam Girdwood { 303001d7584cSLiam Girdwood if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 303101d7584cSLiam Girdwood ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 303201d7584cSLiam Girdwood be->dpcm[stream].runtime_update)) 303301d7584cSLiam Girdwood return 1; 303401d7584cSLiam Girdwood return 0; 303501d7584cSLiam Girdwood } 303601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 303701d7584cSLiam Girdwood 303801d7584cSLiam Girdwood /* get the substream for this BE */ 303901d7584cSLiam Girdwood struct snd_pcm_substream * 304001d7584cSLiam Girdwood snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 304101d7584cSLiam Girdwood { 304201d7584cSLiam Girdwood return be->pcm->streams[stream].substream; 304301d7584cSLiam Girdwood } 304401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 304501d7584cSLiam Girdwood 3046085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, 3047085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, 3048085d22beSKuninori Morimoto int stream, 3049085d22beSKuninori Morimoto const enum snd_soc_dpcm_state *states, 3050085d22beSKuninori Morimoto int num_states) 305101d7584cSLiam Girdwood { 305201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 305301d7584cSLiam Girdwood int state; 3054a9764869SKaiChieh Chuang int ret = 1; 3055a9764869SKaiChieh Chuang unsigned long flags; 3056085d22beSKuninori Morimoto int i; 305701d7584cSLiam Girdwood 3058a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3059d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 306001d7584cSLiam Girdwood 306101d7584cSLiam Girdwood if (dpcm->fe == fe) 306201d7584cSLiam Girdwood continue; 306301d7584cSLiam Girdwood 306401d7584cSLiam Girdwood state = dpcm->fe->dpcm[stream].state; 3065085d22beSKuninori Morimoto for (i = 0; i < num_states; i++) { 3066085d22beSKuninori Morimoto if (state == states[i]) { 3067a9764869SKaiChieh Chuang ret = 0; 3068a9764869SKaiChieh Chuang break; 306901d7584cSLiam Girdwood } 3070a9764869SKaiChieh Chuang } 3071085d22beSKuninori Morimoto } 3072a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 307301d7584cSLiam Girdwood 3074085d22beSKuninori Morimoto /* it's safe to do this BE DAI */ 3075a9764869SKaiChieh Chuang return ret; 307601d7584cSLiam Girdwood } 3077085d22beSKuninori Morimoto 3078085d22beSKuninori Morimoto /* 3079085d22beSKuninori Morimoto * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 3080085d22beSKuninori Morimoto * are not running, paused or suspended for the specified stream direction. 3081085d22beSKuninori Morimoto */ 3082085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 3083085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, int stream) 3084085d22beSKuninori Morimoto { 3085085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3086085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3087085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3088085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3089085d22beSKuninori Morimoto }; 3090085d22beSKuninori Morimoto 3091085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 3092085d22beSKuninori Morimoto } 309301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 309401d7584cSLiam Girdwood 309501d7584cSLiam Girdwood /* 309601d7584cSLiam Girdwood * We can only change hw params a BE DAI if any of it's FE are not prepared, 309701d7584cSLiam Girdwood * running, paused or suspended for the specified stream direction. 309801d7584cSLiam Girdwood */ 309901d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 310001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 310101d7584cSLiam Girdwood { 3102085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3103085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3104085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3105085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3106085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PREPARE, 3107085d22beSKuninori Morimoto }; 310801d7584cSLiam Girdwood 3109085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 311001d7584cSLiam Girdwood } 311101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 3112