xref: /linux/sound/soc/sof/intel/hda-pcm.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
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
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 <linux/moduleparam.h>
19 #include <sound/hda_register.h>
20 #include <sound/pcm_params.h>
21 #include <trace/events/sof_intel.h>
22 #include "../sof-audio.h"
23 #include "../ops.h"
24 #include "hda.h"
25 
26 #define SDnFMT_BASE(x)	((x) << 14)
27 #define SDnFMT_MULT(x)	(((x) - 1) << 11)
28 #define SDnFMT_DIV(x)	(((x) - 1) << 8)
29 #define SDnFMT_BITS(x)	((x) << 4)
30 #define SDnFMT_CHAN(x)	((x) << 0)
31 
32 static bool hda_always_enable_dmi_l1;
33 module_param_named(always_enable_dmi_l1, hda_always_enable_dmi_l1, bool, 0444);
34 MODULE_PARM_DESC(always_enable_dmi_l1, "SOF HDA always enable DMI l1");
35 
36 static bool hda_disable_rewinds;
37 module_param_named(disable_rewinds, hda_disable_rewinds, bool, 0444);
38 MODULE_PARM_DESC(disable_rewinds, "SOF HDA disable rewinds");
39 
40 static int hda_force_pause_support = -1;
41 module_param_named(force_pause_support, hda_force_pause_support, int, 0444);
42 MODULE_PARM_DESC(force_pause_support,
43 		 "Pause support: -1: Use default, 0: Disable, 1: Enable (default -1)");
44 
hda_dsp_get_mult_div(struct snd_sof_dev * sdev,int rate)45 u32 hda_dsp_get_mult_div(struct snd_sof_dev *sdev, int rate)
46 {
47 	switch (rate) {
48 	case 8000:
49 		return SDnFMT_DIV(6);
50 	case 9600:
51 		return SDnFMT_DIV(5);
52 	case 11025:
53 		return SDnFMT_BASE(1) | SDnFMT_DIV(4);
54 	case 16000:
55 		return SDnFMT_DIV(3);
56 	case 22050:
57 		return SDnFMT_BASE(1) | SDnFMT_DIV(2);
58 	case 32000:
59 		return SDnFMT_DIV(3) | SDnFMT_MULT(2);
60 	case 44100:
61 		return SDnFMT_BASE(1);
62 	case 48000:
63 		return 0;
64 	case 88200:
65 		return SDnFMT_BASE(1) | SDnFMT_MULT(2);
66 	case 96000:
67 		return SDnFMT_MULT(2);
68 	case 176400:
69 		return SDnFMT_BASE(1) | SDnFMT_MULT(4);
70 	case 192000:
71 		return SDnFMT_MULT(4);
72 	default:
73 		dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n",
74 			 rate);
75 		return 0; /* use 48KHz if not found */
76 	}
77 };
78 
hda_dsp_get_bits(struct snd_sof_dev * sdev,int sample_bits)79 u32 hda_dsp_get_bits(struct snd_sof_dev *sdev, int sample_bits)
80 {
81 	switch (sample_bits) {
82 	case 8:
83 		return SDnFMT_BITS(0);
84 	case 16:
85 		return SDnFMT_BITS(1);
86 	case 20:
87 		return SDnFMT_BITS(2);
88 	case 24:
89 		return SDnFMT_BITS(3);
90 	case 32:
91 		return SDnFMT_BITS(4);
92 	default:
93 		dev_warn(sdev->dev, "can't find %d bits using 16bit\n",
94 			 sample_bits);
95 		return SDnFMT_BITS(1); /* use 16bits format if not found */
96 	}
97 };
98 
hda_dsp_pcm_hw_params(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_sof_platform_stream_params * platform_params)99 int hda_dsp_pcm_hw_params(struct snd_sof_dev *sdev,
100 			  struct snd_pcm_substream *substream,
101 			  struct snd_pcm_hw_params *params,
102 			  struct snd_sof_platform_stream_params *platform_params)
103 {
104 	struct hdac_stream *hstream = substream->runtime->private_data;
105 	struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
106 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
107 	struct snd_dma_buffer *dmab;
108 	int ret;
109 
110 	hstream->substream = substream;
111 
112 	dmab = substream->runtime->dma_buffer_p;
113 
114 	/*
115 	 * Use the codec required format val (which is link_bps adjusted) when
116 	 * the DSP is not in use
117 	 */
118 	if (!sdev->dspless_mode_selected) {
119 		u32 rate = hda_dsp_get_mult_div(sdev, params_rate(params));
120 		u32 bits = hda_dsp_get_bits(sdev, params_width(params));
121 
122 		hstream->format_val = rate | bits | (params_channels(params) - 1);
123 	}
124 
125 	hstream->bufsize = params_buffer_bytes(params);
126 	hstream->period_bytes = params_period_bytes(params);
127 	hstream->no_period_wakeup  =
128 			(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
129 			(params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
130 
131 	ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, params);
132 	if (ret < 0) {
133 		dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
134 		return ret;
135 	}
136 
137 	/* enable SPIB when rewinds are disabled */
138 	if (hda_disable_rewinds)
139 		hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_ENABLE, 0);
140 	else
141 		hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
142 
143 	if (hda)
144 		platform_params->no_ipc_position = hda->no_ipc_position;
145 
146 	platform_params->stream_tag = hstream->stream_tag;
147 
148 	return 0;
149 }
150 EXPORT_SYMBOL_NS(hda_dsp_pcm_hw_params, "SND_SOC_SOF_INTEL_HDA_COMMON");
151 
152 /* update SPIB register with appl position */
hda_dsp_pcm_ack(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)153 int hda_dsp_pcm_ack(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
154 {
155 	struct hdac_stream *hstream = substream->runtime->private_data;
156 	struct snd_pcm_runtime *runtime = substream->runtime;
157 	ssize_t appl_pos, buf_size;
158 	u32 spib;
159 
160 	appl_pos = frames_to_bytes(runtime, runtime->control->appl_ptr);
161 	buf_size = frames_to_bytes(runtime, runtime->buffer_size);
162 
163 	spib = appl_pos % buf_size;
164 
165 	/* Allowable value for SPIB is 1 byte to max buffer size */
166 	if (!spib)
167 		spib = buf_size;
168 
169 	sof_io_write(sdev, hstream->spib_addr, spib);
170 
171 	return 0;
172 }
173 EXPORT_SYMBOL_NS(hda_dsp_pcm_ack, "SND_SOC_SOF_INTEL_HDA_COMMON");
174 
hda_dsp_pcm_trigger(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,int cmd)175 int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev,
176 			struct snd_pcm_substream *substream, int cmd)
177 {
178 	struct hdac_stream *hstream = substream->runtime->private_data;
179 	struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
180 
181 	return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
182 }
183 EXPORT_SYMBOL_NS(hda_dsp_pcm_trigger, "SND_SOC_SOF_INTEL_HDA_COMMON");
184 
hda_dsp_pcm_pointer(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)185 snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev,
186 				      struct snd_pcm_substream *substream)
187 {
188 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
189 	struct snd_soc_component *scomp = sdev->component;
190 	struct hdac_stream *hstream = substream->runtime->private_data;
191 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
192 	struct snd_sof_pcm *spcm;
193 	snd_pcm_uframes_t pos;
194 
195 	spcm = snd_sof_find_spcm_dai(scomp, rtd);
196 	if (!spcm) {
197 		dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
198 				     rtd->dai_link->id);
199 		return 0;
200 	}
201 
202 	if (hda && !hda->no_ipc_position) {
203 		/* read position from IPC position */
204 		pos = spcm->stream[substream->stream].posn.host_posn;
205 		goto found;
206 	}
207 
208 	pos = hda_dsp_stream_get_position(hstream, substream->stream, true);
209 found:
210 	pos = bytes_to_frames(substream->runtime, pos);
211 
212 	trace_sof_intel_hda_dsp_pcm(sdev, hstream, substream, pos);
213 	return pos;
214 }
215 EXPORT_SYMBOL_NS(hda_dsp_pcm_pointer, "SND_SOC_SOF_INTEL_HDA_COMMON");
216 
hda_dsp_pcm_open(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)217 int hda_dsp_pcm_open(struct snd_sof_dev *sdev,
218 		     struct snd_pcm_substream *substream)
219 {
220 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
221 	struct snd_pcm_runtime *runtime = substream->runtime;
222 	struct snd_soc_component *scomp = sdev->component;
223 	struct hdac_ext_stream *dsp_stream;
224 	struct snd_sof_pcm *spcm;
225 	int direction = substream->stream;
226 	u32 flags = 0;
227 
228 	spcm = snd_sof_find_spcm_dai(scomp, rtd);
229 	if (!spcm) {
230 		dev_err(sdev->dev, "error: can't find PCM with DAI ID %d\n", rtd->dai_link->id);
231 		return -EINVAL;
232 	}
233 
234 	/*
235 	 * if we want the .ack to work, we need to prevent the control from being mapped.
236 	 * The status can still be mapped.
237 	 */
238 	if (hda_disable_rewinds)
239 		runtime->hw.info |= SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR;
240 
241 	/*
242 	 * All playback streams are DMI L1 capable, capture streams need
243 	 * pause push/release to be disabled
244 	 */
245 	if (hda_always_enable_dmi_l1 && direction == SNDRV_PCM_STREAM_CAPTURE)
246 		runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE;
247 
248 	/*
249 	 * Do not advertise the PAUSE support if it is forced to be disabled via
250 	 * module parameter or if the pause_supported is false for the PCM
251 	 * device
252 	 */
253 	if (hda_force_pause_support == 0 ||
254 	    (hda_force_pause_support == -1 &&
255 	     !spcm->stream[substream->stream].pause_supported))
256 		runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE;
257 
258 	if (hda_always_enable_dmi_l1 ||
259 	    direction == SNDRV_PCM_STREAM_PLAYBACK ||
260 	    spcm->stream[substream->stream].d0i3_compatible)
261 		flags |= SOF_HDA_STREAM_DMI_L1_COMPATIBLE;
262 
263 	dsp_stream = hda_dsp_stream_get(sdev, direction, flags);
264 	if (!dsp_stream) {
265 		dev_err(sdev->dev, "error: no stream available\n");
266 		return -ENODEV;
267 	}
268 
269 	/* minimum as per HDA spec */
270 	snd_pcm_hw_constraint_step(substream->runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
271 
272 	/* avoid circular buffer wrap in middle of period */
273 	snd_pcm_hw_constraint_integer(substream->runtime,
274 				      SNDRV_PCM_HW_PARAM_PERIODS);
275 
276 	/* Limit the maximum number of periods to not exceed the BDL entries count */
277 	if (runtime->hw.periods_max > HDA_DSP_MAX_BDL_ENTRIES)
278 		snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
279 					     runtime->hw.periods_min,
280 					     HDA_DSP_MAX_BDL_ENTRIES);
281 
282 	/* Only S16 and S32 supported by HDA hardware when used without DSP */
283 	if (sdev->dspless_mode_selected)
284 		snd_pcm_hw_constraint_mask64(substream->runtime, SNDRV_PCM_HW_PARAM_FORMAT,
285 					     SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S32);
286 
287 	/*
288 	 * The dsp_max_burst_size_in_ms is the length of the maximum burst size
289 	 * of the host DMA in the ALSA buffer.
290 	 *
291 	 * On playback start the DMA will transfer dsp_max_burst_size_in_ms
292 	 * amount of data in one initial burst to fill up the host DMA buffer.
293 	 * Consequent DMA burst sizes are shorter and their length can vary.
294 	 * To make sure that userspace allocate large enough ALSA buffer we need
295 	 * to place a constraint on the buffer time.
296 	 *
297 	 * On capture the DMA will transfer 1ms chunks.
298 	 *
299 	 * Exact dsp_max_burst_size_in_ms constraint is racy, so set the
300 	 * constraint to a minimum of 2x dsp_max_burst_size_in_ms.
301 	 */
302 	if (spcm->stream[direction].dsp_max_burst_size_in_ms)
303 		snd_pcm_hw_constraint_minmax(substream->runtime,
304 			SNDRV_PCM_HW_PARAM_BUFFER_TIME,
305 			spcm->stream[direction].dsp_max_burst_size_in_ms * USEC_PER_MSEC * 2,
306 			UINT_MAX);
307 
308 	/* binding pcm substream to hda stream */
309 	substream->runtime->private_data = &dsp_stream->hstream;
310 
311 	/*
312 	 * Reset the llp cache values (they are used for LLP compensation in
313 	 * case the counter is not reset)
314 	 */
315 	dsp_stream->pplcllpl = 0;
316 	dsp_stream->pplcllpu = 0;
317 
318 	return 0;
319 }
320 EXPORT_SYMBOL_NS(hda_dsp_pcm_open, "SND_SOC_SOF_INTEL_HDA_COMMON");
321 
hda_dsp_pcm_close(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)322 int hda_dsp_pcm_close(struct snd_sof_dev *sdev,
323 		      struct snd_pcm_substream *substream)
324 {
325 	struct hdac_stream *hstream = substream->runtime->private_data;
326 	int direction = substream->stream;
327 	int ret;
328 
329 	ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
330 
331 	if (ret) {
332 		dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name);
333 		return -ENODEV;
334 	}
335 
336 	/* unbinding pcm substream to hda stream */
337 	substream->runtime->private_data = NULL;
338 	return 0;
339 }
340 EXPORT_SYMBOL_NS(hda_dsp_pcm_close, "SND_SOC_SOF_INTEL_HDA_COMMON");
341