0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/moduleparam.h>
0019 #include <sound/hda_register.h>
0020 #include <sound/pcm_params.h>
0021 #include "../sof-audio.h"
0022 #include "../ops.h"
0023 #include "hda.h"
0024
0025 #define SDnFMT_BASE(x) ((x) << 14)
0026 #define SDnFMT_MULT(x) (((x) - 1) << 11)
0027 #define SDnFMT_DIV(x) (((x) - 1) << 8)
0028 #define SDnFMT_BITS(x) ((x) << 4)
0029 #define SDnFMT_CHAN(x) ((x) << 0)
0030
0031 static bool hda_always_enable_dmi_l1;
0032 module_param_named(always_enable_dmi_l1, hda_always_enable_dmi_l1, bool, 0444);
0033 MODULE_PARM_DESC(always_enable_dmi_l1, "SOF HDA always enable DMI l1");
0034
0035 static bool hda_disable_rewinds = IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_DISABLE_REWINDS);
0036 module_param_named(disable_rewinds, hda_disable_rewinds, bool, 0444);
0037 MODULE_PARM_DESC(disable_rewinds, "SOF HDA disable rewinds");
0038
0039 u32 hda_dsp_get_mult_div(struct snd_sof_dev *sdev, int rate)
0040 {
0041 switch (rate) {
0042 case 8000:
0043 return SDnFMT_DIV(6);
0044 case 9600:
0045 return SDnFMT_DIV(5);
0046 case 11025:
0047 return SDnFMT_BASE(1) | SDnFMT_DIV(4);
0048 case 16000:
0049 return SDnFMT_DIV(3);
0050 case 22050:
0051 return SDnFMT_BASE(1) | SDnFMT_DIV(2);
0052 case 32000:
0053 return SDnFMT_DIV(3) | SDnFMT_MULT(2);
0054 case 44100:
0055 return SDnFMT_BASE(1);
0056 case 48000:
0057 return 0;
0058 case 88200:
0059 return SDnFMT_BASE(1) | SDnFMT_MULT(2);
0060 case 96000:
0061 return SDnFMT_MULT(2);
0062 case 176400:
0063 return SDnFMT_BASE(1) | SDnFMT_MULT(4);
0064 case 192000:
0065 return SDnFMT_MULT(4);
0066 default:
0067 dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n",
0068 rate);
0069 return 0;
0070 }
0071 };
0072
0073 u32 hda_dsp_get_bits(struct snd_sof_dev *sdev, int sample_bits)
0074 {
0075 switch (sample_bits) {
0076 case 8:
0077 return SDnFMT_BITS(0);
0078 case 16:
0079 return SDnFMT_BITS(1);
0080 case 20:
0081 return SDnFMT_BITS(2);
0082 case 24:
0083 return SDnFMT_BITS(3);
0084 case 32:
0085 return SDnFMT_BITS(4);
0086 default:
0087 dev_warn(sdev->dev, "can't find %d bits using 16bit\n",
0088 sample_bits);
0089 return SDnFMT_BITS(1);
0090 }
0091 };
0092
0093 int hda_dsp_pcm_hw_params(struct snd_sof_dev *sdev,
0094 struct snd_pcm_substream *substream,
0095 struct snd_pcm_hw_params *params,
0096 struct snd_sof_platform_stream_params *platform_params)
0097 {
0098 struct hdac_stream *hstream = substream->runtime->private_data;
0099 struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
0100 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
0101 struct snd_dma_buffer *dmab;
0102 int ret;
0103 u32 size, rate, bits;
0104
0105 size = params_buffer_bytes(params);
0106 rate = hda_dsp_get_mult_div(sdev, params_rate(params));
0107 bits = hda_dsp_get_bits(sdev, params_width(params));
0108
0109 hstream->substream = substream;
0110
0111 dmab = substream->runtime->dma_buffer_p;
0112
0113 hstream->format_val = rate | bits | (params_channels(params) - 1);
0114 hstream->bufsize = size;
0115 hstream->period_bytes = params_period_bytes(params);
0116 hstream->no_period_wakeup =
0117 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
0118 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
0119
0120 ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, params);
0121 if (ret < 0) {
0122 dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
0123 return ret;
0124 }
0125
0126
0127 if (hda_disable_rewinds)
0128 hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_ENABLE, 0);
0129 else
0130 hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
0131
0132 if (hda)
0133 platform_params->no_ipc_position = hda->no_ipc_position;
0134
0135 platform_params->stream_tag = hstream->stream_tag;
0136
0137 return 0;
0138 }
0139
0140
0141 int hda_dsp_pcm_ack(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
0142 {
0143 struct hdac_stream *hstream = substream->runtime->private_data;
0144 struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
0145 struct snd_pcm_runtime *runtime = substream->runtime;
0146 ssize_t appl_pos, buf_size;
0147 u32 spib;
0148
0149 appl_pos = frames_to_bytes(runtime, runtime->control->appl_ptr);
0150 buf_size = frames_to_bytes(runtime, runtime->buffer_size);
0151
0152 spib = appl_pos % buf_size;
0153
0154
0155 if (!spib)
0156 spib = buf_size;
0157
0158 sof_io_write(sdev, hext_stream->spib_addr, spib);
0159
0160 return 0;
0161 }
0162
0163 int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev,
0164 struct snd_pcm_substream *substream, int cmd)
0165 {
0166 struct hdac_stream *hstream = substream->runtime->private_data;
0167 struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
0168
0169 return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
0170 }
0171
0172 snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev,
0173 struct snd_pcm_substream *substream)
0174 {
0175 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0176 struct snd_soc_component *scomp = sdev->component;
0177 struct hdac_stream *hstream = substream->runtime->private_data;
0178 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
0179 struct snd_sof_pcm *spcm;
0180 snd_pcm_uframes_t pos;
0181
0182 spcm = snd_sof_find_spcm_dai(scomp, rtd);
0183 if (!spcm) {
0184 dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
0185 rtd->dai_link->id);
0186 return 0;
0187 }
0188
0189 if (hda && !hda->no_ipc_position) {
0190
0191 pos = spcm->stream[substream->stream].posn.host_posn;
0192 goto found;
0193 }
0194
0195 pos = hda_dsp_stream_get_position(hstream, substream->stream, true);
0196 found:
0197 pos = bytes_to_frames(substream->runtime, pos);
0198
0199 dev_vdbg(sdev->dev, "PCM: stream %d dir %d position %lu\n",
0200 hstream->index, substream->stream, pos);
0201 return pos;
0202 }
0203
0204 int hda_dsp_pcm_open(struct snd_sof_dev *sdev,
0205 struct snd_pcm_substream *substream)
0206 {
0207 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0208 struct snd_pcm_runtime *runtime = substream->runtime;
0209 struct snd_soc_component *scomp = sdev->component;
0210 struct hdac_ext_stream *dsp_stream;
0211 struct snd_sof_pcm *spcm;
0212 int direction = substream->stream;
0213 u32 flags = 0;
0214
0215 spcm = snd_sof_find_spcm_dai(scomp, rtd);
0216 if (!spcm) {
0217 dev_err(sdev->dev, "error: can't find PCM with DAI ID %d\n", rtd->dai_link->id);
0218 return -EINVAL;
0219 }
0220
0221
0222
0223
0224
0225 if (hda_disable_rewinds)
0226 runtime->hw.info |= SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR;
0227
0228
0229
0230
0231
0232 if (hda_always_enable_dmi_l1 && direction == SNDRV_PCM_STREAM_CAPTURE)
0233 runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE;
0234
0235 if (hda_always_enable_dmi_l1 ||
0236 direction == SNDRV_PCM_STREAM_PLAYBACK ||
0237 spcm->stream[substream->stream].d0i3_compatible)
0238 flags |= SOF_HDA_STREAM_DMI_L1_COMPATIBLE;
0239
0240 dsp_stream = hda_dsp_stream_get(sdev, direction, flags);
0241 if (!dsp_stream) {
0242 dev_err(sdev->dev, "error: no stream available\n");
0243 return -ENODEV;
0244 }
0245
0246
0247 snd_pcm_hw_constraint_step(substream->runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
0248
0249
0250 snd_pcm_hw_constraint_integer(substream->runtime,
0251 SNDRV_PCM_HW_PARAM_PERIODS);
0252
0253
0254 substream->runtime->private_data = &dsp_stream->hstream;
0255 return 0;
0256 }
0257
0258 int hda_dsp_pcm_close(struct snd_sof_dev *sdev,
0259 struct snd_pcm_substream *substream)
0260 {
0261 struct hdac_stream *hstream = substream->runtime->private_data;
0262 int direction = substream->stream;
0263 int ret;
0264
0265 ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
0266
0267 if (ret) {
0268 dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name);
0269 return -ENODEV;
0270 }
0271
0272
0273 substream->runtime->private_data = NULL;
0274 return 0;
0275 }