1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 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. 5 // 6 // Copyright(c) 2018 Intel Corporation. All rights reserved. 7 // 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> 12 // 13 14 /* 15 * Hardware interface for generic Intel audio DSP HDA IP 16 */ 17 18 #include <sound/hda_register.h> 19 #include <sound/pcm_params.h> 20 #include "../sof-audio.h" 21 #include "../ops.h" 22 #include "hda.h" 23 24 #define SDnFMT_BASE(x) ((x) << 14) 25 #define SDnFMT_MULT(x) (((x) - 1) << 11) 26 #define SDnFMT_DIV(x) (((x) - 1) << 8) 27 #define SDnFMT_BITS(x) ((x) << 4) 28 #define SDnFMT_CHAN(x) ((x) << 0) 29 30 u32 hda_dsp_get_mult_div(struct snd_sof_dev *sdev, int rate) 31 { 32 switch (rate) { 33 case 8000: 34 return SDnFMT_DIV(6); 35 case 9600: 36 return SDnFMT_DIV(5); 37 case 11025: 38 return SDnFMT_BASE(1) | SDnFMT_DIV(4); 39 case 16000: 40 return SDnFMT_DIV(3); 41 case 22050: 42 return SDnFMT_BASE(1) | SDnFMT_DIV(2); 43 case 32000: 44 return SDnFMT_DIV(3) | SDnFMT_MULT(2); 45 case 44100: 46 return SDnFMT_BASE(1); 47 case 48000: 48 return 0; 49 case 88200: 50 return SDnFMT_BASE(1) | SDnFMT_MULT(2); 51 case 96000: 52 return SDnFMT_MULT(2); 53 case 176400: 54 return SDnFMT_BASE(1) | SDnFMT_MULT(4); 55 case 192000: 56 return SDnFMT_MULT(4); 57 default: 58 dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n", 59 rate); 60 return 0; /* use 48KHz if not found */ 61 } 62 }; 63 64 u32 hda_dsp_get_bits(struct snd_sof_dev *sdev, int sample_bits) 65 { 66 switch (sample_bits) { 67 case 8: 68 return SDnFMT_BITS(0); 69 case 16: 70 return SDnFMT_BITS(1); 71 case 20: 72 return SDnFMT_BITS(2); 73 case 24: 74 return SDnFMT_BITS(3); 75 case 32: 76 return SDnFMT_BITS(4); 77 default: 78 dev_warn(sdev->dev, "can't find %d bits using 16bit\n", 79 sample_bits); 80 return SDnFMT_BITS(1); /* use 16bits format if not found */ 81 } 82 }; 83 84 int hda_dsp_pcm_hw_params(struct snd_sof_dev *sdev, 85 struct snd_pcm_substream *substream, 86 struct snd_pcm_hw_params *params, 87 struct sof_ipc_stream_params *ipc_params) 88 { 89 struct hdac_stream *hstream = substream->runtime->private_data; 90 struct hdac_ext_stream *stream = stream_to_hdac_ext_stream(hstream); 91 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 92 struct snd_dma_buffer *dmab; 93 struct sof_ipc_fw_version *v = &sdev->fw_ready.version; 94 int ret; 95 u32 size, rate, bits; 96 97 size = params_buffer_bytes(params); 98 rate = hda_dsp_get_mult_div(sdev, params_rate(params)); 99 bits = hda_dsp_get_bits(sdev, params_width(params)); 100 101 hstream->substream = substream; 102 103 dmab = substream->runtime->dma_buffer_p; 104 105 hstream->format_val = rate | bits | (params_channels(params) - 1); 106 hstream->bufsize = size; 107 hstream->period_bytes = params_period_bytes(params); 108 hstream->no_period_wakeup = 109 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) && 110 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP); 111 112 ret = hda_dsp_stream_hw_params(sdev, stream, dmab, params); 113 if (ret < 0) { 114 dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret); 115 return ret; 116 } 117 118 /* disable SPIB, to enable buffer wrap for stream */ 119 hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0); 120 121 /* update no_stream_position flag for ipc params */ 122 if (hda && hda->no_ipc_position) { 123 /* For older ABIs set host_period_bytes to zero to inform 124 * FW we don't want position updates. Newer versions use 125 * no_stream_position for this purpose. 126 */ 127 if (v->abi_version < SOF_ABI_VER(3, 10, 0)) 128 ipc_params->host_period_bytes = 0; 129 else 130 ipc_params->no_stream_position = 1; 131 } 132 133 ipc_params->stream_tag = hstream->stream_tag; 134 135 return 0; 136 } 137 138 int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev, 139 struct snd_pcm_substream *substream, int cmd) 140 { 141 struct hdac_stream *hstream = substream->runtime->private_data; 142 struct hdac_ext_stream *stream = stream_to_hdac_ext_stream(hstream); 143 144 return hda_dsp_stream_trigger(sdev, stream, cmd); 145 } 146 147 snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev, 148 struct snd_pcm_substream *substream) 149 { 150 struct snd_soc_pcm_runtime *rtd = substream->private_data; 151 struct snd_soc_component *scomp = sdev->component; 152 struct hdac_stream *hstream = substream->runtime->private_data; 153 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata; 154 struct snd_sof_pcm *spcm; 155 snd_pcm_uframes_t pos; 156 157 spcm = snd_sof_find_spcm_dai(scomp, rtd); 158 if (!spcm) { 159 dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n", 160 rtd->dai_link->id); 161 return 0; 162 } 163 164 if (hda && !hda->no_ipc_position) { 165 /* read position from IPC position */ 166 pos = spcm->stream[substream->stream].posn.host_posn; 167 goto found; 168 } 169 170 /* 171 * DPIB/posbuf position mode: 172 * For Playback, Use DPIB register from HDA space which 173 * reflects the actual data transferred. 174 * For Capture, Use the position buffer for pointer, as DPIB 175 * is not accurate enough, its update may be completed 176 * earlier than the data written to DDR. 177 */ 178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 179 pos = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, 180 AZX_REG_VS_SDXDPIB_XBASE + 181 (AZX_REG_VS_SDXDPIB_XINTERVAL * 182 hstream->index)); 183 } else { 184 /* 185 * For capture stream, we need more workaround to fix the 186 * position incorrect issue: 187 * 188 * 1. Wait at least 20us before reading position buffer after 189 * the interrupt generated(IOC), to make sure position update 190 * happens on frame boundary i.e. 20.833uSec for 48KHz. 191 * 2. Perform a dummy Read to DPIB register to flush DMA 192 * position value. 193 * 3. Read the DMA Position from posbuf. Now the readback 194 * value should be >= period boundary. 195 */ 196 usleep_range(20, 21); 197 snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, 198 AZX_REG_VS_SDXDPIB_XBASE + 199 (AZX_REG_VS_SDXDPIB_XINTERVAL * 200 hstream->index)); 201 pos = snd_hdac_stream_get_pos_posbuf(hstream); 202 } 203 204 if (pos >= hstream->bufsize) 205 pos = 0; 206 207 found: 208 pos = bytes_to_frames(substream->runtime, pos); 209 210 dev_vdbg(sdev->dev, "PCM: stream %d dir %d position %lu\n", 211 hstream->index, substream->stream, pos); 212 return pos; 213 } 214 215 int hda_dsp_pcm_open(struct snd_sof_dev *sdev, 216 struct snd_pcm_substream *substream) 217 { 218 struct hdac_ext_stream *dsp_stream; 219 int direction = substream->stream; 220 221 dsp_stream = hda_dsp_stream_get(sdev, direction); 222 223 if (!dsp_stream) { 224 dev_err(sdev->dev, "error: no stream available\n"); 225 return -ENODEV; 226 } 227 228 /* binding pcm substream to hda stream */ 229 substream->runtime->private_data = &dsp_stream->hstream; 230 return 0; 231 } 232 233 int hda_dsp_pcm_close(struct snd_sof_dev *sdev, 234 struct snd_pcm_substream *substream) 235 { 236 struct hdac_stream *hstream = substream->runtime->private_data; 237 int direction = substream->stream; 238 int ret; 239 240 ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag); 241 242 if (ret) { 243 dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name); 244 return -ENODEV; 245 } 246 247 /* unbinding pcm substream to hda stream */ 248 substream->runtime->private_data = NULL; 249 return 0; 250 } 251