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