xref: /openbmc/linux/sound/soc/sof/intel/hda-pcm.c (revision 1a18374f)
1 // SPDX-License-Identifier: (GPL-2.0 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 "../ops.h"
21 #include "hda.h"
22 
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)
28 
29 static inline u32 get_mult_div(struct snd_sof_dev *sdev, int rate)
30 {
31 	switch (rate) {
32 	case 8000:
33 		return SDnFMT_DIV(6);
34 	case 9600:
35 		return SDnFMT_DIV(5);
36 	case 11025:
37 		return SDnFMT_BASE(1) | SDnFMT_DIV(4);
38 	case 16000:
39 		return SDnFMT_DIV(3);
40 	case 22050:
41 		return SDnFMT_BASE(1) | SDnFMT_DIV(2);
42 	case 32000:
43 		return SDnFMT_DIV(3) | SDnFMT_MULT(2);
44 	case 44100:
45 		return SDnFMT_BASE(1);
46 	case 48000:
47 		return 0;
48 	case 88200:
49 		return SDnFMT_BASE(1) | SDnFMT_MULT(2);
50 	case 96000:
51 		return SDnFMT_MULT(2);
52 	case 176400:
53 		return SDnFMT_BASE(1) | SDnFMT_MULT(4);
54 	case 192000:
55 		return SDnFMT_MULT(4);
56 	default:
57 		dev_warn(sdev->dev, "can't find div rate %d using 48kHz\n",
58 			 rate);
59 		return 0; /* use 48KHz if not found */
60 	}
61 };
62 
63 static inline u32 get_bits(struct snd_sof_dev *sdev, int sample_bits)
64 {
65 	switch (sample_bits) {
66 	case 8:
67 		return SDnFMT_BITS(0);
68 	case 16:
69 		return SDnFMT_BITS(1);
70 	case 20:
71 		return SDnFMT_BITS(2);
72 	case 24:
73 		return SDnFMT_BITS(3);
74 	case 32:
75 		return SDnFMT_BITS(4);
76 	default:
77 		dev_warn(sdev->dev, "can't find %d bits using 16bit\n",
78 			 sample_bits);
79 		return SDnFMT_BITS(1); /* use 16bits format if not found */
80 	}
81 };
82 
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)
87 {
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;
93 	int ret;
94 	u32 size, rate, bits;
95 
96 	size = params_buffer_bytes(params);
97 	rate = get_mult_div(sdev, params_rate(params));
98 	bits = get_bits(sdev, params_width(params));
99 
100 	hstream->substream = substream;
101 
102 	dmab = substream->runtime->dma_buffer_p;
103 
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);
110 
111 	ret = hda_dsp_stream_hw_params(sdev, stream, dmab, params);
112 	if (ret < 0) {
113 		dev_err(sdev->dev, "error: hdac prepare failed: %x\n", ret);
114 		return ret;
115 	}
116 
117 	/* disable SPIB, to enable buffer wrap for stream */
118 	hda_dsp_stream_spib_config(sdev, stream, HDA_DSP_SPIB_DISABLE, 0);
119 
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.
125 		 */
126 		if (v->abi_version < SOF_ABI_VER(3, 10, 0))
127 			ipc_params->host_period_bytes = 0;
128 		else
129 			ipc_params->no_stream_position = 1;
130 	}
131 
132 	ipc_params->stream_tag = hstream->stream_tag;
133 
134 	return 0;
135 }
136 
137 int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev,
138 			struct snd_pcm_substream *substream, int cmd)
139 {
140 	struct hdac_stream *hstream = substream->runtime->private_data;
141 	struct hdac_ext_stream *stream = stream_to_hdac_ext_stream(hstream);
142 
143 	return hda_dsp_stream_trigger(sdev, stream, cmd);
144 }
145 
146 snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev,
147 				      struct snd_pcm_substream *substream)
148 {
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;
154 
155 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
156 	if (!spcm) {
157 		dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
158 				     rtd->dai_link->id);
159 		return 0;
160 	}
161 
162 	if (hda && !hda->no_ipc_position) {
163 		/* read position from IPC position */
164 		pos = spcm->stream[substream->stream].posn.host_posn;
165 		goto found;
166 	}
167 
168 	/*
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.
175 	 */
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 *
180 					hstream->index));
181 	} else {
182 		/*
183 		 * For capture stream, we need more workaround to fix the
184 		 * position incorrect issue:
185 		 *
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
190 		 * position value.
191 		 * 3. Read the DMA Position from posbuf. Now the readback
192 		 * value should be >= period boundary.
193 		 */
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 *
198 				  hstream->index));
199 		pos = snd_hdac_stream_get_pos_posbuf(hstream);
200 	}
201 
202 	if (pos >= hstream->bufsize)
203 		pos = 0;
204 
205 found:
206 	pos = bytes_to_frames(substream->runtime, pos);
207 
208 	dev_vdbg(sdev->dev, "PCM: stream %d dir %d position %lu\n",
209 		 hstream->index, substream->stream, pos);
210 	return pos;
211 }
212 
213 int hda_dsp_pcm_open(struct snd_sof_dev *sdev,
214 		     struct snd_pcm_substream *substream)
215 {
216 	struct hdac_ext_stream *dsp_stream;
217 	int direction = substream->stream;
218 
219 	dsp_stream = hda_dsp_stream_get(sdev, direction);
220 
221 	if (!dsp_stream) {
222 		dev_err(sdev->dev, "error: no stream available\n");
223 		return -ENODEV;
224 	}
225 
226 	/* binding pcm substream to hda stream */
227 	substream->runtime->private_data = &dsp_stream->hstream;
228 	return 0;
229 }
230 
231 int hda_dsp_pcm_close(struct snd_sof_dev *sdev,
232 		      struct snd_pcm_substream *substream)
233 {
234 	struct hdac_stream *hstream = substream->runtime->private_data;
235 	int direction = substream->stream;
236 	int ret;
237 
238 	ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
239 
240 	if (ret) {
241 		dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name);
242 		return -ENODEV;
243 	}
244 
245 	/* unbinding pcm substream to hda stream */
246 	substream->runtime->private_data = NULL;
247 	return 0;
248 }
249