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