Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
  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. 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 <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
 32static bool hda_always_enable_dmi_l1;
 33module_param_named(always_enable_dmi_l1, hda_always_enable_dmi_l1, bool, 0444);
 34MODULE_PARM_DESC(always_enable_dmi_l1, "SOF HDA always enable DMI l1");
 35
 36static bool hda_disable_rewinds = IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_DISABLE_REWINDS);
 37module_param_named(disable_rewinds, hda_disable_rewinds, bool, 0444);
 38MODULE_PARM_DESC(disable_rewinds, "SOF HDA disable rewinds");
 39
 40u32 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
 74u32 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
 94int 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	u32 size, rate, bits;
105
106	size = params_buffer_bytes(params);
107	rate = hda_dsp_get_mult_div(sdev, params_rate(params));
108	bits = hda_dsp_get_bits(sdev, params_width(params));
109
110	hstream->substream = substream;
111
112	dmab = substream->runtime->dma_buffer_p;
113
114	hstream->format_val = rate | bits | (params_channels(params) - 1);
115	hstream->bufsize = size;
116	hstream->period_bytes = params_period_bytes(params);
117	hstream->no_period_wakeup  =
118			(params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
119			(params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
120
121	ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, params);
122	if (ret < 0) {
123		dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
124		return ret;
125	}
126
127	/* enable SPIB when rewinds are disabled */
128	if (hda_disable_rewinds)
129		hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_ENABLE, 0);
130	else
131		hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
132
133	if (hda)
134		platform_params->no_ipc_position = hda->no_ipc_position;
135
136	platform_params->stream_tag = hstream->stream_tag;
137
138	return 0;
139}
140
141/* update SPIB register with appl position */
142int hda_dsp_pcm_ack(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream)
143{
144	struct hdac_stream *hstream = substream->runtime->private_data;
145	struct snd_pcm_runtime *runtime = substream->runtime;
146	ssize_t appl_pos, buf_size;
147	u32 spib;
148
149	appl_pos = frames_to_bytes(runtime, runtime->control->appl_ptr);
150	buf_size = frames_to_bytes(runtime, runtime->buffer_size);
151
152	spib = appl_pos % buf_size;
153
154	/* Allowable value for SPIB is 1 byte to max buffer size */
155	if (!spib)
156		spib = buf_size;
157
158	sof_io_write(sdev, hstream->spib_addr, spib);
159
160	return 0;
161}
162
163int hda_dsp_pcm_trigger(struct snd_sof_dev *sdev,
164			struct snd_pcm_substream *substream, int cmd)
165{
166	struct hdac_stream *hstream = substream->runtime->private_data;
167	struct hdac_ext_stream *hext_stream = stream_to_hdac_ext_stream(hstream);
168
169	return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
170}
171
172snd_pcm_uframes_t hda_dsp_pcm_pointer(struct snd_sof_dev *sdev,
173				      struct snd_pcm_substream *substream)
174{
175	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
176	struct snd_soc_component *scomp = sdev->component;
177	struct hdac_stream *hstream = substream->runtime->private_data;
178	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
179	struct snd_sof_pcm *spcm;
180	snd_pcm_uframes_t pos;
181
182	spcm = snd_sof_find_spcm_dai(scomp, rtd);
183	if (!spcm) {
184		dev_warn_ratelimited(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
185				     rtd->dai_link->id);
186		return 0;
187	}
188
189	if (hda && !hda->no_ipc_position) {
190		/* read position from IPC position */
191		pos = spcm->stream[substream->stream].posn.host_posn;
192		goto found;
193	}
194
195	pos = hda_dsp_stream_get_position(hstream, substream->stream, true);
196found:
197	pos = bytes_to_frames(substream->runtime, pos);
198
199	trace_sof_intel_hda_dsp_pcm(sdev, hstream, substream, pos);
200	return pos;
201}
202
203int hda_dsp_pcm_open(struct snd_sof_dev *sdev,
204		     struct snd_pcm_substream *substream)
205{
206	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
207	struct snd_pcm_runtime *runtime = substream->runtime;
208	struct snd_soc_component *scomp = sdev->component;
209	struct hdac_ext_stream *dsp_stream;
210	struct snd_sof_pcm *spcm;
211	int direction = substream->stream;
212	u32 flags = 0;
213
214	spcm = snd_sof_find_spcm_dai(scomp, rtd);
215	if (!spcm) {
216		dev_err(sdev->dev, "error: can't find PCM with DAI ID %d\n", rtd->dai_link->id);
217		return -EINVAL;
218	}
219
220	/*
221	 * if we want the .ack to work, we need to prevent the control from being mapped.
222	 * The status can still be mapped.
223	 */
224	if (hda_disable_rewinds)
225		runtime->hw.info |= SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR;
226
227	/*
228	 * All playback streams are DMI L1 capable, capture streams need
229	 * pause push/release to be disabled
230	 */
231	if (hda_always_enable_dmi_l1 && direction == SNDRV_PCM_STREAM_CAPTURE)
232		runtime->hw.info &= ~SNDRV_PCM_INFO_PAUSE;
233
234	if (hda_always_enable_dmi_l1 ||
235	    direction == SNDRV_PCM_STREAM_PLAYBACK ||
236	    spcm->stream[substream->stream].d0i3_compatible)
237		flags |= SOF_HDA_STREAM_DMI_L1_COMPATIBLE;
238
239	dsp_stream = hda_dsp_stream_get(sdev, direction, flags);
240	if (!dsp_stream) {
241		dev_err(sdev->dev, "error: no stream available\n");
242		return -ENODEV;
243	}
244
245	/* minimum as per HDA spec */
246	snd_pcm_hw_constraint_step(substream->runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
247
248	/* avoid circular buffer wrap in middle of period */
249	snd_pcm_hw_constraint_integer(substream->runtime,
250				      SNDRV_PCM_HW_PARAM_PERIODS);
251
252	/* binding pcm substream to hda stream */
253	substream->runtime->private_data = &dsp_stream->hstream;
254	return 0;
255}
256
257int hda_dsp_pcm_close(struct snd_sof_dev *sdev,
258		      struct snd_pcm_substream *substream)
259{
260	struct hdac_stream *hstream = substream->runtime->private_data;
261	int direction = substream->stream;
262	int ret;
263
264	ret = hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
265
266	if (ret) {
267		dev_dbg(sdev->dev, "stream %s not opened!\n", substream->name);
268		return -ENODEV;
269	}
270
271	/* unbinding pcm substream to hda stream */
272	substream->runtime->private_data = NULL;
273	return 0;
274}