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: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 // Rander Wang <rander.wang@intel.com>
11 // Keyon Jie <yang.jie@linux.intel.com>
15 * Hardware interface for generic Intel audio DSP HDA IP
18 #include <sound/hda_register.h>
19 #include <sound/pcm_params.h>
23 #define SDnFMT_BASE(x) ((x) << 14)
24 #define SDnFMT_MULT(x) (((x) - 1) << 11)
25 #define SDnFMT_DIV(x) (((x) - 1) << 8)
26 #define SDnFMT_BITS(x) ((x) << 4)
27 #define SDnFMT_CHAN(x) ((x) << 0)
29 static inline u32 get_mult_div(struct snd_sof_dev *sdev, int rate)
37 return SDnFMT_BASE(1) | SDnFMT_DIV(4);
41 return SDnFMT_BASE(1) | SDnFMT_DIV(2);
43 return SDnFMT_DIV(3) | SDnFMT_MULT(2);
45 return SDnFMT_BASE(1);
49 return SDnFMT_BASE(1) | SDnFMT_MULT(2);
51 return SDnFMT_MULT(2);
53 return SDnFMT_BASE(1) | SDnFMT_MULT(4);
55 return SDnFMT_MULT(4);
57 dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n",
59 return 0; /* use 48KHz if not found */
63 static inline u32 get_bits(struct snd_sof_dev *sdev, int sample_bits)
65 switch (sample_bits) {
67 return SDnFMT_BITS(0);
69 return SDnFMT_BITS(1);
71 return SDnFMT_BITS(2);
73 return SDnFMT_BITS(3);
75 return SDnFMT_BITS(4);
77 dev_warn(sdev->dev, "can't find %d bits using 16bit\n",
79 return SDnFMT_BITS(1); /* use 16bits format if not found */
83 int hda_dsp_pcm_hw_params(struct snd_sof_dev *sdev,
84 struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *params,
86 struct sof_ipc_stream_params *ipc_params)
88 struct hdac_stream *hstream = substream->runtime->private_data;
89 struct hdac_ext_stream *stream = stream_to_hdac_ext_stream(hstream);
90 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
91 struct snd_dma_buffer *dmab;
92 struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
96 size = params_buffer_bytes(params);
97 rate = get_mult_div(sdev, params_rate(params));
98 bits = get_bits(sdev, params_width(params));
100 hstream->substream = substream;
102 dmab = substream->runtime->dma_buffer_p;
104 hstream->format_val = rate | bits | (params_channels(params) - 1);
105 hstream->bufsize = size;
106 hstream->period_bytes = params_period_bytes(params);
107 hstream->no_period_wakeup =
108 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
109 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
111 ret = hda_dsp_stream_hw_params(sdev, stream, dmab, params);
113 dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret);
117 /* disable SPIB, to enable buffer wrap for stream */
118 hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0);
120 /* update no_stream_position flag for ipc params */
121 if (hda && hda->no_ipc_position) {
122 /* For older ABIs set host_period_bytes to zero to inform
123 * FW we don't want position updates. Newer versions use
124 * no_stream_position for this purpose.
126 if (v->abi_version < SOF_ABI_VER(3, 10, 0))
127 ipc_params->host_period_bytes = 0;
129 ipc_params->no_stream_position = 1;
132 ipc_params->stream_tag = hstream->stream_tag;
137 int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev,
138 struct snd_pcm_substream *substream, int cmd)
140 struct hdac_stream *hstream = substream->runtime->private_data;
141 struct hdac_ext_stream *stream = stream_to_hdac_ext_stream(hstream);
143 return hda_dsp_stream_trigger(sdev, stream, cmd);
146 snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev,
147 struct snd_pcm_substream *substream)
149 struct snd_soc_pcm_runtime *rtd = substream->private_data;
150 struct hdac_stream *hstream = substream->runtime->private_data;
151 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
152 struct snd_sof_pcm *spcm;
153 snd_pcm_uframes_t pos;
155 spcm = snd_sof_find_spcm_dai(sdev, rtd);
157 dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
162 if (hda && !hda->no_ipc_position) {
163 /* read position from IPC position */
164 pos = spcm->stream[substream->stream].posn.host_posn;
169 * DPIB/posbuf position mode:
170 * For Playback, Use DPIB register from HDA space which
171 * reflects the actual data transferred.
172 * For Capture, Use the position buffer for pointer, as DPIB
173 * is not accurate enough, its update may be completed
174 * earlier than the data written to DDR.
176 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
177 pos = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
178 AZX_REG_VS_SDXDPIB_XBASE +
179 (AZX_REG_VS_SDXDPIB_XINTERVAL *
183 * For capture stream, we need more workaround to fix the
184 * position incorrect issue:
186 * 1. Wait at least 20us before reading position buffer after
187 * the interrupt generated(IOC), to make sure position update
188 * happens on frame boundary i.e. 20.833uSec for 48KHz.
189 * 2. Perform a dummy Read to DPIB register to flush DMA
191 * 3. Read the DMA Position from posbuf. Now the readback
192 * value should be >= period boundary.
194 usleep_range(20, 21);
195 snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
196 AZX_REG_VS_SDXDPIB_XBASE +
197 (AZX_REG_VS_SDXDPIB_XINTERVAL *
199 pos = snd_hdac_stream_get_pos_posbuf(hstream);
202 if (pos >= hstream->bufsize)
206 pos = bytes_to_frames(substream->runtime, pos);
208 dev_vdbg(sdev->dev, "PCM: stream %d dir %d position %lu\n",
209 hstream->index, substream->stream, pos);
213 int hda_dsp_pcm_open(struct snd_sof_dev *sdev,
214 struct snd_pcm_substream *substream)
216 struct hdac_ext_stream *dsp_stream;
217 int direction = substream->stream;
219 dsp_stream = hda_dsp_stream_get(sdev, direction);
222 dev_err(sdev->dev, "error: no stream available\n");
226 /* binding pcm substream to hda stream */
227 substream->runtime->private_data = &dsp_stream->hstream;
231 int hda_dsp_pcm_close(struct snd_sof_dev *sdev,
232 struct snd_pcm_substream *substream)
234 struct hdac_stream *hstream = substream->runtime->private_data;
235 int direction = substream->stream;
238 ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
241 dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name);
245 /* unbinding pcm substream to hda stream */
246 substream->runtime->private_data = NULL;