sst-haswell-pcm.c 37.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Intel SST Haswell/Broadwell PCM Support
 *
 * Copyright (C) 2013, Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * 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/module.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/delay.h>
21
#include <linux/pm_runtime.h>
22 23 24 25 26 27 28 29 30 31
#include <asm/page.h>
#include <asm/pgtable.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/dmaengine_pcm.h>
#include <sound/soc.h>
#include <sound/tlv.h>
#include <sound/compress_driver.h>

32 33 34
#include "../haswell/sst-haswell-ipc.h"
#include "../common/sst-dsp-priv.h"
#include "../common/sst-dsp.h"
35 36 37 38

#define HSW_PCM_COUNT		6
#define HSW_VOLUME_MAX		0x7FFFFFFF	/* 0dB */

39 40 41 42 43
#define SST_OLD_POSITION(d, r, o) ((d) +		\
			frames_to_bytes(r, o))
#define SST_SAMPLES(r, x) (bytes_to_samples(r,	\
			frames_to_bytes(r, (x))))

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/* simple volume table */
static const u32 volume_map[] = {
	HSW_VOLUME_MAX >> 30,
	HSW_VOLUME_MAX >> 29,
	HSW_VOLUME_MAX >> 28,
	HSW_VOLUME_MAX >> 27,
	HSW_VOLUME_MAX >> 26,
	HSW_VOLUME_MAX >> 25,
	HSW_VOLUME_MAX >> 24,
	HSW_VOLUME_MAX >> 23,
	HSW_VOLUME_MAX >> 22,
	HSW_VOLUME_MAX >> 21,
	HSW_VOLUME_MAX >> 20,
	HSW_VOLUME_MAX >> 19,
	HSW_VOLUME_MAX >> 18,
	HSW_VOLUME_MAX >> 17,
	HSW_VOLUME_MAX >> 16,
	HSW_VOLUME_MAX >> 15,
	HSW_VOLUME_MAX >> 14,
	HSW_VOLUME_MAX >> 13,
	HSW_VOLUME_MAX >> 12,
	HSW_VOLUME_MAX >> 11,
	HSW_VOLUME_MAX >> 10,
	HSW_VOLUME_MAX >> 9,
	HSW_VOLUME_MAX >> 8,
	HSW_VOLUME_MAX >> 7,
	HSW_VOLUME_MAX >> 6,
	HSW_VOLUME_MAX >> 5,
	HSW_VOLUME_MAX >> 4,
	HSW_VOLUME_MAX >> 3,
	HSW_VOLUME_MAX >> 2,
	HSW_VOLUME_MAX >> 1,
	HSW_VOLUME_MAX >> 0,
};

#define HSW_PCM_PERIODS_MAX	64
#define HSW_PCM_PERIODS_MIN	2

82 83 84 85 86 87
#define HSW_PCM_DAI_ID_SYSTEM	0
#define HSW_PCM_DAI_ID_OFFLOAD0	1
#define HSW_PCM_DAI_ID_OFFLOAD1	2
#define HSW_PCM_DAI_ID_LOOPBACK	3


88 89 90 91 92 93
static const struct snd_pcm_hardware hsw_pcm_hardware = {
	.info			= SNDRV_PCM_INFO_MMAP |
				  SNDRV_PCM_INFO_MMAP_VALID |
				  SNDRV_PCM_INFO_INTERLEAVED |
				  SNDRV_PCM_INFO_PAUSE |
				  SNDRV_PCM_INFO_RESUME |
94 95
				  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
				  SNDRV_PCM_INFO_DRAIN_TRIGGER,
96
	.formats		= SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
97 98 99 100 101 102 103 104
				  SNDRV_PCM_FMTBIT_S32_LE,
	.period_bytes_min	= PAGE_SIZE,
	.period_bytes_max	= (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
	.periods_min		= HSW_PCM_PERIODS_MIN,
	.periods_max		= HSW_PCM_PERIODS_MAX,
	.buffer_bytes_max	= HSW_PCM_PERIODS_MAX * PAGE_SIZE,
};

105 106
struct hsw_pcm_module_map {
	int dai_id;
107
	int stream;
108 109 110
	enum sst_hsw_module_id mod_id;
};

111 112 113 114
/* private data for each PCM DSP stream */
struct hsw_pcm_data {
	int dai_id;
	struct sst_hsw_stream *stream;
115
	struct sst_module_runtime *runtime;
116 117
	struct sst_module_runtime_context context;
	struct snd_pcm *hsw_pcm;
118 119 120 121 122
	u32 volume[2];
	struct snd_pcm_substream *substream;
	struct snd_compr_stream *cstream;
	unsigned int wpos;
	struct mutex mutex;
123
	bool allocated;
124
	int persistent_offset;
125 126
};

127
enum hsw_pm_state {
128 129 130
	HSW_PM_STATE_D0 = 0,
	HSW_PM_STATE_RTD3 = 1,
	HSW_PM_STATE_D3 = 2,
131 132 133 134 135 136
};

/* private data for the driver */
struct hsw_priv_data {
	/* runtime DSP */
	struct sst_hsw *hsw;
137 138 139
	struct device *dev;
	enum hsw_pm_state pm_state;
	struct snd_soc_card *soc_card;
140
	struct sst_module_runtime *runtime_waves; /* sound effect module */
141 142

	/* page tables */
143
	struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
144 145

	/* DAI data */
146 147 148 149 150 151 152 153 154 155 156
	struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
};


/* static mappings between PCMs and modules - may be dynamic in future */
static struct hsw_pcm_module_map mod_map[] = {
	{HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
	{HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
	{HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
	{HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
	{HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
157 158
};

159 160
static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static inline u32 hsw_mixer_to_ipc(unsigned int value)
{
	if (value >= ARRAY_SIZE(volume_map))
		return volume_map[0];
	else
		return volume_map[value];
}

static inline unsigned int hsw_ipc_to_mixer(u32 value)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
		if (volume_map[i] >= value)
			return i;
	}

	return i - 1;
}

static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
184
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
185 186
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
187 188
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(platform);
189
	struct hsw_pcm_data *pcm_data;
190 191
	struct sst_hsw *hsw = pdata->hsw;
	u32 volume;
192 193 194 195 196
	int dai, stream;

	dai = mod_map[mc->reg].dai_id;
	stream = mod_map[mc->reg].stream;
	pcm_data = &pdata->pcm[dai][stream];
197 198

	mutex_lock(&pcm_data->mutex);
199
	pm_runtime_get_sync(pdata->dev);
200 201 202 203 204 205

	if (!pcm_data->stream) {
		pcm_data->volume[0] =
			hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
		pcm_data->volume[1] =
			hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
206 207
		pm_runtime_mark_last_busy(pdata->dev);
		pm_runtime_put_autosuspend(pdata->dev);
208 209 210 211 212 213 214
		mutex_unlock(&pcm_data->mutex);
		return 0;
	}

	if (ucontrol->value.integer.value[0] ==
		ucontrol->value.integer.value[1]) {
		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
215 216
		/* apply volume value to all channels */
		sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
217 218 219 220 221 222 223
	} else {
		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
		sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
		sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
	}

224 225
	pm_runtime_mark_last_busy(pdata->dev);
	pm_runtime_put_autosuspend(pdata->dev);
226 227 228 229 230 231 232
	mutex_unlock(&pcm_data->mutex);
	return 0;
}

static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
233
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
234 235
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
236 237
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(platform);
238
	struct hsw_pcm_data *pcm_data;
239 240
	struct sst_hsw *hsw = pdata->hsw;
	u32 volume;
241 242 243 244 245
	int dai, stream;

	dai = mod_map[mc->reg].dai_id;
	stream = mod_map[mc->reg].stream;
	pcm_data = &pdata->pcm[dai][stream];
246 247

	mutex_lock(&pcm_data->mutex);
248
	pm_runtime_get_sync(pdata->dev);
249 250 251 252 253 254

	if (!pcm_data->stream) {
		ucontrol->value.integer.value[0] =
			hsw_ipc_to_mixer(pcm_data->volume[0]);
		ucontrol->value.integer.value[1] =
			hsw_ipc_to_mixer(pcm_data->volume[1]);
255 256
		pm_runtime_mark_last_busy(pdata->dev);
		pm_runtime_put_autosuspend(pdata->dev);
257 258 259 260 261 262 263 264
		mutex_unlock(&pcm_data->mutex);
		return 0;
	}

	sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
	ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
	sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
	ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
265 266 267

	pm_runtime_mark_last_busy(pdata->dev);
	pm_runtime_put_autosuspend(pdata->dev);
268 269 270 271 272 273 274 275
	mutex_unlock(&pcm_data->mutex);

	return 0;
}

static int hsw_volume_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
276 277
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
278 279 280
	struct sst_hsw *hsw = pdata->hsw;
	u32 volume;

281 282
	pm_runtime_get_sync(pdata->dev);

283 284 285 286
	if (ucontrol->value.integer.value[0] ==
		ucontrol->value.integer.value[1]) {

		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
287
		sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
288 289 290 291 292 293 294 295 296

	} else {
		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
		sst_hsw_mixer_set_volume(hsw, 0, 0, volume);

		volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
		sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
	}

297 298
	pm_runtime_mark_last_busy(pdata->dev);
	pm_runtime_put_autosuspend(pdata->dev);
299 300 301 302 303 304
	return 0;
}

static int hsw_volume_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
305 306
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
307 308 309
	struct sst_hsw *hsw = pdata->hsw;
	unsigned int volume = 0;

310
	pm_runtime_get_sync(pdata->dev);
311 312 313 314 315 316
	sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
	ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);

	sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
	ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);

317 318
	pm_runtime_mark_last_busy(pdata->dev);
	pm_runtime_put_autosuspend(pdata->dev);
319 320 321
	return 0;
}

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
static int hsw_waves_switch_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
	struct sst_hsw *hsw = pdata->hsw;
	enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;

	ucontrol->value.integer.value[0] =
		(sst_hsw_is_module_active(hsw, id) ||
		sst_hsw_is_module_enabled_rtd3(hsw, id));
	return 0;
}

static int hsw_waves_switch_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
	struct sst_hsw *hsw = pdata->hsw;
	int ret = 0;
	enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
	bool switch_on = (bool)ucontrol->value.integer.value[0];

	/* if module is in RAM on the DSP, apply user settings to module through
	 * ipc. If module is not in RAM on the DSP, store user setting for
	 * track */
	if (sst_hsw_is_module_loaded(hsw, id)) {
		if (switch_on == sst_hsw_is_module_active(hsw, id))
			return 0;

		if (switch_on)
			ret = sst_hsw_module_enable(hsw, id, 0);
		else
			ret = sst_hsw_module_disable(hsw, id, 0);
	} else {
		if (switch_on == sst_hsw_is_module_enabled_rtd3(hsw, id))
			return 0;

		if (switch_on)
			sst_hsw_set_module_enabled_rtd3(hsw, id);
		else
			sst_hsw_set_module_disabled_rtd3(hsw, id);
	}

	return ret;
}

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 402
static int hsw_waves_param_get(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
	struct sst_hsw *hsw = pdata->hsw;

	/* return a matching line from param buffer */
	return sst_hsw_load_param_line(hsw, ucontrol->value.bytes.data);
}

static int hsw_waves_param_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
	struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
	struct sst_hsw *hsw = pdata->hsw;
	int ret;
	enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
	int param_id = ucontrol->value.bytes.data[0];
	int param_size = WAVES_PARAM_COUNT;

	/* clear param buffer and reset buffer index */
	if (param_id == 0xFF) {
		sst_hsw_reset_param_buf(hsw);
		return 0;
	}

	/* store params into buffer */
	ret = sst_hsw_store_param_line(hsw, ucontrol->value.bytes.data);
	if (ret < 0)
		return ret;

403
	if (sst_hsw_is_module_active(hsw, id))
404 405 406 407 408
		ret = sst_hsw_module_set_param(hsw, id, 0, param_id,
				param_size, ucontrol->value.bytes.data);
	return ret;
}

409 410 411 412 413 414 415
/* TLV used by both global and stream volumes */
static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);

/* System Pin has no volume control */
static const struct snd_kcontrol_new hsw_volume_controls[] = {
	/* Global DSP volume */
	SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
J
Jie Yang 已提交
416
		ARRAY_SIZE(volume_map) - 1, 0,
417 418 419
		hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
	/* Offload 0 volume */
	SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
J
Jie Yang 已提交
420
		ARRAY_SIZE(volume_map) - 1, 0,
421 422 423
		hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
	/* Offload 1 volume */
	SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
J
Jie Yang 已提交
424
		ARRAY_SIZE(volume_map) - 1, 0,
425 426
		hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
	/* Mic Capture volume */
427
	SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
J
Jie Yang 已提交
428
		ARRAY_SIZE(volume_map) - 1, 0,
429
		hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
430 431 432
	/* enable/disable module waves */
	SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
		hsw_waves_switch_get, hsw_waves_switch_put),
433 434 435
	/* set parameters to module waves */
	SND_SOC_BYTES_EXT("Waves Set Param", WAVES_PARAM_COUNT,
		hsw_waves_param_get, hsw_waves_param_put),
436 437 438
};

/* Create DMA buffer page table for DSP */
439 440 441
static int create_adsp_page_table(struct snd_pcm_substream *substream,
	struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
	unsigned char *dma_area, size_t size, int pcm)
442
{
443 444
	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
	int i, pages, stream = substream->stream;
445

446
	pages = snd_sgbuf_aligned_pages(size);
447 448 449 450 451 452

	dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
		dma_area, size, pages);

	for (i = 0; i < pages; i++) {
		u32 idx = (((i << 2) + i)) >> 1;
453
		u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
454 455 456 457
		u32 *pg_table;

		dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);

458
		pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

		if (i & 1)
			*pg_table |= (pfn << 4);
		else
			*pg_table |= pfn;
	}

	return 0;
}

/* this may get called several times by oss emulation */
static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
477
	struct hsw_pcm_data *pcm_data;
478 479 480
	struct sst_hsw *hsw = pdata->hsw;
	struct sst_module *module_data;
	struct sst_dsp *dsp;
481
	struct snd_dma_buffer *dmab;
482 483 484 485
	enum sst_hsw_stream_type stream_type;
	enum sst_hsw_stream_path_id path_id;
	u32 rate, bits, map, pages, module_id;
	u8 channels;
486 487 488 489
	int ret, dai;

	dai = mod_map[rtd->cpu_dai->id].dai_id;
	pcm_data = &pdata->pcm[dai][substream->stream];
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	/* check if we are being called a subsequent time */
	if (pcm_data->allocated) {
		ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
		if (ret < 0)
			dev_dbg(rtd->dev, "error: reset stream failed %d\n",
				ret);

		ret = sst_hsw_stream_free(hsw, pcm_data->stream);
		if (ret < 0) {
			dev_dbg(rtd->dev, "error: free stream failed %d\n",
				ret);
			return ret;
		}
		pcm_data->allocated = false;

		pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
			hsw_notify_pointer, pcm_data);
		if (pcm_data->stream == NULL) {
			dev_err(rtd->dev, "error: failed to create stream\n");
			return -EINVAL;
		}
	}

514 515 516 517 518 519 520 521 522
	/* stream direction */
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
	else
		path_id = SST_HSW_STREAM_PATH_SSP0_IN;

	/* DSP stream type depends on DAI ID */
	switch (rtd->cpu_dai->id) {
	case 0:
523 524 525 526 527 528 529 530
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
			module_id = SST_HSW_MODULE_PCM_SYSTEM;
		}
		else {
			stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
			module_id = SST_HSW_MODULE_PCM_CAPTURE;
		}
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
		break;
	case 1:
	case 2:
		stream_type = SST_HSW_STREAM_TYPE_RENDER;
		module_id = SST_HSW_MODULE_PCM;
		break;
	case 3:
		/* path ID needs to be OUT for loopback */
		stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
		path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
		module_id = SST_HSW_MODULE_PCM_REFERENCE;
		break;
	default:
		dev_err(rtd->dev, "error: invalid DAI ID %d\n",
			rtd->cpu_dai->id);
		return -EINVAL;
	};

	ret = sst_hsw_stream_format(hsw, pcm_data->stream,
		path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
	if (ret < 0) {
		dev_err(rtd->dev, "error: failed to set format %d\n", ret);
		return ret;
	}

	rate = params_rate(params);
	ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
	if (ret < 0) {
		dev_err(rtd->dev, "error: could not set rate %d\n", rate);
		return ret;
	}

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		bits = SST_HSW_DEPTH_16BIT;
		sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
569 570 571 572 573 574 575 576 577
		bits = SST_HSW_DEPTH_32BIT;
		sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
		break;
	case SNDRV_PCM_FORMAT_S8:
		bits = SST_HSW_DEPTH_8BIT;
		sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		bits = SST_HSW_DEPTH_32BIT;
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
		sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
		break;
	default:
		dev_err(rtd->dev, "error: invalid format %d\n",
			params_format(params));
		return -EINVAL;
	}

	ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
	if (ret < 0) {
		dev_err(rtd->dev, "error: could not set bits %d\n", bits);
		return ret;
	}

	channels = params_channels(params);
	map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
	sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
			map, SST_HSW_CHANNEL_CONFIG_STEREO);

	ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
	if (ret < 0) {
		dev_err(rtd->dev, "error: could not set channels %d\n",
			channels);
		return ret;
	}

	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
	if (ret < 0) {
		dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
			params_buffer_bytes(params), ret);
		return ret;
	}

611 612 613 614
	dmab = snd_pcm_get_dma_buf(substream);

	ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
		runtime->dma_bytes, rtd->cpu_dai->id);
615 616 617 618 619 620 621 622 623 624 625 626
	if (ret < 0)
		return ret;

	sst_hsw_stream_set_style(hsw, pcm_data->stream,
		SST_HSW_INTERLEAVING_PER_CHANNEL);

	if (runtime->dma_bytes % PAGE_SIZE)
		pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
	else
		pages = runtime->dma_bytes / PAGE_SIZE;

	ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
627
		pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
628
		pages, runtime->dma_bytes, 0,
629
		snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
630 631 632 633 634 635 636 637 638 639 640 641 642
	if (ret < 0) {
		dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
		return ret;
	}

	dsp = sst_hsw_get_dsp(hsw);

	module_data = sst_module_get_from_id(dsp, module_id);
	if (module_data == NULL) {
		dev_err(rtd->dev, "error: failed to get module config\n");
		return -EINVAL;
	}

643 644
	sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
		pcm_data->runtime);
645 646 647 648 649 650

	ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
	if (ret < 0) {
		dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
		return ret;
	}
651 652 653 654 655 656 657 658 659

	if (!pcm_data->allocated) {
		/* Set previous saved volume */
		sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
				0, pcm_data->volume[0]);
		sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
				1, pcm_data->volume[1]);
		pcm_data->allocated = true;
	}
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

	ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
	if (ret < 0)
		dev_err(rtd->dev, "error: failed to pause %d\n", ret);

	return 0;
}

static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
{
	snd_pcm_lib_free_pages(substream);
	return 0;
}

static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
679
	struct hsw_pcm_data *pcm_data;
680
	struct sst_hsw_stream *sst_stream;
681
	struct sst_hsw *hsw = pdata->hsw;
682 683
	struct snd_pcm_runtime *runtime = substream->runtime;
	snd_pcm_uframes_t pos;
684 685 686 687
	int dai;

	dai = mod_map[rtd->cpu_dai->id].dai_id;
	pcm_data = &pdata->pcm[dai][substream->stream];
688
	sst_stream = pcm_data->stream;
689 690 691 692 693

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
694
		sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
695 696 697 698 699
		sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
700
		sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
701 702
		sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
		break;
703 704 705 706 707
	case SNDRV_PCM_TRIGGER_DRAIN:
		pos = runtime->control->appl_ptr % runtime->buffer_size;
		sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
		sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
		break;
708 709 710 711 712 713 714 715 716 717 718 719 720
	default:
		break;
	}

	return 0;
}

static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
{
	struct hsw_pcm_data *pcm_data = data;
	struct snd_pcm_substream *substream = pcm_data->substream;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
721 722 723
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
	struct sst_hsw *hsw = pdata->hsw;
724
	u32 pos;
725 726 727 728 729 730 731
	snd_pcm_uframes_t position = bytes_to_frames(runtime,
		 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
	unsigned char *dma_area = runtime->dma_area;
	snd_pcm_uframes_t dma_frames =
		bytes_to_frames(runtime, runtime->dma_bytes);
	snd_pcm_uframes_t old_position;
	ssize_t samples;
732 733 734 735

	pos = frames_to_bytes(runtime,
		(runtime->control->appl_ptr % runtime->buffer_size));

736
	dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
737

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	/* SST fw don't know where to stop dma
	 * So, SST driver need to clean the data which has been consumed
	 */
	if (dma_area == NULL || dma_frames <= 0
		|| (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
		|| !sst_hsw_stream_get_silence_start(hsw, stream)) {
		snd_pcm_period_elapsed(substream);
		return pos;
	}

	old_position = sst_hsw_stream_get_old_position(hsw, stream);
	if (position > old_position) {
		if (position < dma_frames) {
			samples = SST_SAMPLES(runtime, position - old_position);
			snd_pcm_format_set_silence(runtime->format,
				SST_OLD_POSITION(dma_area,
					runtime, old_position),
				samples);
		} else
			dev_err(rtd->dev, "PCM: position is wrong\n");
	} else {
		if (old_position < dma_frames) {
			samples = SST_SAMPLES(runtime,
				dma_frames - old_position);
			snd_pcm_format_set_silence(runtime->format,
				SST_OLD_POSITION(dma_area,
					runtime, old_position),
				samples);
		} else
			dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
		if (position < dma_frames) {
			samples = SST_SAMPLES(runtime, position);
			snd_pcm_format_set_silence(runtime->format,
				dma_area, samples);
		} else
			dev_err(rtd->dev, "PCM: position is wrong\n");
	}
	sst_hsw_stream_set_old_position(hsw, stream, position);

777 778 779 780 781 782 783 784 785 786 787
	/* let alsa know we have play a period */
	snd_pcm_period_elapsed(substream);
	return pos;
}

static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
788
	struct hsw_pcm_data *pcm_data;
789 790
	struct sst_hsw *hsw = pdata->hsw;
	snd_pcm_uframes_t offset;
791
	uint64_t ppos;
792 793 794 795 796 797
	u32 position;
	int dai;

	dai = mod_map[rtd->cpu_dai->id].dai_id;
	pcm_data = &pdata->pcm[dai][substream->stream];
	position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
798

799 800
	offset = bytes_to_frames(runtime, position);
	ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
801

802
	dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
803
		position, ppos);
804 805 806 807 808 809 810 811 812 813
	return offset;
}

static int hsw_pcm_open(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
	struct hsw_pcm_data *pcm_data;
	struct sst_hsw *hsw = pdata->hsw;
814
	int dai;
815

816 817
	dai = mod_map[rtd->cpu_dai->id].dai_id;
	pcm_data = &pdata->pcm[dai][substream->stream];
818 819

	mutex_lock(&pcm_data->mutex);
820
	pm_runtime_get_sync(pdata->dev);
821 822 823 824 825 826 827 828 829 830

	snd_soc_pcm_set_drvdata(rtd, pcm_data);
	pcm_data->substream = substream;

	snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);

	pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
		hsw_notify_pointer, pcm_data);
	if (pcm_data->stream == NULL) {
		dev_err(rtd->dev, "error: failed to create stream\n");
831 832
		pm_runtime_mark_last_busy(pdata->dev);
		pm_runtime_put_autosuspend(pdata->dev);
833 834 835 836 837 838 839 840 841 842 843 844 845
		mutex_unlock(&pcm_data->mutex);
		return -EINVAL;
	}

	mutex_unlock(&pcm_data->mutex);
	return 0;
}

static int hsw_pcm_close(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct hsw_priv_data *pdata =
		snd_soc_platform_get_drvdata(rtd->platform);
846
	struct hsw_pcm_data *pcm_data;
847
	struct sst_hsw *hsw = pdata->hsw;
848 849 850 851
	int ret, dai;

	dai = mod_map[rtd->cpu_dai->id].dai_id;
	pcm_data = &pdata->pcm[dai][substream->stream];
852 853 854 855 856 857 858 859 860 861 862 863 864

	mutex_lock(&pcm_data->mutex);
	ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
	if (ret < 0) {
		dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
		goto out;
	}

	ret = sst_hsw_stream_free(hsw, pcm_data->stream);
	if (ret < 0) {
		dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
		goto out;
	}
865
	pcm_data->allocated = 0;
866 867 868
	pcm_data->stream = NULL;

out:
869 870
	pm_runtime_mark_last_busy(pdata->dev);
	pm_runtime_put_autosuspend(pdata->dev);
871 872 873 874 875 876 877 878 879 880 881 882
	mutex_unlock(&pcm_data->mutex);
	return ret;
}

static struct snd_pcm_ops hsw_pcm_ops = {
	.open		= hsw_pcm_open,
	.close		= hsw_pcm_close,
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= hsw_pcm_hw_params,
	.hw_free	= hsw_pcm_hw_free,
	.trigger	= hsw_pcm_trigger,
	.pointer	= hsw_pcm_pointer,
883
	.page		= snd_pcm_sgbuf_ops_page,
884 885
};

886 887 888 889 890 891 892
static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
{
	struct sst_hsw *hsw = pdata->hsw;
	struct hsw_pcm_data *pcm_data;
	int i;

	for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
893
		pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
894

895
		/* create new runtime module, use same offset if recreated */
896 897 898 899 900 901 902 903
		pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
			mod_map[i].mod_id, pcm_data->persistent_offset);
		if (pcm_data->runtime == NULL)
			goto err;
		pcm_data->persistent_offset =
			pcm_data->runtime->persistent_offset;
	}

904 905
	/* create runtime blocks for module waves */
	if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
906 907 908
		pdata->runtime_waves = sst_hsw_runtime_module_create(hsw,
			SST_HSW_MODULE_WAVES, 0);
		if (pdata->runtime_waves == NULL)
909 910 911
			goto err;
	}

912 913 914 915
	return 0;

err:
	for (--i; i >= 0; i--) {
916
		pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
917 918 919 920 921 922 923 924
		sst_hsw_runtime_module_free(pcm_data->runtime);
	}

	return -ENODEV;
}

static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
{
925
	struct sst_hsw *hsw = pdata->hsw;
926 927 928 929
	struct hsw_pcm_data *pcm_data;
	int i;

	for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
930
		pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
931 932 933
		sst_hsw_runtime_module_free(pcm_data->runtime);
	}
	if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
934
		sst_hsw_runtime_module_free(pdata->runtime_waves);
935 936 937
	}
}

938 939 940
static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_pcm *pcm = rtd->pcm;
941 942
	struct snd_soc_platform *platform = rtd->platform;
	struct sst_pdata *pdata = dev_get_platdata(platform->dev);
943
	struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
944
	struct device *dev = pdata->dma_dev;
945 946 947 948 949
	int ret = 0;

	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
		ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
950
			SNDRV_DMA_TYPE_DEV_SG,
951
			dev,
952 953 954 955 956 957 958 959
			hsw_pcm_hardware.buffer_bytes_max,
			hsw_pcm_hardware.buffer_bytes_max);
		if (ret) {
			dev_err(rtd->dev, "dma buffer allocation failed %d\n",
				ret);
			return ret;
		}
	}
960 961 962 963
	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
		priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
		priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
964 965 966 967 968

	return ret;
}

#define HSW_FORMATS \
969
	(SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
970 971 972 973

static struct snd_soc_dai_driver hsw_dais[] = {
	{
		.name  = "System Pin",
974
		.id = HSW_PCM_DAI_ID_SYSTEM,
975 976 977 978 979
		.playback = {
			.stream_name = "System Playback",
			.channels_min = 2,
			.channels_max = 2,
			.rates = SNDRV_PCM_RATE_48000,
980
			.formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
981
		},
982 983 984 985 986 987 988
		.capture = {
			.stream_name = "Analog Capture",
			.channels_min = 2,
			.channels_max = 4,
			.rates = SNDRV_PCM_RATE_48000,
			.formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
		},
989 990 991 992
	},
	{
		/* PCM */
		.name  = "Offload0 Pin",
993
		.id = HSW_PCM_DAI_ID_OFFLOAD0,
994 995 996 997 998 999 1000 1001 1002 1003 1004
		.playback = {
			.stream_name = "Offload0 Playback",
			.channels_min = 2,
			.channels_max = 2,
			.rates = SNDRV_PCM_RATE_8000_192000,
			.formats = HSW_FORMATS,
		},
	},
	{
		/* PCM */
		.name  = "Offload1 Pin",
1005
		.id = HSW_PCM_DAI_ID_OFFLOAD1,
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		.playback = {
			.stream_name = "Offload1 Playback",
			.channels_min = 2,
			.channels_max = 2,
			.rates = SNDRV_PCM_RATE_8000_192000,
			.formats = HSW_FORMATS,
		},
	},
	{
		.name  = "Loopback Pin",
1016
		.id = HSW_PCM_DAI_ID_LOOPBACK,
1017 1018 1019 1020
		.capture = {
			.stream_name = "Loopback Capture",
			.channels_min = 2,
			.channels_max = 2,
1021 1022
			.rates = SNDRV_PCM_RATE_48000,
			.formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		},
	},
};

static const struct snd_soc_dapm_widget widgets[] = {

	/* Backend DAIs  */
	SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),

	/* Global Playback Mixer */
	SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
};

static const struct snd_soc_dapm_route graph[] = {

	/* Playback Mixer */
	{"Playback VMixer", NULL, "System Playback"},
	{"Playback VMixer", NULL, "Offload0 Playback"},
	{"Playback VMixer", NULL, "Offload1 Playback"},

	{"SSP0 CODEC OUT", NULL, "Playback VMixer"},

	{"Analog Capture", NULL, "SSP0 CODEC IN"},
};

static int hsw_pcm_probe(struct snd_soc_platform *platform)
{
1053
	struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
1054
	struct sst_pdata *pdata = dev_get_platdata(platform->dev);
1055
	struct device *dma_dev, *dev;
1056
	int i, ret = 0;
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	if (!pdata)
		return -ENODEV;

	dev = platform->dev;
	dma_dev = pdata->dma_dev;

	priv_data->hsw = pdata->dsp;
	priv_data->dev = platform->dev;
	priv_data->pm_state = HSW_PM_STATE_D0;
	priv_data->soc_card = platform->component.card;

1069 1070 1071 1072 1073
	/* allocate DSP buffer page tables */
	for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {

		/* playback */
		if (hsw_dais[i].playback.channels_min) {
1074
			mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
1075 1076 1077
			ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
				PAGE_SIZE, &priv_data->dmab[i][0]);
			if (ret < 0)
1078 1079 1080 1081 1082
				goto err;
		}

		/* capture */
		if (hsw_dais[i].capture.channels_min) {
1083
			mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
1084 1085 1086
			ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
				PAGE_SIZE, &priv_data->dmab[i][1]);
			if (ret < 0)
1087 1088 1089 1090
				goto err;
		}
	}

1091
	/* allocate runtime modules */
1092 1093 1094
	ret = hsw_pcm_create_modules(priv_data);
	if (ret < 0)
		goto err;
1095

1096 1097 1098 1099 1100 1101 1102
	/* enable runtime PM with auto suspend */
	pm_runtime_set_autosuspend_delay(platform->dev,
		SST_RUNTIME_SUSPEND_DELAY);
	pm_runtime_use_autosuspend(platform->dev);
	pm_runtime_enable(platform->dev);
	pm_runtime_idle(platform->dev);

1103 1104 1105
	return 0;

err:
1106
	for (--i; i >= 0; i--) {
1107
		if (hsw_dais[i].playback.channels_min)
1108
			snd_dma_free_pages(&priv_data->dmab[i][0]);
1109
		if (hsw_dais[i].capture.channels_min)
1110
			snd_dma_free_pages(&priv_data->dmab[i][1]);
1111
	}
1112
	return ret;
1113 1114 1115 1116 1117 1118 1119 1120
}

static int hsw_pcm_remove(struct snd_soc_platform *platform)
{
	struct hsw_priv_data *priv_data =
		snd_soc_platform_get_drvdata(platform);
	int i;

1121 1122 1123
	pm_runtime_disable(platform->dev);
	hsw_pcm_free_modules(priv_data);

1124 1125
	for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
		if (hsw_dais[i].playback.channels_min)
1126
			snd_dma_free_pages(&priv_data->dmab[i][0]);
1127
		if (hsw_dais[i].capture.channels_min)
1128
			snd_dma_free_pages(&priv_data->dmab[i][1]);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	}

	return 0;
}

static struct snd_soc_platform_driver hsw_soc_platform = {
	.probe		= hsw_pcm_probe,
	.remove		= hsw_pcm_remove,
	.ops		= &hsw_pcm_ops,
	.pcm_new	= hsw_pcm_new,
};

static const struct snd_soc_component_driver hsw_dai_component = {
1142 1143 1144 1145 1146 1147 1148
	.name = "haswell-dai",
	.controls = hsw_volume_controls,
	.num_controls = ARRAY_SIZE(hsw_volume_controls),
	.dapm_widgets = widgets,
	.num_dapm_widgets = ARRAY_SIZE(widgets),
	.dapm_routes = graph,
	.num_dapm_routes = ARRAY_SIZE(graph),
1149 1150 1151 1152 1153
};

static int hsw_pcm_dev_probe(struct platform_device *pdev)
{
	struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1154
	struct hsw_priv_data *priv_data;
1155 1156
	int ret;

1157 1158 1159 1160 1161 1162 1163
	if (!sst_pdata)
		return -EINVAL;

	priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
	if (!priv_data)
		return -ENOMEM;

1164 1165 1166 1167
	ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
	if (ret < 0)
		return -ENODEV;

1168 1169 1170
	priv_data->hsw = sst_pdata->dsp;
	platform_set_drvdata(pdev, priv_data);

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
	if (ret < 0)
		goto err_plat;

	ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
		hsw_dais, ARRAY_SIZE(hsw_dais));
	if (ret < 0)
		goto err_comp;

	return 0;

err_comp:
	snd_soc_unregister_platform(&pdev->dev);
err_plat:
	sst_hsw_dsp_free(&pdev->dev, sst_pdata);
	return 0;
}

static int hsw_pcm_dev_remove(struct platform_device *pdev)
{
	struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);

	snd_soc_unregister_platform(&pdev->dev);
	snd_soc_unregister_component(&pdev->dev);
	sst_hsw_dsp_free(&pdev->dev, sst_pdata);

	return 0;
}

1200
#ifdef CONFIG_PM
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

static int hsw_pcm_runtime_idle(struct device *dev)
{
	return 0;
}

static int hsw_pcm_runtime_suspend(struct device *dev)
{
	struct hsw_priv_data *pdata = dev_get_drvdata(dev);
	struct sst_hsw *hsw = pdata->hsw;
1211
	int ret;
1212

1213
	if (pdata->pm_state >= HSW_PM_STATE_RTD3)
1214 1215
		return 0;

1216 1217 1218 1219 1220 1221 1222
	/* fw modules will be unloaded on RTD3, set flag to track */
	if (sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
		ret = sst_hsw_module_disable(hsw, SST_HSW_MODULE_WAVES, 0);
		if (ret < 0)
			return ret;
		sst_hsw_set_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
	}
1223 1224
	sst_hsw_dsp_runtime_suspend(hsw);
	sst_hsw_dsp_runtime_sleep(hsw);
1225
	pdata->pm_state = HSW_PM_STATE_RTD3;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

	return 0;
}

static int hsw_pcm_runtime_resume(struct device *dev)
{
	struct hsw_priv_data *pdata = dev_get_drvdata(dev);
	struct sst_hsw *hsw = pdata->hsw;
	int ret;

1236
	if (pdata->pm_state != HSW_PM_STATE_RTD3)
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
		return 0;

	ret = sst_hsw_dsp_load(hsw);
	if (ret < 0) {
		dev_err(dev, "failed to reload %d\n", ret);
		return ret;
	}

	ret = hsw_pcm_create_modules(pdata);
	if (ret < 0) {
		dev_err(dev, "failed to create modules %d\n", ret);
		return ret;
	}

	ret = sst_hsw_dsp_runtime_resume(hsw);
	if (ret < 0)
		return ret;
	else if (ret == 1) /* no action required */
		return 0;

1257 1258 1259
	/* check flag when resume */
	if (sst_hsw_is_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES)) {
		ret = sst_hsw_module_enable(hsw, SST_HSW_MODULE_WAVES, 0);
1260 1261 1262 1263
		if (ret < 0)
			return ret;
		/* put parameters from buffer to dsp */
		ret = sst_hsw_launch_param_buf(hsw);
1264 1265 1266 1267 1268 1269
		if (ret < 0)
			return ret;
		/* unset flag */
		sst_hsw_set_module_disabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
	}

1270 1271 1272 1273
	pdata->pm_state = HSW_PM_STATE_D0;
	return ret;
}

1274 1275 1276 1277 1278 1279
#else
#define hsw_pcm_runtime_idle		NULL
#define hsw_pcm_runtime_suspend		NULL
#define hsw_pcm_runtime_resume		NULL
#endif

1280
#ifdef CONFIG_PM
1281

1282 1283 1284 1285 1286 1287 1288
static void hsw_pcm_complete(struct device *dev)
{
	struct hsw_priv_data *pdata = dev_get_drvdata(dev);
	struct sst_hsw *hsw = pdata->hsw;
	struct hsw_pcm_data *pcm_data;
	int i, err;

1289
	if (pdata->pm_state != HSW_PM_STATE_D3)
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
		return;

	err = sst_hsw_dsp_load(hsw);
	if (err < 0) {
		dev_err(dev, "failed to reload %d\n", err);
		return;
	}

	err = hsw_pcm_create_modules(pdata);
	if (err < 0) {
		dev_err(dev, "failed to create modules %d\n", err);
		return;
	}

1304
	for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1305
		pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

		if (!pcm_data->substream)
			continue;

		err = sst_module_runtime_restore(pcm_data->runtime,
			&pcm_data->context);
		if (err < 0)
			dev_err(dev, "failed to restore context for PCM %d\n", i);
	}

	snd_soc_resume(pdata->soc_card->dev);

	err = sst_hsw_dsp_runtime_resume(hsw);
	if (err < 0)
		return;
	else if (err == 1) /* no action required */
		return;

	pdata->pm_state = HSW_PM_STATE_D0;
	return;
}

static int hsw_pcm_prepare(struct device *dev)
{
	struct hsw_priv_data *pdata = dev_get_drvdata(dev);
	struct sst_hsw *hsw = pdata->hsw;
	struct hsw_pcm_data *pcm_data;
	int i, err;

	if (pdata->pm_state == HSW_PM_STATE_D3)
		return 0;
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	else if (pdata->pm_state == HSW_PM_STATE_D0) {
		/* suspend all active streams */
		for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
			pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];

			if (!pcm_data->substream)
				continue;
			dev_dbg(dev, "suspending pcm %d\n", i);
			snd_pcm_suspend_all(pcm_data->hsw_pcm);

			/* We need to wait until the DSP FW stops the streams */
			msleep(2);
		}
1350

1351 1352 1353 1354 1355 1356
		/* preserve persistent memory */
		for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
			pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];

			if (!pcm_data->substream)
				continue;
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
			dev_dbg(dev, "saving context pcm %d\n", i);
			err = sst_module_runtime_save(pcm_data->runtime,
				&pcm_data->context);
			if (err < 0)
				dev_err(dev, "failed to save context for PCM %d\n", i);
		}
		/* enter D3 state and stall */
		sst_hsw_dsp_runtime_suspend(hsw);
		/* put the DSP to sleep */
		sst_hsw_dsp_runtime_sleep(hsw);
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	}

	snd_soc_suspend(pdata->soc_card->dev);
	snd_soc_poweroff(pdata->soc_card->dev);

	pdata->pm_state = HSW_PM_STATE_D3;

	return 0;
}

1378 1379 1380 1381 1382
#else
#define hsw_pcm_prepare		NULL
#define hsw_pcm_complete	NULL
#endif

1383 1384 1385 1386 1387 1388 1389 1390
static const struct dev_pm_ops hsw_pcm_pm = {
	.runtime_idle = hsw_pcm_runtime_idle,
	.runtime_suspend = hsw_pcm_runtime_suspend,
	.runtime_resume = hsw_pcm_runtime_resume,
	.prepare = hsw_pcm_prepare,
	.complete = hsw_pcm_complete,
};

1391 1392 1393
static struct platform_driver hsw_pcm_driver = {
	.driver = {
		.name = "haswell-pcm-audio",
1394
		.pm = &hsw_pcm_pm,
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	},

	.probe = hsw_pcm_dev_probe,
	.remove = hsw_pcm_dev_remove,
};
module_platform_driver(hsw_pcm_driver);

MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:haswell-pcm-audio");