sst_drv_interface.c 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 *  sst_drv_interface.c - Intel SST Driver for audio engine
 *
 *  Copyright (C) 2008-14 Intel Corp
 *  Authors:	Vinod Koul <vinod.koul@intel.com>
 *		Harsha Priya <priya.harsha@intel.com>
 *		Dharageswari R <dharageswari.r@intel.com)
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/fs.h>
#include <linux/firmware.h>
#include <linux/pm_runtime.h>
#include <linux/pm_qos.h>
#include <linux/math64.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/compress_driver.h>
#include <asm/platform_sst_audio.h>
#include "../sst-mfld-platform.h"
#include "sst.h"
#include "../sst-dsp.h"



#define NUM_CODEC 2
#define MIN_FRAGMENT 2
#define MAX_FRAGMENT 4
#define MIN_FRAGMENT_SIZE (50 * 1024)
#define MAX_FRAGMENT_SIZE (1024 * 1024)
#define SST_GET_BYTES_PER_SAMPLE(pcm_wd_sz)  (((pcm_wd_sz + 15) >> 4) << 1)

int free_stream_context(struct intel_sst_drv *ctx, unsigned int str_id)
{
	struct stream_info *stream;
	int ret = 0;

	stream = get_stream_info(ctx, str_id);
	if (stream) {
		/* str_id is valid, so stream is alloacted */
		ret = sst_free_stream(ctx, str_id);
		if (ret)
			sst_clean_stream(&ctx->streams[str_id]);
		return ret;
58 59
	} else {
		dev_err(ctx->dev, "we tried to free stream context %d which was freed!!!\n", str_id);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	}
	return ret;
}

int sst_get_stream_allocated(struct intel_sst_drv *ctx,
	struct snd_sst_params *str_param,
	struct snd_sst_lib_download **lib_dnld)
{
	int retval;

	retval = ctx->ops->alloc_stream(ctx, str_param);
	if (retval > 0)
		dev_dbg(ctx->dev, "Stream allocated %d\n", retval);
	return retval;

}

/*
 * sst_get_sfreq - this function returns the frequency of the stream
 *
 * @str_param : stream params
 */
int sst_get_sfreq(struct snd_sst_params *str_param)
{
	switch (str_param->codec) {
	case SST_CODEC_TYPE_PCM:
		return str_param->sparams.uc.pcm_params.sfreq;
	case SST_CODEC_TYPE_AAC:
		return str_param->sparams.uc.aac_params.externalsr;
	case SST_CODEC_TYPE_MP3:
		return 0;
	default:
		return -EINVAL;
	}
}

/*
V
Vinod Koul 已提交
97
 * sst_get_num_channel - get number of channels for the stream
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
 *
 * @str_param : stream params
 */
int sst_get_num_channel(struct snd_sst_params *str_param)
{
	switch (str_param->codec) {
	case SST_CODEC_TYPE_PCM:
		return str_param->sparams.uc.pcm_params.num_chan;
	case SST_CODEC_TYPE_MP3:
		return str_param->sparams.uc.mp3_params.num_chan;
	case SST_CODEC_TYPE_AAC:
		return str_param->sparams.uc.aac_params.num_chan;
	default:
		return -EINVAL;
	}
}

/*
 * sst_get_stream - this function prepares for stream allocation
 *
 * @str_param : stream param
 */
int sst_get_stream(struct intel_sst_drv *ctx,
			struct snd_sst_params *str_param)
{
	int retval;
	struct stream_info *str_info;

	/* stream is not allocated, we are allocating */
	retval = ctx->ops->alloc_stream(ctx, str_param);
	if (retval <= 0) {
		return -EIO;
	}
	/* store sampling freq */
	str_info = &ctx->streams[retval];
	str_info->sfreq = sst_get_sfreq(str_param);

	return retval;
}

static int sst_power_control(struct device *dev, bool state)
{
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	dev_dbg(ctx->dev, "state:%d", state);
	if (state == true)
		return pm_runtime_get_sync(dev);
	else
		return sst_pm_runtime_put(ctx);
}

/*
 * sst_open_pcm_stream - Open PCM interface
 *
 * @str_param: parameters of pcm stream
 *
 * This function is called by MID sound card driver to open
 * a new pcm interface
 */
static int sst_open_pcm_stream(struct device *dev,
		struct snd_sst_params *str_param)
{
	int retval;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	if (!str_param)
		return -EINVAL;

	retval = sst_get_stream(ctx, str_param);
167
	if (retval > 0)
168
		ctx->stream_cnt++;
169
	else
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		dev_err(ctx->dev, "sst_get_stream returned err %d\n", retval);

	return retval;
}

/*
 * sst_close_pcm_stream - Close PCM interface
 *
 * @str_id: stream id to be closed
 *
 * This function is called by MID sound card driver to close
 * an existing pcm interface
 */
static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)
{
	struct stream_info *stream;
	int retval = 0;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	stream = get_stream_info(ctx, str_id);
	if (!stream) {
		dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);
		return -EINVAL;
	}

	if (stream->status == STREAM_RESET) {
		/* silently fail here as we have cleaned the stream earlier */
		dev_dbg(ctx->dev, "stream in reset state...\n");

		retval = 0;
		goto put;
	}

	retval = free_stream_context(ctx, str_id);
put:
	stream->pcm_substream = NULL;
	stream->status = STREAM_UN_INIT;
	stream->period_elapsed = NULL;
	ctx->stream_cnt--;

210 211
	if (retval)
		dev_err(ctx->dev, "free stream returned err %d\n", retval);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

	dev_dbg(ctx->dev, "Exit\n");
	return 0;
}

static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,
		struct pcm_stream_info *info,
		struct snd_pcm_substream *substream,
		struct snd_sst_tstamp *fw_tstamp)
{
	size_t delay_bytes, delay_frames;
	size_t buffer_sz;
	u32 pointer_bytes, pointer_samples;

	dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",
			fw_tstamp->ring_buffer_counter);
	dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",
			 fw_tstamp->hardware_counter);
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -
					fw_tstamp->hardware_counter);
	else
		delay_bytes = (size_t) (fw_tstamp->hardware_counter -
					fw_tstamp->ring_buffer_counter);
	delay_frames = bytes_to_frames(substream->runtime, delay_bytes);
	buffer_sz = snd_pcm_lib_buffer_bytes(substream);
	div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);
	pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);

	dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);

	info->buffer_ptr = pointer_samples / substream->runtime->channels;

	info->pcm_delay = delay_frames / substream->runtime->channels;
	dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",
			info->buffer_ptr, info->pcm_delay);
	return 0;
}

static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)
{
	struct stream_info *stream;
	struct snd_pcm_substream *substream;
	struct snd_sst_tstamp fw_tstamp;
	unsigned int str_id;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	str_id = info->str_id;
	stream = get_stream_info(ctx, str_id);
	if (!stream)
		return -EINVAL;

	if (!stream->pcm_substream)
		return -EINVAL;
	substream = stream->pcm_substream;

	memcpy_fromio(&fw_tstamp,
		((void *)(ctx->mailbox + ctx->tstamp)
			+ (str_id * sizeof(fw_tstamp))),
		sizeof(fw_tstamp));
	return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);
}

static int sst_stream_start(struct device *dev, int str_id)
{
	struct stream_info *str_info;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	if (ctx->sst_state != SST_FW_RUNNING)
		return 0;
	str_info = get_stream_info(ctx, str_id);
	if (!str_info)
		return -EINVAL;
	str_info->prev = str_info->status;
	str_info->status = STREAM_RUNNING;
	sst_start_stream(ctx, str_id);

	return 0;
}

static int sst_stream_drop(struct device *dev, int str_id)
{
	struct stream_info *str_info;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	if (ctx->sst_state != SST_FW_RUNNING)
		return 0;

	str_info = get_stream_info(ctx, str_id);
	if (!str_info)
		return -EINVAL;
	str_info->prev = STREAM_UN_INIT;
	str_info->status = STREAM_INIT;
	return sst_drop_stream(ctx, str_id);
}

static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)
{
	int str_id = 0;
	struct stream_info *stream;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	str_id = str_info->str_id;

	if (ctx->sst_state != SST_FW_RUNNING)
		return 0;

	stream = get_stream_info(ctx, str_id);
	if (!stream)
		return -EINVAL;

	dev_dbg(ctx->dev, "setting the period ptrs\n");
	stream->pcm_substream = str_info->arg;
	stream->period_elapsed = str_info->period_elapsed;
	stream->sfreq = str_info->sfreq;
	stream->prev = stream->status;
	stream->status = STREAM_INIT;
	dev_dbg(ctx->dev,
		"pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",
		stream->pcm_substream, stream->period_elapsed,
		stream->sfreq, stream->status);

	return 0;
}

/*
 * sst_set_byte_stream - Set generic params
 *
 * @cmd: control cmd to be set
 * @arg: command argument
 *
 * This function is called by MID sound card driver to configure
 * SST runtime params.
 */
static int sst_send_byte_stream(struct device *dev,
		struct snd_sst_bytes_v2 *bytes)
{
	int ret_val = 0;
	struct intel_sst_drv *ctx = dev_get_drvdata(dev);

	if (NULL == bytes)
		return -EINVAL;
	ret_val = pm_runtime_get_sync(ctx->dev);
	if (ret_val < 0)
		return ret_val;

	ret_val = sst_send_byte_stream_mrfld(ctx, bytes);
	sst_pm_runtime_put(ctx);

	return ret_val;
}

static struct sst_ops pcm_ops = {
	.open = sst_open_pcm_stream,
	.stream_init = sst_stream_init,
	.stream_start = sst_stream_start,
	.stream_drop = sst_stream_drop,
	.stream_read_tstamp = sst_read_timestamp,
	.send_byte_stream = sst_send_byte_stream,
	.close = sst_close_pcm_stream,
	.power = sst_power_control,
};

static struct sst_device sst_dsp_device = {
	.name = "Intel(R) SST LPE",
	.dev = NULL,
	.ops = &pcm_ops,
};

/*
 * sst_register - function to register DSP
 *
 * This functions registers DSP with the platform driver
 */
int sst_register(struct device *dev)
{
	int ret_val;

	sst_dsp_device.dev = dev;
	ret_val = sst_register_dsp(&sst_dsp_device);
	if (ret_val)
		dev_err(dev, "Unable to register DSP with platform driver\n");

	return ret_val;
}

int sst_unregister(struct device *dev)
{
	return sst_unregister_dsp(&sst_dsp_device);
}