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 31*c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS 32*c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state) 33*c3212829SKuninori Morimoto { 34*c3212829SKuninori Morimoto switch (state) { 35*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_NEW: 36*c3212829SKuninori Morimoto return "new"; 37*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_OPEN: 38*c3212829SKuninori Morimoto return "open"; 39*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_PARAMS: 40*c3212829SKuninori Morimoto return "hw_params"; 41*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PREPARE: 42*c3212829SKuninori Morimoto return "prepare"; 43*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_START: 44*c3212829SKuninori Morimoto return "start"; 45*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_STOP: 46*c3212829SKuninori Morimoto return "stop"; 47*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_SUSPEND: 48*c3212829SKuninori Morimoto return "suspend"; 49*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PAUSED: 50*c3212829SKuninori Morimoto return "paused"; 51*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_FREE: 52*c3212829SKuninori Morimoto return "hw_free"; 53*c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_CLOSE: 54*c3212829SKuninori Morimoto return "close"; 55*c3212829SKuninori Morimoto } 56*c3212829SKuninori Morimoto 57*c3212829SKuninori Morimoto return "unknown"; 58*c3212829SKuninori Morimoto } 59*c3212829SKuninori Morimoto 60*c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe, 61*c3212829SKuninori Morimoto int stream, char *buf, size_t size) 62*c3212829SKuninori Morimoto { 63*c3212829SKuninori Morimoto struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params; 64*c3212829SKuninori Morimoto struct snd_soc_dpcm *dpcm; 65*c3212829SKuninori Morimoto ssize_t offset = 0; 66*c3212829SKuninori Morimoto unsigned long flags; 67*c3212829SKuninori Morimoto 68*c3212829SKuninori Morimoto /* FE state */ 69*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 70*c3212829SKuninori Morimoto "[%s - %s]\n", fe->dai_link->name, 71*c3212829SKuninori Morimoto stream ? "Capture" : "Playback"); 72*c3212829SKuninori Morimoto 73*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, "State: %s\n", 74*c3212829SKuninori Morimoto dpcm_state_string(fe->dpcm[stream].state)); 75*c3212829SKuninori Morimoto 76*c3212829SKuninori Morimoto if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 77*c3212829SKuninori Morimoto (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 78*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 79*c3212829SKuninori Morimoto "Hardware Params: " 80*c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 81*c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 82*c3212829SKuninori Morimoto params_channels(params), 83*c3212829SKuninori Morimoto params_rate(params)); 84*c3212829SKuninori Morimoto 85*c3212829SKuninori Morimoto /* BEs state */ 86*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, "Backends:\n"); 87*c3212829SKuninori Morimoto 88*c3212829SKuninori Morimoto if (list_empty(&fe->dpcm[stream].be_clients)) { 89*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 90*c3212829SKuninori Morimoto " No active DSP links\n"); 91*c3212829SKuninori Morimoto goto out; 92*c3212829SKuninori Morimoto } 93*c3212829SKuninori Morimoto 94*c3212829SKuninori Morimoto spin_lock_irqsave(&fe->card->dpcm_lock, flags); 95*c3212829SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 96*c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 97*c3212829SKuninori Morimoto params = &dpcm->hw_params; 98*c3212829SKuninori Morimoto 99*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 100*c3212829SKuninori Morimoto "- %s\n", be->dai_link->name); 101*c3212829SKuninori Morimoto 102*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 103*c3212829SKuninori Morimoto " State: %s\n", 104*c3212829SKuninori Morimoto dpcm_state_string(be->dpcm[stream].state)); 105*c3212829SKuninori Morimoto 106*c3212829SKuninori Morimoto if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) && 107*c3212829SKuninori Morimoto (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP)) 108*c3212829SKuninori Morimoto offset += snprintf(buf + offset, size - offset, 109*c3212829SKuninori Morimoto " Hardware Params: " 110*c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n", 111*c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)), 112*c3212829SKuninori Morimoto params_channels(params), 113*c3212829SKuninori Morimoto params_rate(params)); 114*c3212829SKuninori Morimoto } 115*c3212829SKuninori Morimoto spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 116*c3212829SKuninori Morimoto out: 117*c3212829SKuninori Morimoto return offset; 118*c3212829SKuninori Morimoto } 119*c3212829SKuninori Morimoto 120*c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf, 121*c3212829SKuninori Morimoto size_t count, loff_t *ppos) 122*c3212829SKuninori Morimoto { 123*c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *fe = file->private_data; 124*c3212829SKuninori Morimoto ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0; 125*c3212829SKuninori Morimoto int stream; 126*c3212829SKuninori Morimoto char *buf; 127*c3212829SKuninori Morimoto 128*c3212829SKuninori Morimoto buf = kmalloc(out_count, GFP_KERNEL); 129*c3212829SKuninori Morimoto if (!buf) 130*c3212829SKuninori Morimoto return -ENOMEM; 131*c3212829SKuninori Morimoto 132*c3212829SKuninori Morimoto for_each_pcm_streams(stream) 133*c3212829SKuninori Morimoto if (snd_soc_dai_stream_valid(fe->cpu_dai, stream)) 134*c3212829SKuninori Morimoto offset += dpcm_show_state(fe, stream, 135*c3212829SKuninori Morimoto buf + offset, 136*c3212829SKuninori Morimoto out_count - offset); 137*c3212829SKuninori Morimoto 138*c3212829SKuninori Morimoto ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset); 139*c3212829SKuninori Morimoto 140*c3212829SKuninori Morimoto kfree(buf); 141*c3212829SKuninori Morimoto return ret; 142*c3212829SKuninori Morimoto } 143*c3212829SKuninori Morimoto 144*c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = { 145*c3212829SKuninori Morimoto .open = simple_open, 146*c3212829SKuninori Morimoto .read = dpcm_state_read_file, 147*c3212829SKuninori Morimoto .llseek = default_llseek, 148*c3212829SKuninori Morimoto }; 149*c3212829SKuninori Morimoto 150*c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd) 151*c3212829SKuninori Morimoto { 152*c3212829SKuninori Morimoto if (!rtd->dai_link) 153*c3212829SKuninori Morimoto return; 154*c3212829SKuninori Morimoto 155*c3212829SKuninori Morimoto if (!rtd->dai_link->dynamic) 156*c3212829SKuninori Morimoto return; 157*c3212829SKuninori Morimoto 158*c3212829SKuninori Morimoto if (!rtd->card->debugfs_card_root) 159*c3212829SKuninori Morimoto return; 160*c3212829SKuninori Morimoto 161*c3212829SKuninori Morimoto rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name, 162*c3212829SKuninori Morimoto rtd->card->debugfs_card_root); 163*c3212829SKuninori Morimoto 164*c3212829SKuninori Morimoto debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root, 165*c3212829SKuninori Morimoto rtd, &dpcm_state_fops); 166*c3212829SKuninori Morimoto } 167*c3212829SKuninori Morimoto #endif 168*c3212829SKuninori Morimoto 169f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd, 170f183f927SKuninori Morimoto struct snd_pcm_substream *substream) 171f183f927SKuninori Morimoto { 172f183f927SKuninori Morimoto if (rtd->dai_link->ops && 173f183f927SKuninori Morimoto rtd->dai_link->ops->startup) 174f183f927SKuninori Morimoto return rtd->dai_link->ops->startup(substream); 175f183f927SKuninori Morimoto return 0; 176f183f927SKuninori Morimoto } 177f183f927SKuninori Morimoto 1780be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd, 1790be429f9SKuninori Morimoto struct snd_pcm_substream *substream) 1800be429f9SKuninori Morimoto { 1810be429f9SKuninori Morimoto if (rtd->dai_link->ops && 1820be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown) 1830be429f9SKuninori Morimoto rtd->dai_link->ops->shutdown(substream); 1840be429f9SKuninori Morimoto } 1850be429f9SKuninori Morimoto 18644c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd, 18744c1a75bSKuninori Morimoto struct snd_pcm_substream *substream) 18844c1a75bSKuninori Morimoto { 18944c1a75bSKuninori Morimoto if (rtd->dai_link->ops && 19044c1a75bSKuninori Morimoto rtd->dai_link->ops->prepare) 19144c1a75bSKuninori Morimoto return rtd->dai_link->ops->prepare(substream); 19244c1a75bSKuninori Morimoto return 0; 19344c1a75bSKuninori Morimoto } 19444c1a75bSKuninori Morimoto 195de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd, 196de9ad990SKuninori Morimoto struct snd_pcm_substream *substream, 197de9ad990SKuninori Morimoto struct snd_pcm_hw_params *params) 198de9ad990SKuninori Morimoto { 199de9ad990SKuninori Morimoto if (rtd->dai_link->ops && 200de9ad990SKuninori Morimoto rtd->dai_link->ops->hw_params) 201de9ad990SKuninori Morimoto return rtd->dai_link->ops->hw_params(substream, params); 202de9ad990SKuninori Morimoto return 0; 203de9ad990SKuninori Morimoto } 204de9ad990SKuninori Morimoto 20549f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd, 20649f020e5SKuninori Morimoto struct snd_pcm_substream *substream) 20749f020e5SKuninori Morimoto { 20849f020e5SKuninori Morimoto if (rtd->dai_link->ops && 20949f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free) 21049f020e5SKuninori Morimoto rtd->dai_link->ops->hw_free(substream); 21149f020e5SKuninori Morimoto } 21249f020e5SKuninori Morimoto 213ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd, 214ad2bf9f2SKuninori Morimoto struct snd_pcm_substream *substream, 215ad2bf9f2SKuninori Morimoto int cmd) 216ad2bf9f2SKuninori Morimoto { 217ad2bf9f2SKuninori Morimoto if (rtd->dai_link->ops && 218ad2bf9f2SKuninori Morimoto rtd->dai_link->ops->trigger) 219ad2bf9f2SKuninori Morimoto return rtd->dai_link->ops->trigger(substream, cmd); 220ad2bf9f2SKuninori Morimoto return 0; 221ad2bf9f2SKuninori Morimoto } 222ad2bf9f2SKuninori Morimoto 2237a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd, 2247a5aaba4SKuninori Morimoto int stream, int action) 2257a5aaba4SKuninori Morimoto { 2267a5aaba4SKuninori Morimoto struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 2277a5aaba4SKuninori Morimoto struct snd_soc_dai *codec_dai; 2287a5aaba4SKuninori Morimoto int i; 2297a5aaba4SKuninori Morimoto 2307a5aaba4SKuninori Morimoto lockdep_assert_held(&rtd->card->pcm_mutex); 2317a5aaba4SKuninori Morimoto 2320f6011fdSKuninori Morimoto cpu_dai->stream_active[stream] += action; 2337a5aaba4SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 2340f6011fdSKuninori Morimoto codec_dai->stream_active[stream] += action; 2357a5aaba4SKuninori Morimoto 2367a5aaba4SKuninori Morimoto cpu_dai->active += action; 2377a5aaba4SKuninori Morimoto cpu_dai->component->active += action; 2387a5aaba4SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 2397a5aaba4SKuninori Morimoto codec_dai->active += action; 2407a5aaba4SKuninori Morimoto codec_dai->component->active += action; 2417a5aaba4SKuninori Morimoto } 2427a5aaba4SKuninori Morimoto } 2437a5aaba4SKuninori Morimoto 24490996f43SLars-Peter Clausen /** 24524894b76SLars-Peter Clausen * snd_soc_runtime_activate() - Increment active count for PCM runtime components 24624894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is activated 24724894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 24824894b76SLars-Peter Clausen * 24924894b76SLars-Peter Clausen * Increments the active count for all the DAIs and components attached to a PCM 25024894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is opened. 25124894b76SLars-Peter Clausen * 25272b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 25324894b76SLars-Peter Clausen */ 25424894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream) 25524894b76SLars-Peter Clausen { 2567a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, 1); 25724894b76SLars-Peter Clausen } 25824894b76SLars-Peter Clausen 25924894b76SLars-Peter Clausen /** 26024894b76SLars-Peter Clausen * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components 26124894b76SLars-Peter Clausen * @rtd: ASoC PCM runtime that is deactivated 26224894b76SLars-Peter Clausen * @stream: Direction of the PCM stream 26324894b76SLars-Peter Clausen * 26424894b76SLars-Peter Clausen * Decrements the active count for all the DAIs and components attached to a PCM 26524894b76SLars-Peter Clausen * runtime. Should typically be called when a stream is closed. 26624894b76SLars-Peter Clausen * 26772b745e3SPeter Ujfalusi * Must be called with the rtd->card->pcm_mutex being held 26824894b76SLars-Peter Clausen */ 26924894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream) 27024894b76SLars-Peter Clausen { 2717a5aaba4SKuninori Morimoto snd_soc_runtime_action(rtd, stream, -1); 27224894b76SLars-Peter Clausen } 27324894b76SLars-Peter Clausen 27424894b76SLars-Peter Clausen /** 275208a1589SLars-Peter Clausen * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay 276208a1589SLars-Peter Clausen * @rtd: The ASoC PCM runtime that should be checked. 277208a1589SLars-Peter Clausen * 278208a1589SLars-Peter Clausen * This function checks whether the power down delay should be ignored for a 279208a1589SLars-Peter Clausen * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has 280208a1589SLars-Peter Clausen * been configured to ignore the delay, or if none of the components benefits 281208a1589SLars-Peter Clausen * from having the delay. 282208a1589SLars-Peter Clausen */ 283208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd) 284208a1589SLars-Peter Clausen { 285fbb16563SKuninori Morimoto struct snd_soc_component *component; 2862e5894d7SBenoit Cousson bool ignore = true; 287613fb500SKuninori Morimoto int i; 2882e5894d7SBenoit Cousson 289208a1589SLars-Peter Clausen if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time) 290208a1589SLars-Peter Clausen return true; 291208a1589SLars-Peter Clausen 292613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 29372c38184SKuninori Morimoto ignore &= !component->driver->use_pmdown_time; 294fbb16563SKuninori Morimoto 295fbb16563SKuninori Morimoto return ignore; 296208a1589SLars-Peter Clausen } 297208a1589SLars-Peter Clausen 298208a1589SLars-Peter Clausen /** 29990996f43SLars-Peter Clausen * snd_soc_set_runtime_hwparams - set the runtime hardware parameters 30090996f43SLars-Peter Clausen * @substream: the pcm substream 30190996f43SLars-Peter Clausen * @hw: the hardware parameters 30290996f43SLars-Peter Clausen * 30390996f43SLars-Peter Clausen * Sets the substream runtime hardware parameters. 30490996f43SLars-Peter Clausen */ 30590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, 30690996f43SLars-Peter Clausen const struct snd_pcm_hardware *hw) 30790996f43SLars-Peter Clausen { 30890996f43SLars-Peter Clausen struct snd_pcm_runtime *runtime = substream->runtime; 30990996f43SLars-Peter Clausen runtime->hw.info = hw->info; 31090996f43SLars-Peter Clausen runtime->hw.formats = hw->formats; 31190996f43SLars-Peter Clausen runtime->hw.period_bytes_min = hw->period_bytes_min; 31290996f43SLars-Peter Clausen runtime->hw.period_bytes_max = hw->period_bytes_max; 31390996f43SLars-Peter Clausen runtime->hw.periods_min = hw->periods_min; 31490996f43SLars-Peter Clausen runtime->hw.periods_max = hw->periods_max; 31590996f43SLars-Peter Clausen runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; 31690996f43SLars-Peter Clausen runtime->hw.fifo_size = hw->fifo_size; 31790996f43SLars-Peter Clausen return 0; 31890996f43SLars-Peter Clausen } 31990996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); 32090996f43SLars-Peter Clausen 32101d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */ 32223607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir, 32301d7584cSLiam Girdwood int event) 32401d7584cSLiam Girdwood { 32501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 32601d7584cSLiam Girdwood 3278d6258a4SKuninori Morimoto for_each_dpcm_be(fe, dir, dpcm) { 32801d7584cSLiam Girdwood 32901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 33001d7584cSLiam Girdwood 331103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n", 33201d7584cSLiam Girdwood be->dai_link->name, event, dir); 33301d7584cSLiam Girdwood 334b1cd2e34SBanajit Goswami if ((event == SND_SOC_DAPM_STREAM_STOP) && 335b1cd2e34SBanajit Goswami (be->dpcm[dir].users >= 1)) 336b1cd2e34SBanajit Goswami continue; 337b1cd2e34SBanajit Goswami 33801d7584cSLiam Girdwood snd_soc_dapm_stream_event(be, dir, event); 33901d7584cSLiam Girdwood } 34001d7584cSLiam Girdwood 34101d7584cSLiam Girdwood snd_soc_dapm_stream_event(fe, dir, event); 34201d7584cSLiam Girdwood 34301d7584cSLiam Girdwood return 0; 34401d7584cSLiam Girdwood } 34501d7584cSLiam Girdwood 34617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream, 34717841020SDong Aisheng struct snd_soc_dai *soc_dai) 348ddee627cSLiam Girdwood { 349ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 350ddee627cSLiam Girdwood int ret; 351ddee627cSLiam Girdwood 3523635bf09SNicolin Chen if (soc_dai->rate && (soc_dai->driver->symmetric_rates || 3533635bf09SNicolin Chen rtd->dai_link->symmetric_rates)) { 3543635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", 3553635bf09SNicolin Chen soc_dai->rate); 356ddee627cSLiam Girdwood 3574dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 358ddee627cSLiam Girdwood SNDRV_PCM_HW_PARAM_RATE, 3594dcdd43bSLars-Peter Clausen soc_dai->rate); 360ddee627cSLiam Girdwood if (ret < 0) { 36117841020SDong Aisheng dev_err(soc_dai->dev, 3623635bf09SNicolin Chen "ASoC: Unable to apply rate constraint: %d\n", 363103d84a3SLiam Girdwood ret); 364ddee627cSLiam Girdwood return ret; 365ddee627cSLiam Girdwood } 3663635bf09SNicolin Chen } 3673635bf09SNicolin Chen 3683635bf09SNicolin Chen if (soc_dai->channels && (soc_dai->driver->symmetric_channels || 3693635bf09SNicolin Chen rtd->dai_link->symmetric_channels)) { 3703635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n", 3713635bf09SNicolin Chen soc_dai->channels); 3723635bf09SNicolin Chen 3734dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 3743635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_CHANNELS, 3753635bf09SNicolin Chen soc_dai->channels); 3763635bf09SNicolin Chen if (ret < 0) { 3773635bf09SNicolin Chen dev_err(soc_dai->dev, 3783635bf09SNicolin Chen "ASoC: Unable to apply channel symmetry constraint: %d\n", 3793635bf09SNicolin Chen ret); 3803635bf09SNicolin Chen return ret; 3813635bf09SNicolin Chen } 3823635bf09SNicolin Chen } 3833635bf09SNicolin Chen 3843635bf09SNicolin Chen if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits || 3853635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits)) { 3863635bf09SNicolin Chen dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n", 3873635bf09SNicolin Chen soc_dai->sample_bits); 3883635bf09SNicolin Chen 3894dcdd43bSLars-Peter Clausen ret = snd_pcm_hw_constraint_single(substream->runtime, 3903635bf09SNicolin Chen SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 3913635bf09SNicolin Chen soc_dai->sample_bits); 3923635bf09SNicolin Chen if (ret < 0) { 3933635bf09SNicolin Chen dev_err(soc_dai->dev, 3943635bf09SNicolin Chen "ASoC: Unable to apply sample bits symmetry constraint: %d\n", 3953635bf09SNicolin Chen ret); 3963635bf09SNicolin Chen return ret; 3973635bf09SNicolin Chen } 3983635bf09SNicolin Chen } 399ddee627cSLiam Girdwood 400ddee627cSLiam Girdwood return 0; 401ddee627cSLiam Girdwood } 402ddee627cSLiam Girdwood 4033635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream, 4043635bf09SNicolin Chen struct snd_pcm_hw_params *params) 4053635bf09SNicolin Chen { 4063635bf09SNicolin Chen struct snd_soc_pcm_runtime *rtd = substream->private_data; 4073635bf09SNicolin Chen struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 4080b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 4092e5894d7SBenoit Cousson unsigned int rate, channels, sample_bits, symmetry, i; 4103635bf09SNicolin Chen 4113635bf09SNicolin Chen rate = params_rate(params); 4123635bf09SNicolin Chen channels = params_channels(params); 4133635bf09SNicolin Chen sample_bits = snd_pcm_format_physical_width(params_format(params)); 4143635bf09SNicolin Chen 4153635bf09SNicolin Chen /* reject unmatched parameters when applying symmetry */ 4163635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_rates || 4173635bf09SNicolin Chen rtd->dai_link->symmetric_rates; 4182e5894d7SBenoit Cousson 4190b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4200b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_rates; 4212e5894d7SBenoit Cousson 4223635bf09SNicolin Chen if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) { 4233635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n", 4243635bf09SNicolin Chen cpu_dai->rate, rate); 4253635bf09SNicolin Chen return -EINVAL; 4263635bf09SNicolin Chen } 4273635bf09SNicolin Chen 4283635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_channels || 4293635bf09SNicolin Chen rtd->dai_link->symmetric_channels; 4302e5894d7SBenoit Cousson 4310b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4320b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_channels; 4332e5894d7SBenoit Cousson 4343635bf09SNicolin Chen if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) { 4353635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n", 4363635bf09SNicolin Chen cpu_dai->channels, channels); 4373635bf09SNicolin Chen return -EINVAL; 4383635bf09SNicolin Chen } 4393635bf09SNicolin Chen 4403635bf09SNicolin Chen symmetry = cpu_dai->driver->symmetric_samplebits || 4413635bf09SNicolin Chen rtd->dai_link->symmetric_samplebits; 4422e5894d7SBenoit Cousson 4430b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4440b7990e3SKuninori Morimoto symmetry |= codec_dai->driver->symmetric_samplebits; 4452e5894d7SBenoit Cousson 4463635bf09SNicolin Chen if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) { 4473635bf09SNicolin Chen dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n", 4483635bf09SNicolin Chen cpu_dai->sample_bits, sample_bits); 4493635bf09SNicolin Chen return -EINVAL; 4503635bf09SNicolin Chen } 451ddee627cSLiam Girdwood 452ddee627cSLiam Girdwood return 0; 453ddee627cSLiam Girdwood } 454ddee627cSLiam Girdwood 45562e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream) 45662e5f676SLars-Peter Clausen { 45762e5f676SLars-Peter Clausen struct snd_soc_pcm_runtime *rtd = substream->private_data; 45862e5f676SLars-Peter Clausen struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver; 45962e5f676SLars-Peter Clausen struct snd_soc_dai_link *link = rtd->dai_link; 4600b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 4612e5894d7SBenoit Cousson unsigned int symmetry, i; 46262e5f676SLars-Peter Clausen 4632e5894d7SBenoit Cousson symmetry = cpu_driver->symmetric_rates || link->symmetric_rates || 4642e5894d7SBenoit Cousson cpu_driver->symmetric_channels || link->symmetric_channels || 4652e5894d7SBenoit Cousson cpu_driver->symmetric_samplebits || link->symmetric_samplebits; 4662e5894d7SBenoit Cousson 4670b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 4682e5894d7SBenoit Cousson symmetry = symmetry || 4690b7990e3SKuninori Morimoto codec_dai->driver->symmetric_rates || 4700b7990e3SKuninori Morimoto codec_dai->driver->symmetric_channels || 4710b7990e3SKuninori Morimoto codec_dai->driver->symmetric_samplebits; 4722e5894d7SBenoit Cousson 4732e5894d7SBenoit Cousson return symmetry; 47462e5f676SLars-Peter Clausen } 47562e5f676SLars-Peter Clausen 4762e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits) 47758ba9b25SMark Brown { 4782e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 479c6068d3aSTakashi Iwai int ret; 48058ba9b25SMark Brown 48158ba9b25SMark Brown if (!bits) 48258ba9b25SMark Brown return; 48358ba9b25SMark Brown 4840e2a3751SLars-Peter Clausen ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits); 48558ba9b25SMark Brown if (ret != 0) 4860e2a3751SLars-Peter Clausen dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n", 4870e2a3751SLars-Peter Clausen bits, ret); 48858ba9b25SMark Brown } 48958ba9b25SMark Brown 490c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream) 491bd477c31SLars-Peter Clausen { 492c8dd1fecSBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 493c8dd1fecSBenoit Cousson struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 4942e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 49557be9206SKuninori Morimoto struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu; 49657be9206SKuninori Morimoto int stream = substream->stream; 4972e5894d7SBenoit Cousson int i; 498c8dd1fecSBenoit Cousson unsigned int bits = 0, cpu_bits; 499c8dd1fecSBenoit Cousson 5000b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 50157be9206SKuninori Morimoto pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream); 50257be9206SKuninori Morimoto 50357be9206SKuninori Morimoto if (pcm_codec->sig_bits == 0) { 5042e5894d7SBenoit Cousson bits = 0; 5052e5894d7SBenoit Cousson break; 5062e5894d7SBenoit Cousson } 50757be9206SKuninori Morimoto bits = max(pcm_codec->sig_bits, bits); 5082e5894d7SBenoit Cousson } 50957be9206SKuninori Morimoto 51057be9206SKuninori Morimoto pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream); 51157be9206SKuninori Morimoto cpu_bits = pcm_cpu->sig_bits; 512c8dd1fecSBenoit Cousson 5132e5894d7SBenoit Cousson soc_pcm_set_msb(substream, bits); 5142e5894d7SBenoit Cousson soc_pcm_set_msb(substream, cpu_bits); 515c8dd1fecSBenoit Cousson } 516c8dd1fecSBenoit Cousson 5172e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream) 518bd477c31SLars-Peter Clausen { 5192e5894d7SBenoit Cousson struct snd_pcm_runtime *runtime = substream->runtime; 52078e45c99SLars-Peter Clausen struct snd_pcm_hardware *hw = &runtime->hw; 5212e5894d7SBenoit Cousson struct snd_soc_pcm_runtime *rtd = substream->private_data; 5220b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 5232e5894d7SBenoit Cousson struct snd_soc_pcm_stream *codec_stream; 5242e5894d7SBenoit Cousson struct snd_soc_pcm_stream *cpu_stream; 5252e5894d7SBenoit Cousson unsigned int chan_min = 0, chan_max = UINT_MAX; 5262e5894d7SBenoit Cousson unsigned int rate_min = 0, rate_max = UINT_MAX; 5272e5894d7SBenoit Cousson unsigned int rates = UINT_MAX; 5282e5894d7SBenoit Cousson u64 formats = ULLONG_MAX; 529acf253c1SKuninori Morimoto int stream = substream->stream; 5302e5894d7SBenoit Cousson int i; 53178e45c99SLars-Peter Clausen 532acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(rtd->cpu_dai, stream); 53378e45c99SLars-Peter Clausen 5342e5894d7SBenoit Cousson /* first calculate min/max only for CODECs in the DAI link */ 5350b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 536cde79035SRicard Wanderlof 537cde79035SRicard Wanderlof /* 538cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type. 539cde79035SRicard Wanderlof * Otherwise, since the rate, channel, and format values will 540cde79035SRicard Wanderlof * zero in that case, we would have no usable settings left, 541cde79035SRicard Wanderlof * causing the resulting setup to fail. 542cde79035SRicard Wanderlof * At least one CODEC should match, otherwise we should have 543cde79035SRicard Wanderlof * bailed out on a higher level, since there would be no 544cde79035SRicard Wanderlof * CODEC to support the transfer direction in that case. 545cde79035SRicard Wanderlof */ 5460b7990e3SKuninori Morimoto if (!snd_soc_dai_stream_valid(codec_dai, 547cde79035SRicard Wanderlof substream->stream)) 548cde79035SRicard Wanderlof continue; 549cde79035SRicard Wanderlof 550acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream); 551acf253c1SKuninori Morimoto 5522e5894d7SBenoit Cousson chan_min = max(chan_min, codec_stream->channels_min); 5532e5894d7SBenoit Cousson chan_max = min(chan_max, codec_stream->channels_max); 5542e5894d7SBenoit Cousson rate_min = max(rate_min, codec_stream->rate_min); 5552e5894d7SBenoit Cousson rate_max = min_not_zero(rate_max, codec_stream->rate_max); 5562e5894d7SBenoit Cousson formats &= codec_stream->formats; 5572e5894d7SBenoit Cousson rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates); 5582e5894d7SBenoit Cousson } 5592e5894d7SBenoit Cousson 5602e5894d7SBenoit Cousson /* 5612e5894d7SBenoit Cousson * chan min/max cannot be enforced if there are multiple CODEC DAIs 5622e5894d7SBenoit Cousson * connected to a single CPU DAI, use CPU DAI's directly and let 5632e5894d7SBenoit Cousson * channel allocation be fixed up later 5642e5894d7SBenoit Cousson */ 5652e5894d7SBenoit Cousson if (rtd->num_codecs > 1) { 5662e5894d7SBenoit Cousson chan_min = cpu_stream->channels_min; 5672e5894d7SBenoit Cousson chan_max = cpu_stream->channels_max; 5682e5894d7SBenoit Cousson } 5692e5894d7SBenoit Cousson 5702e5894d7SBenoit Cousson hw->channels_min = max(chan_min, cpu_stream->channels_min); 5712e5894d7SBenoit Cousson hw->channels_max = min(chan_max, cpu_stream->channels_max); 5722e5894d7SBenoit Cousson if (hw->formats) 5732e5894d7SBenoit Cousson hw->formats &= formats & cpu_stream->formats; 5742e5894d7SBenoit Cousson else 5752e5894d7SBenoit Cousson hw->formats = formats & cpu_stream->formats; 5762e5894d7SBenoit Cousson hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates); 57778e45c99SLars-Peter Clausen 57878e45c99SLars-Peter Clausen snd_pcm_limit_hw_rates(runtime); 57978e45c99SLars-Peter Clausen 58078e45c99SLars-Peter Clausen hw->rate_min = max(hw->rate_min, cpu_stream->rate_min); 5812e5894d7SBenoit Cousson hw->rate_min = max(hw->rate_min, rate_min); 58278e45c99SLars-Peter Clausen hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max); 5832e5894d7SBenoit Cousson hw->rate_max = min_not_zero(hw->rate_max, rate_max); 584bd477c31SLars-Peter Clausen } 585bd477c31SLars-Peter Clausen 586dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream) 587e7ecfdb7SKuninori Morimoto { 588e7ecfdb7SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 589e7ecfdb7SKuninori Morimoto struct snd_soc_component *component; 590613fb500SKuninori Morimoto int i, ret = 0; 591e7ecfdb7SKuninori Morimoto 592613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 5934a81e8f3SKuninori Morimoto ret = snd_soc_component_module_get_when_open(component); 5944a81e8f3SKuninori Morimoto if (ret < 0) { 595e7ecfdb7SKuninori Morimoto dev_err(component->dev, 596e7ecfdb7SKuninori Morimoto "ASoC: can't get module %s\n", 597e7ecfdb7SKuninori Morimoto component->name); 5984a81e8f3SKuninori Morimoto return ret; 599e7ecfdb7SKuninori Morimoto } 600e7ecfdb7SKuninori Morimoto 601ae2f4849SKuninori Morimoto ret = snd_soc_component_open(component, substream); 602e7ecfdb7SKuninori Morimoto if (ret < 0) { 603e7ecfdb7SKuninori Morimoto dev_err(component->dev, 604e7ecfdb7SKuninori Morimoto "ASoC: can't open component %s: %d\n", 605e7ecfdb7SKuninori Morimoto component->name, ret); 606e7ecfdb7SKuninori Morimoto return ret; 607e7ecfdb7SKuninori Morimoto } 608e7ecfdb7SKuninori Morimoto } 609dd03907bSKuninori Morimoto 610e7ecfdb7SKuninori Morimoto return 0; 611e7ecfdb7SKuninori Morimoto } 612e7ecfdb7SKuninori Morimoto 613dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream) 614244e2936SCharles Keepax { 615244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 616244e2936SCharles Keepax struct snd_soc_component *component; 617e82ebffcSKuninori Morimoto int i, r, ret = 0; 618244e2936SCharles Keepax 619613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 620e82ebffcSKuninori Morimoto r = snd_soc_component_close(component, substream); 621e82ebffcSKuninori Morimoto if (r < 0) 622e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 623e82ebffcSKuninori Morimoto 6244a81e8f3SKuninori Morimoto snd_soc_component_module_put_when_close(component); 625244e2936SCharles Keepax } 626244e2936SCharles Keepax 6273672beb8SKuninori Morimoto return ret; 628244e2936SCharles Keepax } 629244e2936SCharles Keepax 63058ba9b25SMark Brown /* 63162c86d1dSKuninori Morimoto * Called by ALSA when a PCM substream is closed. Private data can be 63262c86d1dSKuninori Morimoto * freed here. The cpu DAI, codec DAI, machine and components are also 63362c86d1dSKuninori Morimoto * shutdown. 63462c86d1dSKuninori Morimoto */ 63562c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream) 63662c86d1dSKuninori Morimoto { 63762c86d1dSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = substream->private_data; 63862c86d1dSKuninori Morimoto struct snd_soc_component *component; 63962c86d1dSKuninori Morimoto struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 64062c86d1dSKuninori Morimoto struct snd_soc_dai *codec_dai; 64162c86d1dSKuninori Morimoto int i; 64262c86d1dSKuninori Morimoto 64362c86d1dSKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 64462c86d1dSKuninori Morimoto 64562c86d1dSKuninori Morimoto snd_soc_runtime_deactivate(rtd, substream->stream); 64662c86d1dSKuninori Morimoto 64762c86d1dSKuninori Morimoto snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream); 64862c86d1dSKuninori Morimoto 64962c86d1dSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 65062c86d1dSKuninori Morimoto 65162c86d1dSKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 65262c86d1dSKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 65362c86d1dSKuninori Morimoto 65462c86d1dSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 65562c86d1dSKuninori Morimoto 65662c86d1dSKuninori Morimoto soc_pcm_components_close(substream); 65762c86d1dSKuninori Morimoto 65862c86d1dSKuninori Morimoto snd_soc_dapm_stream_stop(rtd, substream->stream); 65962c86d1dSKuninori Morimoto 66062c86d1dSKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 66162c86d1dSKuninori Morimoto 66262c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) { 66362c86d1dSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 66462c86d1dSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 66562c86d1dSKuninori Morimoto } 66662c86d1dSKuninori Morimoto 66762c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component) 66862c86d1dSKuninori Morimoto if (!component->active) 66962c86d1dSKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 67062c86d1dSKuninori Morimoto 67162c86d1dSKuninori Morimoto return 0; 67262c86d1dSKuninori Morimoto } 67362c86d1dSKuninori Morimoto 67462c86d1dSKuninori Morimoto /* 675ddee627cSLiam Girdwood * Called by ALSA when a PCM substream is opened, the runtime->hw record is 676ddee627cSLiam Girdwood * then initialized and any private data can be allocated. This also calls 677ef050becSCharles Keepax * startup for the cpu DAI, component, machine and codec DAI. 678ddee627cSLiam Girdwood */ 679ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream) 680ddee627cSLiam Girdwood { 681ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 682ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 68390be711eSKuninori Morimoto struct snd_soc_component *component; 684ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 6852e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 6862e5894d7SBenoit Cousson const char *codec_dai_name = "multicodec"; 687244e2936SCharles Keepax int i, ret = 0; 688ddee627cSLiam Girdwood 68976c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 69076c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev); 69190be711eSKuninori Morimoto 692613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) 69390be711eSKuninori Morimoto pm_runtime_get_sync(component->dev); 694d6652ef8SMark Brown 69572b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 696ddee627cSLiam Girdwood 6975d9fa03eSKuninori Morimoto ret = soc_pcm_components_open(substream); 6985d9fa03eSKuninori Morimoto if (ret < 0) 6995d9fa03eSKuninori Morimoto goto component_err; 7005d9fa03eSKuninori Morimoto 7015d9fa03eSKuninori Morimoto ret = soc_rtd_startup(rtd, substream); 7025d9fa03eSKuninori Morimoto if (ret < 0) { 7035d9fa03eSKuninori Morimoto pr_err("ASoC: %s startup failed: %d\n", 7045d9fa03eSKuninori Morimoto rtd->dai_link->name, ret); 7055d9fa03eSKuninori Morimoto goto component_err; 7065d9fa03eSKuninori Morimoto } 7075d9fa03eSKuninori Morimoto 708ddee627cSLiam Girdwood /* startup the audio subsystem */ 7095a52a045SKuninori Morimoto ret = snd_soc_dai_startup(cpu_dai, substream); 710ddee627cSLiam Girdwood if (ret < 0) { 7115a52a045SKuninori Morimoto dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n", 7125a52a045SKuninori Morimoto cpu_dai->name, ret); 7135d9fa03eSKuninori Morimoto goto cpu_dai_err; 714ddee627cSLiam Girdwood } 715ddee627cSLiam Girdwood 7160b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 7175a52a045SKuninori Morimoto ret = snd_soc_dai_startup(codec_dai, substream); 718ddee627cSLiam Girdwood if (ret < 0) { 7192e5894d7SBenoit Cousson dev_err(codec_dai->dev, 7202e5894d7SBenoit Cousson "ASoC: can't open codec %s: %d\n", 7212e5894d7SBenoit Cousson codec_dai->name, ret); 7225d9fa03eSKuninori Morimoto goto config_err; 723ddee627cSLiam Girdwood } 724ddee627cSLiam Girdwood 7252e5894d7SBenoit Cousson if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 7262e5894d7SBenoit Cousson codec_dai->tx_mask = 0; 7272e5894d7SBenoit Cousson else 7282e5894d7SBenoit Cousson codec_dai->rx_mask = 0; 7292e5894d7SBenoit Cousson } 7302e5894d7SBenoit Cousson 73101d7584cSLiam Girdwood /* Dynamic PCM DAI links compat checks use dynamic capabilities */ 73201d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) 73301d7584cSLiam Girdwood goto dynamic; 73401d7584cSLiam Girdwood 735ddee627cSLiam Girdwood /* Check that the codec and cpu DAIs are compatible */ 7362e5894d7SBenoit Cousson soc_pcm_init_runtime_hw(substream); 7372e5894d7SBenoit Cousson 7382e5894d7SBenoit Cousson if (rtd->num_codecs == 1) 7392e5894d7SBenoit Cousson codec_dai_name = rtd->codec_dai->name; 740ddee627cSLiam Girdwood 74162e5f676SLars-Peter Clausen if (soc_pcm_has_symmetry(substream)) 74262e5f676SLars-Peter Clausen runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 74362e5f676SLars-Peter Clausen 744ddee627cSLiam Girdwood ret = -EINVAL; 745ddee627cSLiam Girdwood if (!runtime->hw.rates) { 746103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n", 7472e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 748ddee627cSLiam Girdwood goto config_err; 749ddee627cSLiam Girdwood } 750ddee627cSLiam Girdwood if (!runtime->hw.formats) { 751103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n", 7522e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 753ddee627cSLiam Girdwood goto config_err; 754ddee627cSLiam Girdwood } 755ddee627cSLiam Girdwood if (!runtime->hw.channels_min || !runtime->hw.channels_max || 756ddee627cSLiam Girdwood runtime->hw.channels_min > runtime->hw.channels_max) { 757103d84a3SLiam Girdwood printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n", 7582e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 759ddee627cSLiam Girdwood goto config_err; 760ddee627cSLiam Girdwood } 761ddee627cSLiam Girdwood 762c8dd1fecSBenoit Cousson soc_pcm_apply_msb(substream); 76358ba9b25SMark Brown 764ddee627cSLiam Girdwood /* Symmetry only applies if we've already got an active stream. */ 76517841020SDong Aisheng if (cpu_dai->active) { 76617841020SDong Aisheng ret = soc_pcm_apply_symmetry(substream, cpu_dai); 76717841020SDong Aisheng if (ret != 0) 76817841020SDong Aisheng goto config_err; 76917841020SDong Aisheng } 77017841020SDong Aisheng 7710b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 7720b7990e3SKuninori Morimoto if (codec_dai->active) { 7730b7990e3SKuninori Morimoto ret = soc_pcm_apply_symmetry(substream, codec_dai); 774ddee627cSLiam Girdwood if (ret != 0) 775ddee627cSLiam Girdwood goto config_err; 776ddee627cSLiam Girdwood } 7772e5894d7SBenoit Cousson } 778ddee627cSLiam Girdwood 779103d84a3SLiam Girdwood pr_debug("ASoC: %s <-> %s info:\n", 7802e5894d7SBenoit Cousson codec_dai_name, cpu_dai->name); 781103d84a3SLiam Girdwood pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates); 782103d84a3SLiam Girdwood pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min, 783ddee627cSLiam Girdwood runtime->hw.channels_max); 784103d84a3SLiam Girdwood pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min, 785ddee627cSLiam Girdwood runtime->hw.rate_max); 786ddee627cSLiam Girdwood 78701d7584cSLiam Girdwood dynamic: 78824894b76SLars-Peter Clausen 78924894b76SLars-Peter Clausen snd_soc_runtime_activate(rtd, substream->stream); 79024894b76SLars-Peter Clausen 79172b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 792ddee627cSLiam Girdwood return 0; 793ddee627cSLiam Girdwood 794ddee627cSLiam Girdwood config_err: 795b56be800SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 796330fcb51SKuninori Morimoto snd_soc_dai_shutdown(codec_dai, substream); 7975d9fa03eSKuninori Morimoto cpu_dai_err: 7985d9fa03eSKuninori Morimoto snd_soc_dai_shutdown(cpu_dai, substream); 7992e5894d7SBenoit Cousson 8005d9fa03eSKuninori Morimoto soc_rtd_shutdown(rtd, substream); 801b8135864SKuninori Morimoto component_err: 802dd03907bSKuninori Morimoto soc_pcm_components_close(substream); 803e7ecfdb7SKuninori Morimoto 80472b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 805d6652ef8SMark Brown 806613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 80790be711eSKuninori Morimoto pm_runtime_mark_last_busy(component->dev); 80890be711eSKuninori Morimoto pm_runtime_put_autosuspend(component->dev); 8093f809783SSanyog Kale } 8103f809783SSanyog Kale 81176c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component) 81276c39e86SKuninori Morimoto if (!component->active) 81376c39e86SKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev); 814d6652ef8SMark Brown 815ddee627cSLiam Girdwood return ret; 816ddee627cSLiam Girdwood } 817ddee627cSLiam Girdwood 8184bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd) 819a342031cSJerome Brunet { 820a342031cSJerome Brunet /* 821a342031cSJerome Brunet * Currently nothing to do for c2c links 822a342031cSJerome Brunet * Since c2c links are internal nodes in the DAPM graph and 823a342031cSJerome Brunet * don't interface with the outside world or application layer 824a342031cSJerome Brunet * we don't have to do any special handling on close. 825a342031cSJerome Brunet */ 826a342031cSJerome Brunet } 827a342031cSJerome Brunet 828ddee627cSLiam Girdwood /* 829ddee627cSLiam Girdwood * Called by ALSA when the PCM substream is prepared, can set format, sample 830ddee627cSLiam Girdwood * rate, etc. This function is non atomic and can be called multiple times, 831ddee627cSLiam Girdwood * it can refer to the runtime info. 832ddee627cSLiam Girdwood */ 833ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream) 834ddee627cSLiam Girdwood { 835ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 836b8135864SKuninori Morimoto struct snd_soc_component *component; 837ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 8382e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 8392e5894d7SBenoit Cousson int i, ret = 0; 840ddee627cSLiam Girdwood 84172b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 842ddee627cSLiam Girdwood 84344c1a75bSKuninori Morimoto ret = soc_rtd_prepare(rtd, substream); 844ddee627cSLiam Girdwood if (ret < 0) { 84544c1a75bSKuninori Morimoto dev_err(rtd->card->dev, 84644c1a75bSKuninori Morimoto "ASoC: machine prepare error: %d\n", ret); 847ddee627cSLiam Girdwood goto out; 848ddee627cSLiam Girdwood } 849ddee627cSLiam Girdwood 850613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 8516d537233SKuninori Morimoto ret = snd_soc_component_prepare(component, substream); 852b8135864SKuninori Morimoto if (ret < 0) { 853b8135864SKuninori Morimoto dev_err(component->dev, 854b8135864SKuninori Morimoto "ASoC: platform prepare error: %d\n", ret); 855b8135864SKuninori Morimoto goto out; 856b8135864SKuninori Morimoto } 857b8135864SKuninori Morimoto } 858b8135864SKuninori Morimoto 8590b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 8604beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(codec_dai, substream); 861ddee627cSLiam Girdwood if (ret < 0) { 8622e5894d7SBenoit Cousson dev_err(codec_dai->dev, 86390cc7f1cSJarkko Nikula "ASoC: codec DAI prepare error: %d\n", 86490cc7f1cSJarkko Nikula ret); 865ddee627cSLiam Girdwood goto out; 866ddee627cSLiam Girdwood } 867ddee627cSLiam Girdwood } 868ddee627cSLiam Girdwood 8694beb8e10SKuninori Morimoto ret = snd_soc_dai_prepare(cpu_dai, substream); 870ddee627cSLiam Girdwood if (ret < 0) { 87190cc7f1cSJarkko Nikula dev_err(cpu_dai->dev, 87290cc7f1cSJarkko Nikula "ASoC: cpu DAI prepare error: %d\n", ret); 873ddee627cSLiam Girdwood goto out; 874ddee627cSLiam Girdwood } 875ddee627cSLiam Girdwood 876ddee627cSLiam Girdwood /* cancel any delayed stream shutdown that is pending */ 877ddee627cSLiam Girdwood if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 8789bffb1fbSMisael Lopez Cruz rtd->pop_wait) { 8799bffb1fbSMisael Lopez Cruz rtd->pop_wait = 0; 880ddee627cSLiam Girdwood cancel_delayed_work(&rtd->delayed_work); 881ddee627cSLiam Girdwood } 882ddee627cSLiam Girdwood 883d9b0951bSLiam Girdwood snd_soc_dapm_stream_event(rtd, substream->stream, 884ddee627cSLiam Girdwood SND_SOC_DAPM_STREAM_START); 885ddee627cSLiam Girdwood 8860b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) 8870b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 0, 8882e5894d7SBenoit Cousson substream->stream); 889ae11601bSRamesh Babu snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream); 890ddee627cSLiam Girdwood 891ddee627cSLiam Girdwood out: 89272b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 893ddee627cSLiam Girdwood return ret; 894ddee627cSLiam Girdwood } 895ddee627cSLiam Girdwood 8962e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params, 8972e5894d7SBenoit Cousson unsigned int mask) 8982e5894d7SBenoit Cousson { 8992e5894d7SBenoit Cousson struct snd_interval *interval; 9002e5894d7SBenoit Cousson int channels = hweight_long(mask); 9012e5894d7SBenoit Cousson 9022e5894d7SBenoit Cousson interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 9032e5894d7SBenoit Cousson interval->min = channels; 9042e5894d7SBenoit Cousson interval->max = channels; 9052e5894d7SBenoit Cousson } 9062e5894d7SBenoit Cousson 907244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream, 908244e2936SCharles Keepax struct snd_soc_component *last) 909244e2936SCharles Keepax { 910244e2936SCharles Keepax struct snd_soc_pcm_runtime *rtd = substream->private_data; 911244e2936SCharles Keepax struct snd_soc_component *component; 912e82ebffcSKuninori Morimoto int i, r, ret = 0; 913244e2936SCharles Keepax 914613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 915244e2936SCharles Keepax if (component == last) 916244e2936SCharles Keepax break; 917244e2936SCharles Keepax 918e82ebffcSKuninori Morimoto r = snd_soc_component_hw_free(component, substream); 919e82ebffcSKuninori Morimoto if (r < 0) 920e82ebffcSKuninori Morimoto ret = r; /* use last ret */ 921244e2936SCharles Keepax } 922244e2936SCharles Keepax 923eae7136aSKuninori Morimoto return ret; 924244e2936SCharles Keepax } 925244e2936SCharles Keepax 926ddee627cSLiam Girdwood /* 927ddee627cSLiam Girdwood * Called by ALSA when the hardware params are set by application. This 928ddee627cSLiam Girdwood * function can also be called multiple times and can allocate buffers 929ddee627cSLiam Girdwood * (using snd_pcm_lib_* ). It's non-atomic. 930ddee627cSLiam Girdwood */ 931ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream, 932ddee627cSLiam Girdwood struct snd_pcm_hw_params *params) 933ddee627cSLiam Girdwood { 934ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 935b8135864SKuninori Morimoto struct snd_soc_component *component; 936ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 9370b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 938244e2936SCharles Keepax int i, ret = 0; 939ddee627cSLiam Girdwood 94072b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 9415cca5951SShengjiu Wang 9425cca5951SShengjiu Wang ret = soc_pcm_params_symmetry(substream, params); 9435cca5951SShengjiu Wang if (ret) 9445cca5951SShengjiu Wang goto out; 9455cca5951SShengjiu Wang 946de9ad990SKuninori Morimoto ret = soc_rtd_hw_params(rtd, substream, params); 947ddee627cSLiam Girdwood if (ret < 0) { 948de9ad990SKuninori Morimoto dev_err(rtd->card->dev, 949de9ad990SKuninori Morimoto "ASoC: machine hw_params failed: %d\n", ret); 950ddee627cSLiam Girdwood goto out; 951ddee627cSLiam Girdwood } 952ddee627cSLiam Girdwood 9530b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 9542e5894d7SBenoit Cousson struct snd_pcm_hw_params codec_params; 9552e5894d7SBenoit Cousson 956cde79035SRicard Wanderlof /* 957cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type, 958cde79035SRicard Wanderlof * the idea being that if a CODEC is not used for the currently 959cde79035SRicard Wanderlof * set up transfer direction, it should not need to be 960cde79035SRicard Wanderlof * configured, especially since the configuration used might 961cde79035SRicard Wanderlof * not even be supported by that CODEC. There may be cases 962cde79035SRicard Wanderlof * however where a CODEC needs to be set up although it is 963cde79035SRicard Wanderlof * actually not being used for the transfer, e.g. if a 964cde79035SRicard Wanderlof * capture-only CODEC is acting as an LRCLK and/or BCLK master 965cde79035SRicard Wanderlof * for the DAI link including a playback-only CODEC. 966cde79035SRicard Wanderlof * If this becomes necessary, we will have to augment the 967cde79035SRicard Wanderlof * machine driver setup with information on how to act, so 968cde79035SRicard Wanderlof * we can do the right thing here. 969cde79035SRicard Wanderlof */ 970cde79035SRicard Wanderlof if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 971cde79035SRicard Wanderlof continue; 972cde79035SRicard Wanderlof 9732e5894d7SBenoit Cousson /* copy params for each codec */ 9742e5894d7SBenoit Cousson codec_params = *params; 9752e5894d7SBenoit Cousson 9762e5894d7SBenoit Cousson /* fixup params based on TDM slot masks */ 977570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 978570f18b6SRander Wang codec_dai->tx_mask) 9792e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 9802e5894d7SBenoit Cousson codec_dai->tx_mask); 981570f18b6SRander Wang 982570f18b6SRander Wang if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && 983570f18b6SRander Wang codec_dai->rx_mask) 9842e5894d7SBenoit Cousson soc_pcm_codec_params_fixup(&codec_params, 9852e5894d7SBenoit Cousson codec_dai->rx_mask); 9862e5894d7SBenoit Cousson 987aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(codec_dai, substream, 988aa6166c2SKuninori Morimoto &codec_params); 98993e6958aSBenoit Cousson if(ret < 0) 990ddee627cSLiam Girdwood goto codec_err; 991ddee627cSLiam Girdwood 9922e5894d7SBenoit Cousson codec_dai->rate = params_rate(&codec_params); 9932e5894d7SBenoit Cousson codec_dai->channels = params_channels(&codec_params); 9942e5894d7SBenoit Cousson codec_dai->sample_bits = snd_pcm_format_physical_width( 9952e5894d7SBenoit Cousson params_format(&codec_params)); 996078a85f2SCharles Keepax 997078a85f2SCharles Keepax snd_soc_dapm_update_dai(substream, &codec_params, codec_dai); 998ddee627cSLiam Girdwood } 999ddee627cSLiam Girdwood 1000aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(cpu_dai, substream, params); 100193e6958aSBenoit Cousson if (ret < 0) 1002ddee627cSLiam Girdwood goto interface_err; 1003ddee627cSLiam Girdwood 1004ca58221dSKuninori Morimoto /* store the parameters for each DAIs */ 1005ca58221dSKuninori Morimoto cpu_dai->rate = params_rate(params); 1006ca58221dSKuninori Morimoto cpu_dai->channels = params_channels(params); 1007ca58221dSKuninori Morimoto cpu_dai->sample_bits = 1008ca58221dSKuninori Morimoto snd_pcm_format_physical_width(params_format(params)); 1009ca58221dSKuninori Morimoto 1010ca58221dSKuninori Morimoto snd_soc_dapm_update_dai(substream, params, cpu_dai); 1011ca58221dSKuninori Morimoto 1012613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 1013245c539aSKuninori Morimoto ret = snd_soc_component_hw_params(component, substream, params); 1014244e2936SCharles Keepax if (ret < 0) { 1015b8135864SKuninori Morimoto dev_err(component->dev, 1016b8135864SKuninori Morimoto "ASoC: %s hw params failed: %d\n", 1017244e2936SCharles Keepax component->name, ret); 1018b8135864SKuninori Morimoto goto component_err; 1019244e2936SCharles Keepax } 1020244e2936SCharles Keepax } 1021244e2936SCharles Keepax component = NULL; 1022b8135864SKuninori Morimoto 1023ddee627cSLiam Girdwood out: 102472b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1025ddee627cSLiam Girdwood return ret; 1026ddee627cSLiam Girdwood 1027b8135864SKuninori Morimoto component_err: 1028244e2936SCharles Keepax soc_pcm_components_hw_free(substream, component); 1029b8135864SKuninori Morimoto 1030846faaedSKuninori Morimoto snd_soc_dai_hw_free(cpu_dai, substream); 10312371abdcSKuninori Morimoto cpu_dai->rate = 0; 1032ddee627cSLiam Girdwood 1033ddee627cSLiam Girdwood interface_err: 10342e5894d7SBenoit Cousson i = rtd->num_codecs; 1035ddee627cSLiam Girdwood 1036ddee627cSLiam Girdwood codec_err: 10376d11b128SKuninori Morimoto for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) { 1038f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1039f47b9ad9SJerome Brunet continue; 1040f47b9ad9SJerome Brunet 1041846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 10422e5894d7SBenoit Cousson codec_dai->rate = 0; 10432e5894d7SBenoit Cousson } 10442e5894d7SBenoit Cousson 104549f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1046ddee627cSLiam Girdwood 104772b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1048ddee627cSLiam Girdwood return ret; 1049ddee627cSLiam Girdwood } 1050ddee627cSLiam Girdwood 1051ddee627cSLiam Girdwood /* 1052ddee627cSLiam Girdwood * Frees resources allocated by hw_params, can be called multiple times 1053ddee627cSLiam Girdwood */ 1054ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream) 1055ddee627cSLiam Girdwood { 1056ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1057ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 10582e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 10597f62b6eeSNicolin Chen bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 10602e5894d7SBenoit Cousson int i; 1061ddee627cSLiam Girdwood 106272b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 1063ddee627cSLiam Girdwood 1064d3383420SNicolin Chen /* clear the corresponding DAIs parameters when going to be inactive */ 1065d3383420SNicolin Chen if (cpu_dai->active == 1) { 1066d3383420SNicolin Chen cpu_dai->rate = 0; 1067d3383420SNicolin Chen cpu_dai->channels = 0; 1068d3383420SNicolin Chen cpu_dai->sample_bits = 0; 1069d3383420SNicolin Chen } 1070d3383420SNicolin Chen 10710b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 1072d3383420SNicolin Chen if (codec_dai->active == 1) { 1073d3383420SNicolin Chen codec_dai->rate = 0; 1074d3383420SNicolin Chen codec_dai->channels = 0; 1075d3383420SNicolin Chen codec_dai->sample_bits = 0; 1076d3383420SNicolin Chen } 10772e5894d7SBenoit Cousson } 1078d3383420SNicolin Chen 1079ddee627cSLiam Girdwood /* apply codec digital mute */ 10800b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 10810f6011fdSKuninori Morimoto int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK]; 10820f6011fdSKuninori Morimoto int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE]; 10830f6011fdSKuninori Morimoto 10840f6011fdSKuninori Morimoto if ((playback && playback_active == 1) || 10850f6011fdSKuninori Morimoto (!playback && capture_active == 1)) 10860b7990e3SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 1, 10872e5894d7SBenoit Cousson substream->stream); 10882e5894d7SBenoit Cousson } 1089ddee627cSLiam Girdwood 1090ddee627cSLiam Girdwood /* free any machine hw params */ 109149f020e5SKuninori Morimoto soc_rtd_hw_free(rtd, substream); 1092ddee627cSLiam Girdwood 1093b8135864SKuninori Morimoto /* free any component resources */ 1094244e2936SCharles Keepax soc_pcm_components_hw_free(substream, NULL); 1095b8135864SKuninori Morimoto 1096ddee627cSLiam Girdwood /* now free hw params for the DAIs */ 10970b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 1098f47b9ad9SJerome Brunet if (!snd_soc_dai_stream_valid(codec_dai, substream->stream)) 1099f47b9ad9SJerome Brunet continue; 1100f47b9ad9SJerome Brunet 1101846faaedSKuninori Morimoto snd_soc_dai_hw_free(codec_dai, substream); 11022e5894d7SBenoit Cousson } 1103ddee627cSLiam Girdwood 1104846faaedSKuninori Morimoto snd_soc_dai_hw_free(cpu_dai, substream); 1105ddee627cSLiam Girdwood 110672b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 1107ddee627cSLiam Girdwood return 0; 1108ddee627cSLiam Girdwood } 1109ddee627cSLiam Girdwood 11104378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd) 1111ddee627cSLiam Girdwood { 1112ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1113b8135864SKuninori Morimoto struct snd_soc_component *component; 1114ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 11152e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 11162e5894d7SBenoit Cousson int i, ret; 1117ddee627cSLiam Girdwood 1118ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 1119ddee627cSLiam Girdwood if (ret < 0) 1120ddee627cSLiam Girdwood return ret; 1121ddee627cSLiam Girdwood 1122613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11235693d50cSKuninori Morimoto ret = snd_soc_component_trigger(component, substream, cmd); 1124b8135864SKuninori Morimoto if (ret < 0) 1125b8135864SKuninori Morimoto return ret; 1126b8135864SKuninori Morimoto } 1127b8135864SKuninori Morimoto 1128901e822bSDan Carpenter ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 1129ddee627cSLiam Girdwood if (ret < 0) 1130ddee627cSLiam Girdwood return ret; 11314792b0dbSJarkko Nikula 11324378f1fbSPeter Ujfalusi for_each_rtd_codec_dai(rtd, i, codec_dai) { 11334378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 11344378f1fbSPeter Ujfalusi if (ret < 0) 11354378f1fbSPeter Ujfalusi return ret; 11364378f1fbSPeter Ujfalusi } 11374378f1fbSPeter Ujfalusi 11384378f1fbSPeter Ujfalusi return 0; 11394378f1fbSPeter Ujfalusi } 11404378f1fbSPeter Ujfalusi 11414378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd) 11424378f1fbSPeter Ujfalusi { 11434378f1fbSPeter Ujfalusi struct snd_soc_pcm_runtime *rtd = substream->private_data; 11444378f1fbSPeter Ujfalusi struct snd_soc_component *component; 11454378f1fbSPeter Ujfalusi struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 11464378f1fbSPeter Ujfalusi struct snd_soc_dai *codec_dai; 11474378f1fbSPeter Ujfalusi int i, ret; 11484378f1fbSPeter Ujfalusi 11494378f1fbSPeter Ujfalusi for_each_rtd_codec_dai(rtd, i, codec_dai) { 11504378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(codec_dai, substream, cmd); 11514378f1fbSPeter Ujfalusi if (ret < 0) 11524378f1fbSPeter Ujfalusi return ret; 11534378f1fbSPeter Ujfalusi } 11544378f1fbSPeter Ujfalusi 11554378f1fbSPeter Ujfalusi ret = snd_soc_dai_trigger(cpu_dai, substream, cmd); 11564378f1fbSPeter Ujfalusi if (ret < 0) 11574378f1fbSPeter Ujfalusi return ret; 11584378f1fbSPeter Ujfalusi 1159613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 11604378f1fbSPeter Ujfalusi ret = snd_soc_component_trigger(component, substream, cmd); 11614378f1fbSPeter Ujfalusi if (ret < 0) 11624378f1fbSPeter Ujfalusi return ret; 11634378f1fbSPeter Ujfalusi } 11644378f1fbSPeter Ujfalusi 1165ad2bf9f2SKuninori Morimoto ret = soc_rtd_trigger(rtd, substream, cmd); 11664792b0dbSJarkko Nikula if (ret < 0) 11674792b0dbSJarkko Nikula return ret; 11684792b0dbSJarkko Nikula 1169ddee627cSLiam Girdwood return 0; 1170ddee627cSLiam Girdwood } 1171ddee627cSLiam Girdwood 11724378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 11734378f1fbSPeter Ujfalusi { 11744378f1fbSPeter Ujfalusi int ret; 11754378f1fbSPeter Ujfalusi 11764378f1fbSPeter Ujfalusi switch (cmd) { 11774378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_START: 11784378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_RESUME: 11794378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 11804378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_start(substream, cmd); 11814378f1fbSPeter Ujfalusi break; 11824378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_STOP: 11834378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_SUSPEND: 11844378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 11854378f1fbSPeter Ujfalusi ret = soc_pcm_trigger_stop(substream, cmd); 11864378f1fbSPeter Ujfalusi break; 11874378f1fbSPeter Ujfalusi default: 11884378f1fbSPeter Ujfalusi return -EINVAL; 11894378f1fbSPeter Ujfalusi } 11904378f1fbSPeter Ujfalusi 11914378f1fbSPeter Ujfalusi return ret; 11924378f1fbSPeter Ujfalusi } 11934378f1fbSPeter Ujfalusi 119445c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, 119545c0a188SMark Brown int cmd) 119607bf84aaSLiam Girdwood { 119707bf84aaSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 119807bf84aaSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 11992e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 12002e5894d7SBenoit Cousson int i, ret; 120107bf84aaSLiam Girdwood 12020b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 12035c0769afSKuninori Morimoto ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd); 120407bf84aaSLiam Girdwood if (ret < 0) 120507bf84aaSLiam Girdwood return ret; 120607bf84aaSLiam Girdwood } 120707bf84aaSLiam Girdwood 1208901e822bSDan Carpenter ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd); 120907bf84aaSLiam Girdwood if (ret < 0) 121007bf84aaSLiam Girdwood return ret; 12115c0769afSKuninori Morimoto 121207bf84aaSLiam Girdwood return 0; 121307bf84aaSLiam Girdwood } 1214ddee627cSLiam Girdwood /* 1215ddee627cSLiam Girdwood * soc level wrapper for pointer callback 1216ef050becSCharles Keepax * If cpu_dai, codec_dai, component driver has the delay callback, then 1217ddee627cSLiam Girdwood * the runtime->delay will be updated accordingly. 1218ddee627cSLiam Girdwood */ 1219ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream) 1220ddee627cSLiam Girdwood { 1221ddee627cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 1222ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 12232e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 1224ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 1225ddee627cSLiam Girdwood snd_pcm_uframes_t offset = 0; 1226ddee627cSLiam Girdwood snd_pcm_sframes_t delay = 0; 12272e5894d7SBenoit Cousson snd_pcm_sframes_t codec_delay = 0; 12282e5894d7SBenoit Cousson int i; 1229ddee627cSLiam Girdwood 12309fb4c2bfSAkshu Agrawal /* clearing the previous total delay */ 12319fb4c2bfSAkshu Agrawal runtime->delay = 0; 12329fb4c2bfSAkshu Agrawal 12330035e256SKuninori Morimoto offset = snd_soc_pcm_component_pointer(substream); 1234b8135864SKuninori Morimoto 12359fb4c2bfSAkshu Agrawal /* base delay if assigned in pointer callback */ 12369fb4c2bfSAkshu Agrawal delay = runtime->delay; 1237b8135864SKuninori Morimoto 12381dea80d4SKuninori Morimoto delay += snd_soc_dai_delay(cpu_dai, substream); 1239ddee627cSLiam Girdwood 12400b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 12412e5894d7SBenoit Cousson codec_delay = max(codec_delay, 12421dea80d4SKuninori Morimoto snd_soc_dai_delay(codec_dai, substream)); 12432e5894d7SBenoit Cousson } 12442e5894d7SBenoit Cousson delay += codec_delay; 1245ddee627cSLiam Girdwood 1246ddee627cSLiam Girdwood runtime->delay = delay; 1247ddee627cSLiam Girdwood 1248ddee627cSLiam Girdwood return offset; 1249ddee627cSLiam Girdwood } 1250ddee627cSLiam Girdwood 125101d7584cSLiam Girdwood /* connect a FE and BE */ 125201d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe, 125301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 125401d7584cSLiam Girdwood { 125501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1256a9764869SKaiChieh Chuang unsigned long flags; 1257bd0b609eSTakashi Iwai #ifdef CONFIG_DEBUG_FS 12580632fa04SHans de Goede char *name; 1259bd0b609eSTakashi Iwai #endif 126001d7584cSLiam Girdwood 126101d7584cSLiam Girdwood /* only add new dpcms */ 12628d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 126301d7584cSLiam Girdwood if (dpcm->be == be && dpcm->fe == fe) 126401d7584cSLiam Girdwood return 0; 126501d7584cSLiam Girdwood } 126601d7584cSLiam Girdwood 126701d7584cSLiam Girdwood dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL); 126801d7584cSLiam Girdwood if (!dpcm) 126901d7584cSLiam Girdwood return -ENOMEM; 127001d7584cSLiam Girdwood 127101d7584cSLiam Girdwood dpcm->be = be; 127201d7584cSLiam Girdwood dpcm->fe = fe; 127301d7584cSLiam Girdwood be->dpcm[stream].runtime = fe->dpcm[stream].runtime; 127401d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW; 1275a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 127601d7584cSLiam Girdwood list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients); 127701d7584cSLiam Girdwood list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients); 1278a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 127901d7584cSLiam Girdwood 128001d7584cSLiam Girdwood dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n", 128101d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 128201d7584cSLiam Girdwood stream ? "<-" : "->", be->dai_link->name); 128301d7584cSLiam Girdwood 1284f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS 12850632fa04SHans de Goede name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name, 12860632fa04SHans de Goede stream ? "capture" : "playback"); 12870632fa04SHans de Goede if (name) { 12880632fa04SHans de Goede dpcm->debugfs_state = debugfs_create_dir(name, 1289fee531d6SGreg Kroah-Hartman fe->debugfs_dpcm_root); 12900632fa04SHans de Goede debugfs_create_u32("state", 0644, dpcm->debugfs_state, 12910632fa04SHans de Goede &dpcm->state); 12920632fa04SHans de Goede kfree(name); 12930632fa04SHans de Goede } 1294f86dcef8SLiam Girdwood #endif 129501d7584cSLiam Girdwood return 1; 129601d7584cSLiam Girdwood } 129701d7584cSLiam Girdwood 129801d7584cSLiam Girdwood /* reparent a BE onto another FE */ 129901d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe, 130001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 130101d7584cSLiam Girdwood { 130201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 130301d7584cSLiam Girdwood struct snd_pcm_substream *fe_substream, *be_substream; 130401d7584cSLiam Girdwood 130501d7584cSLiam Girdwood /* reparent if BE is connected to other FEs */ 130601d7584cSLiam Girdwood if (!be->dpcm[stream].users) 130701d7584cSLiam Girdwood return; 130801d7584cSLiam Girdwood 130901d7584cSLiam Girdwood be_substream = snd_soc_dpcm_get_substream(be, stream); 131001d7584cSLiam Girdwood 1311d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 131201d7584cSLiam Girdwood if (dpcm->fe == fe) 131301d7584cSLiam Girdwood continue; 131401d7584cSLiam Girdwood 131501d7584cSLiam Girdwood dev_dbg(fe->dev, "reparent %s path %s %s %s\n", 131601d7584cSLiam Girdwood stream ? "capture" : "playback", 131701d7584cSLiam Girdwood dpcm->fe->dai_link->name, 131801d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 131901d7584cSLiam Girdwood 132001d7584cSLiam Girdwood fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream); 132101d7584cSLiam Girdwood be_substream->runtime = fe_substream->runtime; 132201d7584cSLiam Girdwood break; 132301d7584cSLiam Girdwood } 132401d7584cSLiam Girdwood } 132501d7584cSLiam Girdwood 132601d7584cSLiam Girdwood /* disconnect a BE and FE */ 132723607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream) 132801d7584cSLiam Girdwood { 132901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm, *d; 1330a9764869SKaiChieh Chuang unsigned long flags; 133101d7584cSLiam Girdwood 13328d6258a4SKuninori Morimoto for_each_dpcm_be_safe(fe, stream, dpcm, d) { 1333103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n", 133401d7584cSLiam Girdwood stream ? "capture" : "playback", 133501d7584cSLiam Girdwood dpcm->be->dai_link->name); 133601d7584cSLiam Girdwood 133701d7584cSLiam Girdwood if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE) 133801d7584cSLiam Girdwood continue; 133901d7584cSLiam Girdwood 134001d7584cSLiam Girdwood dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n", 134101d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name, 134201d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name); 134301d7584cSLiam Girdwood 134401d7584cSLiam Girdwood /* BEs still alive need new FE */ 134501d7584cSLiam Girdwood dpcm_be_reparent(fe, dpcm->be, stream); 134601d7584cSLiam Girdwood 1347f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS 1348fee531d6SGreg Kroah-Hartman debugfs_remove_recursive(dpcm->debugfs_state); 1349f86dcef8SLiam Girdwood #endif 1350a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 135101d7584cSLiam Girdwood list_del(&dpcm->list_be); 135201d7584cSLiam Girdwood list_del(&dpcm->list_fe); 1353a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 135401d7584cSLiam Girdwood kfree(dpcm); 135501d7584cSLiam Girdwood } 135601d7584cSLiam Girdwood } 135701d7584cSLiam Girdwood 135801d7584cSLiam Girdwood /* get BE for DAI widget and stream */ 135901d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card, 136001d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget, int stream) 136101d7584cSLiam Girdwood { 136201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 136393597faeSKuninori Morimoto struct snd_soc_dapm_widget *w; 13647afecb30SKuninori Morimoto struct snd_soc_dai *dai; 13651a497983SMengdong Lin int i; 136601d7584cSLiam Girdwood 13673c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name); 13683c146465SLiam Girdwood 1369bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, be) { 137001d7584cSLiam Girdwood 137135ea0655SLiam Girdwood if (!be->dai_link->no_pcm) 137235ea0655SLiam Girdwood continue; 137335ea0655SLiam Girdwood 13740c01f6caSKuninori Morimoto w = snd_soc_dai_get_widget(be->cpu_dai, stream); 137593597faeSKuninori Morimoto 13763c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: try BE : %s\n", 137793597faeSKuninori Morimoto w ? w->name : "(not set)"); 13783c146465SLiam Girdwood 137993597faeSKuninori Morimoto if (w == widget) 138001d7584cSLiam Girdwood return be; 13812e5894d7SBenoit Cousson 13827afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 13830c01f6caSKuninori Morimoto w = snd_soc_dai_get_widget(dai, stream); 138493597faeSKuninori Morimoto 138593597faeSKuninori Morimoto if (w == widget) 13862e5894d7SBenoit Cousson return be; 13872e5894d7SBenoit Cousson } 138801d7584cSLiam Girdwood } 138901d7584cSLiam Girdwood 13909d6ee365SJerome Brunet /* Widget provided is not a BE */ 139101d7584cSLiam Girdwood return NULL; 139201d7584cSLiam Girdwood } 139301d7584cSLiam Girdwood 139401d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list, 139501d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget) 139601d7584cSLiam Girdwood { 139709e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *w; 139801d7584cSLiam Girdwood int i; 139901d7584cSLiam Girdwood 140009e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, w) 140109e88f8aSKuninori Morimoto if (widget == w) 140201d7584cSLiam Girdwood return 1; 140301d7584cSLiam Girdwood 140401d7584cSLiam Girdwood return 0; 140501d7584cSLiam Girdwood } 140601d7584cSLiam Girdwood 14075fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, 14085fdd022cSPiotr Stankiewicz enum snd_soc_dapm_direction dir) 14095fdd022cSPiotr Stankiewicz { 14105fdd022cSPiotr Stankiewicz struct snd_soc_card *card = widget->dapm->card; 14115fdd022cSPiotr Stankiewicz struct snd_soc_pcm_runtime *rtd; 1412c2cd8216SKuninori Morimoto int stream; 14135fdd022cSPiotr Stankiewicz 1414c2cd8216SKuninori Morimoto /* adjust dir to stream */ 1415c2cd8216SKuninori Morimoto if (dir == SND_SOC_DAPM_DIR_OUT) 1416c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK; 1417c2cd8216SKuninori Morimoto else 1418c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE; 1419c2cd8216SKuninori Morimoto 1420027a4838SKuninori Morimoto rtd = dpcm_get_be(card, widget, stream); 1421027a4838SKuninori Morimoto if (rtd) 14225fdd022cSPiotr Stankiewicz return true; 14235fdd022cSPiotr Stankiewicz 14245fdd022cSPiotr Stankiewicz return false; 14255fdd022cSPiotr Stankiewicz } 14265fdd022cSPiotr Stankiewicz 142723607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe, 14281ce43acfSLars-Peter Clausen int stream, struct snd_soc_dapm_widget_list **list) 142901d7584cSLiam Girdwood { 143001d7584cSLiam Girdwood struct snd_soc_dai *cpu_dai = fe->cpu_dai; 143101d7584cSLiam Girdwood int paths; 143201d7584cSLiam Girdwood 143301d7584cSLiam Girdwood /* get number of valid DAI paths and their widgets */ 14346742064aSPiotr Stankiewicz paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list, 14355fdd022cSPiotr Stankiewicz dpcm_end_walk_at_be); 143601d7584cSLiam Girdwood 1437103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths, 143801d7584cSLiam Girdwood stream ? "capture" : "playback"); 143901d7584cSLiam Girdwood 144001d7584cSLiam Girdwood return paths; 144101d7584cSLiam Girdwood } 144201d7584cSLiam Girdwood 144352645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list) 144452645e33SKuninori Morimoto { 144552645e33SKuninori Morimoto snd_soc_dapm_dai_free_widgets(list); 144652645e33SKuninori Morimoto } 144752645e33SKuninori Morimoto 144801d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream, 144901d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 145001d7584cSLiam Girdwood { 145101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 145201d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 145301d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget; 14547afecb30SKuninori Morimoto struct snd_soc_dai *dai; 145501d7584cSLiam Girdwood int prune = 0; 1456bed646dcSKuninori Morimoto int do_prune; 145701d7584cSLiam Girdwood 145801d7584cSLiam Girdwood /* Destroy any old FE <--> BE connections */ 14598d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 14602e5894d7SBenoit Cousson unsigned int i; 146101d7584cSLiam Girdwood 146201d7584cSLiam Girdwood /* is there a valid CPU DAI widget for this BE */ 14630c01f6caSKuninori Morimoto widget = snd_soc_dai_get_widget(dpcm->be->cpu_dai, stream); 146401d7584cSLiam Girdwood 146501d7584cSLiam Girdwood /* prune the BE if it's no longer in our active list */ 146601d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 146701d7584cSLiam Girdwood continue; 146801d7584cSLiam Girdwood 146901d7584cSLiam Girdwood /* is there a valid CODEC DAI widget for this BE */ 1470bed646dcSKuninori Morimoto do_prune = 1; 14717afecb30SKuninori Morimoto for_each_rtd_codec_dai(dpcm->be, i, dai) { 14720c01f6caSKuninori Morimoto widget = snd_soc_dai_get_widget(dai, stream); 147301d7584cSLiam Girdwood 147401d7584cSLiam Girdwood /* prune the BE if it's no longer in our active list */ 147501d7584cSLiam Girdwood if (widget && widget_in_list(list, widget)) 1476bed646dcSKuninori Morimoto do_prune = 0; 14772e5894d7SBenoit Cousson } 1478bed646dcSKuninori Morimoto if (!do_prune) 1479bed646dcSKuninori Morimoto continue; 148001d7584cSLiam Girdwood 1481103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n", 148201d7584cSLiam Girdwood stream ? "capture" : "playback", 148301d7584cSLiam Girdwood dpcm->be->dai_link->name, fe->dai_link->name); 148401d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 148501d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 148601d7584cSLiam Girdwood prune++; 148701d7584cSLiam Girdwood } 148801d7584cSLiam Girdwood 1489103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune); 149001d7584cSLiam Girdwood return prune; 149101d7584cSLiam Girdwood } 149201d7584cSLiam Girdwood 149301d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream, 149401d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_) 149501d7584cSLiam Girdwood { 149601d7584cSLiam Girdwood struct snd_soc_card *card = fe->card; 149701d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_; 149801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be; 149909e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *widget; 150001d7584cSLiam Girdwood int i, new = 0, err; 150101d7584cSLiam Girdwood 150201d7584cSLiam Girdwood /* Create any new FE <--> BE connections */ 150309e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, widget) { 150401d7584cSLiam Girdwood 150509e88f8aSKuninori Morimoto switch (widget->id) { 15064616274dSMark Brown case snd_soc_dapm_dai_in: 1507c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_PLAYBACK) 1508c5b8540dSKoro Chen continue; 1509c5b8540dSKoro Chen break; 15104616274dSMark Brown case snd_soc_dapm_dai_out: 1511c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_CAPTURE) 1512c5b8540dSKoro Chen continue; 15134616274dSMark Brown break; 15144616274dSMark Brown default: 151501d7584cSLiam Girdwood continue; 15164616274dSMark Brown } 151701d7584cSLiam Girdwood 151801d7584cSLiam Girdwood /* is there a valid BE rtd for this widget */ 151909e88f8aSKuninori Morimoto be = dpcm_get_be(card, widget, stream); 152001d7584cSLiam Girdwood if (!be) { 1521103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no BE found for %s\n", 152209e88f8aSKuninori Morimoto widget->name); 152301d7584cSLiam Girdwood continue; 152401d7584cSLiam Girdwood } 152501d7584cSLiam Girdwood 152601d7584cSLiam Girdwood /* make sure BE is a real BE */ 152701d7584cSLiam Girdwood if (!be->dai_link->no_pcm) 152801d7584cSLiam Girdwood continue; 152901d7584cSLiam Girdwood 153001d7584cSLiam Girdwood /* don't connect if FE is not running */ 153123607025SLiam Girdwood if (!fe->dpcm[stream].runtime && !fe->fe_compr) 153201d7584cSLiam Girdwood continue; 153301d7584cSLiam Girdwood 153401d7584cSLiam Girdwood /* newly connected FE and BE */ 153501d7584cSLiam Girdwood err = dpcm_be_connect(fe, be, stream); 153601d7584cSLiam Girdwood if (err < 0) { 1537103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: can't connect %s\n", 153809e88f8aSKuninori Morimoto widget->name); 153901d7584cSLiam Girdwood break; 154001d7584cSLiam Girdwood } else if (err == 0) /* already connected */ 154101d7584cSLiam Girdwood continue; 154201d7584cSLiam Girdwood 154301d7584cSLiam Girdwood /* new */ 154401d7584cSLiam Girdwood be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE; 154501d7584cSLiam Girdwood new++; 154601d7584cSLiam Girdwood } 154701d7584cSLiam Girdwood 1548103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new); 154901d7584cSLiam Girdwood return new; 155001d7584cSLiam Girdwood } 155101d7584cSLiam Girdwood 155201d7584cSLiam Girdwood /* 155301d7584cSLiam Girdwood * Find the corresponding BE DAIs that source or sink audio to this 155401d7584cSLiam Girdwood * FE substream. 155501d7584cSLiam Girdwood */ 155623607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe, 155701d7584cSLiam Girdwood int stream, struct snd_soc_dapm_widget_list **list, int new) 155801d7584cSLiam Girdwood { 155901d7584cSLiam Girdwood if (new) 156001d7584cSLiam Girdwood return dpcm_add_paths(fe, stream, list); 156101d7584cSLiam Girdwood else 156201d7584cSLiam Girdwood return dpcm_prune_paths(fe, stream, list); 156301d7584cSLiam Girdwood } 156401d7584cSLiam Girdwood 156523607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream) 156601d7584cSLiam Girdwood { 156701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 1568a9764869SKaiChieh Chuang unsigned long flags; 156901d7584cSLiam Girdwood 1570a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 15718d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 157201d7584cSLiam Girdwood dpcm->be->dpcm[stream].runtime_update = 157301d7584cSLiam Girdwood SND_SOC_DPCM_UPDATE_NO; 1574a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 157501d7584cSLiam Girdwood } 157601d7584cSLiam Girdwood 157701d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, 157801d7584cSLiam Girdwood int stream) 157901d7584cSLiam Girdwood { 158001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 158101d7584cSLiam Girdwood 158201d7584cSLiam Girdwood /* disable any enabled and non active backends */ 15838d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 158401d7584cSLiam Girdwood 158501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 158601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 158701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 158801d7584cSLiam Girdwood 158901d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1590103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 159101d7584cSLiam Girdwood stream ? "capture" : "playback", 159201d7584cSLiam Girdwood be->dpcm[stream].state); 159301d7584cSLiam Girdwood 159401d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 159501d7584cSLiam Girdwood continue; 159601d7584cSLiam Girdwood 159701d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 159801d7584cSLiam Girdwood continue; 159901d7584cSLiam Girdwood 160001d7584cSLiam Girdwood soc_pcm_close(be_substream); 160101d7584cSLiam Girdwood be_substream->runtime = NULL; 160201d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 160301d7584cSLiam Girdwood } 160401d7584cSLiam Girdwood } 160501d7584cSLiam Girdwood 160623607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream) 160701d7584cSLiam Girdwood { 160801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 160901d7584cSLiam Girdwood int err, count = 0; 161001d7584cSLiam Girdwood 161101d7584cSLiam Girdwood /* only startup BE DAIs that are either sinks or sources to this FE DAI */ 16128d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 161301d7584cSLiam Girdwood 161401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 161501d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 161601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 161701d7584cSLiam Girdwood 16182062b4c5SRussell King - ARM Linux if (!be_substream) { 16192062b4c5SRussell King - ARM Linux dev_err(be->dev, "ASoC: no backend %s stream\n", 16202062b4c5SRussell King - ARM Linux stream ? "capture" : "playback"); 16212062b4c5SRussell King - ARM Linux continue; 16222062b4c5SRussell King - ARM Linux } 16232062b4c5SRussell King - ARM Linux 162401d7584cSLiam Girdwood /* is this op for this BE ? */ 162501d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 162601d7584cSLiam Girdwood continue; 162701d7584cSLiam Girdwood 162801d7584cSLiam Girdwood /* first time the dpcm is open ? */ 162901d7584cSLiam Girdwood if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) 1630103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: too many users %s at open %d\n", 163101d7584cSLiam Girdwood stream ? "capture" : "playback", 163201d7584cSLiam Girdwood be->dpcm[stream].state); 163301d7584cSLiam Girdwood 163401d7584cSLiam Girdwood if (be->dpcm[stream].users++ != 0) 163501d7584cSLiam Girdwood continue; 163601d7584cSLiam Girdwood 163701d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) && 163801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE)) 163901d7584cSLiam Girdwood continue; 164001d7584cSLiam Girdwood 16412062b4c5SRussell King - ARM Linux dev_dbg(be->dev, "ASoC: open %s BE %s\n", 16422062b4c5SRussell King - ARM Linux stream ? "capture" : "playback", be->dai_link->name); 164301d7584cSLiam Girdwood 164401d7584cSLiam Girdwood be_substream->runtime = be->dpcm[stream].runtime; 164501d7584cSLiam Girdwood err = soc_pcm_open(be_substream); 164601d7584cSLiam Girdwood if (err < 0) { 1647103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: BE open failed %d\n", err); 164801d7584cSLiam Girdwood be->dpcm[stream].users--; 164901d7584cSLiam Girdwood if (be->dpcm[stream].users < 0) 1650103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at unwind %d\n", 165101d7584cSLiam Girdwood stream ? "capture" : "playback", 165201d7584cSLiam Girdwood be->dpcm[stream].state); 165301d7584cSLiam Girdwood 165401d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 165501d7584cSLiam Girdwood goto unwind; 165601d7584cSLiam Girdwood } 165701d7584cSLiam Girdwood 165801d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 165901d7584cSLiam Girdwood count++; 166001d7584cSLiam Girdwood } 166101d7584cSLiam Girdwood 166201d7584cSLiam Girdwood return count; 166301d7584cSLiam Girdwood 166401d7584cSLiam Girdwood unwind: 166501d7584cSLiam Girdwood /* disable any enabled and non active backends */ 16668d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 166701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 166801d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 166901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 167001d7584cSLiam Girdwood 167101d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 167201d7584cSLiam Girdwood continue; 167301d7584cSLiam Girdwood 167401d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1675103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close %d\n", 167601d7584cSLiam Girdwood stream ? "capture" : "playback", 167701d7584cSLiam Girdwood be->dpcm[stream].state); 167801d7584cSLiam Girdwood 167901d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 168001d7584cSLiam Girdwood continue; 168101d7584cSLiam Girdwood 168201d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) 168301d7584cSLiam Girdwood continue; 168401d7584cSLiam Girdwood 168501d7584cSLiam Girdwood soc_pcm_close(be_substream); 168601d7584cSLiam Girdwood be_substream->runtime = NULL; 168701d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 168801d7584cSLiam Girdwood } 168901d7584cSLiam Girdwood 169001d7584cSLiam Girdwood return err; 169101d7584cSLiam Girdwood } 169201d7584cSLiam Girdwood 169308ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime, 1694435ffb76SJerome Brunet struct snd_soc_pcm_stream *stream) 169508ae9b45SLars-Peter Clausen { 169608ae9b45SLars-Peter Clausen runtime->hw.rate_min = stream->rate_min; 1697e33ffbd9SCharles Keepax runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX); 169808ae9b45SLars-Peter Clausen runtime->hw.channels_min = stream->channels_min; 169908ae9b45SLars-Peter Clausen runtime->hw.channels_max = stream->channels_max; 1700002220a9SLars-Peter Clausen if (runtime->hw.formats) 1701435ffb76SJerome Brunet runtime->hw.formats &= stream->formats; 1702002220a9SLars-Peter Clausen else 1703435ffb76SJerome Brunet runtime->hw.formats = stream->formats; 170408ae9b45SLars-Peter Clausen runtime->hw.rates = stream->rates; 170508ae9b45SLars-Peter Clausen } 170608ae9b45SLars-Peter Clausen 1707435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream, 1708435ffb76SJerome Brunet u64 *formats) 1709b073ed4eSKuninori Morimoto { 1710b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *fe = substream->private_data; 1711b073ed4eSKuninori Morimoto struct snd_soc_dpcm *dpcm; 17127afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1713b073ed4eSKuninori Morimoto int stream = substream->stream; 1714b073ed4eSKuninori Morimoto 1715b073ed4eSKuninori Morimoto if (!fe->dai_link->dpcm_merged_format) 1716435ffb76SJerome Brunet return; 1717b073ed4eSKuninori Morimoto 1718b073ed4eSKuninori Morimoto /* 1719b073ed4eSKuninori Morimoto * It returns merged BE codec format 1720b073ed4eSKuninori Morimoto * if FE want to use it (= dpcm_merged_format) 1721b073ed4eSKuninori Morimoto */ 1722b073ed4eSKuninori Morimoto 17238d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1724b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be; 1725b073ed4eSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream; 1726b073ed4eSKuninori Morimoto int i; 1727b073ed4eSKuninori Morimoto 17287afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 17294febced1SJerome Brunet /* 17304febced1SJerome Brunet * Skip CODECs which don't support the current stream 17314febced1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 17324febced1SJerome Brunet */ 17337afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 17344febced1SJerome Brunet continue; 17354febced1SJerome Brunet 1736acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1737b073ed4eSKuninori Morimoto 1738435ffb76SJerome Brunet *formats &= codec_stream->formats; 1739435ffb76SJerome Brunet } 1740b073ed4eSKuninori Morimoto } 1741b073ed4eSKuninori Morimoto } 1742b073ed4eSKuninori Morimoto 1743435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream, 1744f4c277b8SJiada Wang unsigned int *channels_min, 1745f4c277b8SJiada Wang unsigned int *channels_max) 1746f4c277b8SJiada Wang { 1747f4c277b8SJiada Wang struct snd_soc_pcm_runtime *fe = substream->private_data; 1748f4c277b8SJiada Wang struct snd_soc_dpcm *dpcm; 1749f4c277b8SJiada Wang int stream = substream->stream; 1750f4c277b8SJiada Wang 1751f4c277b8SJiada Wang if (!fe->dai_link->dpcm_merged_chan) 1752f4c277b8SJiada Wang return; 1753f4c277b8SJiada Wang 1754f4c277b8SJiada Wang /* 1755f4c277b8SJiada Wang * It returns merged BE codec channel; 1756f4c277b8SJiada Wang * if FE want to use it (= dpcm_merged_chan) 1757f4c277b8SJiada Wang */ 1758f4c277b8SJiada Wang 17598d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1760f4c277b8SJiada Wang struct snd_soc_pcm_runtime *be = dpcm->be; 1761f4c277b8SJiada Wang struct snd_soc_pcm_stream *codec_stream; 17624f2bd18bSJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 1763f4c277b8SJiada Wang 1764acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); 17654f2bd18bSJerome Brunet 17664f2bd18bSJerome Brunet *channels_min = max(*channels_min, cpu_stream->channels_min); 17674f2bd18bSJerome Brunet *channels_max = min(*channels_max, cpu_stream->channels_max); 17684f2bd18bSJerome Brunet 17694f2bd18bSJerome Brunet /* 17704f2bd18bSJerome Brunet * chan min/max cannot be enforced if there are multiple CODEC 17714f2bd18bSJerome Brunet * DAIs connected to a single CPU DAI, use CPU DAI's directly 17724f2bd18bSJerome Brunet */ 17734f2bd18bSJerome Brunet if (be->num_codecs == 1) { 1774acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream); 1775f4c277b8SJiada Wang 1776f4c277b8SJiada Wang *channels_min = max(*channels_min, 1777f4c277b8SJiada Wang codec_stream->channels_min); 1778f4c277b8SJiada Wang *channels_max = min(*channels_max, 1779f4c277b8SJiada Wang codec_stream->channels_max); 1780f4c277b8SJiada Wang } 1781f4c277b8SJiada Wang } 1782f4c277b8SJiada Wang } 1783f4c277b8SJiada Wang 1784baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream, 1785baacd8d1SJerome Brunet unsigned int *rates, 1786baacd8d1SJerome Brunet unsigned int *rate_min, 1787baacd8d1SJerome Brunet unsigned int *rate_max) 1788baacd8d1SJerome Brunet { 1789baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *fe = substream->private_data; 1790baacd8d1SJerome Brunet struct snd_soc_dpcm *dpcm; 1791baacd8d1SJerome Brunet int stream = substream->stream; 1792baacd8d1SJerome Brunet 1793baacd8d1SJerome Brunet if (!fe->dai_link->dpcm_merged_rate) 1794baacd8d1SJerome Brunet return; 1795baacd8d1SJerome Brunet 1796baacd8d1SJerome Brunet /* 1797baacd8d1SJerome Brunet * It returns merged BE codec channel; 1798baacd8d1SJerome Brunet * if FE want to use it (= dpcm_merged_chan) 1799baacd8d1SJerome Brunet */ 1800baacd8d1SJerome Brunet 18018d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1802baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *be = dpcm->be; 1803baacd8d1SJerome Brunet struct snd_soc_pcm_stream *codec_stream; 1804baacd8d1SJerome Brunet struct snd_soc_pcm_stream *cpu_stream; 18057afecb30SKuninori Morimoto struct snd_soc_dai *dai; 1806baacd8d1SJerome Brunet int i; 1807baacd8d1SJerome Brunet 1808acf253c1SKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream); 1809baacd8d1SJerome Brunet 1810baacd8d1SJerome Brunet *rate_min = max(*rate_min, cpu_stream->rate_min); 1811baacd8d1SJerome Brunet *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max); 1812baacd8d1SJerome Brunet *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates); 1813baacd8d1SJerome Brunet 18147afecb30SKuninori Morimoto for_each_rtd_codec_dai(be, i, dai) { 1815baacd8d1SJerome Brunet /* 1816baacd8d1SJerome Brunet * Skip CODECs which don't support the current stream 1817baacd8d1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details 1818baacd8d1SJerome Brunet */ 18197afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream)) 1820baacd8d1SJerome Brunet continue; 1821baacd8d1SJerome Brunet 1822acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream); 1823baacd8d1SJerome Brunet 1824baacd8d1SJerome Brunet *rate_min = max(*rate_min, codec_stream->rate_min); 1825baacd8d1SJerome Brunet *rate_max = min_not_zero(*rate_max, 1826baacd8d1SJerome Brunet codec_stream->rate_max); 1827baacd8d1SJerome Brunet *rates = snd_pcm_rate_mask_intersect(*rates, 1828baacd8d1SJerome Brunet codec_stream->rates); 1829baacd8d1SJerome Brunet } 1830baacd8d1SJerome Brunet } 1831baacd8d1SJerome Brunet } 1832baacd8d1SJerome Brunet 183345c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream) 183401d7584cSLiam Girdwood { 183501d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime; 183601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 183701d7584cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 183801d7584cSLiam Girdwood struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver; 183901d7584cSLiam Girdwood 184008ae9b45SLars-Peter Clausen if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1841435ffb76SJerome Brunet dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback); 184208ae9b45SLars-Peter Clausen else 1843435ffb76SJerome Brunet dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture); 1844f4c277b8SJiada Wang 1845435ffb76SJerome Brunet dpcm_runtime_merge_format(substream, &runtime->hw.formats); 1846435ffb76SJerome Brunet dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min, 1847435ffb76SJerome Brunet &runtime->hw.channels_max); 1848baacd8d1SJerome Brunet dpcm_runtime_merge_rate(substream, &runtime->hw.rates, 1849baacd8d1SJerome Brunet &runtime->hw.rate_min, &runtime->hw.rate_max); 185001d7584cSLiam Girdwood } 185101d7584cSLiam Girdwood 1852ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd); 1853ea9d0d77STakashi Iwai 1854ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock 1855ea9d0d77STakashi Iwai * for avoiding the race with trigger callback. 1856ea9d0d77STakashi Iwai * If the state is unset and a trigger is pending while the previous operation, 1857ea9d0d77STakashi Iwai * process the pending trigger action here. 1858ea9d0d77STakashi Iwai */ 1859ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe, 1860ea9d0d77STakashi Iwai int stream, enum snd_soc_dpcm_update state) 1861ea9d0d77STakashi Iwai { 1862ea9d0d77STakashi Iwai struct snd_pcm_substream *substream = 1863ea9d0d77STakashi Iwai snd_soc_dpcm_get_substream(fe, stream); 1864ea9d0d77STakashi Iwai 1865ea9d0d77STakashi Iwai snd_pcm_stream_lock_irq(substream); 1866ea9d0d77STakashi Iwai if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) { 1867ea9d0d77STakashi Iwai dpcm_fe_dai_do_trigger(substream, 1868ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending - 1); 1869ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = 0; 1870ea9d0d77STakashi Iwai } 1871ea9d0d77STakashi Iwai fe->dpcm[stream].runtime_update = state; 1872ea9d0d77STakashi Iwai snd_pcm_stream_unlock_irq(substream); 1873ea9d0d77STakashi Iwai } 1874ea9d0d77STakashi Iwai 1875906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream, 1876906c7d69SPC Liao int stream) 1877906c7d69SPC Liao { 1878906c7d69SPC Liao struct snd_soc_dpcm *dpcm; 1879906c7d69SPC Liao struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 1880906c7d69SPC Liao struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai; 1881906c7d69SPC Liao int err; 1882906c7d69SPC Liao 1883906c7d69SPC Liao /* apply symmetry for FE */ 1884906c7d69SPC Liao if (soc_pcm_has_symmetry(fe_substream)) 1885906c7d69SPC Liao fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1886906c7d69SPC Liao 1887906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1888906c7d69SPC Liao if (fe_cpu_dai->active) { 1889906c7d69SPC Liao err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai); 1890906c7d69SPC Liao if (err < 0) 1891906c7d69SPC Liao return err; 1892906c7d69SPC Liao } 1893906c7d69SPC Liao 1894906c7d69SPC Liao /* apply symmetry for BE */ 18958d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 1896906c7d69SPC Liao struct snd_soc_pcm_runtime *be = dpcm->be; 1897906c7d69SPC Liao struct snd_pcm_substream *be_substream = 1898906c7d69SPC Liao snd_soc_dpcm_get_substream(be, stream); 18996246f283SJerome Brunet struct snd_soc_pcm_runtime *rtd; 19000b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai; 1901906c7d69SPC Liao int i; 1902906c7d69SPC Liao 19036246f283SJerome Brunet /* A backend may not have the requested substream */ 19046246f283SJerome Brunet if (!be_substream) 19056246f283SJerome Brunet continue; 19066246f283SJerome Brunet 19076246f283SJerome Brunet rtd = be_substream->private_data; 1908f1176614SJeeja KP if (rtd->dai_link->be_hw_params_fixup) 1909f1176614SJeeja KP continue; 1910f1176614SJeeja KP 1911906c7d69SPC Liao if (soc_pcm_has_symmetry(be_substream)) 1912906c7d69SPC Liao be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 1913906c7d69SPC Liao 1914906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */ 1915906c7d69SPC Liao if (rtd->cpu_dai->active) { 191699bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 191799bcedbdSKai Chieh Chuang rtd->cpu_dai); 1918906c7d69SPC Liao if (err < 0) 1919906c7d69SPC Liao return err; 1920906c7d69SPC Liao } 1921906c7d69SPC Liao 19220b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 19230b7990e3SKuninori Morimoto if (codec_dai->active) { 192499bcedbdSKai Chieh Chuang err = soc_pcm_apply_symmetry(fe_substream, 19250b7990e3SKuninori Morimoto codec_dai); 1926906c7d69SPC Liao if (err < 0) 1927906c7d69SPC Liao return err; 1928906c7d69SPC Liao } 1929906c7d69SPC Liao } 1930906c7d69SPC Liao } 1931906c7d69SPC Liao 1932906c7d69SPC Liao return 0; 1933906c7d69SPC Liao } 1934906c7d69SPC Liao 193501d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream) 193601d7584cSLiam Girdwood { 193701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 193801d7584cSLiam Girdwood struct snd_pcm_runtime *runtime = fe_substream->runtime; 193901d7584cSLiam Girdwood int stream = fe_substream->stream, ret = 0; 194001d7584cSLiam Girdwood 1941ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 194201d7584cSLiam Girdwood 194301d7584cSLiam Girdwood ret = dpcm_be_dai_startup(fe, fe_substream->stream); 194401d7584cSLiam Girdwood if (ret < 0) { 1945103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret); 194601d7584cSLiam Girdwood goto be_err; 194701d7584cSLiam Girdwood } 194801d7584cSLiam Girdwood 1949103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name); 195001d7584cSLiam Girdwood 195101d7584cSLiam Girdwood /* start the DAI frontend */ 195201d7584cSLiam Girdwood ret = soc_pcm_open(fe_substream); 195301d7584cSLiam Girdwood if (ret < 0) { 1954103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret); 195501d7584cSLiam Girdwood goto unwind; 195601d7584cSLiam Girdwood } 195701d7584cSLiam Girdwood 195801d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 195901d7584cSLiam Girdwood 196001d7584cSLiam Girdwood dpcm_set_fe_runtime(fe_substream); 196101d7584cSLiam Girdwood snd_pcm_limit_hw_rates(runtime); 196201d7584cSLiam Girdwood 1963906c7d69SPC Liao ret = dpcm_apply_symmetry(fe_substream, stream); 1964906c7d69SPC Liao if (ret < 0) { 1965906c7d69SPC Liao dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n", 1966906c7d69SPC Liao ret); 1967906c7d69SPC Liao goto unwind; 1968906c7d69SPC Liao } 1969906c7d69SPC Liao 1970ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 197101d7584cSLiam Girdwood return 0; 197201d7584cSLiam Girdwood 197301d7584cSLiam Girdwood unwind: 197401d7584cSLiam Girdwood dpcm_be_dai_startup_unwind(fe, fe_substream->stream); 197501d7584cSLiam Girdwood be_err: 1976ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 197701d7584cSLiam Girdwood return ret; 197801d7584cSLiam Girdwood } 197901d7584cSLiam Girdwood 198023607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 198101d7584cSLiam Girdwood { 198201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 198301d7584cSLiam Girdwood 198401d7584cSLiam Girdwood /* only shutdown BEs that are either sinks or sources to this FE DAI */ 19858d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 198601d7584cSLiam Girdwood 198701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 198801d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 198901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 199001d7584cSLiam Girdwood 199101d7584cSLiam Girdwood /* is this op for this BE ? */ 199201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 199301d7584cSLiam Girdwood continue; 199401d7584cSLiam Girdwood 199501d7584cSLiam Girdwood if (be->dpcm[stream].users == 0) 1996103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at close - state %d\n", 199701d7584cSLiam Girdwood stream ? "capture" : "playback", 199801d7584cSLiam Girdwood be->dpcm[stream].state); 199901d7584cSLiam Girdwood 200001d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0) 200101d7584cSLiam Girdwood continue; 200201d7584cSLiam Girdwood 200301d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 20049c0ac70aSKai Chieh Chuang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) { 20059c0ac70aSKai Chieh Chuang soc_pcm_hw_free(be_substream); 20069c0ac70aSKai Chieh Chuang be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 20079c0ac70aSKai Chieh Chuang } 200801d7584cSLiam Girdwood 2009103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: close BE %s\n", 201094d215ccS彭东林 be->dai_link->name); 201101d7584cSLiam Girdwood 201201d7584cSLiam Girdwood soc_pcm_close(be_substream); 201301d7584cSLiam Girdwood be_substream->runtime = NULL; 201401d7584cSLiam Girdwood 201501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 201601d7584cSLiam Girdwood } 201701d7584cSLiam Girdwood return 0; 201801d7584cSLiam Girdwood } 201901d7584cSLiam Girdwood 202001d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream) 202101d7584cSLiam Girdwood { 202201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 202301d7584cSLiam Girdwood int stream = substream->stream; 202401d7584cSLiam Girdwood 2025ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 202601d7584cSLiam Girdwood 202701d7584cSLiam Girdwood /* shutdown the BEs */ 202801d7584cSLiam Girdwood dpcm_be_dai_shutdown(fe, substream->stream); 202901d7584cSLiam Girdwood 2030103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name); 203101d7584cSLiam Girdwood 203201d7584cSLiam Girdwood /* now shutdown the frontend */ 203301d7584cSLiam Girdwood soc_pcm_close(substream); 203401d7584cSLiam Girdwood 203501d7584cSLiam Girdwood /* run the stream event for each BE */ 2036b0edff42SKuninori Morimoto snd_soc_dapm_stream_stop(fe, stream); 203701d7584cSLiam Girdwood 203801d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 2039ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 204001d7584cSLiam Girdwood return 0; 204101d7584cSLiam Girdwood } 204201d7584cSLiam Girdwood 204323607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream) 204401d7584cSLiam Girdwood { 204501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 204601d7584cSLiam Girdwood 204701d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 204801d7584cSLiam Girdwood * to this frontend DAI */ 20498d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 205001d7584cSLiam Girdwood 205101d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 205201d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 205301d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 205401d7584cSLiam Girdwood 205501d7584cSLiam Girdwood /* is this op for this BE ? */ 205601d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 205701d7584cSLiam Girdwood continue; 205801d7584cSLiam Girdwood 205901d7584cSLiam Girdwood /* only free hw when no longer used - check all FEs */ 206001d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 206101d7584cSLiam Girdwood continue; 206201d7584cSLiam Girdwood 206336fba62cSQiao Zhou /* do not free hw if this BE is used by other FE */ 206436fba62cSQiao Zhou if (be->dpcm[stream].users > 1) 206536fba62cSQiao Zhou continue; 206636fba62cSQiao Zhou 206701d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 206801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 206901d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 207008b27848SPatrick Lai (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) && 20715e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 20725e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 207301d7584cSLiam Girdwood continue; 207401d7584cSLiam Girdwood 2075103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: hw_free BE %s\n", 207694d215ccS彭东林 be->dai_link->name); 207701d7584cSLiam Girdwood 207801d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 207901d7584cSLiam Girdwood 208001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 208101d7584cSLiam Girdwood } 208201d7584cSLiam Girdwood 208301d7584cSLiam Girdwood return 0; 208401d7584cSLiam Girdwood } 208501d7584cSLiam Girdwood 208645c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream) 208701d7584cSLiam Girdwood { 208801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 208901d7584cSLiam Girdwood int err, stream = substream->stream; 209001d7584cSLiam Girdwood 209101d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2092ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 209301d7584cSLiam Girdwood 2094103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name); 209501d7584cSLiam Girdwood 209601d7584cSLiam Girdwood /* call hw_free on the frontend */ 209701d7584cSLiam Girdwood err = soc_pcm_hw_free(substream); 209801d7584cSLiam Girdwood if (err < 0) 2099103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE %s failed\n", 210001d7584cSLiam Girdwood fe->dai_link->name); 210101d7584cSLiam Girdwood 210201d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources 210301d7584cSLiam Girdwood * to this frontend DAI */ 210401d7584cSLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 210501d7584cSLiam Girdwood 210601d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE; 2107ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 210801d7584cSLiam Girdwood 210901d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 211001d7584cSLiam Girdwood return 0; 211101d7584cSLiam Girdwood } 211201d7584cSLiam Girdwood 211323607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream) 211401d7584cSLiam Girdwood { 211501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 211601d7584cSLiam Girdwood int ret; 211701d7584cSLiam Girdwood 21188d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 211901d7584cSLiam Girdwood 212001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 212101d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 212201d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 212301d7584cSLiam Girdwood 212401d7584cSLiam Girdwood /* is this op for this BE ? */ 212501d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 212601d7584cSLiam Girdwood continue; 212701d7584cSLiam Girdwood 212801d7584cSLiam Girdwood /* copy params for each dpcm */ 212901d7584cSLiam Girdwood memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params, 213001d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 213101d7584cSLiam Girdwood 213201d7584cSLiam Girdwood /* perform any hw_params fixups */ 213301d7584cSLiam Girdwood if (be->dai_link->be_hw_params_fixup) { 213401d7584cSLiam Girdwood ret = be->dai_link->be_hw_params_fixup(be, 213501d7584cSLiam Girdwood &dpcm->hw_params); 213601d7584cSLiam Girdwood if (ret < 0) { 213701d7584cSLiam Girdwood dev_err(be->dev, 2138103d84a3SLiam Girdwood "ASoC: hw_params BE fixup failed %d\n", 213901d7584cSLiam Girdwood ret); 214001d7584cSLiam Girdwood goto unwind; 214101d7584cSLiam Girdwood } 214201d7584cSLiam Girdwood } 214301d7584cSLiam Girdwood 2144ae061d2aSLibin Yang /* copy the fixed-up hw params for BE dai */ 2145ae061d2aSLibin Yang memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params, 2146ae061d2aSLibin Yang sizeof(struct snd_pcm_hw_params)); 2147ae061d2aSLibin Yang 2148b0639bd2SKuninori Morimoto /* only allow hw_params() if no connected FEs are running */ 2149b0639bd2SKuninori Morimoto if (!snd_soc_dpcm_can_be_params(fe, be, stream)) 2150b0639bd2SKuninori Morimoto continue; 2151b0639bd2SKuninori Morimoto 2152b0639bd2SKuninori Morimoto if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 2153b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 2154b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE)) 2155b0639bd2SKuninori Morimoto continue; 2156b0639bd2SKuninori Morimoto 2157b0639bd2SKuninori Morimoto dev_dbg(be->dev, "ASoC: hw_params BE %s\n", 215894d215ccS彭东林 be->dai_link->name); 2159b0639bd2SKuninori Morimoto 216001d7584cSLiam Girdwood ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params); 216101d7584cSLiam Girdwood if (ret < 0) { 216201d7584cSLiam Girdwood dev_err(dpcm->be->dev, 2163103d84a3SLiam Girdwood "ASoC: hw_params BE failed %d\n", ret); 216401d7584cSLiam Girdwood goto unwind; 216501d7584cSLiam Girdwood } 216601d7584cSLiam Girdwood 216701d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 216801d7584cSLiam Girdwood } 216901d7584cSLiam Girdwood return 0; 217001d7584cSLiam Girdwood 217101d7584cSLiam Girdwood unwind: 217201d7584cSLiam Girdwood /* disable any enabled and non active backends */ 21738d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) { 217401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 217501d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 217601d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 217701d7584cSLiam Girdwood 217801d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 217901d7584cSLiam Girdwood continue; 218001d7584cSLiam Girdwood 218101d7584cSLiam Girdwood /* only allow hw_free() if no connected FEs are running */ 218201d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 218301d7584cSLiam Girdwood continue; 218401d7584cSLiam Girdwood 218501d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) && 218601d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 218701d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) && 218801d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 218901d7584cSLiam Girdwood continue; 219001d7584cSLiam Girdwood 219101d7584cSLiam Girdwood soc_pcm_hw_free(be_substream); 219201d7584cSLiam Girdwood } 219301d7584cSLiam Girdwood 219401d7584cSLiam Girdwood return ret; 219501d7584cSLiam Girdwood } 219601d7584cSLiam Girdwood 219745c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream, 219801d7584cSLiam Girdwood struct snd_pcm_hw_params *params) 219901d7584cSLiam Girdwood { 220001d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 220101d7584cSLiam Girdwood int ret, stream = substream->stream; 220201d7584cSLiam Girdwood 220301d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2204ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 220501d7584cSLiam Girdwood 220601d7584cSLiam Girdwood memcpy(&fe->dpcm[substream->stream].hw_params, params, 220701d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params)); 220801d7584cSLiam Girdwood ret = dpcm_be_dai_hw_params(fe, substream->stream); 220901d7584cSLiam Girdwood if (ret < 0) { 2210103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret); 221101d7584cSLiam Girdwood goto out; 221201d7584cSLiam Girdwood } 221301d7584cSLiam Girdwood 2214103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n", 221501d7584cSLiam Girdwood fe->dai_link->name, params_rate(params), 221601d7584cSLiam Girdwood params_channels(params), params_format(params)); 221701d7584cSLiam Girdwood 221801d7584cSLiam Girdwood /* call hw_params on the frontend */ 221901d7584cSLiam Girdwood ret = soc_pcm_hw_params(substream, params); 222001d7584cSLiam Girdwood if (ret < 0) { 2221103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret); 222201d7584cSLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 222301d7584cSLiam Girdwood } else 222401d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS; 222501d7584cSLiam Girdwood 222601d7584cSLiam Girdwood out: 2227ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 222801d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 222901d7584cSLiam Girdwood return ret; 223001d7584cSLiam Girdwood } 223101d7584cSLiam Girdwood 223201d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm, 223301d7584cSLiam Girdwood struct snd_pcm_substream *substream, int cmd) 223401d7584cSLiam Girdwood { 223501d7584cSLiam Girdwood int ret; 223601d7584cSLiam Girdwood 2237103d84a3SLiam Girdwood dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n", 223894d215ccS彭东林 dpcm->be->dai_link->name, cmd); 223901d7584cSLiam Girdwood 224001d7584cSLiam Girdwood ret = soc_pcm_trigger(substream, cmd); 224101d7584cSLiam Girdwood if (ret < 0) 2242103d84a3SLiam Girdwood dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret); 224301d7584cSLiam Girdwood 224401d7584cSLiam Girdwood return ret; 224501d7584cSLiam Girdwood } 224601d7584cSLiam Girdwood 224723607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, 224845c0a188SMark Brown int cmd) 224901d7584cSLiam Girdwood { 225001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 225101d7584cSLiam Girdwood int ret = 0; 225201d7584cSLiam Girdwood 22538d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 225401d7584cSLiam Girdwood 225501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 225601d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 225701d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 225801d7584cSLiam Girdwood 225901d7584cSLiam Girdwood /* is this op for this BE ? */ 226001d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 226101d7584cSLiam Girdwood continue; 226201d7584cSLiam Girdwood 226301d7584cSLiam Girdwood switch (cmd) { 226401d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 226501d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) && 226601d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)) 226701d7584cSLiam Girdwood continue; 226801d7584cSLiam Girdwood 226901d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 227001d7584cSLiam Girdwood if (ret) 227101d7584cSLiam Girdwood return ret; 227201d7584cSLiam Girdwood 227301d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 227401d7584cSLiam Girdwood break; 227501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 227601d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND)) 227701d7584cSLiam Girdwood continue; 227801d7584cSLiam Girdwood 227901d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 228001d7584cSLiam Girdwood if (ret) 228101d7584cSLiam Girdwood return ret; 228201d7584cSLiam Girdwood 228301d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 228401d7584cSLiam Girdwood break; 228501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 228601d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 228701d7584cSLiam Girdwood continue; 228801d7584cSLiam Girdwood 228901d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 229001d7584cSLiam Girdwood if (ret) 229101d7584cSLiam Girdwood return ret; 229201d7584cSLiam Girdwood 229301d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 229401d7584cSLiam Girdwood break; 229501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 229601d7584cSLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 229701d7584cSLiam Girdwood continue; 229801d7584cSLiam Girdwood 229901d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream)) 230001d7584cSLiam Girdwood continue; 230101d7584cSLiam Girdwood 230201d7584cSLiam Girdwood ret = dpcm_do_trigger(dpcm, be_substream, cmd); 230301d7584cSLiam Girdwood if (ret) 230401d7584cSLiam Girdwood return ret; 230501d7584cSLiam Girdwood 230601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 230701d7584cSLiam Girdwood break; 230801d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 2309868a6ca8SNicolin Chen if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 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_SUSPEND; 232001d7584cSLiam Girdwood break; 232101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 232201d7584cSLiam Girdwood 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_PAUSED; 233301d7584cSLiam Girdwood break; 233401d7584cSLiam Girdwood } 233501d7584cSLiam Girdwood } 233601d7584cSLiam Girdwood 233701d7584cSLiam Girdwood return ret; 233801d7584cSLiam Girdwood } 233901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger); 234001d7584cSLiam Girdwood 2341acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream, 2342acbf2774SRanjani Sridharan int cmd, bool fe_first) 2343acbf2774SRanjani Sridharan { 2344acbf2774SRanjani Sridharan struct snd_soc_pcm_runtime *fe = substream->private_data; 2345acbf2774SRanjani Sridharan int ret; 2346acbf2774SRanjani Sridharan 2347acbf2774SRanjani Sridharan /* call trigger on the frontend before the backend. */ 2348acbf2774SRanjani Sridharan if (fe_first) { 2349acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n", 2350acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2351acbf2774SRanjani Sridharan 2352acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2353acbf2774SRanjani Sridharan if (ret < 0) 2354acbf2774SRanjani Sridharan return ret; 2355acbf2774SRanjani Sridharan 2356acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2357acbf2774SRanjani Sridharan return ret; 2358acbf2774SRanjani Sridharan } 2359acbf2774SRanjani Sridharan 2360acbf2774SRanjani Sridharan /* call trigger on the frontend after the backend. */ 2361acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd); 2362acbf2774SRanjani Sridharan if (ret < 0) 2363acbf2774SRanjani Sridharan return ret; 2364acbf2774SRanjani Sridharan 2365acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n", 2366acbf2774SRanjani Sridharan fe->dai_link->name, cmd); 2367acbf2774SRanjani Sridharan 2368acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd); 2369acbf2774SRanjani Sridharan 2370acbf2774SRanjani Sridharan return ret; 2371acbf2774SRanjani Sridharan } 2372acbf2774SRanjani Sridharan 2373ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd) 237401d7584cSLiam Girdwood { 237501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 2376acbf2774SRanjani Sridharan int stream = substream->stream; 2377acbf2774SRanjani Sridharan int ret = 0; 237801d7584cSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 237901d7584cSLiam Girdwood 238001d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 238101d7584cSLiam Girdwood 238201d7584cSLiam Girdwood switch (trigger) { 238301d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_PRE: 2384acbf2774SRanjani Sridharan switch (cmd) { 2385acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2386acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2387acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2388acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2389acbf2774SRanjani Sridharan break; 2390acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2391acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2392acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2393acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2394acbf2774SRanjani Sridharan break; 2395acbf2774SRanjani Sridharan default: 2396acbf2774SRanjani Sridharan ret = -EINVAL; 2397acbf2774SRanjani Sridharan break; 239801d7584cSLiam Girdwood } 239901d7584cSLiam Girdwood break; 240001d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_POST: 2401acbf2774SRanjani Sridharan switch (cmd) { 2402acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START: 2403acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME: 2404acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2405acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false); 2406acbf2774SRanjani Sridharan break; 2407acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP: 2408acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND: 2409acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2410acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true); 2411acbf2774SRanjani Sridharan break; 2412acbf2774SRanjani Sridharan default: 2413acbf2774SRanjani Sridharan ret = -EINVAL; 2414acbf2774SRanjani Sridharan break; 241501d7584cSLiam Girdwood } 241601d7584cSLiam Girdwood break; 241707bf84aaSLiam Girdwood case SND_SOC_DPCM_TRIGGER_BESPOKE: 241807bf84aaSLiam Girdwood /* bespoke trigger() - handles both FE and BEs */ 241907bf84aaSLiam Girdwood 2420103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n", 242107bf84aaSLiam Girdwood fe->dai_link->name, cmd); 242207bf84aaSLiam Girdwood 242307bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, cmd); 242407bf84aaSLiam Girdwood break; 242501d7584cSLiam Girdwood default: 2426103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd, 242701d7584cSLiam Girdwood fe->dai_link->name); 242801d7584cSLiam Girdwood ret = -EINVAL; 242901d7584cSLiam Girdwood goto out; 243001d7584cSLiam Girdwood } 243101d7584cSLiam Girdwood 2432acbf2774SRanjani Sridharan if (ret < 0) { 2433acbf2774SRanjani Sridharan dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n", 2434acbf2774SRanjani Sridharan cmd, ret); 2435acbf2774SRanjani Sridharan goto out; 2436acbf2774SRanjani Sridharan } 2437acbf2774SRanjani Sridharan 243801d7584cSLiam Girdwood switch (cmd) { 243901d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 244001d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 244101d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 244201d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 244301d7584cSLiam Girdwood break; 244401d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 244501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 244601d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 244701d7584cSLiam Girdwood break; 24489f169b9fSPatrick Lai case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 24499f169b9fSPatrick Lai fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 24509f169b9fSPatrick Lai break; 245101d7584cSLiam Girdwood } 245201d7584cSLiam Girdwood 245301d7584cSLiam Girdwood out: 245401d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 245501d7584cSLiam Girdwood return ret; 245601d7584cSLiam Girdwood } 245701d7584cSLiam Girdwood 2458ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd) 2459ea9d0d77STakashi Iwai { 2460ea9d0d77STakashi Iwai struct snd_soc_pcm_runtime *fe = substream->private_data; 2461ea9d0d77STakashi Iwai int stream = substream->stream; 2462ea9d0d77STakashi Iwai 2463ea9d0d77STakashi Iwai /* if FE's runtime_update is already set, we're in race; 2464ea9d0d77STakashi Iwai * process this trigger later at exit 2465ea9d0d77STakashi Iwai */ 2466ea9d0d77STakashi Iwai if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) { 2467ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = cmd + 1; 2468ea9d0d77STakashi Iwai return 0; /* delayed, assuming it's successful */ 2469ea9d0d77STakashi Iwai } 2470ea9d0d77STakashi Iwai 2471ea9d0d77STakashi Iwai /* we're alone, let's trigger */ 2472ea9d0d77STakashi Iwai return dpcm_fe_dai_do_trigger(substream, cmd); 2473ea9d0d77STakashi Iwai } 2474ea9d0d77STakashi Iwai 247523607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream) 247601d7584cSLiam Girdwood { 247701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 247801d7584cSLiam Girdwood int ret = 0; 247901d7584cSLiam Girdwood 24808d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 248101d7584cSLiam Girdwood 248201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 248301d7584cSLiam Girdwood struct snd_pcm_substream *be_substream = 248401d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream); 248501d7584cSLiam Girdwood 248601d7584cSLiam Girdwood /* is this op for this BE ? */ 248701d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream)) 248801d7584cSLiam Girdwood continue; 248901d7584cSLiam Girdwood 249001d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) && 249195f444dcSKoro Chen (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) && 24925087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) && 24935087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) 249401d7584cSLiam Girdwood continue; 249501d7584cSLiam Girdwood 2496103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: prepare BE %s\n", 249794d215ccS彭东林 be->dai_link->name); 249801d7584cSLiam Girdwood 249901d7584cSLiam Girdwood ret = soc_pcm_prepare(be_substream); 250001d7584cSLiam Girdwood if (ret < 0) { 2501103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: backend prepare failed %d\n", 250201d7584cSLiam Girdwood ret); 250301d7584cSLiam Girdwood break; 250401d7584cSLiam Girdwood } 250501d7584cSLiam Girdwood 250601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 250701d7584cSLiam Girdwood } 250801d7584cSLiam Girdwood return ret; 250901d7584cSLiam Girdwood } 251001d7584cSLiam Girdwood 251145c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream) 251201d7584cSLiam Girdwood { 251301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = substream->private_data; 251401d7584cSLiam Girdwood int stream = substream->stream, ret = 0; 251501d7584cSLiam Girdwood 251601d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 251701d7584cSLiam Girdwood 2518103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name); 251901d7584cSLiam Girdwood 2520ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE); 252101d7584cSLiam Girdwood 252201d7584cSLiam Girdwood /* there is no point preparing this FE if there are no BEs */ 252301d7584cSLiam Girdwood if (list_empty(&fe->dpcm[stream].be_clients)) { 2524103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n", 252501d7584cSLiam Girdwood fe->dai_link->name); 252601d7584cSLiam Girdwood ret = -EINVAL; 252701d7584cSLiam Girdwood goto out; 252801d7584cSLiam Girdwood } 252901d7584cSLiam Girdwood 253001d7584cSLiam Girdwood ret = dpcm_be_dai_prepare(fe, substream->stream); 253101d7584cSLiam Girdwood if (ret < 0) 253201d7584cSLiam Girdwood goto out; 253301d7584cSLiam Girdwood 253401d7584cSLiam Girdwood /* call prepare on the frontend */ 253501d7584cSLiam Girdwood ret = soc_pcm_prepare(substream); 253601d7584cSLiam Girdwood if (ret < 0) { 2537103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: prepare FE %s failed\n", 253801d7584cSLiam Girdwood fe->dai_link->name); 253901d7584cSLiam Girdwood goto out; 254001d7584cSLiam Girdwood } 254101d7584cSLiam Girdwood 254201d7584cSLiam Girdwood /* run the stream event for each BE */ 254301d7584cSLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 254401d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 254501d7584cSLiam Girdwood 254601d7584cSLiam Girdwood out: 2547ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 254801d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 254901d7584cSLiam Girdwood 255001d7584cSLiam Girdwood return ret; 255101d7584cSLiam Girdwood } 255201d7584cSLiam Girdwood 2553618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream) 2554618dae11SLiam Girdwood { 255507bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 255607bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 255707bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2558618dae11SLiam Girdwood int err; 255901d7584cSLiam Girdwood 2560103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n", 2561618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2562618dae11SLiam Girdwood 256307bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 256407bf84aaSLiam Girdwood /* call bespoke trigger - FE takes care of all BE triggers */ 2565103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n", 256607bf84aaSLiam Girdwood fe->dai_link->name); 256707bf84aaSLiam Girdwood 256807bf84aaSLiam Girdwood err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP); 256907bf84aaSLiam Girdwood if (err < 0) 2570103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 257107bf84aaSLiam Girdwood } else { 2572103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n", 257307bf84aaSLiam Girdwood fe->dai_link->name); 257407bf84aaSLiam Girdwood 2575618dae11SLiam Girdwood err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP); 2576618dae11SLiam Girdwood if (err < 0) 2577103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err); 257807bf84aaSLiam Girdwood } 2579618dae11SLiam Girdwood 2580618dae11SLiam Girdwood err = dpcm_be_dai_hw_free(fe, stream); 2581618dae11SLiam Girdwood if (err < 0) 2582103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err); 2583618dae11SLiam Girdwood 2584618dae11SLiam Girdwood err = dpcm_be_dai_shutdown(fe, stream); 2585618dae11SLiam Girdwood if (err < 0) 2586103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err); 2587618dae11SLiam Girdwood 2588618dae11SLiam Girdwood /* run the stream event for each BE */ 2589618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2590618dae11SLiam Girdwood 2591618dae11SLiam Girdwood return 0; 2592618dae11SLiam Girdwood } 2593618dae11SLiam Girdwood 2594618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream) 2595618dae11SLiam Girdwood { 259607bf84aaSLiam Girdwood struct snd_pcm_substream *substream = 259707bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream); 2598618dae11SLiam Girdwood struct snd_soc_dpcm *dpcm; 259907bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream]; 2600618dae11SLiam Girdwood int ret; 2601a9764869SKaiChieh Chuang unsigned long flags; 2602618dae11SLiam Girdwood 2603103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n", 2604618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name); 2605618dae11SLiam Girdwood 2606618dae11SLiam Girdwood /* Only start the BE if the FE is ready */ 2607618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE || 2608618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) 2609618dae11SLiam Girdwood return -EINVAL; 2610618dae11SLiam Girdwood 2611618dae11SLiam Girdwood /* startup must always be called for new BEs */ 2612618dae11SLiam Girdwood ret = dpcm_be_dai_startup(fe, stream); 2613fffc0ca2SDan Carpenter if (ret < 0) 2614618dae11SLiam Girdwood goto disconnect; 2615618dae11SLiam Girdwood 2616618dae11SLiam Girdwood /* keep going if FE state is > open */ 2617618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN) 2618618dae11SLiam Girdwood return 0; 2619618dae11SLiam Girdwood 2620618dae11SLiam Girdwood ret = dpcm_be_dai_hw_params(fe, stream); 2621fffc0ca2SDan Carpenter if (ret < 0) 2622618dae11SLiam Girdwood goto close; 2623618dae11SLiam Girdwood 2624618dae11SLiam Girdwood /* keep going if FE state is > hw_params */ 2625618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS) 2626618dae11SLiam Girdwood return 0; 2627618dae11SLiam Girdwood 2628618dae11SLiam Girdwood 2629618dae11SLiam Girdwood ret = dpcm_be_dai_prepare(fe, stream); 2630fffc0ca2SDan Carpenter if (ret < 0) 2631618dae11SLiam Girdwood goto hw_free; 2632618dae11SLiam Girdwood 2633618dae11SLiam Girdwood /* run the stream event for each BE */ 2634618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP); 2635618dae11SLiam Girdwood 2636618dae11SLiam Girdwood /* keep going if FE state is > prepare */ 2637618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE || 2638618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP) 2639618dae11SLiam Girdwood return 0; 2640618dae11SLiam Girdwood 264107bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) { 264207bf84aaSLiam Girdwood /* call trigger on the frontend - FE takes care of all BE triggers */ 2643103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n", 264407bf84aaSLiam Girdwood fe->dai_link->name); 264507bf84aaSLiam Girdwood 264607bf84aaSLiam Girdwood ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START); 264707bf84aaSLiam Girdwood if (ret < 0) { 2648103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret); 264907bf84aaSLiam Girdwood goto hw_free; 265007bf84aaSLiam Girdwood } 265107bf84aaSLiam Girdwood } else { 2652103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n", 2653618dae11SLiam Girdwood fe->dai_link->name); 2654618dae11SLiam Girdwood 2655618dae11SLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream, 2656618dae11SLiam Girdwood SNDRV_PCM_TRIGGER_START); 2657618dae11SLiam Girdwood if (ret < 0) { 2658103d84a3SLiam Girdwood dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret); 2659618dae11SLiam Girdwood goto hw_free; 2660618dae11SLiam Girdwood } 266107bf84aaSLiam Girdwood } 2662618dae11SLiam Girdwood 2663618dae11SLiam Girdwood return 0; 2664618dae11SLiam Girdwood 2665618dae11SLiam Girdwood hw_free: 2666618dae11SLiam Girdwood dpcm_be_dai_hw_free(fe, stream); 2667618dae11SLiam Girdwood close: 2668618dae11SLiam Girdwood dpcm_be_dai_shutdown(fe, stream); 2669618dae11SLiam Girdwood disconnect: 2670618dae11SLiam Girdwood /* disconnect any non started BEs */ 2671a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 26728d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) { 2673618dae11SLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be; 2674618dae11SLiam Girdwood if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) 2675618dae11SLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 2676618dae11SLiam Girdwood } 2677a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 2678618dae11SLiam Girdwood 2679618dae11SLiam Girdwood return ret; 2680618dae11SLiam Girdwood } 2681618dae11SLiam Girdwood 2682de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new) 2683618dae11SLiam Girdwood { 2684618dae11SLiam Girdwood struct snd_soc_dapm_widget_list *list; 26857083f877SKuninori Morimoto int stream; 2686de15d7ffSJerome Brunet int count, paths; 2687580dff36SKuninori Morimoto int ret; 2688618dae11SLiam Girdwood 2689618dae11SLiam Girdwood if (!fe->dai_link->dynamic) 2690de15d7ffSJerome Brunet return 0; 2691618dae11SLiam Girdwood 2692618dae11SLiam Girdwood /* only check active links */ 2693618dae11SLiam Girdwood if (!fe->cpu_dai->active) 2694de15d7ffSJerome Brunet return 0; 2695618dae11SLiam Girdwood 2696618dae11SLiam Girdwood /* DAPM sync will call this to update DSP paths */ 2697de15d7ffSJerome Brunet dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n", 2698de15d7ffSJerome Brunet new ? "new" : "old", fe->dai_link->name); 2699618dae11SLiam Girdwood 27007083f877SKuninori Morimoto for_each_pcm_streams(stream) { 2701075207d2SQiao Zhou 27027083f877SKuninori Morimoto /* skip if FE doesn't have playback/capture capability */ 27037083f877SKuninori Morimoto if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) || 27047083f877SKuninori Morimoto !snd_soc_dai_stream_valid(fe->codec_dai, stream)) 27057083f877SKuninori Morimoto continue; 2706618dae11SLiam Girdwood 27077083f877SKuninori Morimoto /* skip if FE isn't currently playing/capturing */ 27087083f877SKuninori Morimoto if (!fe->cpu_dai->stream_active[stream] || 27097083f877SKuninori Morimoto !fe->codec_dai->stream_active[stream]) 27107083f877SKuninori Morimoto continue; 27117083f877SKuninori Morimoto 27127083f877SKuninori Morimoto paths = dpcm_path_get(fe, stream, &list); 2713618dae11SLiam Girdwood if (paths < 0) { 2714103d84a3SLiam Girdwood dev_warn(fe->dev, "ASoC: %s no valid %s path\n", 27157083f877SKuninori Morimoto fe->dai_link->name, 27167083f877SKuninori Morimoto stream == SNDRV_PCM_STREAM_PLAYBACK ? 27177083f877SKuninori Morimoto "playback" : "capture"); 2718618dae11SLiam Girdwood return paths; 2719618dae11SLiam Girdwood } 2720618dae11SLiam Girdwood 27217083f877SKuninori Morimoto /* update any playback/capture paths */ 27227083f877SKuninori Morimoto count = dpcm_process_paths(fe, stream, &list, new); 2723de15d7ffSJerome Brunet if (count) { 2724580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE); 2725de15d7ffSJerome Brunet if (new) 2726580dff36SKuninori Morimoto ret = dpcm_run_update_startup(fe, stream); 2727de15d7ffSJerome Brunet else 2728580dff36SKuninori Morimoto ret = dpcm_run_update_shutdown(fe, stream); 2729580dff36SKuninori Morimoto if (ret < 0) 2730580dff36SKuninori Morimoto dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n"); 2731580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO); 2732de15d7ffSJerome Brunet 27337083f877SKuninori Morimoto dpcm_clear_pending_state(fe, stream); 27347083f877SKuninori Morimoto dpcm_be_disconnect(fe, stream); 2735618dae11SLiam Girdwood } 2736618dae11SLiam Girdwood 27377ed9de76SQiao Zhou dpcm_path_put(&list); 2738618dae11SLiam Girdwood } 2739618dae11SLiam Girdwood 2740de15d7ffSJerome Brunet return 0; 2741618dae11SLiam Girdwood } 2742618dae11SLiam Girdwood 2743de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and 2744de15d7ffSJerome Brunet * any DAI links. 2745de15d7ffSJerome Brunet */ 2746de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card) 2747de15d7ffSJerome Brunet { 2748de15d7ffSJerome Brunet struct snd_soc_pcm_runtime *fe; 2749de15d7ffSJerome Brunet int ret = 0; 2750de15d7ffSJerome Brunet 2751de15d7ffSJerome Brunet mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2752de15d7ffSJerome Brunet /* shutdown all old paths first */ 2753bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2754de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 0); 2755de15d7ffSJerome Brunet if (ret) 2756de15d7ffSJerome Brunet goto out; 2757de15d7ffSJerome Brunet } 2758de15d7ffSJerome Brunet 2759de15d7ffSJerome Brunet /* bring new paths up */ 2760bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) { 2761de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 1); 2762de15d7ffSJerome Brunet if (ret) 2763de15d7ffSJerome Brunet goto out; 2764de15d7ffSJerome Brunet } 2765de15d7ffSJerome Brunet 2766de15d7ffSJerome Brunet out: 2767618dae11SLiam Girdwood mutex_unlock(&card->mutex); 2768de15d7ffSJerome Brunet return ret; 2769618dae11SLiam Girdwood } 277001d7584cSLiam Girdwood 277145c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream) 277201d7584cSLiam Girdwood { 277301d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 277401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 277501d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list; 277601d7584cSLiam Girdwood int ret; 277701d7584cSLiam Girdwood int stream = fe_substream->stream; 277801d7584cSLiam Girdwood 277901d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 278001d7584cSLiam Girdwood fe->dpcm[stream].runtime = fe_substream->runtime; 278101d7584cSLiam Girdwood 27828f70e515SQiao Zhou ret = dpcm_path_get(fe, stream, &list); 27838f70e515SQiao Zhou if (ret < 0) { 2784cae06eb9SKuninori Morimoto goto open_end; 27858f70e515SQiao Zhou } else if (ret == 0) { 2786103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", 278701d7584cSLiam Girdwood fe->dai_link->name, stream ? "capture" : "playback"); 278801d7584cSLiam Girdwood } 278901d7584cSLiam Girdwood 279001d7584cSLiam Girdwood /* calculate valid and active FE <-> BE dpcms */ 279101d7584cSLiam Girdwood dpcm_process_paths(fe, stream, &list, 1); 279201d7584cSLiam Girdwood 279301d7584cSLiam Girdwood ret = dpcm_fe_dai_startup(fe_substream); 279401d7584cSLiam Girdwood if (ret < 0) { 279501d7584cSLiam Girdwood /* clean up all links */ 27968d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 279701d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 279801d7584cSLiam Girdwood 279901d7584cSLiam Girdwood dpcm_be_disconnect(fe, stream); 280001d7584cSLiam Girdwood fe->dpcm[stream].runtime = NULL; 280101d7584cSLiam Girdwood } 280201d7584cSLiam Girdwood 280301d7584cSLiam Girdwood dpcm_clear_pending_state(fe, stream); 280401d7584cSLiam Girdwood dpcm_path_put(&list); 2805cae06eb9SKuninori Morimoto open_end: 280601d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 280701d7584cSLiam Girdwood return ret; 280801d7584cSLiam Girdwood } 280901d7584cSLiam Girdwood 281045c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream) 281101d7584cSLiam Girdwood { 281201d7584cSLiam Girdwood struct snd_soc_pcm_runtime *fe = fe_substream->private_data; 281301d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 281401d7584cSLiam Girdwood int stream = fe_substream->stream, ret; 281501d7584cSLiam Girdwood 281601d7584cSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 281701d7584cSLiam Girdwood ret = dpcm_fe_dai_shutdown(fe_substream); 281801d7584cSLiam Girdwood 281901d7584cSLiam Girdwood /* mark FE's links ready to prune */ 28208d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 282101d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 282201d7584cSLiam Girdwood 282301d7584cSLiam Girdwood dpcm_be_disconnect(fe, stream); 282401d7584cSLiam Girdwood 282501d7584cSLiam Girdwood fe->dpcm[stream].runtime = NULL; 282601d7584cSLiam Girdwood mutex_unlock(&fe->card->mutex); 282701d7584cSLiam Girdwood return ret; 282801d7584cSLiam Girdwood } 282901d7584cSLiam Girdwood 2830ddee627cSLiam Girdwood /* create a new pcm */ 2831ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num) 2832ddee627cSLiam Girdwood { 28332e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai; 2834ddee627cSLiam Girdwood struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 28352b544dd7SKuninori Morimoto struct snd_soc_component *component; 2836ddee627cSLiam Girdwood struct snd_pcm *pcm; 2837ddee627cSLiam Girdwood char new_name[64]; 2838ddee627cSLiam Girdwood int ret = 0, playback = 0, capture = 0; 28392e5894d7SBenoit Cousson int i; 2840ddee627cSLiam Girdwood 284101d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) { 28421e9de42fSLiam Girdwood playback = rtd->dai_link->dpcm_playback; 28431e9de42fSLiam Girdwood capture = rtd->dai_link->dpcm_capture; 284401d7584cSLiam Girdwood } else { 2845a342031cSJerome Brunet /* Adapt stream for codec2codec links */ 2846a4877a6fSStephan Gerhold int cpu_capture = rtd->dai_link->params ? 2847a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 2848a4877a6fSStephan Gerhold int cpu_playback = rtd->dai_link->params ? 2849a4877a6fSStephan Gerhold SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; 2850a342031cSJerome Brunet 28510b7990e3SKuninori Morimoto for_each_rtd_codec_dai(rtd, i, codec_dai) { 2852467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 2853a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_playback)) 2854ddee627cSLiam Girdwood playback = 1; 2855467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 2856a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_capture)) 2857ddee627cSLiam Girdwood capture = 1; 285801d7584cSLiam Girdwood } 28592e5894d7SBenoit Cousson } 28602e5894d7SBenoit Cousson 2861d6bead02SFabio Estevam if (rtd->dai_link->playback_only) { 2862d6bead02SFabio Estevam playback = 1; 2863d6bead02SFabio Estevam capture = 0; 2864d6bead02SFabio Estevam } 2865d6bead02SFabio Estevam 2866d6bead02SFabio Estevam if (rtd->dai_link->capture_only) { 2867d6bead02SFabio Estevam playback = 0; 2868d6bead02SFabio Estevam capture = 1; 2869d6bead02SFabio Estevam } 2870d6bead02SFabio Estevam 287101d7584cSLiam Girdwood /* create the PCM */ 2872a342031cSJerome Brunet if (rtd->dai_link->params) { 2873a342031cSJerome Brunet snprintf(new_name, sizeof(new_name), "codec2codec(%s)", 2874a342031cSJerome Brunet rtd->dai_link->stream_name); 2875a342031cSJerome Brunet 2876a342031cSJerome Brunet ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 2877a342031cSJerome Brunet playback, capture, &pcm); 2878a342031cSJerome Brunet } else if (rtd->dai_link->no_pcm) { 287901d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)", 288001d7584cSLiam Girdwood rtd->dai_link->stream_name); 288101d7584cSLiam Girdwood 288201d7584cSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 288301d7584cSLiam Girdwood playback, capture, &pcm); 288401d7584cSLiam Girdwood } else { 288501d7584cSLiam Girdwood if (rtd->dai_link->dynamic) 288601d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s (*)", 288701d7584cSLiam Girdwood rtd->dai_link->stream_name); 288801d7584cSLiam Girdwood else 288901d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s %s-%d", 28902e5894d7SBenoit Cousson rtd->dai_link->stream_name, 28912e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? 28922e5894d7SBenoit Cousson "multicodec" : rtd->codec_dai->name, num); 289301d7584cSLiam Girdwood 289401d7584cSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback, 289501d7584cSLiam Girdwood capture, &pcm); 289601d7584cSLiam Girdwood } 2897ddee627cSLiam Girdwood if (ret < 0) { 2898103d84a3SLiam Girdwood dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n", 28995cb9b748SLiam Girdwood rtd->dai_link->name); 2900ddee627cSLiam Girdwood return ret; 2901ddee627cSLiam Girdwood } 2902103d84a3SLiam Girdwood dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name); 2903ddee627cSLiam Girdwood 2904ddee627cSLiam Girdwood /* DAPM dai link stream work */ 2905a342031cSJerome Brunet if (rtd->dai_link->params) 29064bf2e385SCurtis Malainey rtd->close_delayed_work_func = codec2codec_close_delayed_work; 2907a342031cSJerome Brunet else 290883f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work; 2909ddee627cSLiam Girdwood 291048c7699fSVinod Koul pcm->nonatomic = rtd->dai_link->nonatomic; 2911ddee627cSLiam Girdwood rtd->pcm = pcm; 2912ddee627cSLiam Girdwood pcm->private_data = rtd; 291301d7584cSLiam Girdwood 2914a342031cSJerome Brunet if (rtd->dai_link->no_pcm || rtd->dai_link->params) { 291501d7584cSLiam Girdwood if (playback) 291601d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 291701d7584cSLiam Girdwood if (capture) 291801d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 291901d7584cSLiam Girdwood goto out; 292001d7584cSLiam Girdwood } 292101d7584cSLiam Girdwood 292201d7584cSLiam Girdwood /* ASoC PCM operations */ 292301d7584cSLiam Girdwood if (rtd->dai_link->dynamic) { 292401d7584cSLiam Girdwood rtd->ops.open = dpcm_fe_dai_open; 292501d7584cSLiam Girdwood rtd->ops.hw_params = dpcm_fe_dai_hw_params; 292601d7584cSLiam Girdwood rtd->ops.prepare = dpcm_fe_dai_prepare; 292701d7584cSLiam Girdwood rtd->ops.trigger = dpcm_fe_dai_trigger; 292801d7584cSLiam Girdwood rtd->ops.hw_free = dpcm_fe_dai_hw_free; 292901d7584cSLiam Girdwood rtd->ops.close = dpcm_fe_dai_close; 293001d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 293101d7584cSLiam Girdwood } else { 293201d7584cSLiam Girdwood rtd->ops.open = soc_pcm_open; 293301d7584cSLiam Girdwood rtd->ops.hw_params = soc_pcm_hw_params; 293401d7584cSLiam Girdwood rtd->ops.prepare = soc_pcm_prepare; 293501d7584cSLiam Girdwood rtd->ops.trigger = soc_pcm_trigger; 293601d7584cSLiam Girdwood rtd->ops.hw_free = soc_pcm_hw_free; 293701d7584cSLiam Girdwood rtd->ops.close = soc_pcm_close; 293801d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer; 293901d7584cSLiam Girdwood } 294001d7584cSLiam Girdwood 2941613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 29422b544dd7SKuninori Morimoto const struct snd_soc_component_driver *drv = component->driver; 2943b8135864SKuninori Morimoto 29443b1c952cSTakashi Iwai if (drv->ioctl) 29453b1c952cSTakashi Iwai rtd->ops.ioctl = snd_soc_pcm_component_ioctl; 29461e5ddb6bSTakashi Iwai if (drv->sync_stop) 29471e5ddb6bSTakashi Iwai rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop; 2948e9067bb5SKuninori Morimoto if (drv->copy_user) 294982d81f5cSKuninori Morimoto rtd->ops.copy_user = snd_soc_pcm_component_copy_user; 2950e9067bb5SKuninori Morimoto if (drv->page) 29519c712e4fSKuninori Morimoto rtd->ops.page = snd_soc_pcm_component_page; 2952e9067bb5SKuninori Morimoto if (drv->mmap) 2953205875e1SKuninori Morimoto rtd->ops.mmap = snd_soc_pcm_component_mmap; 2954b8135864SKuninori Morimoto } 2955b8135864SKuninori Morimoto 2956ddee627cSLiam Girdwood if (playback) 295701d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops); 2958ddee627cSLiam Girdwood 2959ddee627cSLiam Girdwood if (capture) 296001d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops); 2961ddee627cSLiam Girdwood 2962b2b2afbbSKuninori Morimoto ret = snd_soc_pcm_component_new(rtd); 2963ddee627cSLiam Girdwood if (ret < 0) { 29647484291eSKuninori Morimoto dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret); 2965ddee627cSLiam Girdwood return ret; 2966ddee627cSLiam Girdwood } 2967c641e5b2SJohan Hovold 29683d21ef0bSTakashi Iwai pcm->no_device_suspend = true; 296901d7584cSLiam Girdwood out: 29702e5894d7SBenoit Cousson dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", 29712e5894d7SBenoit Cousson (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name, 2972ddee627cSLiam Girdwood cpu_dai->name); 2973ddee627cSLiam Girdwood return ret; 2974ddee627cSLiam Girdwood } 297501d7584cSLiam Girdwood 297601d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */ 297701d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream) 297801d7584cSLiam Girdwood { 297901d7584cSLiam Girdwood if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) 298001d7584cSLiam Girdwood return 1; 298101d7584cSLiam Girdwood return 0; 298201d7584cSLiam Girdwood } 298301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update); 298401d7584cSLiam Girdwood 298501d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */ 298601d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe, 298701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 298801d7584cSLiam Girdwood { 298901d7584cSLiam Girdwood if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) || 299001d7584cSLiam Girdwood ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) && 299101d7584cSLiam Girdwood be->dpcm[stream].runtime_update)) 299201d7584cSLiam Girdwood return 1; 299301d7584cSLiam Girdwood return 0; 299401d7584cSLiam Girdwood } 299501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update); 299601d7584cSLiam Girdwood 299701d7584cSLiam Girdwood /* get the substream for this BE */ 299801d7584cSLiam Girdwood struct snd_pcm_substream * 299901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream) 300001d7584cSLiam Girdwood { 300101d7584cSLiam Girdwood return be->pcm->streams[stream].substream; 300201d7584cSLiam Girdwood } 300301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream); 300401d7584cSLiam Girdwood 3005085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe, 3006085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, 3007085d22beSKuninori Morimoto int stream, 3008085d22beSKuninori Morimoto const enum snd_soc_dpcm_state *states, 3009085d22beSKuninori Morimoto int num_states) 301001d7584cSLiam Girdwood { 301101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm; 301201d7584cSLiam Girdwood int state; 3013a9764869SKaiChieh Chuang int ret = 1; 3014a9764869SKaiChieh Chuang unsigned long flags; 3015085d22beSKuninori Morimoto int i; 301601d7584cSLiam Girdwood 3017a9764869SKaiChieh Chuang spin_lock_irqsave(&fe->card->dpcm_lock, flags); 3018d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) { 301901d7584cSLiam Girdwood 302001d7584cSLiam Girdwood if (dpcm->fe == fe) 302101d7584cSLiam Girdwood continue; 302201d7584cSLiam Girdwood 302301d7584cSLiam Girdwood state = dpcm->fe->dpcm[stream].state; 3024085d22beSKuninori Morimoto for (i = 0; i < num_states; i++) { 3025085d22beSKuninori Morimoto if (state == states[i]) { 3026a9764869SKaiChieh Chuang ret = 0; 3027a9764869SKaiChieh Chuang break; 302801d7584cSLiam Girdwood } 3029a9764869SKaiChieh Chuang } 3030085d22beSKuninori Morimoto } 3031a9764869SKaiChieh Chuang spin_unlock_irqrestore(&fe->card->dpcm_lock, flags); 303201d7584cSLiam Girdwood 3033085d22beSKuninori Morimoto /* it's safe to do this BE DAI */ 3034a9764869SKaiChieh Chuang return ret; 303501d7584cSLiam Girdwood } 3036085d22beSKuninori Morimoto 3037085d22beSKuninori Morimoto /* 3038085d22beSKuninori Morimoto * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE 3039085d22beSKuninori Morimoto * are not running, paused or suspended for the specified stream direction. 3040085d22beSKuninori Morimoto */ 3041085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe, 3042085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, int stream) 3043085d22beSKuninori Morimoto { 3044085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3045085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3046085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3047085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3048085d22beSKuninori Morimoto }; 3049085d22beSKuninori Morimoto 3050085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 3051085d22beSKuninori Morimoto } 305201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop); 305301d7584cSLiam Girdwood 305401d7584cSLiam Girdwood /* 305501d7584cSLiam Girdwood * We can only change hw params a BE DAI if any of it's FE are not prepared, 305601d7584cSLiam Girdwood * running, paused or suspended for the specified stream direction. 305701d7584cSLiam Girdwood */ 305801d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe, 305901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream) 306001d7584cSLiam Girdwood { 3061085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = { 3062085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START, 3063085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED, 3064085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND, 3065085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PREPARE, 3066085d22beSKuninori Morimoto }; 306701d7584cSLiam Girdwood 3068085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state)); 306901d7584cSLiam Girdwood } 307001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params); 3071