1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
11 #include <sound/pcm_params.h>
12 #include <sound/hdaudio_ext.h>
13 #include "../sof-priv.h"
16 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
18 struct hda_pipe_params {
25 snd_pcm_format_t format;
28 unsigned int host_bps;
29 unsigned int link_bps;
33 * Unlike GP dma, there is a set of stream registers in hda controller
34 * to control the link dma channels. Each register controls one link
35 * dma channel and the relation is fixed. To make sure FW uses correct
36 * link dma channels, host allocates stream registers and sends the
37 * corresponding link dma channels to FW to allocate link dma channel
39 * FIXME: this API is abused in the sense that tx_num and rx_num are
40 * passed as arguments, not returned. We need to find a better way to
41 * retrieve the stream tag allocated for the link DMA
43 static int hda_link_dma_get_channels(struct snd_soc_dai *dai,
45 unsigned int *tx_slot,
47 unsigned int *rx_slot)
50 struct hdac_ext_stream *stream;
51 struct snd_pcm_substream substream;
52 struct snd_sof_dev *sdev =
53 snd_soc_component_get_drvdata(dai->component);
55 bus = sof_to_bus(sdev);
57 memset(&substream, 0, sizeof(substream));
59 substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
60 stream = snd_hdac_ext_stream_assign(bus, &substream,
61 HDAC_EXT_STREAM_TYPE_LINK);
63 dev_err(bus->dev, "error: failed to find a free hda ext stream for playback");
67 snd_soc_dai_set_dma_data(dai, &substream, stream);
68 *tx_slot = hdac_stream(stream)->stream_tag - 1;
70 dev_dbg(bus->dev, "link dma channel %d for playback", *tx_slot);
74 substream.stream = SNDRV_PCM_STREAM_CAPTURE;
75 stream = snd_hdac_ext_stream_assign(bus, &substream,
76 HDAC_EXT_STREAM_TYPE_LINK);
78 dev_err(bus->dev, "error: failed to find a free hda ext stream for capture");
82 snd_soc_dai_set_dma_data(dai, &substream, stream);
83 *rx_slot = hdac_stream(stream)->stream_tag - 1;
85 dev_dbg(bus->dev, "link dma channel %d for capture", *rx_slot);
91 static int hda_link_dma_params(struct hdac_ext_stream *stream,
92 struct hda_pipe_params *params)
94 struct hdac_stream *hstream = &stream->hstream;
95 unsigned char stream_tag = hstream->stream_tag;
96 struct hdac_bus *bus = hstream->bus;
97 struct hdac_ext_link *link;
98 unsigned int format_val;
100 snd_hdac_ext_stream_decouple(bus, stream, true);
101 snd_hdac_ext_link_stream_reset(stream);
103 format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
105 params->link_bps, 0);
107 dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
108 format_val, params->s_freq, params->ch, params->format);
110 snd_hdac_ext_link_stream_setup(stream, format_val);
112 if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
113 list_for_each_entry(link, &bus->hlink_list, list) {
114 if (link->index == params->link_index)
115 snd_hdac_ext_link_set_stream_id(link,
120 stream->link_prepared = 1;
125 static int hda_link_hw_params(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *params,
127 struct snd_soc_dai *dai)
129 struct hdac_stream *hstream = substream->runtime->private_data;
130 struct hdac_bus *bus = hstream->bus;
131 struct hdac_ext_stream *link_dev;
132 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
133 struct snd_soc_dai *codec_dai = rtd->codec_dai;
134 struct sof_intel_hda_stream *hda_stream;
135 struct hda_pipe_params p_params = {0};
136 struct hdac_ext_link *link;
139 link_dev = snd_soc_dai_get_dma_data(dai, substream);
141 hda_stream = container_of(link_dev, struct sof_intel_hda_stream,
143 hda_stream->hw_params_upon_resume = 0;
145 link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name);
149 stream_tag = hdac_stream(link_dev)->stream_tag;
151 /* set the stream tag in the codec dai dma params */
152 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
153 snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0);
155 snd_soc_dai_set_tdm_slot(codec_dai, 0, stream_tag, 0, 0);
157 p_params.s_fmt = snd_pcm_format_width(params_format(params));
158 p_params.ch = params_channels(params);
159 p_params.s_freq = params_rate(params);
160 p_params.stream = substream->stream;
161 p_params.link_dma_id = stream_tag - 1;
162 p_params.link_index = link->index;
163 p_params.format = params_format(params);
165 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
166 p_params.link_bps = codec_dai->driver->playback.sig_bits;
168 p_params.link_bps = codec_dai->driver->capture.sig_bits;
170 return hda_link_dma_params(link_dev, &p_params);
173 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream,
174 struct snd_soc_dai *dai)
176 struct hdac_ext_stream *link_dev =
177 snd_soc_dai_get_dma_data(dai, substream);
178 struct sof_intel_hda_stream *hda_stream;
179 struct snd_sof_dev *sdev =
180 snd_soc_component_get_drvdata(dai->component);
181 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
182 int stream = substream->stream;
184 hda_stream = container_of(link_dev, struct sof_intel_hda_stream,
187 /* setup hw_params again only if resuming from system suspend */
188 if (!hda_stream->hw_params_upon_resume)
191 dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream);
193 return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params,
197 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream,
198 int cmd, struct snd_soc_dai *dai)
200 struct hdac_ext_stream *link_dev =
201 snd_soc_dai_get_dma_data(dai, substream);
204 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
206 case SNDRV_PCM_TRIGGER_RESUME:
207 /* set up hw_params */
208 ret = hda_link_pcm_prepare(substream, dai);
211 "error: setting up hw_params during resume\n");
216 case SNDRV_PCM_TRIGGER_START:
217 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
218 snd_hdac_ext_link_stream_start(link_dev);
220 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
221 case SNDRV_PCM_TRIGGER_SUSPEND:
222 case SNDRV_PCM_TRIGGER_STOP:
223 snd_hdac_ext_link_stream_clear(link_dev);
232 * FIXME: This API is also abused since it's used for two purposes.
233 * when the substream argument is NULL this function is used for cleanups
234 * that aren't necessarily required, and called explicitly by handling
235 * ASoC core structures, which is not recommended.
236 * This part will be reworked in follow-up patches.
238 static int hda_link_hw_free(struct snd_pcm_substream *substream,
239 struct snd_soc_dai *dai)
242 unsigned int stream_tag;
243 struct hdac_bus *bus;
244 struct hdac_ext_link *link;
245 struct hdac_stream *hstream;
246 struct hdac_ext_stream *stream;
247 struct snd_soc_pcm_runtime *rtd;
248 struct hdac_ext_stream *link_dev;
249 struct snd_pcm_substream pcm_substream;
251 memset(&pcm_substream, 0, sizeof(pcm_substream));
253 hstream = substream->runtime->private_data;
255 rtd = snd_pcm_substream_chip(substream);
256 link_dev = snd_soc_dai_get_dma_data(dai, substream);
257 snd_hdac_ext_stream_decouple(bus, link_dev, false);
258 name = rtd->codec_dai->component->name;
259 link = snd_hdac_ext_bus_get_link(bus, name);
263 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
264 stream_tag = hdac_stream(link_dev)->stream_tag;
265 snd_hdac_ext_link_clear_stream_id(link, stream_tag);
268 link_dev->link_prepared = 0;
270 /* release all hda streams when dai link is unloaded */
271 pcm_substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
272 stream = snd_soc_dai_get_dma_data(dai, &pcm_substream);
274 snd_soc_dai_set_dma_data(dai, &pcm_substream, NULL);
275 snd_hdac_ext_stream_release(stream,
276 HDAC_EXT_STREAM_TYPE_LINK);
279 pcm_substream.stream = SNDRV_PCM_STREAM_CAPTURE;
280 stream = snd_soc_dai_get_dma_data(dai, &pcm_substream);
282 snd_soc_dai_set_dma_data(dai, &pcm_substream, NULL);
283 snd_hdac_ext_stream_release(stream,
284 HDAC_EXT_STREAM_TYPE_LINK);
291 static const struct snd_soc_dai_ops hda_link_dai_ops = {
292 .hw_params = hda_link_hw_params,
293 .hw_free = hda_link_hw_free,
294 .trigger = hda_link_pcm_trigger,
295 .prepare = hda_link_pcm_prepare,
296 .get_channel_map = hda_link_dma_get_channels,
301 * common dai driver for skl+ platforms.
302 * some products who use this DAI array only physically have a subset of
303 * the DAIs, but no harm is done here by adding the whole set.
305 struct snd_soc_dai_driver skl_dai[] = {
325 .name = "DMIC01 Pin",
328 .name = "DMIC16k Pin",
330 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
332 .name = "iDisp1 Pin",
333 .ops = &hda_link_dai_ops,
336 .name = "iDisp2 Pin",
337 .ops = &hda_link_dai_ops,
340 .name = "iDisp3 Pin",
341 .ops = &hda_link_dai_ops,
344 .name = "Analog CPU DAI",
345 .ops = &hda_link_dai_ops,
348 .name = "Digital CPU DAI",
349 .ops = &hda_link_dai_ops,
352 .name = "Alt Analog CPU DAI",
353 .ops = &hda_link_dai_ops,