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>
17ddee627cSLiam Girdwood #include <linux/slab.h>
18ddee627cSLiam Girdwood #include <linux/workqueue.h>
1901d7584cSLiam Girdwood #include <linux/export.h>
20f86dcef8SLiam Girdwood #include <linux/debugfs.h>
21ddee627cSLiam Girdwood #include <sound/core.h>
22ddee627cSLiam Girdwood #include <sound/pcm.h>
23ddee627cSLiam Girdwood #include <sound/pcm_params.h>
24ddee627cSLiam Girdwood #include <sound/soc.h>
2501d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
26a5e6c109SKuninori Morimoto #include <sound/soc-link.h>
27ddee627cSLiam Girdwood #include <sound/initval.h>
28ddee627cSLiam Girdwood
2904110728SKuninori Morimoto #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
_soc_pcm_ret(struct snd_soc_pcm_runtime * rtd,const char * func,int ret)3004110728SKuninori Morimoto static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
3104110728SKuninori Morimoto const char *func, int ret)
3204110728SKuninori Morimoto {
3304110728SKuninori Morimoto /* Positive, Zero values are not errors */
3404110728SKuninori Morimoto if (ret >= 0)
3504110728SKuninori Morimoto return ret;
3604110728SKuninori Morimoto
3704110728SKuninori Morimoto /* Negative values might be errors */
3804110728SKuninori Morimoto switch (ret) {
3904110728SKuninori Morimoto case -EPROBE_DEFER:
4004110728SKuninori Morimoto case -ENOTSUPP:
411f566435SHans de Goede case -EINVAL:
4204110728SKuninori Morimoto break;
4304110728SKuninori Morimoto default:
4404110728SKuninori Morimoto dev_err(rtd->dev,
4504110728SKuninori Morimoto "ASoC: error at %s on %s: %d\n",
4604110728SKuninori Morimoto func, rtd->dai_link->name, ret);
4704110728SKuninori Morimoto }
4804110728SKuninori Morimoto
4904110728SKuninori Morimoto return ret;
5004110728SKuninori Morimoto }
5104110728SKuninori Morimoto
snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime * rtd,int stream)52b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
53b7898396STakashi Iwai int stream)
54b7898396STakashi Iwai {
55b7898396STakashi Iwai snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
56b7898396STakashi Iwai }
57b7898396STakashi Iwai
583c75c0eaSTakashi Iwai #define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
593c75c0eaSTakashi Iwai snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
60b2ae8066STakashi Iwai
snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime * rtd,int stream)61b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
62b7898396STakashi Iwai int stream)
63b7898396STakashi Iwai {
64b7898396STakashi Iwai snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
65b7898396STakashi Iwai }
66b7898396STakashi Iwai
67b2ae8066STakashi Iwai #define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
68b2ae8066STakashi Iwai snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
69b2ae8066STakashi Iwai
7001d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS 8
7101d7584cSLiam Girdwood
soc_cpu_dai_name(struct snd_soc_pcm_runtime * rtd)726fb8944cSKuninori Morimoto static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
736fb8944cSKuninori Morimoto {
743989ade2SKuninori Morimoto return (rtd)->dai_link->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
756fb8944cSKuninori Morimoto }
soc_codec_dai_name(struct snd_soc_pcm_runtime * rtd)766fb8944cSKuninori Morimoto static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
776fb8944cSKuninori Morimoto {
783989ade2SKuninori Morimoto return (rtd)->dai_link->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
796fb8944cSKuninori Morimoto }
806fb8944cSKuninori Morimoto
81c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS
dpcm_state_string(enum snd_soc_dpcm_state state)82c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
83c3212829SKuninori Morimoto {
84c3212829SKuninori Morimoto switch (state) {
85c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_NEW:
86c3212829SKuninori Morimoto return "new";
87c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_OPEN:
88c3212829SKuninori Morimoto return "open";
89c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_PARAMS:
90c3212829SKuninori Morimoto return "hw_params";
91c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PREPARE:
92c3212829SKuninori Morimoto return "prepare";
93c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_START:
94c3212829SKuninori Morimoto return "start";
95c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_STOP:
96c3212829SKuninori Morimoto return "stop";
97c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_SUSPEND:
98c3212829SKuninori Morimoto return "suspend";
99c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_PAUSED:
100c3212829SKuninori Morimoto return "paused";
101c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_HW_FREE:
102c3212829SKuninori Morimoto return "hw_free";
103c3212829SKuninori Morimoto case SND_SOC_DPCM_STATE_CLOSE:
104c3212829SKuninori Morimoto return "close";
105c3212829SKuninori Morimoto }
106c3212829SKuninori Morimoto
107c3212829SKuninori Morimoto return "unknown";
108c3212829SKuninori Morimoto }
109c3212829SKuninori Morimoto
dpcm_show_state(struct snd_soc_pcm_runtime * fe,int stream,char * buf,size_t size)110c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
111c3212829SKuninori Morimoto int stream, char *buf, size_t size)
112c3212829SKuninori Morimoto {
113c3212829SKuninori Morimoto struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
114c3212829SKuninori Morimoto struct snd_soc_dpcm *dpcm;
115c3212829SKuninori Morimoto ssize_t offset = 0;
116c3212829SKuninori Morimoto
117c3212829SKuninori Morimoto /* FE state */
118d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
119c3212829SKuninori Morimoto "[%s - %s]\n", fe->dai_link->name,
120c3212829SKuninori Morimoto stream ? "Capture" : "Playback");
121c3212829SKuninori Morimoto
122d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "State: %s\n",
123c3212829SKuninori Morimoto dpcm_state_string(fe->dpcm[stream].state));
124c3212829SKuninori Morimoto
125c3212829SKuninori Morimoto if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
126c3212829SKuninori Morimoto (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
127d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
128c3212829SKuninori Morimoto "Hardware Params: "
129c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n",
130c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)),
131c3212829SKuninori Morimoto params_channels(params),
132c3212829SKuninori Morimoto params_rate(params));
133c3212829SKuninori Morimoto
134c3212829SKuninori Morimoto /* BEs state */
135d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset, "Backends:\n");
136c3212829SKuninori Morimoto
137c3212829SKuninori Morimoto if (list_empty(&fe->dpcm[stream].be_clients)) {
138d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
139c3212829SKuninori Morimoto " No active DSP links\n");
140c3212829SKuninori Morimoto goto out;
141c3212829SKuninori Morimoto }
142c3212829SKuninori Morimoto
143c3212829SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
144c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be;
14525106550SKuninori Morimoto params = &be->dpcm[stream].hw_params;
146c3212829SKuninori Morimoto
147d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
148c3212829SKuninori Morimoto "- %s\n", be->dai_link->name);
149c3212829SKuninori Morimoto
150d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
151c3212829SKuninori Morimoto " State: %s\n",
152c3212829SKuninori Morimoto dpcm_state_string(be->dpcm[stream].state));
153c3212829SKuninori Morimoto
154c3212829SKuninori Morimoto if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
155c3212829SKuninori Morimoto (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
156d0c9abb8STakashi Iwai offset += scnprintf(buf + offset, size - offset,
157c3212829SKuninori Morimoto " Hardware Params: "
158c3212829SKuninori Morimoto "Format = %s, Channels = %d, Rate = %d\n",
159c3212829SKuninori Morimoto snd_pcm_format_name(params_format(params)),
160c3212829SKuninori Morimoto params_channels(params),
161c3212829SKuninori Morimoto params_rate(params));
162c3212829SKuninori Morimoto }
163c3212829SKuninori Morimoto out:
164c3212829SKuninori Morimoto return offset;
165c3212829SKuninori Morimoto }
166c3212829SKuninori Morimoto
dpcm_state_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)167c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
168c3212829SKuninori Morimoto size_t count, loff_t *ppos)
169c3212829SKuninori Morimoto {
170c3212829SKuninori Morimoto struct snd_soc_pcm_runtime *fe = file->private_data;
171c3212829SKuninori Morimoto ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
172c3212829SKuninori Morimoto int stream;
173c3212829SKuninori Morimoto char *buf;
174c3212829SKuninori Morimoto
1753989ade2SKuninori Morimoto if (fe->dai_link->num_cpus > 1) {
1766e1276a5SBard Liao dev_err(fe->dev,
1776e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__);
1786e1276a5SBard Liao return -EINVAL;
1796e1276a5SBard Liao }
1806e1276a5SBard Liao
181c3212829SKuninori Morimoto buf = kmalloc(out_count, GFP_KERNEL);
182c3212829SKuninori Morimoto if (!buf)
183c3212829SKuninori Morimoto return -ENOMEM;
184c3212829SKuninori Morimoto
185b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
186c3212829SKuninori Morimoto for_each_pcm_streams(stream)
187c2233a26SKuninori Morimoto if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
188c3212829SKuninori Morimoto offset += dpcm_show_state(fe, stream,
189c3212829SKuninori Morimoto buf + offset,
190c3212829SKuninori Morimoto out_count - offset);
191b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
192c3212829SKuninori Morimoto
193c3212829SKuninori Morimoto ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
194c3212829SKuninori Morimoto
195c3212829SKuninori Morimoto kfree(buf);
196c3212829SKuninori Morimoto return ret;
197c3212829SKuninori Morimoto }
198c3212829SKuninori Morimoto
199c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
200c3212829SKuninori Morimoto .open = simple_open,
201c3212829SKuninori Morimoto .read = dpcm_state_read_file,
202c3212829SKuninori Morimoto .llseek = default_llseek,
203c3212829SKuninori Morimoto };
204c3212829SKuninori Morimoto
soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime * rtd)205c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
206c3212829SKuninori Morimoto {
207c3212829SKuninori Morimoto if (!rtd->dai_link->dynamic)
208c3212829SKuninori Morimoto return;
209c3212829SKuninori Morimoto
210c3212829SKuninori Morimoto if (!rtd->card->debugfs_card_root)
211c3212829SKuninori Morimoto return;
212c3212829SKuninori Morimoto
213c3212829SKuninori Morimoto rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
214c3212829SKuninori Morimoto rtd->card->debugfs_card_root);
215c3212829SKuninori Morimoto
216c3212829SKuninori Morimoto debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
217c3212829SKuninori Morimoto rtd, &dpcm_state_fops);
218c3212829SKuninori Morimoto }
219154dae87SKuninori Morimoto
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)220154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
221154dae87SKuninori Morimoto {
222154dae87SKuninori Morimoto char *name;
223154dae87SKuninori Morimoto
224154dae87SKuninori Morimoto name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
225154dae87SKuninori Morimoto stream ? "capture" : "playback");
226154dae87SKuninori Morimoto if (name) {
227154dae87SKuninori Morimoto dpcm->debugfs_state = debugfs_create_dir(
228154dae87SKuninori Morimoto name, dpcm->fe->debugfs_dpcm_root);
229154dae87SKuninori Morimoto debugfs_create_u32("state", 0644, dpcm->debugfs_state,
230154dae87SKuninori Morimoto &dpcm->state);
231154dae87SKuninori Morimoto kfree(name);
232154dae87SKuninori Morimoto }
233154dae87SKuninori Morimoto }
234154dae87SKuninori Morimoto
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)235154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
236154dae87SKuninori Morimoto {
237154dae87SKuninori Morimoto debugfs_remove_recursive(dpcm->debugfs_state);
238154dae87SKuninori Morimoto }
239154dae87SKuninori Morimoto
240154dae87SKuninori Morimoto #else
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)241154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
242154dae87SKuninori Morimoto int stream)
243154dae87SKuninori Morimoto {
244154dae87SKuninori Morimoto }
245154dae87SKuninori Morimoto
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)246154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
247154dae87SKuninori Morimoto {
248154dae87SKuninori Morimoto }
249c3212829SKuninori Morimoto #endif
250c3212829SKuninori Morimoto
2519c6d7f93SKuninori Morimoto /* Set FE's runtime_update state; the state is protected via PCM stream lock
2529c6d7f93SKuninori Morimoto * for avoiding the race with trigger callback.
2539c6d7f93SKuninori Morimoto * If the state is unset and a trigger is pending while the previous operation,
2549c6d7f93SKuninori Morimoto * process the pending trigger action here.
2559c6d7f93SKuninori Morimoto */
2569c6d7f93SKuninori Morimoto static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
dpcm_set_fe_update_state(struct snd_soc_pcm_runtime * fe,int stream,enum snd_soc_dpcm_update state)2579c6d7f93SKuninori Morimoto static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2589c6d7f93SKuninori Morimoto int stream, enum snd_soc_dpcm_update state)
2599c6d7f93SKuninori Morimoto {
2609c6d7f93SKuninori Morimoto struct snd_pcm_substream *substream =
2619c6d7f93SKuninori Morimoto snd_soc_dpcm_get_substream(fe, stream);
2629c6d7f93SKuninori Morimoto
263b7898396STakashi Iwai snd_soc_dpcm_stream_lock_irq(fe, stream);
2649c6d7f93SKuninori Morimoto if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2659c6d7f93SKuninori Morimoto dpcm_fe_dai_do_trigger(substream,
2669c6d7f93SKuninori Morimoto fe->dpcm[stream].trigger_pending - 1);
2679c6d7f93SKuninori Morimoto fe->dpcm[stream].trigger_pending = 0;
2689c6d7f93SKuninori Morimoto }
2699c6d7f93SKuninori Morimoto fe->dpcm[stream].runtime_update = state;
270b7898396STakashi Iwai snd_soc_dpcm_stream_unlock_irq(fe, stream);
2719c6d7f93SKuninori Morimoto }
2729c6d7f93SKuninori Morimoto
dpcm_set_be_update_state(struct snd_soc_pcm_runtime * be,int stream,enum snd_soc_dpcm_update state)273a7e20444SKuninori Morimoto static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
274a7e20444SKuninori Morimoto int stream, enum snd_soc_dpcm_update state)
275a7e20444SKuninori Morimoto {
276a7e20444SKuninori Morimoto be->dpcm[stream].runtime_update = state;
277a7e20444SKuninori Morimoto }
278a7e20444SKuninori Morimoto
279d9051d86SKuninori Morimoto /**
280d9051d86SKuninori Morimoto * snd_soc_runtime_action() - Increment/Decrement active count for
281d9051d86SKuninori Morimoto * PCM runtime components
282d9051d86SKuninori Morimoto * @rtd: ASoC PCM runtime that is activated
283d9051d86SKuninori Morimoto * @stream: Direction of the PCM stream
284b6d6e9eaSColton Lewis * @action: Activate stream if 1. Deactivate if -1.
285d9051d86SKuninori Morimoto *
286d9051d86SKuninori Morimoto * Increments/Decrements the active count for all the DAIs and components
287d9051d86SKuninori Morimoto * attached to a PCM runtime.
288d9051d86SKuninori Morimoto * Should typically be called when a stream is opened.
289d9051d86SKuninori Morimoto *
290d9051d86SKuninori Morimoto * Must be called with the rtd->card->pcm_mutex being held
291d9051d86SKuninori Morimoto */
snd_soc_runtime_action(struct snd_soc_pcm_runtime * rtd,int stream,int action)292d9051d86SKuninori Morimoto void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2937a5aaba4SKuninori Morimoto int stream, int action)
2947a5aaba4SKuninori Morimoto {
295c840f769SKuninori Morimoto struct snd_soc_dai *dai;
2967a5aaba4SKuninori Morimoto int i;
2977a5aaba4SKuninori Morimoto
298b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
2997a5aaba4SKuninori Morimoto
300dc829106SKuninori Morimoto for_each_rtd_dais(rtd, i, dai)
301dc829106SKuninori Morimoto snd_soc_dai_action(dai, stream, action);
3027a5aaba4SKuninori Morimoto }
303d9051d86SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
30424894b76SLars-Peter Clausen
30524894b76SLars-Peter Clausen /**
306208a1589SLars-Peter Clausen * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
307208a1589SLars-Peter Clausen * @rtd: The ASoC PCM runtime that should be checked.
308208a1589SLars-Peter Clausen *
309208a1589SLars-Peter Clausen * This function checks whether the power down delay should be ignored for a
310208a1589SLars-Peter Clausen * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
311208a1589SLars-Peter Clausen * been configured to ignore the delay, or if none of the components benefits
312208a1589SLars-Peter Clausen * from having the delay.
313208a1589SLars-Peter Clausen */
snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime * rtd)314208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
315208a1589SLars-Peter Clausen {
316fbb16563SKuninori Morimoto struct snd_soc_component *component;
3172e5894d7SBenoit Cousson bool ignore = true;
318613fb500SKuninori Morimoto int i;
3192e5894d7SBenoit Cousson
320208a1589SLars-Peter Clausen if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
321208a1589SLars-Peter Clausen return true;
322208a1589SLars-Peter Clausen
323613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component)
32472c38184SKuninori Morimoto ignore &= !component->driver->use_pmdown_time;
325fbb16563SKuninori Morimoto
326fbb16563SKuninori Morimoto return ignore;
327208a1589SLars-Peter Clausen }
328208a1589SLars-Peter Clausen
329208a1589SLars-Peter Clausen /**
33090996f43SLars-Peter Clausen * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
33190996f43SLars-Peter Clausen * @substream: the pcm substream
33290996f43SLars-Peter Clausen * @hw: the hardware parameters
33390996f43SLars-Peter Clausen *
33490996f43SLars-Peter Clausen * Sets the substream runtime hardware parameters.
33590996f43SLars-Peter Clausen */
snd_soc_set_runtime_hwparams(struct snd_pcm_substream * substream,const struct snd_pcm_hardware * hw)33690996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
33790996f43SLars-Peter Clausen const struct snd_pcm_hardware *hw)
33890996f43SLars-Peter Clausen {
33956e749baSKuninori Morimoto substream->runtime->hw = *hw;
34056e749baSKuninori Morimoto
34190996f43SLars-Peter Clausen return 0;
34290996f43SLars-Peter Clausen }
34390996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
34490996f43SLars-Peter Clausen
34501d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
dpcm_dapm_stream_event(struct snd_soc_pcm_runtime * fe,int dir,int event)34623607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
34701d7584cSLiam Girdwood int event)
34801d7584cSLiam Girdwood {
34901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
35001d7584cSLiam Girdwood
351b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(fe);
352b7898396STakashi Iwai
3538d6258a4SKuninori Morimoto for_each_dpcm_be(fe, dir, dpcm) {
35401d7584cSLiam Girdwood
35501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be;
35601d7584cSLiam Girdwood
357103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
35801d7584cSLiam Girdwood be->dai_link->name, event, dir);
35901d7584cSLiam Girdwood
360b1cd2e34SBanajit Goswami if ((event == SND_SOC_DAPM_STREAM_STOP) &&
361b1cd2e34SBanajit Goswami (be->dpcm[dir].users >= 1))
362b1cd2e34SBanajit Goswami continue;
363b1cd2e34SBanajit Goswami
36401d7584cSLiam Girdwood snd_soc_dapm_stream_event(be, dir, event);
36501d7584cSLiam Girdwood }
36601d7584cSLiam Girdwood
36701d7584cSLiam Girdwood snd_soc_dapm_stream_event(fe, dir, event);
36801d7584cSLiam Girdwood
36901d7584cSLiam Girdwood return 0;
37001d7584cSLiam Girdwood }
37101d7584cSLiam Girdwood
soc_pcm_set_dai_params(struct snd_soc_dai * dai,struct snd_pcm_hw_params * params)3722805b8bdSKuninori Morimoto static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
3732805b8bdSKuninori Morimoto struct snd_pcm_hw_params *params)
3742805b8bdSKuninori Morimoto {
3752805b8bdSKuninori Morimoto if (params) {
3762805b8bdSKuninori Morimoto dai->rate = params_rate(params);
3772805b8bdSKuninori Morimoto dai->channels = params_channels(params);
3782805b8bdSKuninori Morimoto dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
3792805b8bdSKuninori Morimoto } else {
3802805b8bdSKuninori Morimoto dai->rate = 0;
3812805b8bdSKuninori Morimoto dai->channels = 0;
3822805b8bdSKuninori Morimoto dai->sample_bits = 0;
3832805b8bdSKuninori Morimoto }
3842805b8bdSKuninori Morimoto }
3852805b8bdSKuninori Morimoto
soc_pcm_apply_symmetry(struct snd_pcm_substream * substream,struct snd_soc_dai * soc_dai)38617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
38717841020SDong Aisheng struct snd_soc_dai *soc_dai)
388ddee627cSLiam Girdwood {
3890ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
390ddee627cSLiam Girdwood int ret;
391ddee627cSLiam Girdwood
392f8fc9ec5SKuninori Morimoto if (!snd_soc_dai_active(soc_dai))
393f8fc9ec5SKuninori Morimoto return 0;
394f8fc9ec5SKuninori Morimoto
395fac110cbSKuninori Morimoto #define __soc_pcm_apply_symmetry(name, NAME) \
396fac110cbSKuninori Morimoto if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
397fac110cbSKuninori Morimoto rtd->dai_link->symmetric_##name)) { \
398fac110cbSKuninori Morimoto dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
399fac110cbSKuninori Morimoto #name, soc_dai->name); \
400fac110cbSKuninori Morimoto \
401fac110cbSKuninori Morimoto ret = snd_pcm_hw_constraint_single(substream->runtime, \
402fac110cbSKuninori Morimoto SNDRV_PCM_HW_PARAM_##NAME,\
403fac110cbSKuninori Morimoto soc_dai->name); \
404fac110cbSKuninori Morimoto if (ret < 0) { \
405fac110cbSKuninori Morimoto dev_err(soc_dai->dev, \
406fac110cbSKuninori Morimoto "ASoC: Unable to apply %s constraint: %d\n",\
407fac110cbSKuninori Morimoto #name, ret); \
408fac110cbSKuninori Morimoto return ret; \
409fac110cbSKuninori Morimoto } \
4103635bf09SNicolin Chen }
4113635bf09SNicolin Chen
412fac110cbSKuninori Morimoto __soc_pcm_apply_symmetry(rate, RATE);
413fac110cbSKuninori Morimoto __soc_pcm_apply_symmetry(channels, CHANNELS);
414fac110cbSKuninori Morimoto __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
415ddee627cSLiam Girdwood
416ddee627cSLiam Girdwood return 0;
417ddee627cSLiam Girdwood }
418ddee627cSLiam Girdwood
soc_pcm_params_symmetry(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)4193635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4203635bf09SNicolin Chen struct snd_pcm_hw_params *params)
4213635bf09SNicolin Chen {
4220ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4232805b8bdSKuninori Morimoto struct snd_soc_dai d;
424c840f769SKuninori Morimoto struct snd_soc_dai *dai;
42519bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai;
4262805b8bdSKuninori Morimoto unsigned int symmetry, i;
4273635bf09SNicolin Chen
428ee39d77eSKuninori Morimoto d.name = __func__;
4292805b8bdSKuninori Morimoto soc_pcm_set_dai_params(&d, params);
4303635bf09SNicolin Chen
4311cacbac4SKuninori Morimoto #define __soc_pcm_params_symmetry(xxx) \
4321cacbac4SKuninori Morimoto symmetry = rtd->dai_link->symmetric_##xxx; \
4333a906721SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) \
4341cacbac4SKuninori Morimoto symmetry |= dai->driver->symmetric_##xxx; \
4353a906721SKuninori Morimoto \
4363a906721SKuninori Morimoto if (symmetry) \
4373a906721SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
4389c2ae363SKuninori Morimoto if (!snd_soc_dai_is_dummy(cpu_dai) && \
4399c2ae363SKuninori Morimoto cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
440ee39d77eSKuninori Morimoto dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
441ee39d77eSKuninori Morimoto #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
4423a906721SKuninori Morimoto return -EINVAL; \
4433a906721SKuninori Morimoto }
4443a906721SKuninori Morimoto
4453635bf09SNicolin Chen /* reject unmatched parameters when applying symmetry */
4463a906721SKuninori Morimoto __soc_pcm_params_symmetry(rate);
4473a906721SKuninori Morimoto __soc_pcm_params_symmetry(channels);
4483a906721SKuninori Morimoto __soc_pcm_params_symmetry(sample_bits);
449ddee627cSLiam Girdwood
450ddee627cSLiam Girdwood return 0;
451ddee627cSLiam Girdwood }
452ddee627cSLiam Girdwood
soc_pcm_update_symmetry(struct snd_pcm_substream * substream)45368cbc557SKuninori Morimoto static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
45462e5f676SLars-Peter Clausen {
4550ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
45662e5f676SLars-Peter Clausen struct snd_soc_dai_link *link = rtd->dai_link;
457c840f769SKuninori Morimoto struct snd_soc_dai *dai;
4582e5894d7SBenoit Cousson unsigned int symmetry, i;
45962e5f676SLars-Peter Clausen
460f14654ddSKuninori Morimoto symmetry = link->symmetric_rate ||
46119bdcc7aSShreyas NC link->symmetric_channels ||
462f14654ddSKuninori Morimoto link->symmetric_sample_bits;
46319bdcc7aSShreyas NC
464c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai)
46519bdcc7aSShreyas NC symmetry = symmetry ||
466f14654ddSKuninori Morimoto dai->driver->symmetric_rate ||
467c840f769SKuninori Morimoto dai->driver->symmetric_channels ||
468f14654ddSKuninori Morimoto dai->driver->symmetric_sample_bits;
4692e5894d7SBenoit Cousson
47068cbc557SKuninori Morimoto if (symmetry)
47168cbc557SKuninori Morimoto substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
47262e5f676SLars-Peter Clausen }
47362e5f676SLars-Peter Clausen
soc_pcm_set_msb(struct snd_pcm_substream * substream,int bits)4742e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
47558ba9b25SMark Brown {
4760ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
477c6068d3aSTakashi Iwai int ret;
47858ba9b25SMark Brown
47958ba9b25SMark Brown if (!bits)
48058ba9b25SMark Brown return;
48158ba9b25SMark Brown
4820e2a3751SLars-Peter Clausen ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
48358ba9b25SMark Brown if (ret != 0)
4840e2a3751SLars-Peter Clausen dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
4850e2a3751SLars-Peter Clausen bits, ret);
48658ba9b25SMark Brown }
48758ba9b25SMark Brown
soc_pcm_apply_msb(struct snd_pcm_substream * substream)488c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
489bd477c31SLars-Peter Clausen {
4900ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
49119bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai;
4922e5894d7SBenoit Cousson struct snd_soc_dai *codec_dai;
49357be9206SKuninori Morimoto int stream = substream->stream;
4942e5894d7SBenoit Cousson int i;
49519bdcc7aSShreyas NC unsigned int bits = 0, cpu_bits = 0;
496c8dd1fecSBenoit Cousson
497a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
4982bc3e1f2SKuninori Morimoto struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
49957be9206SKuninori Morimoto
50057be9206SKuninori Morimoto if (pcm_codec->sig_bits == 0) {
5012e5894d7SBenoit Cousson bits = 0;
5022e5894d7SBenoit Cousson break;
5032e5894d7SBenoit Cousson }
50457be9206SKuninori Morimoto bits = max(pcm_codec->sig_bits, bits);
5052e5894d7SBenoit Cousson }
50657be9206SKuninori Morimoto
507a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5082bc3e1f2SKuninori Morimoto struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
50919bdcc7aSShreyas NC
51019bdcc7aSShreyas NC if (pcm_cpu->sig_bits == 0) {
51119bdcc7aSShreyas NC cpu_bits = 0;
51219bdcc7aSShreyas NC break;
51319bdcc7aSShreyas NC }
51419bdcc7aSShreyas NC cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
51519bdcc7aSShreyas NC }
516c8dd1fecSBenoit Cousson
5172e5894d7SBenoit Cousson soc_pcm_set_msb(substream, bits);
5182e5894d7SBenoit Cousson soc_pcm_set_msb(substream, cpu_bits);
519c8dd1fecSBenoit Cousson }
520c8dd1fecSBenoit Cousson
soc_pcm_hw_init(struct snd_pcm_hardware * hw)521f6c04af5SKuninori Morimoto static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
522f6c04af5SKuninori Morimoto {
523f6c04af5SKuninori Morimoto hw->rates = UINT_MAX;
524f6c04af5SKuninori Morimoto hw->rate_min = 0;
525f6c04af5SKuninori Morimoto hw->rate_max = UINT_MAX;
5266cb56a45SKuninori Morimoto hw->channels_min = 0;
5276cb56a45SKuninori Morimoto hw->channels_max = UINT_MAX;
528debc71f2SKuninori Morimoto hw->formats = ULLONG_MAX;
529f6c04af5SKuninori Morimoto }
530f6c04af5SKuninori Morimoto
soc_pcm_hw_update_rate(struct snd_pcm_hardware * hw,struct snd_soc_pcm_stream * p)531f6c04af5SKuninori Morimoto static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
532f6c04af5SKuninori Morimoto struct snd_soc_pcm_stream *p)
533f6c04af5SKuninori Morimoto {
534f6c04af5SKuninori Morimoto hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
535f6c04af5SKuninori Morimoto
536f6c04af5SKuninori Morimoto /* setup hw->rate_min/max via hw->rates first */
537f6c04af5SKuninori Morimoto snd_pcm_hw_limit_rates(hw);
538f6c04af5SKuninori Morimoto
539f6c04af5SKuninori Morimoto /* update hw->rate_min/max by snd_soc_pcm_stream */
540f6c04af5SKuninori Morimoto hw->rate_min = max(hw->rate_min, p->rate_min);
541f6c04af5SKuninori Morimoto hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
542f6c04af5SKuninori Morimoto }
543f6c04af5SKuninori Morimoto
soc_pcm_hw_update_chan(struct snd_pcm_hardware * hw,struct snd_soc_pcm_stream * p)5446cb56a45SKuninori Morimoto static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
5456cb56a45SKuninori Morimoto struct snd_soc_pcm_stream *p)
5466cb56a45SKuninori Morimoto {
5476cb56a45SKuninori Morimoto hw->channels_min = max(hw->channels_min, p->channels_min);
5486cb56a45SKuninori Morimoto hw->channels_max = min(hw->channels_max, p->channels_max);
5496cb56a45SKuninori Morimoto }
5506cb56a45SKuninori Morimoto
soc_pcm_hw_update_format(struct snd_pcm_hardware * hw,struct snd_soc_pcm_stream * p)551debc71f2SKuninori Morimoto static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
552debc71f2SKuninori Morimoto struct snd_soc_pcm_stream *p)
553debc71f2SKuninori Morimoto {
554debc71f2SKuninori Morimoto hw->formats &= p->formats;
555debc71f2SKuninori Morimoto }
556debc71f2SKuninori Morimoto
5575854a464SSamuel Holland /**
5585854a464SSamuel Holland * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
5595854a464SSamuel Holland * @rtd: ASoC PCM runtime
5605854a464SSamuel Holland * @hw: PCM hardware parameters (output)
5615854a464SSamuel Holland * @stream: Direction of the PCM stream
5625854a464SSamuel Holland *
5635854a464SSamuel Holland * Calculates the subset of stream parameters supported by all DAIs
5645854a464SSamuel Holland * associated with the PCM stream.
5655854a464SSamuel Holland */
snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hardware * hw,int stream)5665854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
5675854a464SSamuel Holland struct snd_pcm_hardware *hw, int stream)
568bd477c31SLars-Peter Clausen {
5690b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai;
57019bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai;
5712e5894d7SBenoit Cousson struct snd_soc_pcm_stream *codec_stream;
5722e5894d7SBenoit Cousson struct snd_soc_pcm_stream *cpu_stream;
57319bdcc7aSShreyas NC unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5742e5894d7SBenoit Cousson int i;
57578e45c99SLars-Peter Clausen
576f6c04af5SKuninori Morimoto soc_pcm_hw_init(hw);
577f6c04af5SKuninori Morimoto
57819bdcc7aSShreyas NC /* first calculate min/max only for CPUs in the DAI link */
579a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5800e9cf4c4SBard Liao
5810e9cf4c4SBard Liao /*
5820e9cf4c4SBard Liao * Skip CPUs which don't support the current stream type.
5830e9cf4c4SBard Liao * Otherwise, since the rate, channel, and format values will
5840e9cf4c4SBard Liao * zero in that case, we would have no usable settings left,
5850e9cf4c4SBard Liao * causing the resulting setup to fail.
5860e9cf4c4SBard Liao */
5875854a464SSamuel Holland if (!snd_soc_dai_stream_valid(cpu_dai, stream))
5880e9cf4c4SBard Liao continue;
5890e9cf4c4SBard Liao
59019bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
59178e45c99SLars-Peter Clausen
5926cb56a45SKuninori Morimoto soc_pcm_hw_update_chan(hw, cpu_stream);
593f6c04af5SKuninori Morimoto soc_pcm_hw_update_rate(hw, cpu_stream);
594debc71f2SKuninori Morimoto soc_pcm_hw_update_format(hw, cpu_stream);
59519bdcc7aSShreyas NC }
5966cb56a45SKuninori Morimoto cpu_chan_min = hw->channels_min;
5976cb56a45SKuninori Morimoto cpu_chan_max = hw->channels_max;
59819bdcc7aSShreyas NC
59919bdcc7aSShreyas NC /* second calculate min/max only for CODECs in the DAI link */
600a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
601cde79035SRicard Wanderlof
602cde79035SRicard Wanderlof /*
603cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type.
604cde79035SRicard Wanderlof * Otherwise, since the rate, channel, and format values will
605cde79035SRicard Wanderlof * zero in that case, we would have no usable settings left,
606cde79035SRicard Wanderlof * causing the resulting setup to fail.
607cde79035SRicard Wanderlof */
60825c2f515SKuninori Morimoto if (!snd_soc_dai_stream_valid(codec_dai, stream))
609cde79035SRicard Wanderlof continue;
610cde79035SRicard Wanderlof
611acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
612acf253c1SKuninori Morimoto
6136cb56a45SKuninori Morimoto soc_pcm_hw_update_chan(hw, codec_stream);
614f6c04af5SKuninori Morimoto soc_pcm_hw_update_rate(hw, codec_stream);
615debc71f2SKuninori Morimoto soc_pcm_hw_update_format(hw, codec_stream);
6162e5894d7SBenoit Cousson }
6172e5894d7SBenoit Cousson
6185854a464SSamuel Holland /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6196cb56a45SKuninori Morimoto if (!hw->channels_min)
6205854a464SSamuel Holland return -EINVAL;
6215854a464SSamuel Holland
6222e5894d7SBenoit Cousson /*
6232e5894d7SBenoit Cousson * chan min/max cannot be enforced if there are multiple CODEC DAIs
62419bdcc7aSShreyas NC * connected to CPU DAI(s), use CPU DAI's directly and let
6252e5894d7SBenoit Cousson * channel allocation be fixed up later
6262e5894d7SBenoit Cousson */
6273989ade2SKuninori Morimoto if (rtd->dai_link->num_codecs > 1) {
6286cb56a45SKuninori Morimoto hw->channels_min = cpu_chan_min;
6296cb56a45SKuninori Morimoto hw->channels_max = cpu_chan_max;
6302e5894d7SBenoit Cousson }
6312e5894d7SBenoit Cousson
6325854a464SSamuel Holland return 0;
6335854a464SSamuel Holland }
6345854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
6355854a464SSamuel Holland
soc_pcm_init_runtime_hw(struct snd_pcm_substream * substream)6365854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
6375854a464SSamuel Holland {
6385854a464SSamuel Holland struct snd_pcm_hardware *hw = &substream->runtime->hw;
6390ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
6405854a464SSamuel Holland u64 formats = hw->formats;
6415854a464SSamuel Holland
6425854a464SSamuel Holland /*
6435854a464SSamuel Holland * At least one CPU and one CODEC should match. Otherwise, we should
6445854a464SSamuel Holland * have bailed out on a higher level, since there would be no CPU or
6455854a464SSamuel Holland * CODEC to support the transfer direction in that case.
6465854a464SSamuel Holland */
6475854a464SSamuel Holland snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
6485854a464SSamuel Holland
6495854a464SSamuel Holland if (formats)
6505854a464SSamuel Holland hw->formats &= formats;
651bd477c31SLars-Peter Clausen }
652bd477c31SLars-Peter Clausen
soc_pcm_components_open(struct snd_pcm_substream * substream)653dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
654e7ecfdb7SKuninori Morimoto {
6550ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
656e7ecfdb7SKuninori Morimoto struct snd_soc_component *component;
657613fb500SKuninori Morimoto int i, ret = 0;
658e7ecfdb7SKuninori Morimoto
659613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) {
66051aff91aSKuninori Morimoto ret = snd_soc_component_module_get_when_open(component, substream);
661bcae1631SKuninori Morimoto if (ret < 0)
662d2aaa8d8SKai Vehmanen break;
663e7ecfdb7SKuninori Morimoto
664ae2f4849SKuninori Morimoto ret = snd_soc_component_open(component, substream);
665bcae1631SKuninori Morimoto if (ret < 0)
666d2aaa8d8SKai Vehmanen break;
667e7ecfdb7SKuninori Morimoto }
668dd03907bSKuninori Morimoto
669d2aaa8d8SKai Vehmanen return ret;
670e7ecfdb7SKuninori Morimoto }
671e7ecfdb7SKuninori Morimoto
soc_pcm_components_close(struct snd_pcm_substream * substream,int rollback)67251aff91aSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream,
67351aff91aSKuninori Morimoto int rollback)
674244e2936SCharles Keepax {
6750ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
676244e2936SCharles Keepax struct snd_soc_component *component;
67733be10b5SKuninori Morimoto int i, ret = 0;
678244e2936SCharles Keepax
679613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) {
68033be10b5SKuninori Morimoto int r = snd_soc_component_close(component, substream, rollback);
681e82ebffcSKuninori Morimoto if (r < 0)
682e82ebffcSKuninori Morimoto ret = r; /* use last ret */
683e82ebffcSKuninori Morimoto
68451aff91aSKuninori Morimoto snd_soc_component_module_put_when_close(component, substream, rollback);
685244e2936SCharles Keepax }
686244e2936SCharles Keepax
6873672beb8SKuninori Morimoto return ret;
688244e2936SCharles Keepax }
689244e2936SCharles Keepax
soc_pcm_clean(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,int rollback)690b7898396STakashi Iwai static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
691b7898396STakashi Iwai struct snd_pcm_substream *substream, int rollback)
69262c86d1dSKuninori Morimoto {
69362c86d1dSKuninori Morimoto struct snd_soc_component *component;
694c840f769SKuninori Morimoto struct snd_soc_dai *dai;
69562c86d1dSKuninori Morimoto int i;
69662c86d1dSKuninori Morimoto
697b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
69862c86d1dSKuninori Morimoto
6991da681e5SChancel Liu if (!rollback) {
70062c86d1dSKuninori Morimoto snd_soc_runtime_deactivate(rtd, substream->stream);
7011da681e5SChancel Liu
702d683e408SChancel Liu /* Make sure DAI parameters cleared if the DAI becomes inactive */
703d683e408SChancel Liu for_each_rtd_dais(rtd, i, dai)
704d683e408SChancel Liu if (snd_soc_dai_active(dai) == 0 &&
705d683e408SChancel Liu (dai->rate || dai->channels || dai->sample_bits))
706d683e408SChancel Liu soc_pcm_set_dai_params(dai, NULL);
7071da681e5SChancel Liu }
70862c86d1dSKuninori Morimoto
709c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai)
710140a4532SKuninori Morimoto snd_soc_dai_shutdown(dai, substream, rollback);
71162c86d1dSKuninori Morimoto
712140a4532SKuninori Morimoto snd_soc_link_shutdown(substream, rollback);
71362c86d1dSKuninori Morimoto
714140a4532SKuninori Morimoto soc_pcm_components_close(substream, rollback);
71562c86d1dSKuninori Morimoto
716140a4532SKuninori Morimoto snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
71762c86d1dSKuninori Morimoto
71862c86d1dSKuninori Morimoto for_each_rtd_components(rtd, i, component)
719b3dea624SKuninori Morimoto if (!snd_soc_component_active(component))
72062c86d1dSKuninori Morimoto pinctrl_pm_select_sleep_state(component->dev);
72162c86d1dSKuninori Morimoto
72262c86d1dSKuninori Morimoto return 0;
72362c86d1dSKuninori Morimoto }
72462c86d1dSKuninori Morimoto
72562c86d1dSKuninori Morimoto /*
726140a4532SKuninori Morimoto * Called by ALSA when a PCM substream is closed. Private data can be
727140a4532SKuninori Morimoto * freed here. The cpu DAI, codec DAI, machine and components are also
728140a4532SKuninori Morimoto * shutdown.
729140a4532SKuninori Morimoto */
__soc_pcm_close(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)730b7898396STakashi Iwai static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
731b7898396STakashi Iwai struct snd_pcm_substream *substream)
732b7898396STakashi Iwai {
733b7898396STakashi Iwai return soc_pcm_clean(rtd, substream, 0);
734b7898396STakashi Iwai }
735b7898396STakashi Iwai
736b7898396STakashi Iwai /* PCM close ops for non-DPCM streams */
soc_pcm_close(struct snd_pcm_substream * substream)737140a4532SKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
738140a4532SKuninori Morimoto {
739b7898396STakashi Iwai struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
740b7898396STakashi Iwai
741b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(rtd);
7426bbabd28SKuninori Morimoto __soc_pcm_close(rtd, substream);
743b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(rtd);
744b7898396STakashi Iwai return 0;
745140a4532SKuninori Morimoto }
746140a4532SKuninori Morimoto
soc_hw_sanity_check(struct snd_pcm_substream * substream)747c393281aSKuninori Morimoto static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
748c393281aSKuninori Morimoto {
749c393281aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
750c393281aSKuninori Morimoto struct snd_pcm_hardware *hw = &substream->runtime->hw;
751c393281aSKuninori Morimoto const char *name_cpu = soc_cpu_dai_name(rtd);
752c393281aSKuninori Morimoto const char *name_codec = soc_codec_dai_name(rtd);
753c393281aSKuninori Morimoto const char *err_msg;
754c393281aSKuninori Morimoto struct device *dev = rtd->dev;
755c393281aSKuninori Morimoto
756c393281aSKuninori Morimoto err_msg = "rates";
757c393281aSKuninori Morimoto if (!hw->rates)
758c393281aSKuninori Morimoto goto config_err;
759c393281aSKuninori Morimoto
760c393281aSKuninori Morimoto err_msg = "formats";
761c393281aSKuninori Morimoto if (!hw->formats)
762c393281aSKuninori Morimoto goto config_err;
763c393281aSKuninori Morimoto
764c393281aSKuninori Morimoto err_msg = "channels";
765c393281aSKuninori Morimoto if (!hw->channels_min || !hw->channels_max ||
766c393281aSKuninori Morimoto hw->channels_min > hw->channels_max)
767c393281aSKuninori Morimoto goto config_err;
768c393281aSKuninori Morimoto
769c393281aSKuninori Morimoto dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
770c393281aSKuninori Morimoto name_cpu);
771c393281aSKuninori Morimoto dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
772c393281aSKuninori Morimoto dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
773c393281aSKuninori Morimoto hw->channels_max);
774c393281aSKuninori Morimoto dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
775c393281aSKuninori Morimoto hw->rate_max);
776c393281aSKuninori Morimoto
777c393281aSKuninori Morimoto return 0;
778c393281aSKuninori Morimoto
779c393281aSKuninori Morimoto config_err:
780c393281aSKuninori Morimoto dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
781c393281aSKuninori Morimoto name_codec, name_cpu, err_msg);
782c393281aSKuninori Morimoto return -EINVAL;
783c393281aSKuninori Morimoto }
784c393281aSKuninori Morimoto
785140a4532SKuninori Morimoto /*
786ddee627cSLiam Girdwood * Called by ALSA when a PCM substream is opened, the runtime->hw record is
787ddee627cSLiam Girdwood * then initialized and any private data can be allocated. This also calls
788ef050becSCharles Keepax * startup for the cpu DAI, component, machine and codec DAI.
789ddee627cSLiam Girdwood */
__soc_pcm_open(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)790b7898396STakashi Iwai static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
791b7898396STakashi Iwai struct snd_pcm_substream *substream)
792ddee627cSLiam Girdwood {
79390be711eSKuninori Morimoto struct snd_soc_component *component;
794c840f769SKuninori Morimoto struct snd_soc_dai *dai;
795244e2936SCharles Keepax int i, ret = 0;
796ddee627cSLiam Girdwood
797b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
798b7898396STakashi Iwai
79976c39e86SKuninori Morimoto for_each_rtd_components(rtd, i, component)
80076c39e86SKuninori Morimoto pinctrl_pm_select_default_state(component->dev);
80190be711eSKuninori Morimoto
802939a5cfbSKuninori Morimoto ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
803939a5cfbSKuninori Morimoto if (ret < 0)
804b7898396STakashi Iwai goto err;
805ddee627cSLiam Girdwood
8065d9fa03eSKuninori Morimoto ret = soc_pcm_components_open(substream);
8075d9fa03eSKuninori Morimoto if (ret < 0)
808140a4532SKuninori Morimoto goto err;
8095d9fa03eSKuninori Morimoto
8107cf3c5b4SKuninori Morimoto ret = snd_soc_link_startup(substream);
811a5e6c109SKuninori Morimoto if (ret < 0)
812140a4532SKuninori Morimoto goto err;
8135d9fa03eSKuninori Morimoto
814ddee627cSLiam Girdwood /* startup the audio subsystem */
815c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) {
816c840f769SKuninori Morimoto ret = snd_soc_dai_startup(dai, substream);
817ce820145SKuninori Morimoto if (ret < 0)
818140a4532SKuninori Morimoto goto err;
8192e5894d7SBenoit Cousson }
8202e5894d7SBenoit Cousson
82101d7584cSLiam Girdwood /* Dynamic PCM DAI links compat checks use dynamic capabilities */
82201d7584cSLiam Girdwood if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
82301d7584cSLiam Girdwood goto dynamic;
82401d7584cSLiam Girdwood
825ddee627cSLiam Girdwood /* Check that the codec and cpu DAIs are compatible */
8262e5894d7SBenoit Cousson soc_pcm_init_runtime_hw(substream);
8272e5894d7SBenoit Cousson
82868cbc557SKuninori Morimoto soc_pcm_update_symmetry(substream);
82962e5f676SLars-Peter Clausen
830c393281aSKuninori Morimoto ret = soc_hw_sanity_check(substream);
831c393281aSKuninori Morimoto if (ret < 0)
832140a4532SKuninori Morimoto goto err;
833ddee627cSLiam Girdwood
834c8dd1fecSBenoit Cousson soc_pcm_apply_msb(substream);
83558ba9b25SMark Brown
836ddee627cSLiam Girdwood /* Symmetry only applies if we've already got an active stream. */
837c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) {
838c840f769SKuninori Morimoto ret = soc_pcm_apply_symmetry(substream, dai);
839ddee627cSLiam Girdwood if (ret != 0)
840140a4532SKuninori Morimoto goto err;
841ddee627cSLiam Girdwood }
84201d7584cSLiam Girdwood dynamic:
84324894b76SLars-Peter Clausen snd_soc_runtime_activate(rtd, substream->stream);
8448e7875aeSKuninori Morimoto ret = 0;
845140a4532SKuninori Morimoto err:
84604110728SKuninori Morimoto if (ret < 0)
847b7898396STakashi Iwai soc_pcm_clean(rtd, substream, 1);
848d6652ef8SMark Brown
84904110728SKuninori Morimoto return soc_pcm_ret(rtd, ret);
850ddee627cSLiam Girdwood }
851ddee627cSLiam Girdwood
852b7898396STakashi Iwai /* PCM open ops for non-DPCM streams */
soc_pcm_open(struct snd_pcm_substream * substream)853b7898396STakashi Iwai static int soc_pcm_open(struct snd_pcm_substream *substream)
854b7898396STakashi Iwai {
855b7898396STakashi Iwai struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
856b7898396STakashi Iwai int ret;
857b7898396STakashi Iwai
858b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(rtd);
859b7898396STakashi Iwai ret = __soc_pcm_open(rtd, substream);
860b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(rtd);
861b7898396STakashi Iwai return ret;
862b7898396STakashi Iwai }
863b7898396STakashi Iwai
864ddee627cSLiam Girdwood /*
865ddee627cSLiam Girdwood * Called by ALSA when the PCM substream is prepared, can set format, sample
866ddee627cSLiam Girdwood * rate, etc. This function is non atomic and can be called multiple times,
867ddee627cSLiam Girdwood * it can refer to the runtime info.
868ddee627cSLiam Girdwood */
__soc_pcm_prepare(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)869b7898396STakashi Iwai static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
870b7898396STakashi Iwai struct snd_pcm_substream *substream)
871ddee627cSLiam Girdwood {
872c840f769SKuninori Morimoto struct snd_soc_dai *dai;
8732e5894d7SBenoit Cousson int i, ret = 0;
874ddee627cSLiam Girdwood
875b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
876ddee627cSLiam Girdwood
8777cf3c5b4SKuninori Morimoto ret = snd_soc_link_prepare(substream);
878a5e6c109SKuninori Morimoto if (ret < 0)
879ddee627cSLiam Girdwood goto out;
880ddee627cSLiam Girdwood
8814f39514fSKuninori Morimoto ret = snd_soc_pcm_component_prepare(substream);
8824f39514fSKuninori Morimoto if (ret < 0)
883b8135864SKuninori Morimoto goto out;
884b8135864SKuninori Morimoto
885d108c7fdSKuninori Morimoto ret = snd_soc_pcm_dai_prepare(substream);
88662462e01SKuninori Morimoto if (ret < 0)
887ddee627cSLiam Girdwood goto out;
888ddee627cSLiam Girdwood
889ddee627cSLiam Girdwood /* cancel any delayed stream shutdown that is pending */
890ddee627cSLiam Girdwood if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
8919bffb1fbSMisael Lopez Cruz rtd->pop_wait) {
8929bffb1fbSMisael Lopez Cruz rtd->pop_wait = 0;
893ddee627cSLiam Girdwood cancel_delayed_work(&rtd->delayed_work);
894ddee627cSLiam Girdwood }
895ddee627cSLiam Girdwood
896d9b0951bSLiam Girdwood snd_soc_dapm_stream_event(rtd, substream->stream,
897ddee627cSLiam Girdwood SND_SOC_DAPM_STREAM_START);
898ddee627cSLiam Girdwood
899*868eb92bSSrinivas Kandagatla for_each_rtd_dais(rtd, i, dai) {
900*868eb92bSSrinivas Kandagatla if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger)
901c840f769SKuninori Morimoto snd_soc_dai_digital_mute(dai, 0, substream->stream);
902*868eb92bSSrinivas Kandagatla }
903ddee627cSLiam Girdwood
904ddee627cSLiam Girdwood out:
90504110728SKuninori Morimoto return soc_pcm_ret(rtd, ret);
906ddee627cSLiam Girdwood }
907ddee627cSLiam Girdwood
908b7898396STakashi Iwai /* PCM prepare ops for non-DPCM streams */
soc_pcm_prepare(struct snd_pcm_substream * substream)909b7898396STakashi Iwai static int soc_pcm_prepare(struct snd_pcm_substream *substream)
910b7898396STakashi Iwai {
911b7898396STakashi Iwai struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
912b7898396STakashi Iwai int ret;
913b7898396STakashi Iwai
914b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(rtd);
915b7898396STakashi Iwai ret = __soc_pcm_prepare(rtd, substream);
916b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(rtd);
917b7898396STakashi Iwai return ret;
918b7898396STakashi Iwai }
919b7898396STakashi Iwai
soc_pcm_codec_params_fixup(struct snd_pcm_hw_params * params,unsigned int mask)9202e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
9212e5894d7SBenoit Cousson unsigned int mask)
9222e5894d7SBenoit Cousson {
9232e5894d7SBenoit Cousson struct snd_interval *interval;
9242e5894d7SBenoit Cousson int channels = hweight_long(mask);
9252e5894d7SBenoit Cousson
9262e5894d7SBenoit Cousson interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
9272e5894d7SBenoit Cousson interval->min = channels;
9282e5894d7SBenoit Cousson interval->max = channels;
9292e5894d7SBenoit Cousson }
9302e5894d7SBenoit Cousson
soc_pcm_hw_clean(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,int rollback)931b7898396STakashi Iwai static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
932b7898396STakashi Iwai struct snd_pcm_substream *substream, int rollback)
933ab49436eSKuninori Morimoto {
934ab49436eSKuninori Morimoto struct snd_soc_dai *dai;
935ab49436eSKuninori Morimoto int i;
936ab49436eSKuninori Morimoto
937b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
938ab49436eSKuninori Morimoto
939d683e408SChancel Liu /* clear the corresponding DAIs parameters when going to be inactive */
940d683e408SChancel Liu for_each_rtd_dais(rtd, i, dai) {
941d683e408SChancel Liu if (snd_soc_dai_active(dai) == 1)
942d683e408SChancel Liu soc_pcm_set_dai_params(dai, NULL);
943d683e408SChancel Liu
944*868eb92bSSrinivas Kandagatla if (snd_soc_dai_stream_active(dai, substream->stream) == 1) {
945*868eb92bSSrinivas Kandagatla if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger)
946d683e408SChancel Liu snd_soc_dai_digital_mute(dai, 1, substream->stream);
947d683e408SChancel Liu }
948*868eb92bSSrinivas Kandagatla }
949d683e408SChancel Liu
950a27b421fSRanjani Sridharan /* run the stream event */
951a27b421fSRanjani Sridharan snd_soc_dapm_stream_stop(rtd, substream->stream);
952a27b421fSRanjani Sridharan
953ab49436eSKuninori Morimoto /* free any machine hw params */
9544662c596SKuninori Morimoto snd_soc_link_hw_free(substream, rollback);
955ab49436eSKuninori Morimoto
956ab49436eSKuninori Morimoto /* free any component resources */
9574662c596SKuninori Morimoto snd_soc_pcm_component_hw_free(substream, rollback);
958ab49436eSKuninori Morimoto
959ab49436eSKuninori Morimoto /* now free hw params for the DAIs */
960121966d0SKuninori Morimoto for_each_rtd_dais(rtd, i, dai)
961121966d0SKuninori Morimoto if (snd_soc_dai_stream_valid(dai, substream->stream))
9624662c596SKuninori Morimoto snd_soc_dai_hw_free(dai, substream, rollback);
963ab49436eSKuninori Morimoto
964ab49436eSKuninori Morimoto return 0;
965ab49436eSKuninori Morimoto }
966ab49436eSKuninori Morimoto
967ab49436eSKuninori Morimoto /*
9684662c596SKuninori Morimoto * Frees resources allocated by hw_params, can be called multiple times
9694662c596SKuninori Morimoto */
__soc_pcm_hw_free(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)970b7898396STakashi Iwai static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
971b7898396STakashi Iwai struct snd_pcm_substream *substream)
972b7898396STakashi Iwai {
973b7898396STakashi Iwai return soc_pcm_hw_clean(rtd, substream, 0);
974b7898396STakashi Iwai }
975b7898396STakashi Iwai
976b7898396STakashi Iwai /* hw_free PCM ops for non-DPCM streams */
soc_pcm_hw_free(struct snd_pcm_substream * substream)9774662c596SKuninori Morimoto static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
9784662c596SKuninori Morimoto {
979b7898396STakashi Iwai struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
980b7898396STakashi Iwai int ret;
981b7898396STakashi Iwai
982b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(rtd);
983b7898396STakashi Iwai ret = __soc_pcm_hw_free(rtd, substream);
984b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(rtd);
985b7898396STakashi Iwai return ret;
9864662c596SKuninori Morimoto }
9874662c596SKuninori Morimoto
9884662c596SKuninori Morimoto /*
989ddee627cSLiam Girdwood * Called by ALSA when the hardware params are set by application. This
990ddee627cSLiam Girdwood * function can also be called multiple times and can allocate buffers
991ddee627cSLiam Girdwood * (using snd_pcm_lib_* ). It's non-atomic.
992ddee627cSLiam Girdwood */
__soc_pcm_hw_params(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)993b7898396STakashi Iwai static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
994b7898396STakashi Iwai struct snd_pcm_substream *substream,
995ddee627cSLiam Girdwood struct snd_pcm_hw_params *params)
996ddee627cSLiam Girdwood {
99719bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai;
9980b7990e3SKuninori Morimoto struct snd_soc_dai *codec_dai;
999396b9079SCharles Keepax struct snd_pcm_hw_params tmp_params;
1000244e2936SCharles Keepax int i, ret = 0;
1001ddee627cSLiam Girdwood
1002b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(rtd);
10035cca5951SShengjiu Wang
10045cca5951SShengjiu Wang ret = soc_pcm_params_symmetry(substream, params);
10055cca5951SShengjiu Wang if (ret)
10065cca5951SShengjiu Wang goto out;
10075cca5951SShengjiu Wang
10087cf3c5b4SKuninori Morimoto ret = snd_soc_link_hw_params(substream, params);
1009a5e6c109SKuninori Morimoto if (ret < 0)
1010ddee627cSLiam Girdwood goto out;
1011ddee627cSLiam Girdwood
1012a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
1013e15ff262SKuninori Morimoto unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
10142e5894d7SBenoit Cousson
1015cde79035SRicard Wanderlof /*
1016cde79035SRicard Wanderlof * Skip CODECs which don't support the current stream type,
1017cde79035SRicard Wanderlof * the idea being that if a CODEC is not used for the currently
1018cde79035SRicard Wanderlof * set up transfer direction, it should not need to be
1019cde79035SRicard Wanderlof * configured, especially since the configuration used might
1020cde79035SRicard Wanderlof * not even be supported by that CODEC. There may be cases
1021cde79035SRicard Wanderlof * however where a CODEC needs to be set up although it is
1022cde79035SRicard Wanderlof * actually not being used for the transfer, e.g. if a
1023cde79035SRicard Wanderlof * capture-only CODEC is acting as an LRCLK and/or BCLK master
1024cde79035SRicard Wanderlof * for the DAI link including a playback-only CODEC.
1025cde79035SRicard Wanderlof * If this becomes necessary, we will have to augment the
1026cde79035SRicard Wanderlof * machine driver setup with information on how to act, so
1027cde79035SRicard Wanderlof * we can do the right thing here.
1028cde79035SRicard Wanderlof */
1029cde79035SRicard Wanderlof if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1030cde79035SRicard Wanderlof continue;
1031cde79035SRicard Wanderlof
10322e5894d7SBenoit Cousson /* copy params for each codec */
1033396b9079SCharles Keepax tmp_params = *params;
10342e5894d7SBenoit Cousson
10352e5894d7SBenoit Cousson /* fixup params based on TDM slot masks */
1036e15ff262SKuninori Morimoto if (tdm_mask)
1037396b9079SCharles Keepax soc_pcm_codec_params_fixup(&tmp_params, tdm_mask);
10382e5894d7SBenoit Cousson
1039aa6166c2SKuninori Morimoto ret = snd_soc_dai_hw_params(codec_dai, substream,
1040396b9079SCharles Keepax &tmp_params);
104193e6958aSBenoit Cousson if(ret < 0)
10424662c596SKuninori Morimoto goto out;
1043ddee627cSLiam Girdwood
1044396b9079SCharles Keepax soc_pcm_set_dai_params(codec_dai, &tmp_params);
1045396b9079SCharles Keepax snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai);
1046ddee627cSLiam Girdwood }
1047ddee627cSLiam Girdwood
1048a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1049ac950278SBard Liao unsigned int ch_mask = 0;
1050ac950278SBard Liao int j;
1051ac950278SBard Liao
10520e9cf4c4SBard Liao /*
10530e9cf4c4SBard Liao * Skip CPUs which don't support the current stream
10540e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details
10550e9cf4c4SBard Liao */
10560e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
10570e9cf4c4SBard Liao continue;
10580e9cf4c4SBard Liao
1059ac950278SBard Liao /* copy params for each cpu */
1060396b9079SCharles Keepax tmp_params = *params;
1061ac950278SBard Liao
1062ac950278SBard Liao if (!rtd->dai_link->codec_ch_maps)
1063ac950278SBard Liao goto hw_params;
1064ac950278SBard Liao /*
1065ac950278SBard Liao * construct cpu channel mask by combining ch_mask of each
1066ac950278SBard Liao * codec which maps to the cpu.
1067ac950278SBard Liao */
1068ac950278SBard Liao for_each_rtd_codec_dais(rtd, j, codec_dai) {
1069ac950278SBard Liao if (rtd->dai_link->codec_ch_maps[j].connected_cpu_id == i)
1070ac950278SBard Liao ch_mask |= rtd->dai_link->codec_ch_maps[j].ch_mask;
1071ac950278SBard Liao }
1072ac950278SBard Liao
1073ac950278SBard Liao /* fixup cpu channel number */
1074ac950278SBard Liao if (ch_mask)
1075396b9079SCharles Keepax soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
1076ac950278SBard Liao
1077ac950278SBard Liao hw_params:
1078396b9079SCharles Keepax ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params);
107993e6958aSBenoit Cousson if (ret < 0)
10804662c596SKuninori Morimoto goto out;
1081ddee627cSLiam Girdwood
108219bdcc7aSShreyas NC /* store the parameters for each DAI */
1083396b9079SCharles Keepax soc_pcm_set_dai_params(cpu_dai, &tmp_params);
1084396b9079SCharles Keepax snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai);
108519bdcc7aSShreyas NC }
1086ca58221dSKuninori Morimoto
10873a36a64aSKuninori Morimoto ret = snd_soc_pcm_component_hw_params(substream, params);
1088ddee627cSLiam Girdwood out:
108904110728SKuninori Morimoto if (ret < 0)
1090b7898396STakashi Iwai soc_pcm_hw_clean(rtd, substream, 1);
1091b8135864SKuninori Morimoto
109204110728SKuninori Morimoto return soc_pcm_ret(rtd, ret);
1093ddee627cSLiam Girdwood }
1094ddee627cSLiam Girdwood
1095b7898396STakashi Iwai /* hw_params PCM ops for non-DPCM streams */
soc_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1096b7898396STakashi Iwai static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1097b7898396STakashi Iwai struct snd_pcm_hw_params *params)
1098b7898396STakashi Iwai {
1099b7898396STakashi Iwai struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1100b7898396STakashi Iwai int ret;
1101b7898396STakashi Iwai
1102b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(rtd);
1103b7898396STakashi Iwai ret = __soc_pcm_hw_params(rtd, substream, params);
1104b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(rtd);
1105b7898396STakashi Iwai return ret;
1106b7898396STakashi Iwai }
1107b7898396STakashi Iwai
1108356caf66SKuninori Morimoto #define TRIGGER_MAX 3
1109356caf66SKuninori Morimoto static int (* const trigger[][TRIGGER_MAX])(struct snd_pcm_substream *substream, int cmd, int rollback) = {
1110356caf66SKuninori Morimoto [SND_SOC_TRIGGER_ORDER_DEFAULT] = {
1111356caf66SKuninori Morimoto snd_soc_link_trigger,
1112356caf66SKuninori Morimoto snd_soc_pcm_component_trigger,
1113356caf66SKuninori Morimoto snd_soc_pcm_dai_trigger,
1114356caf66SKuninori Morimoto },
1115356caf66SKuninori Morimoto [SND_SOC_TRIGGER_ORDER_LDC] = {
1116356caf66SKuninori Morimoto snd_soc_link_trigger,
1117356caf66SKuninori Morimoto snd_soc_pcm_dai_trigger,
1118356caf66SKuninori Morimoto snd_soc_pcm_component_trigger,
1119356caf66SKuninori Morimoto },
1120356caf66SKuninori Morimoto };
1121356caf66SKuninori Morimoto
soc_pcm_trigger(struct snd_pcm_substream * substream,int cmd)11224378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
11234378f1fbSPeter Ujfalusi {
112459dd33f8SVijendar Mukunda struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
112554fc4b72SClaudiu Beznea struct snd_soc_component *component;
1126356caf66SKuninori Morimoto int ret = 0, r = 0, i;
11276374f493SKuninori Morimoto int rollback = 0;
1128356caf66SKuninori Morimoto int start = 0, stop = 0;
11294378f1fbSPeter Ujfalusi
1130356caf66SKuninori Morimoto /*
1131356caf66SKuninori Morimoto * select START/STOP sequence
1132356caf66SKuninori Morimoto */
1133356caf66SKuninori Morimoto for_each_rtd_components(rtd, i, component) {
1134356caf66SKuninori Morimoto if (component->driver->trigger_start)
1135356caf66SKuninori Morimoto start = component->driver->trigger_start;
1136356caf66SKuninori Morimoto if (component->driver->trigger_stop)
1137356caf66SKuninori Morimoto stop = component->driver->trigger_stop;
1138356caf66SKuninori Morimoto }
1139356caf66SKuninori Morimoto if (rtd->dai_link->trigger_start)
1140356caf66SKuninori Morimoto start = rtd->dai_link->trigger_start;
1141356caf66SKuninori Morimoto if (rtd->dai_link->trigger_stop)
1142356caf66SKuninori Morimoto stop = rtd->dai_link->trigger_stop;
1143356caf66SKuninori Morimoto
1144356caf66SKuninori Morimoto if (start < 0 || start >= SND_SOC_TRIGGER_ORDER_MAX ||
1145356caf66SKuninori Morimoto stop < 0 || stop >= SND_SOC_TRIGGER_ORDER_MAX)
1146356caf66SKuninori Morimoto return -EINVAL;
1147356caf66SKuninori Morimoto
1148356caf66SKuninori Morimoto /*
1149356caf66SKuninori Morimoto * START
1150356caf66SKuninori Morimoto */
11514378f1fbSPeter Ujfalusi switch (cmd) {
11524378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_START:
11534378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_RESUME:
11544378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1155356caf66SKuninori Morimoto for (i = 0; i < TRIGGER_MAX; i++) {
1156356caf66SKuninori Morimoto r = trigger[start][i](substream, cmd, 0);
1157356caf66SKuninori Morimoto if (r < 0)
115854fc4b72SClaudiu Beznea break;
115954fc4b72SClaudiu Beznea }
116054fc4b72SClaudiu Beznea }
116154fc4b72SClaudiu Beznea
1162356caf66SKuninori Morimoto /*
1163356caf66SKuninori Morimoto * Rollback if START failed
1164356caf66SKuninori Morimoto * find correspond STOP command
1165356caf66SKuninori Morimoto */
1166356caf66SKuninori Morimoto if (r < 0) {
11676374f493SKuninori Morimoto rollback = 1;
1168356caf66SKuninori Morimoto ret = r;
11696374f493SKuninori Morimoto switch (cmd) {
11706374f493SKuninori Morimoto case SNDRV_PCM_TRIGGER_START:
11716374f493SKuninori Morimoto cmd = SNDRV_PCM_TRIGGER_STOP;
11724378f1fbSPeter Ujfalusi break;
11736374f493SKuninori Morimoto case SNDRV_PCM_TRIGGER_RESUME:
11746374f493SKuninori Morimoto cmd = SNDRV_PCM_TRIGGER_SUSPEND;
11756374f493SKuninori Morimoto break;
11766374f493SKuninori Morimoto case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11776374f493SKuninori Morimoto cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
11786374f493SKuninori Morimoto break;
11796374f493SKuninori Morimoto }
11806374f493SKuninori Morimoto }
11816374f493SKuninori Morimoto
1182356caf66SKuninori Morimoto /*
1183356caf66SKuninori Morimoto * STOP
1184356caf66SKuninori Morimoto */
11856374f493SKuninori Morimoto switch (cmd) {
11864378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_STOP:
11874378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_SUSPEND:
11884378f1fbSPeter Ujfalusi case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1189356caf66SKuninori Morimoto for (i = TRIGGER_MAX; i > 0; i--) {
1190356caf66SKuninori Morimoto r = trigger[stop][i - 1](substream, cmd, rollback);
1191356caf66SKuninori Morimoto if (r < 0)
1192356caf66SKuninori Morimoto ret = r;
119359dd33f8SVijendar Mukunda }
11944378f1fbSPeter Ujfalusi }
11954378f1fbSPeter Ujfalusi
11964378f1fbSPeter Ujfalusi return ret;
11974378f1fbSPeter Ujfalusi }
11984378f1fbSPeter Ujfalusi
1199ddee627cSLiam Girdwood /*
1200ddee627cSLiam Girdwood * soc level wrapper for pointer callback
1201ef050becSCharles Keepax * If cpu_dai, codec_dai, component driver has the delay callback, then
1202dd894f4cSKuninori Morimoto * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1203ddee627cSLiam Girdwood */
soc_pcm_pointer(struct snd_pcm_substream * substream)1204ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1205ddee627cSLiam Girdwood {
1206ddee627cSLiam Girdwood struct snd_pcm_runtime *runtime = substream->runtime;
1207ddee627cSLiam Girdwood snd_pcm_uframes_t offset = 0;
12082e5894d7SBenoit Cousson snd_pcm_sframes_t codec_delay = 0;
120919bdcc7aSShreyas NC snd_pcm_sframes_t cpu_delay = 0;
1210ddee627cSLiam Girdwood
12110035e256SKuninori Morimoto offset = snd_soc_pcm_component_pointer(substream);
1212b8135864SKuninori Morimoto
1213403f830eSKuninori Morimoto /* should be called *after* snd_soc_pcm_component_pointer() */
12148544f08cSKuninori Morimoto snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1215403f830eSKuninori Morimoto snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1216ddee627cSLiam Girdwood
1217dd894f4cSKuninori Morimoto runtime->delay = cpu_delay + codec_delay;
1218ddee627cSLiam Girdwood
1219ddee627cSLiam Girdwood return offset;
1220ddee627cSLiam Girdwood }
1221ddee627cSLiam Girdwood
122201d7584cSLiam Girdwood /* connect a FE and BE */
dpcm_be_connect(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)122301d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
122401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream)
122501d7584cSLiam Girdwood {
1226bbf7d3b1SPierre-Louis Bossart struct snd_pcm_substream *fe_substream;
1227bbf7d3b1SPierre-Louis Bossart struct snd_pcm_substream *be_substream;
122801d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
1229b7898396STakashi Iwai
1230b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(fe);
123101d7584cSLiam Girdwood
123201d7584cSLiam Girdwood /* only add new dpcms */
12338d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
123401d7584cSLiam Girdwood if (dpcm->be == be && dpcm->fe == fe)
123501d7584cSLiam Girdwood return 0;
123601d7584cSLiam Girdwood }
123701d7584cSLiam Girdwood
1238bbf7d3b1SPierre-Louis Bossart fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1239bbf7d3b1SPierre-Louis Bossart be_substream = snd_soc_dpcm_get_substream(be, stream);
1240bbf7d3b1SPierre-Louis Bossart
1241bbf7d3b1SPierre-Louis Bossart if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1242bbf7d3b1SPierre-Louis Bossart dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1243bbf7d3b1SPierre-Louis Bossart __func__);
1244bbf7d3b1SPierre-Louis Bossart return -EINVAL;
1245bbf7d3b1SPierre-Louis Bossart }
1246bbf7d3b1SPierre-Louis Bossart if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
12474ccf0949SPierre-Louis Bossart dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1248bbf7d3b1SPierre-Louis Bossart be_substream->pcm->nonatomic = 1;
1249bbf7d3b1SPierre-Louis Bossart }
1250bbf7d3b1SPierre-Louis Bossart
1251fb6d679fSChristophe JAILLET dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
125201d7584cSLiam Girdwood if (!dpcm)
125301d7584cSLiam Girdwood return -ENOMEM;
125401d7584cSLiam Girdwood
125501d7584cSLiam Girdwood dpcm->be = be;
125601d7584cSLiam Girdwood dpcm->fe = fe;
125701d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1258b7898396STakashi Iwai snd_soc_dpcm_stream_lock_irq(fe, stream);
125901d7584cSLiam Girdwood list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
126001d7584cSLiam Girdwood list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1261b7898396STakashi Iwai snd_soc_dpcm_stream_unlock_irq(fe, stream);
126201d7584cSLiam Girdwood
126301d7584cSLiam Girdwood dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
126401d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name,
126501d7584cSLiam Girdwood stream ? "<-" : "->", be->dai_link->name);
126601d7584cSLiam Girdwood
1267154dae87SKuninori Morimoto dpcm_create_debugfs_state(dpcm, stream);
1268154dae87SKuninori Morimoto
126901d7584cSLiam Girdwood return 1;
127001d7584cSLiam Girdwood }
127101d7584cSLiam Girdwood
127201d7584cSLiam Girdwood /* reparent a BE onto another FE */
dpcm_be_reparent(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)127301d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
127401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream)
127501d7584cSLiam Girdwood {
127601d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
127701d7584cSLiam Girdwood struct snd_pcm_substream *fe_substream, *be_substream;
127801d7584cSLiam Girdwood
127901d7584cSLiam Girdwood /* reparent if BE is connected to other FEs */
128001d7584cSLiam Girdwood if (!be->dpcm[stream].users)
128101d7584cSLiam Girdwood return;
128201d7584cSLiam Girdwood
128301d7584cSLiam Girdwood be_substream = snd_soc_dpcm_get_substream(be, stream);
1284db8f91d4SSrinivasa Rao Mandadapu if (!be_substream)
1285db8f91d4SSrinivasa Rao Mandadapu return;
128601d7584cSLiam Girdwood
1287d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) {
128801d7584cSLiam Girdwood if (dpcm->fe == fe)
128901d7584cSLiam Girdwood continue;
129001d7584cSLiam Girdwood
129101d7584cSLiam Girdwood dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
129201d7584cSLiam Girdwood stream ? "capture" : "playback",
129301d7584cSLiam Girdwood dpcm->fe->dai_link->name,
129401d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name);
129501d7584cSLiam Girdwood
129601d7584cSLiam Girdwood fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
129701d7584cSLiam Girdwood be_substream->runtime = fe_substream->runtime;
129801d7584cSLiam Girdwood break;
129901d7584cSLiam Girdwood }
130001d7584cSLiam Girdwood }
130101d7584cSLiam Girdwood
130201d7584cSLiam Girdwood /* disconnect a BE and FE */
dpcm_be_disconnect(struct snd_soc_pcm_runtime * fe,int stream)130323607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
130401d7584cSLiam Girdwood {
130501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm, *d;
13069f620684STakashi Iwai LIST_HEAD(deleted_dpcms);
130701d7584cSLiam Girdwood
1308b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(fe);
1309b7898396STakashi Iwai
1310b7898396STakashi Iwai snd_soc_dpcm_stream_lock_irq(fe, stream);
13118d6258a4SKuninori Morimoto for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1312103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
131301d7584cSLiam Girdwood stream ? "capture" : "playback",
131401d7584cSLiam Girdwood dpcm->be->dai_link->name);
131501d7584cSLiam Girdwood
131601d7584cSLiam Girdwood if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
131701d7584cSLiam Girdwood continue;
131801d7584cSLiam Girdwood
131901d7584cSLiam Girdwood dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
132001d7584cSLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name,
132101d7584cSLiam Girdwood stream ? "<-" : "->", dpcm->be->dai_link->name);
132201d7584cSLiam Girdwood
132301d7584cSLiam Girdwood /* BEs still alive need new FE */
132401d7584cSLiam Girdwood dpcm_be_reparent(fe, dpcm->be, stream);
132501d7584cSLiam Girdwood
132601d7584cSLiam Girdwood list_del(&dpcm->list_be);
13279f620684STakashi Iwai list_move(&dpcm->list_fe, &deleted_dpcms);
132801d7584cSLiam Girdwood }
1329b7898396STakashi Iwai snd_soc_dpcm_stream_unlock_irq(fe, stream);
13309f620684STakashi Iwai
13319f620684STakashi Iwai while (!list_empty(&deleted_dpcms)) {
13329f620684STakashi Iwai dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
13339f620684STakashi Iwai list_fe);
13349f620684STakashi Iwai list_del(&dpcm->list_fe);
13359f620684STakashi Iwai dpcm_remove_debugfs_state(dpcm);
13369f620684STakashi Iwai kfree(dpcm);
13379f620684STakashi Iwai }
133801d7584cSLiam Girdwood }
133901d7584cSLiam Girdwood
134001d7584cSLiam Girdwood /* get BE for DAI widget and stream */
dpcm_get_be(struct snd_soc_card * card,struct snd_soc_dapm_widget * widget,int stream)134101d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
134201d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget, int stream)
134301d7584cSLiam Girdwood {
134401d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be;
134593597faeSKuninori Morimoto struct snd_soc_dapm_widget *w;
13467afecb30SKuninori Morimoto struct snd_soc_dai *dai;
13471a497983SMengdong Lin int i;
134801d7584cSLiam Girdwood
13493c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
13503c146465SLiam Girdwood
1351bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, be) {
135201d7584cSLiam Girdwood
135335ea0655SLiam Girdwood if (!be->dai_link->no_pcm)
135435ea0655SLiam Girdwood continue;
135535ea0655SLiam Girdwood
135675459065STakashi Iwai if (!snd_soc_dpcm_get_substream(be, stream))
135775459065STakashi Iwai continue;
135875459065STakashi Iwai
1359c840f769SKuninori Morimoto for_each_rtd_dais(be, i, dai) {
136019bdcc7aSShreyas NC w = snd_soc_dai_get_widget(dai, stream);
136193597faeSKuninori Morimoto
13623c146465SLiam Girdwood dev_dbg(card->dev, "ASoC: try BE : %s\n",
136393597faeSKuninori Morimoto w ? w->name : "(not set)");
13643c146465SLiam Girdwood
136593597faeSKuninori Morimoto if (w == widget)
136601d7584cSLiam Girdwood return be;
136719bdcc7aSShreyas NC }
136801d7584cSLiam Girdwood }
136901d7584cSLiam Girdwood
13709d6ee365SJerome Brunet /* Widget provided is not a BE */
137101d7584cSLiam Girdwood return NULL;
137201d7584cSLiam Girdwood }
137301d7584cSLiam Girdwood
widget_in_list(struct snd_soc_dapm_widget_list * list,struct snd_soc_dapm_widget * widget)13745edcf2a3SRanjani Sridharan int widget_in_list(struct snd_soc_dapm_widget_list *list,
137501d7584cSLiam Girdwood struct snd_soc_dapm_widget *widget)
137601d7584cSLiam Girdwood {
137709e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *w;
137801d7584cSLiam Girdwood int i;
137901d7584cSLiam Girdwood
138009e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, w)
138109e88f8aSKuninori Morimoto if (widget == w)
138201d7584cSLiam Girdwood return 1;
138301d7584cSLiam Girdwood
138401d7584cSLiam Girdwood return 0;
138501d7584cSLiam Girdwood }
13865edcf2a3SRanjani Sridharan EXPORT_SYMBOL_GPL(widget_in_list);
138701d7584cSLiam Girdwood
dpcm_end_walk_at_be(struct snd_soc_dapm_widget * widget,enum snd_soc_dapm_direction dir)1388d1a7af09SRanjani Sridharan bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
13895fdd022cSPiotr Stankiewicz {
13905fdd022cSPiotr Stankiewicz struct snd_soc_card *card = widget->dapm->card;
13915fdd022cSPiotr Stankiewicz struct snd_soc_pcm_runtime *rtd;
1392c2cd8216SKuninori Morimoto int stream;
13935fdd022cSPiotr Stankiewicz
1394c2cd8216SKuninori Morimoto /* adjust dir to stream */
1395c2cd8216SKuninori Morimoto if (dir == SND_SOC_DAPM_DIR_OUT)
1396c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK;
1397c2cd8216SKuninori Morimoto else
1398c2cd8216SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE;
1399c2cd8216SKuninori Morimoto
1400027a4838SKuninori Morimoto rtd = dpcm_get_be(card, widget, stream);
1401027a4838SKuninori Morimoto if (rtd)
14025fdd022cSPiotr Stankiewicz return true;
14035fdd022cSPiotr Stankiewicz
14045fdd022cSPiotr Stankiewicz return false;
14055fdd022cSPiotr Stankiewicz }
1406d1a7af09SRanjani Sridharan EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
14075fdd022cSPiotr Stankiewicz
dpcm_path_get(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list)140823607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
14091ce43acfSLars-Peter Clausen int stream, struct snd_soc_dapm_widget_list **list)
141001d7584cSLiam Girdwood {
1411c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
141201d7584cSLiam Girdwood int paths;
141301d7584cSLiam Girdwood
14143989ade2SKuninori Morimoto if (fe->dai_link->num_cpus > 1) {
14156e1276a5SBard Liao dev_err(fe->dev,
14166e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__);
14176e1276a5SBard Liao return -EINVAL;
14186e1276a5SBard Liao }
14196e1276a5SBard Liao
142001d7584cSLiam Girdwood /* get number of valid DAI paths and their widgets */
14216742064aSPiotr Stankiewicz paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1422aa293777SSameer Pujar fe->card->component_chaining ?
1423aa293777SSameer Pujar NULL : dpcm_end_walk_at_be);
142401d7584cSLiam Girdwood
1425d479f00bSKuninori Morimoto if (paths > 0)
1426103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
142701d7584cSLiam Girdwood stream ? "capture" : "playback");
1428d479f00bSKuninori Morimoto else if (paths == 0)
1429d479f00bSKuninori Morimoto dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1430d479f00bSKuninori Morimoto stream ? "capture" : "playback");
143101d7584cSLiam Girdwood
143201d7584cSLiam Girdwood return paths;
143301d7584cSLiam Girdwood }
143401d7584cSLiam Girdwood
dpcm_path_put(struct snd_soc_dapm_widget_list ** list)143552645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
143652645e33SKuninori Morimoto {
143752645e33SKuninori Morimoto snd_soc_dapm_dai_free_widgets(list);
143852645e33SKuninori Morimoto }
143952645e33SKuninori Morimoto
dpcm_be_is_active(struct snd_soc_dpcm * dpcm,int stream,struct snd_soc_dapm_widget_list * list)14408cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
14418cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list *list)
144201d7584cSLiam Girdwood {
14437afecb30SKuninori Morimoto struct snd_soc_dai *dai;
14442e5894d7SBenoit Cousson unsigned int i;
144501d7584cSLiam Girdwood
1446c840f769SKuninori Morimoto /* is there a valid DAI widget for this BE */
1447c840f769SKuninori Morimoto for_each_rtd_dais(dpcm->be, i, dai) {
14487931df9bSKuninori Morimoto struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
144901d7584cSLiam Girdwood
145019bdcc7aSShreyas NC /*
1451c840f769SKuninori Morimoto * The BE is pruned only if none of the dai
145219bdcc7aSShreyas NC * widgets are in the active list.
145319bdcc7aSShreyas NC */
145401d7584cSLiam Girdwood if (widget && widget_in_list(list, widget))
14558cce6569SGuennadi Liakhovetski return true;
145619bdcc7aSShreyas NC }
145701d7584cSLiam Girdwood
14588cce6569SGuennadi Liakhovetski return false;
14598cce6569SGuennadi Liakhovetski }
14608cce6569SGuennadi Liakhovetski
dpcm_prune_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)14618cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
14628cce6569SGuennadi Liakhovetski struct snd_soc_dapm_widget_list **list_)
14638cce6569SGuennadi Liakhovetski {
14648cce6569SGuennadi Liakhovetski struct snd_soc_dpcm *dpcm;
14658cce6569SGuennadi Liakhovetski int prune = 0;
14668cce6569SGuennadi Liakhovetski
14678cce6569SGuennadi Liakhovetski /* Destroy any old FE <--> BE connections */
14688cce6569SGuennadi Liakhovetski for_each_dpcm_be(fe, stream, dpcm) {
14698cce6569SGuennadi Liakhovetski if (dpcm_be_is_active(dpcm, stream, *list_))
1470bed646dcSKuninori Morimoto continue;
147101d7584cSLiam Girdwood
1472103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
147301d7584cSLiam Girdwood stream ? "capture" : "playback",
147401d7584cSLiam Girdwood dpcm->be->dai_link->name, fe->dai_link->name);
147501d7584cSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1476a7e20444SKuninori Morimoto dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
147701d7584cSLiam Girdwood prune++;
147801d7584cSLiam Girdwood }
147901d7584cSLiam Girdwood
1480103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
148101d7584cSLiam Girdwood return prune;
148201d7584cSLiam Girdwood }
148301d7584cSLiam Girdwood
dpcm_add_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)148401d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
148501d7584cSLiam Girdwood struct snd_soc_dapm_widget_list **list_)
148601d7584cSLiam Girdwood {
148701d7584cSLiam Girdwood struct snd_soc_card *card = fe->card;
148801d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list = *list_;
148901d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be;
149009e88f8aSKuninori Morimoto struct snd_soc_dapm_widget *widget;
14910d3a5178SKuninori Morimoto struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
149201d7584cSLiam Girdwood int i, new = 0, err;
149301d7584cSLiam Girdwood
14946932b20dSKuninori Morimoto /* don't connect if FE is not running */
14950d3a5178SKuninori Morimoto if (!fe_substream->runtime && !fe->fe_compr)
14966932b20dSKuninori Morimoto return new;
14976932b20dSKuninori Morimoto
149801d7584cSLiam Girdwood /* Create any new FE <--> BE connections */
149909e88f8aSKuninori Morimoto for_each_dapm_widgets(list, i, widget) {
150001d7584cSLiam Girdwood
150109e88f8aSKuninori Morimoto switch (widget->id) {
15024616274dSMark Brown case snd_soc_dapm_dai_in:
1503c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1504c5b8540dSKoro Chen continue;
1505c5b8540dSKoro Chen break;
15064616274dSMark Brown case snd_soc_dapm_dai_out:
1507c5b8540dSKoro Chen if (stream != SNDRV_PCM_STREAM_CAPTURE)
1508c5b8540dSKoro Chen continue;
15094616274dSMark Brown break;
15104616274dSMark Brown default:
151101d7584cSLiam Girdwood continue;
15124616274dSMark Brown }
151301d7584cSLiam Girdwood
151401d7584cSLiam Girdwood /* is there a valid BE rtd for this widget */
151509e88f8aSKuninori Morimoto be = dpcm_get_be(card, widget, stream);
151601d7584cSLiam Girdwood if (!be) {
1517b6eabd24SShengjiu Wang dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
151809e88f8aSKuninori Morimoto widget->name);
151901d7584cSLiam Girdwood continue;
152001d7584cSLiam Girdwood }
152101d7584cSLiam Girdwood
15229609cfcdSPierre-Louis Bossart /*
15239609cfcdSPierre-Louis Bossart * Filter for systems with 'component_chaining' enabled.
15249609cfcdSPierre-Louis Bossart * This helps to avoid unnecessary re-configuration of an
15259609cfcdSPierre-Louis Bossart * already active BE on such systems.
15269609cfcdSPierre-Louis Bossart */
15279609cfcdSPierre-Louis Bossart if (fe->card->component_chaining &&
15289609cfcdSPierre-Louis Bossart (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
15290c25db3fSSameer Pujar (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
15300c25db3fSSameer Pujar continue;
15310c25db3fSSameer Pujar
153201d7584cSLiam Girdwood /* newly connected FE and BE */
153301d7584cSLiam Girdwood err = dpcm_be_connect(fe, be, stream);
153401d7584cSLiam Girdwood if (err < 0) {
1535103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: can't connect %s\n",
153609e88f8aSKuninori Morimoto widget->name);
153701d7584cSLiam Girdwood break;
153801d7584cSLiam Girdwood } else if (err == 0) /* already connected */
153901d7584cSLiam Girdwood continue;
154001d7584cSLiam Girdwood
154101d7584cSLiam Girdwood /* new */
1542a7e20444SKuninori Morimoto dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
154301d7584cSLiam Girdwood new++;
154401d7584cSLiam Girdwood }
154501d7584cSLiam Girdwood
1546103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
154701d7584cSLiam Girdwood return new;
154801d7584cSLiam Girdwood }
154901d7584cSLiam Girdwood
155001d7584cSLiam Girdwood /*
155101d7584cSLiam Girdwood * Find the corresponding BE DAIs that source or sink audio to this
155201d7584cSLiam Girdwood * FE substream.
155301d7584cSLiam Girdwood */
dpcm_process_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list,int new)155423607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
155501d7584cSLiam Girdwood int stream, struct snd_soc_dapm_widget_list **list, int new)
155601d7584cSLiam Girdwood {
155701d7584cSLiam Girdwood if (new)
155801d7584cSLiam Girdwood return dpcm_add_paths(fe, stream, list);
155901d7584cSLiam Girdwood else
156001d7584cSLiam Girdwood return dpcm_prune_paths(fe, stream, list);
156101d7584cSLiam Girdwood }
156201d7584cSLiam Girdwood
dpcm_clear_pending_state(struct snd_soc_pcm_runtime * fe,int stream)156323607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
156401d7584cSLiam Girdwood {
156501d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
156601d7584cSLiam Girdwood
15678d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm)
1568a7e20444SKuninori Morimoto dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
156901d7584cSLiam Girdwood }
157001d7584cSLiam Girdwood
dpcm_be_dai_stop(struct snd_soc_pcm_runtime * fe,int stream,int do_hw_free,struct snd_soc_dpcm * last)1571531590bbSKuninori Morimoto void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1572531590bbSKuninori Morimoto int do_hw_free, struct snd_soc_dpcm *last)
157301d7584cSLiam Girdwood {
157401d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
157501d7584cSLiam Girdwood
157601d7584cSLiam Girdwood /* disable any enabled and non active backends */
15778d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
157801d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be;
157901d7584cSLiam Girdwood struct snd_pcm_substream *be_substream =
158001d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream);
158101d7584cSLiam Girdwood
1582531590bbSKuninori Morimoto if (dpcm == last)
1583531590bbSKuninori Morimoto return;
1584531590bbSKuninori Morimoto
1585531590bbSKuninori Morimoto /* is this op for this BE ? */
1586531590bbSKuninori Morimoto if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1587531590bbSKuninori Morimoto continue;
1588531590bbSKuninori Morimoto
15891db19c15SKuninori Morimoto 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);
15931db19c15SKuninori Morimoto continue;
15941db19c15SKuninori Morimoto }
159501d7584cSLiam Girdwood
159601d7584cSLiam Girdwood if (--be->dpcm[stream].users != 0)
159701d7584cSLiam Girdwood continue;
159801d7584cSLiam Girdwood
1599531590bbSKuninori Morimoto if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1600531590bbSKuninori Morimoto if (!do_hw_free)
160101d7584cSLiam Girdwood continue;
160201d7584cSLiam Girdwood
1603531590bbSKuninori Morimoto if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1604b7898396STakashi Iwai __soc_pcm_hw_free(be, be_substream);
1605531590bbSKuninori Morimoto be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1606531590bbSKuninori Morimoto }
1607531590bbSKuninori Morimoto }
1608531590bbSKuninori Morimoto
1609b7898396STakashi Iwai __soc_pcm_close(be, be_substream);
161001d7584cSLiam Girdwood be_substream->runtime = NULL;
161101d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
161201d7584cSLiam Girdwood }
161301d7584cSLiam Girdwood }
161401d7584cSLiam Girdwood
dpcm_be_dai_startup(struct snd_soc_pcm_runtime * fe,int stream)161523607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
161601d7584cSLiam Girdwood {
16170d3a5178SKuninori Morimoto struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
161806aaeb87SKuninori Morimoto struct snd_soc_pcm_runtime *be;
161901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
162001d7584cSLiam Girdwood int err, count = 0;
162101d7584cSLiam Girdwood
162201d7584cSLiam Girdwood /* only startup BE DAIs that are either sinks or sources to this FE DAI */
16238d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
162406aaeb87SKuninori Morimoto struct snd_pcm_substream *be_substream;
162501d7584cSLiam Girdwood
162606aaeb87SKuninori Morimoto be = dpcm->be;
162706aaeb87SKuninori Morimoto be_substream = snd_soc_dpcm_get_substream(be, stream);
162801d7584cSLiam Girdwood
16292062b4c5SRussell King - ARM Linux if (!be_substream) {
16302062b4c5SRussell King - ARM Linux dev_err(be->dev, "ASoC: no backend %s stream\n",
16312062b4c5SRussell King - ARM Linux stream ? "capture" : "playback");
16322062b4c5SRussell King - ARM Linux continue;
16332062b4c5SRussell King - ARM Linux }
16342062b4c5SRussell King - ARM Linux
163501d7584cSLiam Girdwood /* is this op for this BE ? */
163601d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
163701d7584cSLiam Girdwood continue;
163801d7584cSLiam Girdwood
163901d7584cSLiam Girdwood /* first time the dpcm is open ? */
16401db19c15SKuninori Morimoto if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1641103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: too many users %s at open %d\n",
164201d7584cSLiam Girdwood stream ? "capture" : "playback",
164301d7584cSLiam Girdwood be->dpcm[stream].state);
16441db19c15SKuninori Morimoto continue;
16451db19c15SKuninori Morimoto }
164601d7584cSLiam Girdwood
164701d7584cSLiam Girdwood if (be->dpcm[stream].users++ != 0)
164801d7584cSLiam Girdwood continue;
164901d7584cSLiam Girdwood
165001d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
165101d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
165201d7584cSLiam Girdwood continue;
165301d7584cSLiam Girdwood
16542062b4c5SRussell King - ARM Linux dev_dbg(be->dev, "ASoC: open %s BE %s\n",
16552062b4c5SRussell King - ARM Linux stream ? "capture" : "playback", be->dai_link->name);
165601d7584cSLiam Girdwood
16570d3a5178SKuninori Morimoto be_substream->runtime = fe_substream->runtime;
1658b7898396STakashi Iwai err = __soc_pcm_open(be, be_substream);
165901d7584cSLiam Girdwood if (err < 0) {
166001d7584cSLiam Girdwood be->dpcm[stream].users--;
166101d7584cSLiam Girdwood if (be->dpcm[stream].users < 0)
1662103d84a3SLiam Girdwood dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
166301d7584cSLiam Girdwood stream ? "capture" : "playback",
166401d7584cSLiam Girdwood be->dpcm[stream].state);
166501d7584cSLiam Girdwood
166601d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
166701d7584cSLiam Girdwood goto unwind;
166801d7584cSLiam Girdwood }
1669848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start = 0;
167001d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
167101d7584cSLiam Girdwood count++;
167201d7584cSLiam Girdwood }
167301d7584cSLiam Girdwood
167401d7584cSLiam Girdwood return count;
167501d7584cSLiam Girdwood
167601d7584cSLiam Girdwood unwind:
1677531590bbSKuninori Morimoto dpcm_be_dai_startup_rollback(fe, stream, dpcm);
167801d7584cSLiam Girdwood
167904110728SKuninori Morimoto return soc_pcm_ret(fe, err);
168001d7584cSLiam Girdwood }
168101d7584cSLiam Girdwood
dpcm_runtime_setup_fe(struct snd_pcm_substream * substream)16825f53898aSKuninori Morimoto static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
16835f53898aSKuninori Morimoto {
16845f53898aSKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
16855f53898aSKuninori Morimoto struct snd_pcm_runtime *runtime = substream->runtime;
16865f53898aSKuninori Morimoto struct snd_pcm_hardware *hw = &runtime->hw;
16875f53898aSKuninori Morimoto struct snd_soc_dai *dai;
16885f53898aSKuninori Morimoto int stream = substream->stream;
1689083a25b1SShengjiu Wang u64 formats = hw->formats;
16905f53898aSKuninori Morimoto int i;
16915f53898aSKuninori Morimoto
16925f53898aSKuninori Morimoto soc_pcm_hw_init(hw);
16935f53898aSKuninori Morimoto
1694083a25b1SShengjiu Wang if (formats)
1695083a25b1SShengjiu Wang hw->formats &= formats;
1696083a25b1SShengjiu Wang
16975f53898aSKuninori Morimoto for_each_rtd_cpu_dais(fe, i, dai) {
16985f53898aSKuninori Morimoto struct snd_soc_pcm_stream *cpu_stream;
16995f53898aSKuninori Morimoto
17005f53898aSKuninori Morimoto /*
17015f53898aSKuninori Morimoto * Skip CPUs which don't support the current stream
17025f53898aSKuninori Morimoto * type. See soc_pcm_init_runtime_hw() for more details
17035f53898aSKuninori Morimoto */
17045f53898aSKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream))
17055f53898aSKuninori Morimoto continue;
17065f53898aSKuninori Morimoto
17075f53898aSKuninori Morimoto cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
17085f53898aSKuninori Morimoto
17095f53898aSKuninori Morimoto soc_pcm_hw_update_rate(hw, cpu_stream);
17105f53898aSKuninori Morimoto soc_pcm_hw_update_chan(hw, cpu_stream);
17115f53898aSKuninori Morimoto soc_pcm_hw_update_format(hw, cpu_stream);
17125f53898aSKuninori Morimoto }
17135f53898aSKuninori Morimoto
17145f53898aSKuninori Morimoto }
17155f53898aSKuninori Morimoto
dpcm_runtime_setup_be_format(struct snd_pcm_substream * substream)17161b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1717b073ed4eSKuninori Morimoto {
17180ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17191b8cb123SKuninori Morimoto struct snd_pcm_runtime *runtime = substream->runtime;
17204b260f42SKuninori Morimoto struct snd_pcm_hardware *hw = &runtime->hw;
1721b073ed4eSKuninori Morimoto struct snd_soc_dpcm *dpcm;
17227afecb30SKuninori Morimoto struct snd_soc_dai *dai;
1723b073ed4eSKuninori Morimoto int stream = substream->stream;
1724b073ed4eSKuninori Morimoto
1725b073ed4eSKuninori Morimoto if (!fe->dai_link->dpcm_merged_format)
1726435ffb76SJerome Brunet return;
1727b073ed4eSKuninori Morimoto
1728b073ed4eSKuninori Morimoto /*
1729b073ed4eSKuninori Morimoto * It returns merged BE codec format
1730b073ed4eSKuninori Morimoto * if FE want to use it (= dpcm_merged_format)
1731b073ed4eSKuninori Morimoto */
1732b073ed4eSKuninori Morimoto
17338d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
1734b073ed4eSKuninori Morimoto struct snd_soc_pcm_runtime *be = dpcm->be;
1735b073ed4eSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream;
1736b073ed4eSKuninori Morimoto int i;
1737b073ed4eSKuninori Morimoto
1738a4be4187SKuninori Morimoto for_each_rtd_codec_dais(be, i, dai) {
17394febced1SJerome Brunet /*
17404febced1SJerome Brunet * Skip CODECs which don't support the current stream
17414febced1SJerome Brunet * type. See soc_pcm_init_runtime_hw() for more details
17424febced1SJerome Brunet */
17437afecb30SKuninori Morimoto if (!snd_soc_dai_stream_valid(dai, stream))
17444febced1SJerome Brunet continue;
17454febced1SJerome Brunet
1746acf253c1SKuninori Morimoto codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1747b073ed4eSKuninori Morimoto
1748debc71f2SKuninori Morimoto soc_pcm_hw_update_format(hw, codec_stream);
1749435ffb76SJerome Brunet }
1750b073ed4eSKuninori Morimoto }
1751b073ed4eSKuninori Morimoto }
1752b073ed4eSKuninori Morimoto
dpcm_runtime_setup_be_chan(struct snd_pcm_substream * substream)17531b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1754f4c277b8SJiada Wang {
17550ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17561b8cb123SKuninori Morimoto struct snd_pcm_runtime *runtime = substream->runtime;
17574b260f42SKuninori Morimoto struct snd_pcm_hardware *hw = &runtime->hw;
1758f4c277b8SJiada Wang struct snd_soc_dpcm *dpcm;
1759f4c277b8SJiada Wang int stream = substream->stream;
1760f4c277b8SJiada Wang
1761f4c277b8SJiada Wang if (!fe->dai_link->dpcm_merged_chan)
1762f4c277b8SJiada Wang return;
1763f4c277b8SJiada Wang
1764f4c277b8SJiada Wang /*
1765f4c277b8SJiada Wang * It returns merged BE codec channel;
1766f4c277b8SJiada Wang * if FE want to use it (= dpcm_merged_chan)
1767f4c277b8SJiada Wang */
1768f4c277b8SJiada Wang
17698d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
1770f4c277b8SJiada Wang struct snd_soc_pcm_runtime *be = dpcm->be;
17714f2bd18bSJerome Brunet struct snd_soc_pcm_stream *cpu_stream;
177219bdcc7aSShreyas NC struct snd_soc_dai *dai;
177319bdcc7aSShreyas NC int i;
1774f4c277b8SJiada Wang
1775a4be4187SKuninori Morimoto for_each_rtd_cpu_dais(be, i, dai) {
17760e9cf4c4SBard Liao /*
17770e9cf4c4SBard Liao * Skip CPUs which don't support the current stream
17780e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details
17790e9cf4c4SBard Liao */
17800e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream))
17810e9cf4c4SBard Liao continue;
17820e9cf4c4SBard Liao
178319bdcc7aSShreyas NC cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
17844f2bd18bSJerome Brunet
17856cb56a45SKuninori Morimoto soc_pcm_hw_update_chan(hw, cpu_stream);
178619bdcc7aSShreyas NC }
17874f2bd18bSJerome Brunet
17884f2bd18bSJerome Brunet /*
17894f2bd18bSJerome Brunet * chan min/max cannot be enforced if there are multiple CODEC
17904f2bd18bSJerome Brunet * DAIs connected to a single CPU DAI, use CPU DAI's directly
17914f2bd18bSJerome Brunet */
17923989ade2SKuninori Morimoto if (be->dai_link->num_codecs == 1) {
17939bdc573dSKuninori Morimoto struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
17949bdc573dSKuninori Morimoto asoc_rtd_to_codec(be, 0), stream);
1795f4c277b8SJiada Wang
17966cb56a45SKuninori Morimoto soc_pcm_hw_update_chan(hw, codec_stream);
1797f4c277b8SJiada Wang }
1798f4c277b8SJiada Wang }
1799f4c277b8SJiada Wang }
1800f4c277b8SJiada Wang
dpcm_runtime_setup_be_rate(struct snd_pcm_substream * substream)18011b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1802baacd8d1SJerome Brunet {
18030ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
18041b8cb123SKuninori Morimoto struct snd_pcm_runtime *runtime = substream->runtime;
18054b260f42SKuninori Morimoto struct snd_pcm_hardware *hw = &runtime->hw;
1806baacd8d1SJerome Brunet struct snd_soc_dpcm *dpcm;
1807baacd8d1SJerome Brunet int stream = substream->stream;
1808baacd8d1SJerome Brunet
1809baacd8d1SJerome Brunet if (!fe->dai_link->dpcm_merged_rate)
1810baacd8d1SJerome Brunet return;
1811baacd8d1SJerome Brunet
1812baacd8d1SJerome Brunet /*
1813baacd8d1SJerome Brunet * It returns merged BE codec channel;
1814baacd8d1SJerome Brunet * if FE want to use it (= dpcm_merged_chan)
1815baacd8d1SJerome Brunet */
1816baacd8d1SJerome Brunet
18178d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
1818baacd8d1SJerome Brunet struct snd_soc_pcm_runtime *be = dpcm->be;
1819c840f769SKuninori Morimoto struct snd_soc_pcm_stream *pcm;
18207afecb30SKuninori Morimoto struct snd_soc_dai *dai;
1821baacd8d1SJerome Brunet int i;
1822baacd8d1SJerome Brunet
1823c840f769SKuninori Morimoto for_each_rtd_dais(be, i, dai) {
18240e9cf4c4SBard Liao /*
1825c840f769SKuninori Morimoto * Skip DAIs which don't support the current stream
18260e9cf4c4SBard Liao * type. See soc_pcm_init_runtime_hw() for more details
18270e9cf4c4SBard Liao */
18280e9cf4c4SBard Liao if (!snd_soc_dai_stream_valid(dai, stream))
18290e9cf4c4SBard Liao continue;
18300e9cf4c4SBard Liao
1831c840f769SKuninori Morimoto pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1832baacd8d1SJerome Brunet
1833f6c04af5SKuninori Morimoto soc_pcm_hw_update_rate(hw, pcm);
1834baacd8d1SJerome Brunet }
1835baacd8d1SJerome Brunet }
1836baacd8d1SJerome Brunet }
1837baacd8d1SJerome Brunet
dpcm_apply_symmetry(struct snd_pcm_substream * fe_substream,int stream)1838906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1839906c7d69SPC Liao int stream)
1840906c7d69SPC Liao {
1841906c7d69SPC Liao struct snd_soc_dpcm *dpcm;
18420ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
184319bdcc7aSShreyas NC struct snd_soc_dai *fe_cpu_dai;
184412ffd726SJaroslav Kysela int err = 0;
184519bdcc7aSShreyas NC int i;
1846906c7d69SPC Liao
1847906c7d69SPC Liao /* apply symmetry for FE */
184868cbc557SKuninori Morimoto soc_pcm_update_symmetry(fe_substream);
1849906c7d69SPC Liao
1850a4be4187SKuninori Morimoto for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1851906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */
1852906c7d69SPC Liao err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1853906c7d69SPC Liao if (err < 0)
1854bbd2bac8SKuninori Morimoto goto error;
1855906c7d69SPC Liao }
1856906c7d69SPC Liao
1857906c7d69SPC Liao /* apply symmetry for BE */
18588d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
1859906c7d69SPC Liao struct snd_soc_pcm_runtime *be = dpcm->be;
1860906c7d69SPC Liao struct snd_pcm_substream *be_substream =
1861906c7d69SPC Liao snd_soc_dpcm_get_substream(be, stream);
18626246f283SJerome Brunet struct snd_soc_pcm_runtime *rtd;
1863c840f769SKuninori Morimoto struct snd_soc_dai *dai;
1864906c7d69SPC Liao
18656246f283SJerome Brunet /* A backend may not have the requested substream */
18666246f283SJerome Brunet if (!be_substream)
18676246f283SJerome Brunet continue;
18686246f283SJerome Brunet
18690ceef681SKuninori Morimoto rtd = asoc_substream_to_rtd(be_substream);
1870f1176614SJeeja KP if (rtd->dai_link->be_hw_params_fixup)
1871f1176614SJeeja KP continue;
1872f1176614SJeeja KP
187368cbc557SKuninori Morimoto soc_pcm_update_symmetry(be_substream);
1874906c7d69SPC Liao
1875906c7d69SPC Liao /* Symmetry only applies if we've got an active stream. */
1876c840f769SKuninori Morimoto for_each_rtd_dais(rtd, i, dai) {
1877c840f769SKuninori Morimoto err = soc_pcm_apply_symmetry(fe_substream, dai);
1878906c7d69SPC Liao if (err < 0)
1879bbd2bac8SKuninori Morimoto goto error;
1880906c7d69SPC Liao }
1881906c7d69SPC Liao }
1882bbd2bac8SKuninori Morimoto error:
188304110728SKuninori Morimoto return soc_pcm_ret(fe, err);
1884906c7d69SPC Liao }
1885906c7d69SPC Liao
dpcm_fe_dai_startup(struct snd_pcm_substream * fe_substream)188601d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
188701d7584cSLiam Girdwood {
18880ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
188901d7584cSLiam Girdwood int stream = fe_substream->stream, ret = 0;
189001d7584cSLiam Girdwood
1891ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
189201d7584cSLiam Girdwood
189325c2f515SKuninori Morimoto ret = dpcm_be_dai_startup(fe, stream);
189406aaeb87SKuninori Morimoto if (ret < 0)
189501d7584cSLiam Girdwood goto be_err;
189601d7584cSLiam Girdwood
1897103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
189801d7584cSLiam Girdwood
189901d7584cSLiam Girdwood /* start the DAI frontend */
1900b7898396STakashi Iwai ret = __soc_pcm_open(fe, fe_substream);
1901e4b044f4SKuninori Morimoto if (ret < 0)
190201d7584cSLiam Girdwood goto unwind;
190301d7584cSLiam Girdwood
190401d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
190501d7584cSLiam Girdwood
19064fe28461SKuninori Morimoto dpcm_runtime_setup_fe(fe_substream);
19074fe28461SKuninori Morimoto
19084fe28461SKuninori Morimoto dpcm_runtime_setup_be_format(fe_substream);
19094fe28461SKuninori Morimoto dpcm_runtime_setup_be_chan(fe_substream);
19104fe28461SKuninori Morimoto dpcm_runtime_setup_be_rate(fe_substream);
191101d7584cSLiam Girdwood
1912906c7d69SPC Liao ret = dpcm_apply_symmetry(fe_substream, stream);
191301d7584cSLiam Girdwood
191401d7584cSLiam Girdwood unwind:
19158a01fbf0SKuninori Morimoto if (ret < 0)
191625c2f515SKuninori Morimoto dpcm_be_dai_startup_unwind(fe, stream);
191701d7584cSLiam Girdwood be_err:
1918ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
191906aaeb87SKuninori Morimoto
192004110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
192101d7584cSLiam Girdwood }
192201d7584cSLiam Girdwood
dpcm_fe_dai_shutdown(struct snd_pcm_substream * substream)192301d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
192401d7584cSLiam Girdwood {
19250ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
192601d7584cSLiam Girdwood int stream = substream->stream;
192701d7584cSLiam Girdwood
1928b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(fe);
1929b7898396STakashi Iwai
1930ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
193101d7584cSLiam Girdwood
193201d7584cSLiam Girdwood /* shutdown the BEs */
193325c2f515SKuninori Morimoto dpcm_be_dai_shutdown(fe, stream);
193401d7584cSLiam Girdwood
1935103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
193601d7584cSLiam Girdwood
193701d7584cSLiam Girdwood /* now shutdown the frontend */
1938b7898396STakashi Iwai __soc_pcm_close(fe, substream);
193901d7584cSLiam Girdwood
1940bb9dd3ceSRanjani Sridharan /* run the stream stop event */
1941bb9dd3ceSRanjani Sridharan dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1942bb9dd3ceSRanjani Sridharan
194301d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1944ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
194501d7584cSLiam Girdwood return 0;
194601d7584cSLiam Girdwood }
194701d7584cSLiam Girdwood
dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime * fe,int stream)1948f52366e6SKuninori Morimoto void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
194901d7584cSLiam Girdwood {
195001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
195101d7584cSLiam Girdwood
195201d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources
195301d7584cSLiam Girdwood * to this frontend DAI */
19548d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
195501d7584cSLiam Girdwood
195601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be;
195701d7584cSLiam Girdwood struct snd_pcm_substream *be_substream =
195801d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream);
195901d7584cSLiam Girdwood
196001d7584cSLiam Girdwood /* is this op for this BE ? */
196101d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
196201d7584cSLiam Girdwood continue;
196301d7584cSLiam Girdwood
196401d7584cSLiam Girdwood /* only free hw when no longer used - check all FEs */
196501d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
196601d7584cSLiam Girdwood continue;
196701d7584cSLiam Girdwood
196836fba62cSQiao Zhou /* do not free hw if this BE is used by other FE */
196936fba62cSQiao Zhou if (be->dpcm[stream].users > 1)
197036fba62cSQiao Zhou continue;
197136fba62cSQiao Zhou
197201d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
197301d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
197401d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
197508b27848SPatrick Lai (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
19765e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
19775e82d2beSVinod Koul (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
197801d7584cSLiam Girdwood continue;
197901d7584cSLiam Girdwood
1980103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
198194d215ccS彭东林 be->dai_link->name);
198201d7584cSLiam Girdwood
1983b7898396STakashi Iwai __soc_pcm_hw_free(be, be_substream);
198401d7584cSLiam Girdwood
198501d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
198601d7584cSLiam Girdwood }
198701d7584cSLiam Girdwood }
198801d7584cSLiam Girdwood
dpcm_fe_dai_hw_free(struct snd_pcm_substream * substream)198945c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
199001d7584cSLiam Girdwood {
19910ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1992f52366e6SKuninori Morimoto int stream = substream->stream;
199301d7584cSLiam Girdwood
1994b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
1995ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
199601d7584cSLiam Girdwood
1997103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
199801d7584cSLiam Girdwood
199901d7584cSLiam Girdwood /* call hw_free on the frontend */
2000b7898396STakashi Iwai soc_pcm_hw_clean(fe, substream, 0);
200101d7584cSLiam Girdwood
200201d7584cSLiam Girdwood /* only hw_params backends that are either sinks or sources
200301d7584cSLiam Girdwood * to this frontend DAI */
2004f52366e6SKuninori Morimoto dpcm_be_dai_hw_free(fe, stream);
200501d7584cSLiam Girdwood
200601d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2007ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
200801d7584cSLiam Girdwood
2009b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
201001d7584cSLiam Girdwood return 0;
201101d7584cSLiam Girdwood }
201201d7584cSLiam Girdwood
dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime * fe,int stream)201323607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
201401d7584cSLiam Girdwood {
201533b6b94fSKuninori Morimoto struct snd_soc_pcm_runtime *be;
201633b6b94fSKuninori Morimoto struct snd_pcm_substream *be_substream;
201701d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
201801d7584cSLiam Girdwood int ret;
201901d7584cSLiam Girdwood
20208d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
202125106550SKuninori Morimoto struct snd_pcm_hw_params hw_params;
202225106550SKuninori Morimoto
202333b6b94fSKuninori Morimoto be = dpcm->be;
202433b6b94fSKuninori Morimoto be_substream = snd_soc_dpcm_get_substream(be, stream);
202501d7584cSLiam Girdwood
202601d7584cSLiam Girdwood /* is this op for this BE ? */
202701d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
202801d7584cSLiam Girdwood continue;
202901d7584cSLiam Girdwood
203001d7584cSLiam Girdwood /* copy params for each dpcm */
203125106550SKuninori Morimoto memcpy(&hw_params, &fe->dpcm[stream].hw_params,
203201d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params));
203301d7584cSLiam Girdwood
203401d7584cSLiam Girdwood /* perform any hw_params fixups */
203525106550SKuninori Morimoto ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
20360cbbf8a0SKuninori Morimoto if (ret < 0)
203701d7584cSLiam Girdwood goto unwind;
203801d7584cSLiam Girdwood
2039ae061d2aSLibin Yang /* copy the fixed-up hw params for BE dai */
204025106550SKuninori Morimoto memcpy(&be->dpcm[stream].hw_params, &hw_params,
2041ae061d2aSLibin Yang sizeof(struct snd_pcm_hw_params));
2042ae061d2aSLibin Yang
2043b0639bd2SKuninori Morimoto /* only allow hw_params() if no connected FEs are running */
2044b0639bd2SKuninori Morimoto if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2045b0639bd2SKuninori Morimoto continue;
2046b0639bd2SKuninori Morimoto
2047b0639bd2SKuninori Morimoto if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2048b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2049b0639bd2SKuninori Morimoto (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2050b0639bd2SKuninori Morimoto continue;
2051b0639bd2SKuninori Morimoto
2052b0639bd2SKuninori Morimoto dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
205394d215ccS彭东林 be->dai_link->name);
2054b0639bd2SKuninori Morimoto
205525106550SKuninori Morimoto ret = __soc_pcm_hw_params(be, be_substream, &hw_params);
2056cb11f79bSKuninori Morimoto if (ret < 0)
205701d7584cSLiam Girdwood goto unwind;
205801d7584cSLiam Girdwood
205901d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
206001d7584cSLiam Girdwood }
206101d7584cSLiam Girdwood return 0;
206201d7584cSLiam Girdwood
206301d7584cSLiam Girdwood unwind:
206433b6b94fSKuninori Morimoto dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
206533b6b94fSKuninori Morimoto __func__, be->dai_link->name, ret);
206633b6b94fSKuninori Morimoto
206701d7584cSLiam Girdwood /* disable any enabled and non active backends */
20688d6258a4SKuninori Morimoto for_each_dpcm_be_rollback(fe, stream, dpcm) {
206933b6b94fSKuninori Morimoto be = dpcm->be;
207033b6b94fSKuninori Morimoto be_substream = snd_soc_dpcm_get_substream(be, stream);
207101d7584cSLiam Girdwood
207201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
207301d7584cSLiam Girdwood continue;
207401d7584cSLiam Girdwood
207501d7584cSLiam Girdwood /* only allow hw_free() if no connected FEs are running */
207601d7584cSLiam Girdwood if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
207701d7584cSLiam Girdwood continue;
207801d7584cSLiam Girdwood
207901d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
208001d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
208101d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
208201d7584cSLiam Girdwood (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
208301d7584cSLiam Girdwood continue;
208401d7584cSLiam Girdwood
2085b7898396STakashi Iwai __soc_pcm_hw_free(be, be_substream);
208601d7584cSLiam Girdwood }
208701d7584cSLiam Girdwood
208801d7584cSLiam Girdwood return ret;
208901d7584cSLiam Girdwood }
209001d7584cSLiam Girdwood
dpcm_fe_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)209145c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
209201d7584cSLiam Girdwood struct snd_pcm_hw_params *params)
209301d7584cSLiam Girdwood {
20940ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
209501d7584cSLiam Girdwood int ret, stream = substream->stream;
209601d7584cSLiam Girdwood
2097b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
2098ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
209901d7584cSLiam Girdwood
210025c2f515SKuninori Morimoto memcpy(&fe->dpcm[stream].hw_params, params,
210101d7584cSLiam Girdwood sizeof(struct snd_pcm_hw_params));
210225c2f515SKuninori Morimoto ret = dpcm_be_dai_hw_params(fe, stream);
210333b6b94fSKuninori Morimoto if (ret < 0)
210401d7584cSLiam Girdwood goto out;
210501d7584cSLiam Girdwood
2106103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
210701d7584cSLiam Girdwood fe->dai_link->name, params_rate(params),
210801d7584cSLiam Girdwood params_channels(params), params_format(params));
210901d7584cSLiam Girdwood
211001d7584cSLiam Girdwood /* call hw_params on the frontend */
2111b7898396STakashi Iwai ret = __soc_pcm_hw_params(fe, substream, params);
2112cb11f79bSKuninori Morimoto if (ret < 0)
211301d7584cSLiam Girdwood dpcm_be_dai_hw_free(fe, stream);
2114cb11f79bSKuninori Morimoto else
211501d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
211601d7584cSLiam Girdwood
211701d7584cSLiam Girdwood out:
2118ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2119b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
212033b6b94fSKuninori Morimoto
212104110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
212201d7584cSLiam Girdwood }
212301d7584cSLiam Girdwood
dpcm_be_dai_trigger(struct snd_soc_pcm_runtime * fe,int stream,int cmd)212423607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
212545c0a188SMark Brown int cmd)
212601d7584cSLiam Girdwood {
2127db3aa39cSKuninori Morimoto struct snd_soc_pcm_runtime *be;
21289995c1d0SPierre-Louis Bossart bool pause_stop_transition;
212901d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
2130b2ae8066STakashi Iwai unsigned long flags;
213101d7584cSLiam Girdwood int ret = 0;
213201d7584cSLiam Girdwood
21338d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
2134db3aa39cSKuninori Morimoto struct snd_pcm_substream *be_substream;
213501d7584cSLiam Girdwood
2136db3aa39cSKuninori Morimoto be = dpcm->be;
2137db3aa39cSKuninori Morimoto be_substream = snd_soc_dpcm_get_substream(be, stream);
213801d7584cSLiam Girdwood
21393c75c0eaSTakashi Iwai snd_soc_dpcm_stream_lock_irqsave_nested(be, stream, flags);
2140b2ae8066STakashi Iwai
214101d7584cSLiam Girdwood /* is this op for this BE ? */
214201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2143b2ae8066STakashi Iwai goto next;
214401d7584cSLiam Girdwood
2145a9faca15SKuninori Morimoto dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2146a9faca15SKuninori Morimoto be->dai_link->name, cmd);
2147a9faca15SKuninori Morimoto
214801d7584cSLiam Girdwood switch (cmd) {
214901d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START:
2150848aedfdSPierre-Louis Bossart if (!be->dpcm[stream].be_start &&
2151848aedfdSPierre-Louis Bossart (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
215221fca8bdS이경택 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
21533202e2f5SMark Brown (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2154b2ae8066STakashi Iwai goto next;
21556479f758SPierre-Louis Bossart
2156848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
2157848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 1)
2158b2ae8066STakashi Iwai goto next;
215901d7584cSLiam Girdwood
2160374b50e2SPierre-Louis Bossart if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2161374b50e2SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream,
2162374b50e2SPierre-Louis Bossart SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2163374b50e2SPierre-Louis Bossart else
2164374b50e2SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream,
2165374b50e2SPierre-Louis Bossart SNDRV_PCM_TRIGGER_START);
2166848aedfdSPierre-Louis Bossart if (ret) {
2167848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2168848aedfdSPierre-Louis Bossart goto next;
2169848aedfdSPierre-Louis Bossart }
2170848aedfdSPierre-Louis Bossart
21713202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
217201d7584cSLiam Girdwood break;
217301d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME:
21743202e2f5SMark Brown if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2175b2ae8066STakashi Iwai goto next;
21766479f758SPierre-Louis Bossart
2177848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
2178848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 1)
2179b2ae8066STakashi Iwai goto next;
218001d7584cSLiam Girdwood
2181848aedfdSPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, cmd);
2182848aedfdSPierre-Louis Bossart if (ret) {
2183848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2184848aedfdSPierre-Louis Bossart goto next;
2185848aedfdSPierre-Louis Bossart }
2186848aedfdSPierre-Louis Bossart
21873202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
218801d7584cSLiam Girdwood break;
218901d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
21903aa1e96aSPierre-Louis Bossart if (!be->dpcm[stream].be_start &&
21913aa1e96aSPierre-Louis Bossart (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
21923aa1e96aSPierre-Louis Bossart (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2193b2ae8066STakashi Iwai goto next;
21946479f758SPierre-Louis Bossart
21959995c1d0SPierre-Louis Bossart fe->dpcm[stream].fe_pause = false;
21969995c1d0SPierre-Louis Bossart be->dpcm[stream].be_pause--;
21979995c1d0SPierre-Louis Bossart
2198848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
2199848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 1)
2200b2ae8066STakashi Iwai goto next;
220101d7584cSLiam Girdwood
2202848aedfdSPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, cmd);
2203848aedfdSPierre-Louis Bossart if (ret) {
2204848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2205848aedfdSPierre-Louis Bossart goto next;
2206848aedfdSPierre-Louis Bossart }
2207848aedfdSPierre-Louis Bossart
22083202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
220901d7584cSLiam Girdwood break;
221001d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP:
221121fca8bdS이경택 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
22123202e2f5SMark Brown (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2213b2ae8066STakashi Iwai goto next;
221401d7584cSLiam Girdwood
2215848aedfdSPierre-Louis Bossart if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2216848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2217848aedfdSPierre-Louis Bossart
2218848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 0)
2219b2ae8066STakashi Iwai goto next;
22200c75fc71SPierre-Louis Bossart
22219995c1d0SPierre-Louis Bossart pause_stop_transition = false;
22229995c1d0SPierre-Louis Bossart if (fe->dpcm[stream].fe_pause) {
22239995c1d0SPierre-Louis Bossart pause_stop_transition = true;
22249995c1d0SPierre-Louis Bossart fe->dpcm[stream].fe_pause = false;
22259995c1d0SPierre-Louis Bossart be->dpcm[stream].be_pause--;
22269995c1d0SPierre-Louis Bossart }
22279995c1d0SPierre-Louis Bossart
22289995c1d0SPierre-Louis Bossart if (be->dpcm[stream].be_pause != 0)
22299995c1d0SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
22309995c1d0SPierre-Louis Bossart else
22319995c1d0SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
22329995c1d0SPierre-Louis Bossart
2233848aedfdSPierre-Louis Bossart if (ret) {
2234848aedfdSPierre-Louis Bossart if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2235848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
22369995c1d0SPierre-Louis Bossart if (pause_stop_transition) {
22379995c1d0SPierre-Louis Bossart fe->dpcm[stream].fe_pause = true;
22389995c1d0SPierre-Louis Bossart be->dpcm[stream].be_pause++;
22399995c1d0SPierre-Louis Bossart }
2240b2ae8066STakashi Iwai goto next;
2241848aedfdSPierre-Louis Bossart }
22420c75fc71SPierre-Louis Bossart
22439995c1d0SPierre-Louis Bossart if (be->dpcm[stream].be_pause != 0)
22449995c1d0SPierre-Louis Bossart be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
22459995c1d0SPierre-Louis Bossart else
22463202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
22479995c1d0SPierre-Louis Bossart
224801d7584cSLiam Girdwood break;
224901d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND:
22503202e2f5SMark Brown if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2251b2ae8066STakashi Iwai goto next;
225201d7584cSLiam Girdwood
2253848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2254848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 0)
2255b2ae8066STakashi Iwai goto next;
225601d7584cSLiam Girdwood
22570c75fc71SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, cmd);
2258848aedfdSPierre-Louis Bossart if (ret) {
2259848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
2260b2ae8066STakashi Iwai goto next;
2261848aedfdSPierre-Louis Bossart }
22620c75fc71SPierre-Louis Bossart
22633202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
226401d7584cSLiam Girdwood break;
226501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
22663202e2f5SMark Brown if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2267b2ae8066STakashi Iwai goto next;
226801d7584cSLiam Girdwood
22699995c1d0SPierre-Louis Bossart fe->dpcm[stream].fe_pause = true;
22709995c1d0SPierre-Louis Bossart be->dpcm[stream].be_pause++;
22719995c1d0SPierre-Louis Bossart
2272848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start--;
2273848aedfdSPierre-Louis Bossart if (be->dpcm[stream].be_start != 0)
2274b2ae8066STakashi Iwai goto next;
22750c75fc71SPierre-Louis Bossart
22760c75fc71SPierre-Louis Bossart ret = soc_pcm_trigger(be_substream, cmd);
2277848aedfdSPierre-Louis Bossart if (ret) {
2278848aedfdSPierre-Louis Bossart be->dpcm[stream].be_start++;
2279b2ae8066STakashi Iwai goto next;
2280848aedfdSPierre-Louis Bossart }
22810c75fc71SPierre-Louis Bossart
22823202e2f5SMark Brown be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
228301d7584cSLiam Girdwood break;
228401d7584cSLiam Girdwood }
2285b2ae8066STakashi Iwai next:
2286b2ae8066STakashi Iwai snd_soc_dpcm_stream_unlock_irqrestore(be, stream, flags);
2287b2ae8066STakashi Iwai if (ret)
2288b2ae8066STakashi Iwai break;
228901d7584cSLiam Girdwood }
229004110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
229101d7584cSLiam Girdwood }
229201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
229301d7584cSLiam Girdwood
dpcm_dai_trigger_fe_be(struct snd_pcm_substream * substream,int cmd,bool fe_first)2294acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2295acbf2774SRanjani Sridharan int cmd, bool fe_first)
2296acbf2774SRanjani Sridharan {
22970ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2298acbf2774SRanjani Sridharan int ret;
2299acbf2774SRanjani Sridharan
2300acbf2774SRanjani Sridharan /* call trigger on the frontend before the backend. */
2301acbf2774SRanjani Sridharan if (fe_first) {
2302acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2303acbf2774SRanjani Sridharan fe->dai_link->name, cmd);
2304acbf2774SRanjani Sridharan
2305acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd);
2306acbf2774SRanjani Sridharan if (ret < 0)
2307acbf2774SRanjani Sridharan return ret;
2308acbf2774SRanjani Sridharan
2309acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2310acbf2774SRanjani Sridharan return ret;
2311acbf2774SRanjani Sridharan }
2312acbf2774SRanjani Sridharan
2313acbf2774SRanjani Sridharan /* call trigger on the frontend after the backend. */
2314acbf2774SRanjani Sridharan ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2315acbf2774SRanjani Sridharan if (ret < 0)
2316acbf2774SRanjani Sridharan return ret;
2317acbf2774SRanjani Sridharan
2318acbf2774SRanjani Sridharan dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2319acbf2774SRanjani Sridharan fe->dai_link->name, cmd);
2320acbf2774SRanjani Sridharan
2321acbf2774SRanjani Sridharan ret = soc_pcm_trigger(substream, cmd);
2322acbf2774SRanjani Sridharan
2323acbf2774SRanjani Sridharan return ret;
2324acbf2774SRanjani Sridharan }
2325acbf2774SRanjani Sridharan
dpcm_fe_dai_do_trigger(struct snd_pcm_substream * substream,int cmd)2326ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
232701d7584cSLiam Girdwood {
23280ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2329acbf2774SRanjani Sridharan int stream = substream->stream;
2330acbf2774SRanjani Sridharan int ret = 0;
233101d7584cSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
233201d7584cSLiam Girdwood
233301d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
233401d7584cSLiam Girdwood
233501d7584cSLiam Girdwood switch (trigger) {
233601d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_PRE:
2337acbf2774SRanjani Sridharan switch (cmd) {
2338acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START:
2339acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME:
2340acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23414c22b80fSCezary Rojewski case SNDRV_PCM_TRIGGER_DRAIN:
2342acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2343acbf2774SRanjani Sridharan break;
2344acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP:
2345acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND:
2346acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2347acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2348acbf2774SRanjani Sridharan break;
2349acbf2774SRanjani Sridharan default:
2350acbf2774SRanjani Sridharan ret = -EINVAL;
2351acbf2774SRanjani Sridharan break;
235201d7584cSLiam Girdwood }
235301d7584cSLiam Girdwood break;
235401d7584cSLiam Girdwood case SND_SOC_DPCM_TRIGGER_POST:
2355acbf2774SRanjani Sridharan switch (cmd) {
2356acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_START:
2357acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_RESUME:
2358acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23594c22b80fSCezary Rojewski case SNDRV_PCM_TRIGGER_DRAIN:
2360acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2361acbf2774SRanjani Sridharan break;
2362acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_STOP:
2363acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_SUSPEND:
2364acbf2774SRanjani Sridharan case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2365acbf2774SRanjani Sridharan ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2366acbf2774SRanjani Sridharan break;
2367acbf2774SRanjani Sridharan default:
2368acbf2774SRanjani Sridharan ret = -EINVAL;
2369acbf2774SRanjani Sridharan break;
237001d7584cSLiam Girdwood }
237101d7584cSLiam Girdwood break;
237207bf84aaSLiam Girdwood case SND_SOC_DPCM_TRIGGER_BESPOKE:
237307bf84aaSLiam Girdwood /* bespoke trigger() - handles both FE and BEs */
237407bf84aaSLiam Girdwood
2375103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
237607bf84aaSLiam Girdwood fe->dai_link->name, cmd);
237707bf84aaSLiam Girdwood
237830819358SKuninori Morimoto ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
237907bf84aaSLiam Girdwood break;
238001d7584cSLiam Girdwood default:
2381103d84a3SLiam Girdwood dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
238201d7584cSLiam Girdwood fe->dai_link->name);
238301d7584cSLiam Girdwood ret = -EINVAL;
238401d7584cSLiam Girdwood goto out;
238501d7584cSLiam Girdwood }
238601d7584cSLiam Girdwood
2387acbf2774SRanjani Sridharan if (ret < 0) {
2388acbf2774SRanjani Sridharan dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2389acbf2774SRanjani Sridharan cmd, ret);
2390acbf2774SRanjani Sridharan goto out;
2391acbf2774SRanjani Sridharan }
2392acbf2774SRanjani Sridharan
239301d7584cSLiam Girdwood switch (cmd) {
239401d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_START:
239501d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME:
239601d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
239701d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
239801d7584cSLiam Girdwood break;
239901d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP:
240001d7584cSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND:
240101d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
240201d7584cSLiam Girdwood break;
24039f169b9fSPatrick Lai case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
24049f169b9fSPatrick Lai fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
24059f169b9fSPatrick Lai break;
240601d7584cSLiam Girdwood }
240701d7584cSLiam Girdwood
240801d7584cSLiam Girdwood out:
240901d7584cSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
241001d7584cSLiam Girdwood return ret;
241101d7584cSLiam Girdwood }
241201d7584cSLiam Girdwood
dpcm_fe_dai_trigger(struct snd_pcm_substream * substream,int cmd)2413ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2414ea9d0d77STakashi Iwai {
24150ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2416ea9d0d77STakashi Iwai int stream = substream->stream;
2417ea9d0d77STakashi Iwai
2418ea9d0d77STakashi Iwai /* if FE's runtime_update is already set, we're in race;
2419ea9d0d77STakashi Iwai * process this trigger later at exit
2420ea9d0d77STakashi Iwai */
2421ea9d0d77STakashi Iwai if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2422ea9d0d77STakashi Iwai fe->dpcm[stream].trigger_pending = cmd + 1;
2423ea9d0d77STakashi Iwai return 0; /* delayed, assuming it's successful */
2424ea9d0d77STakashi Iwai }
2425ea9d0d77STakashi Iwai
2426ea9d0d77STakashi Iwai /* we're alone, let's trigger */
2427ea9d0d77STakashi Iwai return dpcm_fe_dai_do_trigger(substream, cmd);
2428ea9d0d77STakashi Iwai }
2429ea9d0d77STakashi Iwai
dpcm_be_dai_prepare(struct snd_soc_pcm_runtime * fe,int stream)243023607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
243101d7584cSLiam Girdwood {
243201d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
243301d7584cSLiam Girdwood int ret = 0;
243401d7584cSLiam Girdwood
24358d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
243601d7584cSLiam Girdwood
243701d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be;
243801d7584cSLiam Girdwood struct snd_pcm_substream *be_substream =
243901d7584cSLiam Girdwood snd_soc_dpcm_get_substream(be, stream);
244001d7584cSLiam Girdwood
244101d7584cSLiam Girdwood /* is this op for this BE ? */
244201d7584cSLiam Girdwood if (!snd_soc_dpcm_be_can_update(fe, be, stream))
244301d7584cSLiam Girdwood continue;
244401d7584cSLiam Girdwood
2445e123036bSRanjani Sridharan if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2446e123036bSRanjani Sridharan continue;
2447e123036bSRanjani Sridharan
244801d7584cSLiam Girdwood if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
244995f444dcSKoro Chen (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
24505087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
24515087a8f1SLibin Yang (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
245201d7584cSLiam Girdwood continue;
245301d7584cSLiam Girdwood
2454103d84a3SLiam Girdwood dev_dbg(be->dev, "ASoC: prepare BE %s\n",
245594d215ccS彭东林 be->dai_link->name);
245601d7584cSLiam Girdwood
2457b7898396STakashi Iwai ret = __soc_pcm_prepare(be, be_substream);
2458dab7eeb4SKuninori Morimoto if (ret < 0)
245901d7584cSLiam Girdwood break;
246001d7584cSLiam Girdwood
246101d7584cSLiam Girdwood be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
246201d7584cSLiam Girdwood }
2463273db971SKuninori Morimoto
246404110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
246501d7584cSLiam Girdwood }
246601d7584cSLiam Girdwood
dpcm_fe_dai_prepare(struct snd_pcm_substream * substream)246745c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
246801d7584cSLiam Girdwood {
24690ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
247001d7584cSLiam Girdwood int stream = substream->stream, ret = 0;
247101d7584cSLiam Girdwood
2472b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
247301d7584cSLiam Girdwood
2474103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
247501d7584cSLiam Girdwood
2476ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
247701d7584cSLiam Girdwood
247801d7584cSLiam Girdwood /* there is no point preparing this FE if there are no BEs */
247901d7584cSLiam Girdwood if (list_empty(&fe->dpcm[stream].be_clients)) {
24801f566435SHans de Goede /* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles */
24811f566435SHans de Goede dev_err_once(fe->dev, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
24821f566435SHans de Goede fe->dai_link->name);
24831f566435SHans de Goede dev_dbg(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
248401d7584cSLiam Girdwood fe->dai_link->name);
248501d7584cSLiam Girdwood ret = -EINVAL;
248601d7584cSLiam Girdwood goto out;
248701d7584cSLiam Girdwood }
248801d7584cSLiam Girdwood
248925c2f515SKuninori Morimoto ret = dpcm_be_dai_prepare(fe, stream);
249001d7584cSLiam Girdwood if (ret < 0)
249101d7584cSLiam Girdwood goto out;
249201d7584cSLiam Girdwood
249301d7584cSLiam Girdwood /* call prepare on the frontend */
2494b7898396STakashi Iwai ret = __soc_pcm_prepare(fe, substream);
2495dab7eeb4SKuninori Morimoto if (ret < 0)
249601d7584cSLiam Girdwood goto out;
249701d7584cSLiam Girdwood
249801d7584cSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
249901d7584cSLiam Girdwood
250001d7584cSLiam Girdwood out:
2501ea9d0d77STakashi Iwai dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2502b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
250301d7584cSLiam Girdwood
250404110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
250501d7584cSLiam Girdwood }
250601d7584cSLiam Girdwood
dpcm_run_update_shutdown(struct snd_soc_pcm_runtime * fe,int stream)2507618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2508618dae11SLiam Girdwood {
250907bf84aaSLiam Girdwood struct snd_pcm_substream *substream =
251007bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream);
251107bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2512618dae11SLiam Girdwood int err;
251301d7584cSLiam Girdwood
2514103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2515618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name);
2516618dae11SLiam Girdwood
251707bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
251807bf84aaSLiam Girdwood /* call bespoke trigger - FE takes care of all BE triggers */
2519103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
252007bf84aaSLiam Girdwood fe->dai_link->name);
252107bf84aaSLiam Girdwood
252230819358SKuninori Morimoto err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
252307bf84aaSLiam Girdwood } else {
2524103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
252507bf84aaSLiam Girdwood fe->dai_link->name);
252607bf84aaSLiam Girdwood
2527618dae11SLiam Girdwood err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
252807bf84aaSLiam Girdwood }
2529618dae11SLiam Girdwood
2530f52366e6SKuninori Morimoto dpcm_be_dai_hw_free(fe, stream);
2531618dae11SLiam Girdwood
2532531590bbSKuninori Morimoto dpcm_be_dai_shutdown(fe, stream);
2533618dae11SLiam Girdwood
2534618dae11SLiam Girdwood /* run the stream event for each BE */
2535618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2536618dae11SLiam Girdwood
253704110728SKuninori Morimoto return soc_pcm_ret(fe, err);
2538618dae11SLiam Girdwood }
2539618dae11SLiam Girdwood
dpcm_run_update_startup(struct snd_soc_pcm_runtime * fe,int stream)2540618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2541618dae11SLiam Girdwood {
254207bf84aaSLiam Girdwood struct snd_pcm_substream *substream =
254307bf84aaSLiam Girdwood snd_soc_dpcm_get_substream(fe, stream);
2544618dae11SLiam Girdwood struct snd_soc_dpcm *dpcm;
254507bf84aaSLiam Girdwood enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
25464eeed5f4SSouptick Joarder int ret = 0;
2547618dae11SLiam Girdwood
2548103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2549618dae11SLiam Girdwood stream ? "capture" : "playback", fe->dai_link->name);
2550618dae11SLiam Girdwood
2551618dae11SLiam Girdwood /* Only start the BE if the FE is ready */
2552618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
25532c138284S朱灿灿 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
25542c138284S朱灿灿 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
25552c138284S朱灿灿 fe->dai_link->name, fe->dpcm[stream].state);
2556e91b65b3SDan Carpenter ret = -EINVAL;
25572c138284S朱灿灿 goto disconnect;
25582c138284S朱灿灿 }
2559618dae11SLiam Girdwood
2560618dae11SLiam Girdwood /* startup must always be called for new BEs */
2561618dae11SLiam Girdwood ret = dpcm_be_dai_startup(fe, stream);
2562fffc0ca2SDan Carpenter if (ret < 0)
2563618dae11SLiam Girdwood goto disconnect;
2564618dae11SLiam Girdwood
2565618dae11SLiam Girdwood /* keep going if FE state is > open */
2566618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2567618dae11SLiam Girdwood return 0;
2568618dae11SLiam Girdwood
2569618dae11SLiam Girdwood ret = dpcm_be_dai_hw_params(fe, stream);
2570fffc0ca2SDan Carpenter if (ret < 0)
2571618dae11SLiam Girdwood goto close;
2572618dae11SLiam Girdwood
2573618dae11SLiam Girdwood /* keep going if FE state is > hw_params */
2574618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2575618dae11SLiam Girdwood return 0;
2576618dae11SLiam Girdwood
2577618dae11SLiam Girdwood ret = dpcm_be_dai_prepare(fe, stream);
2578fffc0ca2SDan Carpenter if (ret < 0)
2579618dae11SLiam Girdwood goto hw_free;
2580618dae11SLiam Girdwood
2581618dae11SLiam Girdwood /* run the stream event for each BE */
2582618dae11SLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2583618dae11SLiam Girdwood
2584618dae11SLiam Girdwood /* keep going if FE state is > prepare */
2585618dae11SLiam Girdwood if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2586618dae11SLiam Girdwood fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2587618dae11SLiam Girdwood return 0;
2588618dae11SLiam Girdwood
258907bf84aaSLiam Girdwood if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
259007bf84aaSLiam Girdwood /* call trigger on the frontend - FE takes care of all BE triggers */
2591103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
259207bf84aaSLiam Girdwood fe->dai_link->name);
259307bf84aaSLiam Girdwood
259430819358SKuninori Morimoto ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
259562462e01SKuninori Morimoto if (ret < 0)
259607bf84aaSLiam Girdwood goto hw_free;
259707bf84aaSLiam Girdwood } else {
2598103d84a3SLiam Girdwood dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2599618dae11SLiam Girdwood fe->dai_link->name);
2600618dae11SLiam Girdwood
2601618dae11SLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream,
2602618dae11SLiam Girdwood SNDRV_PCM_TRIGGER_START);
2603db3aa39cSKuninori Morimoto if (ret < 0)
2604618dae11SLiam Girdwood goto hw_free;
2605618dae11SLiam Girdwood }
2606618dae11SLiam Girdwood
2607618dae11SLiam Girdwood return 0;
2608618dae11SLiam Girdwood
2609618dae11SLiam Girdwood hw_free:
2610618dae11SLiam Girdwood dpcm_be_dai_hw_free(fe, stream);
2611618dae11SLiam Girdwood close:
2612618dae11SLiam Girdwood dpcm_be_dai_shutdown(fe, stream);
2613618dae11SLiam Girdwood disconnect:
26142c138284S朱灿灿 /* disconnect any pending BEs */
26158d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) {
2616618dae11SLiam Girdwood struct snd_soc_pcm_runtime *be = dpcm->be;
26172c138284S朱灿灿
26182c138284S朱灿灿 /* is this op for this BE ? */
26192c138284S朱灿灿 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
26202c138284S朱灿灿 continue;
26212c138284S朱灿灿
26222c138284S朱灿灿 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
26232c138284S朱灿灿 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2624618dae11SLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2625618dae11SLiam Girdwood }
2626618dae11SLiam Girdwood
262704110728SKuninori Morimoto return soc_pcm_ret(fe, ret);
2628618dae11SLiam Girdwood }
2629618dae11SLiam Girdwood
soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime * fe,int new)2630de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2631618dae11SLiam Girdwood {
2632618dae11SLiam Girdwood struct snd_soc_dapm_widget_list *list;
26337083f877SKuninori Morimoto int stream;
2634de15d7ffSJerome Brunet int count, paths;
2635618dae11SLiam Girdwood
263696bf62f0SPierre-Louis Bossart if (!fe->dai_link->dynamic)
263796bf62f0SPierre-Louis Bossart return 0;
263896bf62f0SPierre-Louis Bossart
26393989ade2SKuninori Morimoto if (fe->dai_link->num_cpus > 1) {
26406e1276a5SBard Liao dev_err(fe->dev,
26416e1276a5SBard Liao "%s doesn't support Multi CPU yet\n", __func__);
26426e1276a5SBard Liao return -EINVAL;
26436e1276a5SBard Liao }
26446e1276a5SBard Liao
2645618dae11SLiam Girdwood /* only check active links */
2646b3dea624SKuninori Morimoto if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2647de15d7ffSJerome Brunet return 0;
2648618dae11SLiam Girdwood
2649618dae11SLiam Girdwood /* DAPM sync will call this to update DSP paths */
2650de15d7ffSJerome Brunet dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2651de15d7ffSJerome Brunet new ? "new" : "old", fe->dai_link->name);
2652618dae11SLiam Girdwood
26537083f877SKuninori Morimoto for_each_pcm_streams(stream) {
2654075207d2SQiao Zhou
26557083f877SKuninori Morimoto /* skip if FE doesn't have playback/capture capability */
2656c2233a26SKuninori Morimoto if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2657c2233a26SKuninori Morimoto !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
26587083f877SKuninori Morimoto continue;
2659618dae11SLiam Girdwood
26607083f877SKuninori Morimoto /* skip if FE isn't currently playing/capturing */
2661b3dea624SKuninori Morimoto if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2662b3dea624SKuninori Morimoto !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
26637083f877SKuninori Morimoto continue;
26647083f877SKuninori Morimoto
26657083f877SKuninori Morimoto paths = dpcm_path_get(fe, stream, &list);
2666d479f00bSKuninori Morimoto if (paths < 0)
2667618dae11SLiam Girdwood return paths;
2668618dae11SLiam Girdwood
26697083f877SKuninori Morimoto /* update any playback/capture paths */
26707083f877SKuninori Morimoto count = dpcm_process_paths(fe, stream, &list, new);
2671de15d7ffSJerome Brunet if (count) {
2672580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2673de15d7ffSJerome Brunet if (new)
267481c82a9eSKuninori Morimoto dpcm_run_update_startup(fe, stream);
2675de15d7ffSJerome Brunet else
267681c82a9eSKuninori Morimoto dpcm_run_update_shutdown(fe, stream);
2677580dff36SKuninori Morimoto dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2678de15d7ffSJerome Brunet
26797083f877SKuninori Morimoto dpcm_clear_pending_state(fe, stream);
26807083f877SKuninori Morimoto dpcm_be_disconnect(fe, stream);
2681618dae11SLiam Girdwood }
2682618dae11SLiam Girdwood
26837ed9de76SQiao Zhou dpcm_path_put(&list);
2684618dae11SLiam Girdwood }
2685618dae11SLiam Girdwood
2686de15d7ffSJerome Brunet return 0;
2687618dae11SLiam Girdwood }
2688618dae11SLiam Girdwood
2689de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2690de15d7ffSJerome Brunet * any DAI links.
2691de15d7ffSJerome Brunet */
snd_soc_dpcm_runtime_update(struct snd_soc_card * card)2692f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2693de15d7ffSJerome Brunet {
2694de15d7ffSJerome Brunet struct snd_soc_pcm_runtime *fe;
2695de15d7ffSJerome Brunet int ret = 0;
2696de15d7ffSJerome Brunet
269738e42f6dSKuninori Morimoto snd_soc_dpcm_mutex_lock(card);
2698de15d7ffSJerome Brunet /* shutdown all old paths first */
2699bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) {
2700de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 0);
2701de15d7ffSJerome Brunet if (ret)
2702de15d7ffSJerome Brunet goto out;
2703de15d7ffSJerome Brunet }
2704de15d7ffSJerome Brunet
2705de15d7ffSJerome Brunet /* bring new paths up */
2706bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, fe) {
2707de15d7ffSJerome Brunet ret = soc_dpcm_fe_runtime_update(fe, 1);
2708de15d7ffSJerome Brunet if (ret)
2709de15d7ffSJerome Brunet goto out;
2710de15d7ffSJerome Brunet }
2711de15d7ffSJerome Brunet
2712de15d7ffSJerome Brunet out:
271338e42f6dSKuninori Morimoto snd_soc_dpcm_mutex_unlock(card);
2714de15d7ffSJerome Brunet return ret;
2715618dae11SLiam Girdwood }
2716f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
271701d7584cSLiam Girdwood
dpcm_fe_dai_cleanup(struct snd_pcm_substream * fe_substream)2718265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
271901d7584cSLiam Girdwood {
27200ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
272101d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
2722265694b6SKuninori Morimoto int stream = fe_substream->stream;
272330fca26fSKuninori Morimoto
2724b7898396STakashi Iwai snd_soc_dpcm_mutex_assert_held(fe);
2725b7898396STakashi Iwai
272630fca26fSKuninori Morimoto /* mark FE's links ready to prune */
272730fca26fSKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm)
272830fca26fSKuninori Morimoto dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
272930fca26fSKuninori Morimoto
273030fca26fSKuninori Morimoto dpcm_be_disconnect(fe, stream);
2731265694b6SKuninori Morimoto }
2732265694b6SKuninori Morimoto
dpcm_fe_dai_close(struct snd_pcm_substream * fe_substream)2733265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2734265694b6SKuninori Morimoto {
27350ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2736265694b6SKuninori Morimoto int ret;
2737265694b6SKuninori Morimoto
2738b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
2739265694b6SKuninori Morimoto ret = dpcm_fe_dai_shutdown(fe_substream);
2740265694b6SKuninori Morimoto
2741265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream);
2742265694b6SKuninori Morimoto
2743b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
274430fca26fSKuninori Morimoto return ret;
274530fca26fSKuninori Morimoto }
274630fca26fSKuninori Morimoto
dpcm_fe_dai_open(struct snd_pcm_substream * fe_substream)274701d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
274801d7584cSLiam Girdwood {
27490ceef681SKuninori Morimoto struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
275001d7584cSLiam Girdwood struct snd_soc_dapm_widget_list *list;
275101d7584cSLiam Girdwood int ret;
275201d7584cSLiam Girdwood int stream = fe_substream->stream;
275301d7584cSLiam Girdwood
2754b7898396STakashi Iwai snd_soc_dpcm_mutex_lock(fe);
275501d7584cSLiam Girdwood
27568f70e515SQiao Zhou ret = dpcm_path_get(fe, stream, &list);
2757d479f00bSKuninori Morimoto if (ret < 0)
2758cae06eb9SKuninori Morimoto goto open_end;
275901d7584cSLiam Girdwood
276001d7584cSLiam Girdwood /* calculate valid and active FE <-> BE dpcms */
276101d7584cSLiam Girdwood dpcm_process_paths(fe, stream, &list, 1);
276201d7584cSLiam Girdwood
276301d7584cSLiam Girdwood ret = dpcm_fe_dai_startup(fe_substream);
2764265694b6SKuninori Morimoto if (ret < 0)
2765265694b6SKuninori Morimoto dpcm_fe_dai_cleanup(fe_substream);
276601d7584cSLiam Girdwood
276701d7584cSLiam Girdwood dpcm_clear_pending_state(fe, stream);
276801d7584cSLiam Girdwood dpcm_path_put(&list);
2769cae06eb9SKuninori Morimoto open_end:
2770b7898396STakashi Iwai snd_soc_dpcm_mutex_unlock(fe);
277101d7584cSLiam Girdwood return ret;
277201d7584cSLiam Girdwood }
277301d7584cSLiam Girdwood
soc_get_playback_capture(struct snd_soc_pcm_runtime * rtd,int * playback,int * capture)27747fc6bebdSKuninori Morimoto static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
27757fc6bebdSKuninori Morimoto int *playback, int *capture)
2776ddee627cSLiam Girdwood {
2777cfcb31c4SKuninori Morimoto struct snd_soc_dai_link *dai_link = rtd->dai_link;
277819bdcc7aSShreyas NC struct snd_soc_dai *cpu_dai;
2779c3e9b6d6SKuninori Morimoto int has_playback = 0;
2780c3e9b6d6SKuninori Morimoto int has_capture = 0;
27812e5894d7SBenoit Cousson int i;
2782ddee627cSLiam Girdwood
2783cfcb31c4SKuninori Morimoto if (dai_link->dynamic && dai_link->num_cpus > 1) {
2784a1c0221fSKuninori Morimoto dev_err(rtd->dev, "DPCM doesn't support Multi CPU for Front-Ends yet\n");
27859b5db059SStephan Gerhold return -EINVAL;
27869b5db059SStephan Gerhold }
27879b5db059SStephan Gerhold
2788cfcb31c4SKuninori Morimoto if (dai_link->dynamic || dai_link->no_pcm) {
2789940a1f43SKuninori Morimoto int stream;
2790940a1f43SKuninori Morimoto
2791cfcb31c4SKuninori Morimoto if (dai_link->dpcm_playback) {
2792b73287f0SPierre-Louis Bossart stream = SNDRV_PCM_STREAM_PLAYBACK;
2793b73287f0SPierre-Louis Bossart
27944f872154SPierre-Louis Bossart for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
27954f872154SPierre-Louis Bossart if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2796c3e9b6d6SKuninori Morimoto has_playback = 1;
27974f872154SPierre-Louis Bossart break;
27984f872154SPierre-Louis Bossart }
27994f872154SPierre-Louis Bossart }
2800c3e9b6d6SKuninori Morimoto if (!has_playback) {
2801b73287f0SPierre-Louis Bossart dev_err(rtd->card->dev,
28024f872154SPierre-Louis Bossart "No CPU DAIs support playback for stream %s\n",
2803cfcb31c4SKuninori Morimoto dai_link->stream_name);
2804b73287f0SPierre-Louis Bossart return -EINVAL;
2805b73287f0SPierre-Louis Bossart }
2806b73287f0SPierre-Louis Bossart }
2807cfcb31c4SKuninori Morimoto if (dai_link->dpcm_capture) {
2808b73287f0SPierre-Louis Bossart stream = SNDRV_PCM_STREAM_CAPTURE;
2809b73287f0SPierre-Louis Bossart
28104f872154SPierre-Louis Bossart for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
28114f872154SPierre-Louis Bossart if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2812c3e9b6d6SKuninori Morimoto has_capture = 1;
28134f872154SPierre-Louis Bossart break;
28144f872154SPierre-Louis Bossart }
28154f872154SPierre-Louis Bossart }
28164f872154SPierre-Louis Bossart
2817c3e9b6d6SKuninori Morimoto if (!has_capture) {
2818b73287f0SPierre-Louis Bossart dev_err(rtd->card->dev,
28194f872154SPierre-Louis Bossart "No CPU DAIs support capture for stream %s\n",
2820cfcb31c4SKuninori Morimoto dai_link->stream_name);
2821b73287f0SPierre-Louis Bossart return -EINVAL;
2822b73287f0SPierre-Louis Bossart }
2823b73287f0SPierre-Louis Bossart }
282401d7584cSLiam Girdwood } else {
2825940a1f43SKuninori Morimoto struct snd_soc_dai *codec_dai;
2826940a1f43SKuninori Morimoto
2827a342031cSJerome Brunet /* Adapt stream for codec2codec links */
28281c943f60SKuninori Morimoto int cpu_capture = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_CAPTURE);
28291c943f60SKuninori Morimoto int cpu_playback = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_PLAYBACK);
2830a342031cSJerome Brunet
2831a4be4187SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
2832cfcb31c4SKuninori Morimoto if (dai_link->num_cpus == 1) {
2833c2233a26SKuninori Morimoto cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2834cfcb31c4SKuninori Morimoto } else if (dai_link->num_cpus == dai_link->num_codecs) {
2835c2233a26SKuninori Morimoto cpu_dai = asoc_rtd_to_cpu(rtd, i);
2836ac950278SBard Liao } else if (rtd->dai_link->num_codecs > rtd->dai_link->num_cpus) {
2837ac950278SBard Liao int cpu_id;
2838ac950278SBard Liao
2839ac950278SBard Liao if (!rtd->dai_link->codec_ch_maps) {
2840ac950278SBard Liao dev_err(rtd->card->dev, "%s: no codec channel mapping table provided\n",
2841ac950278SBard Liao __func__);
2842ac950278SBard Liao return -EINVAL;
2843ac950278SBard Liao }
2844ac950278SBard Liao
2845ac950278SBard Liao cpu_id = rtd->dai_link->codec_ch_maps[i].connected_cpu_id;
2846ac950278SBard Liao cpu_dai = asoc_rtd_to_cpu(rtd, cpu_id);
284719bdcc7aSShreyas NC } else {
284819bdcc7aSShreyas NC dev_err(rtd->card->dev,
2849ac950278SBard Liao "%s codec number %d < cpu number %d is not supported\n",
2850ac950278SBard Liao __func__, rtd->dai_link->num_codecs,
2851ac950278SBard Liao rtd->dai_link->num_cpus);
285219bdcc7aSShreyas NC return -EINVAL;
285319bdcc7aSShreyas NC }
285419bdcc7aSShreyas NC
2855467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2856a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2857c3e9b6d6SKuninori Morimoto has_playback = 1;
2858467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2859a4877a6fSStephan Gerhold snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2860c3e9b6d6SKuninori Morimoto has_capture = 1;
286101d7584cSLiam Girdwood }
28622e5894d7SBenoit Cousson }
28632e5894d7SBenoit Cousson
2864e1f653ceSKuninori Morimoto if (dai_link->playback_only)
2865c3e9b6d6SKuninori Morimoto has_capture = 0;
2866d6bead02SFabio Estevam
2867e1f653ceSKuninori Morimoto if (dai_link->capture_only)
2868c3e9b6d6SKuninori Morimoto has_playback = 0;
2869d6bead02SFabio Estevam
2870c3e9b6d6SKuninori Morimoto if (!has_playback && !has_capture) {
2871092830cfSKuninori Morimoto dev_err(rtd->dev, "substream %s has no playback, no capture\n",
2872cfcb31c4SKuninori Morimoto dai_link->stream_name);
2873092830cfSKuninori Morimoto
2874092830cfSKuninori Morimoto return -EINVAL;
2875092830cfSKuninori Morimoto }
2876092830cfSKuninori Morimoto
2877c3e9b6d6SKuninori Morimoto *playback = has_playback;
2878c3e9b6d6SKuninori Morimoto *capture = has_capture;
2879c3e9b6d6SKuninori Morimoto
28807fc6bebdSKuninori Morimoto return 0;
28817fc6bebdSKuninori Morimoto }
28827fc6bebdSKuninori Morimoto
soc_create_pcm(struct snd_pcm ** pcm,struct snd_soc_pcm_runtime * rtd,int playback,int capture,int num)28832b39123bSKuninori Morimoto static int soc_create_pcm(struct snd_pcm **pcm,
28842b39123bSKuninori Morimoto struct snd_soc_pcm_runtime *rtd,
28852b39123bSKuninori Morimoto int playback, int capture, int num)
28867fc6bebdSKuninori Morimoto {
28877fc6bebdSKuninori Morimoto char new_name[64];
28882b39123bSKuninori Morimoto int ret;
28897fc6bebdSKuninori Morimoto
289001d7584cSLiam Girdwood /* create the PCM */
28917ddc7f91SKuninori Morimoto if (rtd->dai_link->c2c_params) {
2892a342031cSJerome Brunet snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2893a342031cSJerome Brunet rtd->dai_link->stream_name);
2894a342031cSJerome Brunet
2895a342031cSJerome Brunet ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
28962b39123bSKuninori Morimoto playback, capture, pcm);
2897a342031cSJerome Brunet } else if (rtd->dai_link->no_pcm) {
289801d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)",
289901d7584cSLiam Girdwood rtd->dai_link->stream_name);
290001d7584cSLiam Girdwood
290101d7584cSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
29022b39123bSKuninori Morimoto playback, capture, pcm);
290301d7584cSLiam Girdwood } else {
290401d7584cSLiam Girdwood if (rtd->dai_link->dynamic)
290501d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s (*)",
290601d7584cSLiam Girdwood rtd->dai_link->stream_name);
290701d7584cSLiam Girdwood else
290801d7584cSLiam Girdwood snprintf(new_name, sizeof(new_name), "%s %s-%d",
29092e5894d7SBenoit Cousson rtd->dai_link->stream_name,
29106fb8944cSKuninori Morimoto soc_codec_dai_name(rtd), num);
291101d7584cSLiam Girdwood
291201d7584cSLiam Girdwood ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
29132b39123bSKuninori Morimoto capture, pcm);
291401d7584cSLiam Girdwood }
2915ddee627cSLiam Girdwood if (ret < 0) {
2916799827a4SPierre-Louis Bossart dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2917799827a4SPierre-Louis Bossart new_name, rtd->dai_link->name, ret);
2918ddee627cSLiam Girdwood return ret;
2919ddee627cSLiam Girdwood }
2920103d84a3SLiam Girdwood dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2921ddee627cSLiam Girdwood
29222b39123bSKuninori Morimoto return 0;
29232b39123bSKuninori Morimoto }
29242b39123bSKuninori Morimoto
29252b39123bSKuninori Morimoto /* create a new pcm */
soc_new_pcm(struct snd_soc_pcm_runtime * rtd,int num)29262b39123bSKuninori Morimoto int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
29272b39123bSKuninori Morimoto {
29282b39123bSKuninori Morimoto struct snd_soc_component *component;
29292b39123bSKuninori Morimoto struct snd_pcm *pcm;
29302b39123bSKuninori Morimoto int ret = 0, playback = 0, capture = 0;
29312b39123bSKuninori Morimoto int i;
29322b39123bSKuninori Morimoto
29332b39123bSKuninori Morimoto ret = soc_get_playback_capture(rtd, &playback, &capture);
29342b39123bSKuninori Morimoto if (ret < 0)
29352b39123bSKuninori Morimoto return ret;
29362b39123bSKuninori Morimoto
29372b39123bSKuninori Morimoto ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
29382b39123bSKuninori Morimoto if (ret < 0)
29392b39123bSKuninori Morimoto return ret;
29402b39123bSKuninori Morimoto
2941ddee627cSLiam Girdwood /* DAPM dai link stream work */
294210d5d8cbSKuninori Morimoto /*
294310d5d8cbSKuninori Morimoto * Currently nothing to do for c2c links
294410d5d8cbSKuninori Morimoto * Since c2c links are internal nodes in the DAPM graph and
294510d5d8cbSKuninori Morimoto * don't interface with the outside world or application layer
294610d5d8cbSKuninori Morimoto * we don't have to do any special handling on close.
294710d5d8cbSKuninori Morimoto */
29487ddc7f91SKuninori Morimoto if (!rtd->dai_link->c2c_params)
294983f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2950ddee627cSLiam Girdwood
2951ddee627cSLiam Girdwood rtd->pcm = pcm;
2952e04e7b8cSKuninori Morimoto pcm->nonatomic = rtd->dai_link->nonatomic;
2953ddee627cSLiam Girdwood pcm->private_data = rtd;
29544d45d944SKuninori Morimoto pcm->no_device_suspend = true;
295501d7584cSLiam Girdwood
29567ddc7f91SKuninori Morimoto if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
295701d7584cSLiam Girdwood if (playback)
295801d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
295901d7584cSLiam Girdwood if (capture)
296001d7584cSLiam Girdwood pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
296101d7584cSLiam Girdwood goto out;
296201d7584cSLiam Girdwood }
296301d7584cSLiam Girdwood
296401d7584cSLiam Girdwood /* ASoC PCM operations */
296501d7584cSLiam Girdwood if (rtd->dai_link->dynamic) {
296601d7584cSLiam Girdwood rtd->ops.open = dpcm_fe_dai_open;
296701d7584cSLiam Girdwood rtd->ops.hw_params = dpcm_fe_dai_hw_params;
296801d7584cSLiam Girdwood rtd->ops.prepare = dpcm_fe_dai_prepare;
296901d7584cSLiam Girdwood rtd->ops.trigger = dpcm_fe_dai_trigger;
297001d7584cSLiam Girdwood rtd->ops.hw_free = dpcm_fe_dai_hw_free;
297101d7584cSLiam Girdwood rtd->ops.close = dpcm_fe_dai_close;
297201d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer;
297301d7584cSLiam Girdwood } else {
297401d7584cSLiam Girdwood rtd->ops.open = soc_pcm_open;
297501d7584cSLiam Girdwood rtd->ops.hw_params = soc_pcm_hw_params;
297601d7584cSLiam Girdwood rtd->ops.prepare = soc_pcm_prepare;
297701d7584cSLiam Girdwood rtd->ops.trigger = soc_pcm_trigger;
297801d7584cSLiam Girdwood rtd->ops.hw_free = soc_pcm_hw_free;
297901d7584cSLiam Girdwood rtd->ops.close = soc_pcm_close;
298001d7584cSLiam Girdwood rtd->ops.pointer = soc_pcm_pointer;
298101d7584cSLiam Girdwood }
298201d7584cSLiam Girdwood
2983613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) {
29842b544dd7SKuninori Morimoto const struct snd_soc_component_driver *drv = component->driver;
2985b8135864SKuninori Morimoto
29863b1c952cSTakashi Iwai if (drv->ioctl)
29873b1c952cSTakashi Iwai rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
29881e5ddb6bSTakashi Iwai if (drv->sync_stop)
29891e5ddb6bSTakashi Iwai rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
299066201cacSTakashi Iwai if (drv->copy)
299166201cacSTakashi Iwai rtd->ops.copy = snd_soc_pcm_component_copy;
2992e9067bb5SKuninori Morimoto if (drv->page)
29939c712e4fSKuninori Morimoto rtd->ops.page = snd_soc_pcm_component_page;
2994e9067bb5SKuninori Morimoto if (drv->mmap)
2995205875e1SKuninori Morimoto rtd->ops.mmap = snd_soc_pcm_component_mmap;
29968bdfc045SShengjiu Wang if (drv->ack)
29978bdfc045SShengjiu Wang rtd->ops.ack = snd_soc_pcm_component_ack;
2998b8135864SKuninori Morimoto }
2999b8135864SKuninori Morimoto
3000ddee627cSLiam Girdwood if (playback)
300101d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3002ddee627cSLiam Girdwood
3003ddee627cSLiam Girdwood if (capture)
300401d7584cSLiam Girdwood snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3005ddee627cSLiam Girdwood
3006b2b2afbbSKuninori Morimoto ret = snd_soc_pcm_component_new(rtd);
300760adbd8fSKuninori Morimoto if (ret < 0)
3008ddee627cSLiam Girdwood return ret;
300901d7584cSLiam Girdwood out:
30101d5cd525SPierre-Louis Bossart dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
30116fb8944cSKuninori Morimoto soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
3012ddee627cSLiam Girdwood return ret;
3013ddee627cSLiam Girdwood }
301401d7584cSLiam Girdwood
301501d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime * fe,int stream)301601d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
301701d7584cSLiam Girdwood {
301801d7584cSLiam Girdwood if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
301901d7584cSLiam Girdwood return 1;
302001d7584cSLiam Girdwood return 0;
302101d7584cSLiam Girdwood }
302201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
302301d7584cSLiam Girdwood
302401d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)302501d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
302601d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream)
302701d7584cSLiam Girdwood {
302801d7584cSLiam Girdwood if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
302901d7584cSLiam Girdwood ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
303001d7584cSLiam Girdwood be->dpcm[stream].runtime_update))
303101d7584cSLiam Girdwood return 1;
303201d7584cSLiam Girdwood return 0;
303301d7584cSLiam Girdwood }
303401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
303501d7584cSLiam Girdwood
303601d7584cSLiam Girdwood /* get the substream for this BE */
303701d7584cSLiam Girdwood struct snd_pcm_substream *
snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime * be,int stream)303801d7584cSLiam Girdwood snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
303901d7584cSLiam Girdwood {
304001d7584cSLiam Girdwood return be->pcm->streams[stream].substream;
304101d7584cSLiam Girdwood }
304201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
304301d7584cSLiam Girdwood
snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream,const enum snd_soc_dpcm_state * states,int num_states)3044085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3045085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be,
3046085d22beSKuninori Morimoto int stream,
3047085d22beSKuninori Morimoto const enum snd_soc_dpcm_state *states,
3048085d22beSKuninori Morimoto int num_states)
304901d7584cSLiam Girdwood {
305001d7584cSLiam Girdwood struct snd_soc_dpcm *dpcm;
305101d7584cSLiam Girdwood int state;
3052a9764869SKaiChieh Chuang int ret = 1;
3053085d22beSKuninori Morimoto int i;
305401d7584cSLiam Girdwood
3055d2e24d64SKuninori Morimoto for_each_dpcm_fe(be, stream, dpcm) {
305601d7584cSLiam Girdwood
305701d7584cSLiam Girdwood if (dpcm->fe == fe)
305801d7584cSLiam Girdwood continue;
305901d7584cSLiam Girdwood
306001d7584cSLiam Girdwood state = dpcm->fe->dpcm[stream].state;
3061085d22beSKuninori Morimoto for (i = 0; i < num_states; i++) {
3062085d22beSKuninori Morimoto if (state == states[i]) {
3063a9764869SKaiChieh Chuang ret = 0;
3064a9764869SKaiChieh Chuang break;
306501d7584cSLiam Girdwood }
3066a9764869SKaiChieh Chuang }
3067085d22beSKuninori Morimoto }
306801d7584cSLiam Girdwood
3069085d22beSKuninori Morimoto /* it's safe to do this BE DAI */
3070a9764869SKaiChieh Chuang return ret;
307101d7584cSLiam Girdwood }
3072085d22beSKuninori Morimoto
3073085d22beSKuninori Morimoto /*
3074085d22beSKuninori Morimoto * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3075085d22beSKuninori Morimoto * are not running, paused or suspended for the specified stream direction.
3076085d22beSKuninori Morimoto */
snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)3077085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3078085d22beSKuninori Morimoto struct snd_soc_pcm_runtime *be, int stream)
3079085d22beSKuninori Morimoto {
3080085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = {
3081085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START,
3082085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED,
3083085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND,
3084085d22beSKuninori Morimoto };
3085085d22beSKuninori Morimoto
3086085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3087085d22beSKuninori Morimoto }
308801d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
308901d7584cSLiam Girdwood
309001d7584cSLiam Girdwood /*
309101d7584cSLiam Girdwood * We can only change hw params a BE DAI if any of it's FE are not prepared,
309201d7584cSLiam Girdwood * running, paused or suspended for the specified stream direction.
309301d7584cSLiam Girdwood */
snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)309401d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
309501d7584cSLiam Girdwood struct snd_soc_pcm_runtime *be, int stream)
309601d7584cSLiam Girdwood {
3097085d22beSKuninori Morimoto const enum snd_soc_dpcm_state state[] = {
3098085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_START,
3099085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PAUSED,
3100085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_SUSPEND,
3101085d22beSKuninori Morimoto SND_SOC_DPCM_STATE_PREPARE,
3102085d22beSKuninori Morimoto };
310301d7584cSLiam Girdwood
3104085d22beSKuninori Morimoto return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
310501d7584cSLiam Girdwood }
310601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3107e123036bSRanjani Sridharan
3108e123036bSRanjani Sridharan /*
3109e123036bSRanjani Sridharan * We can only prepare a BE DAI if any of it's FE are not prepared,
3110e123036bSRanjani Sridharan * running or paused for the specified stream direction.
3111e123036bSRanjani Sridharan */
snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)3112e123036bSRanjani Sridharan int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe,
3113e123036bSRanjani Sridharan struct snd_soc_pcm_runtime *be, int stream)
3114e123036bSRanjani Sridharan {
3115e123036bSRanjani Sridharan const enum snd_soc_dpcm_state state[] = {
3116e123036bSRanjani Sridharan SND_SOC_DPCM_STATE_START,
3117e123036bSRanjani Sridharan SND_SOC_DPCM_STATE_PAUSED,
3118e123036bSRanjani Sridharan SND_SOC_DPCM_STATE_PREPARE,
3119e123036bSRanjani Sridharan };
3120e123036bSRanjani Sridharan
3121e123036bSRanjani Sridharan return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3122e123036bSRanjani Sridharan }
3123e123036bSRanjani Sridharan EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_prepared);
3124