acp-pcm-dma.c 39.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9
/*
 * AMD ALSA SoC PCM Driver for ACP 2.x
 *
 * Copyright 2014-2015 Advanced Micro Devices, Inc.
 */

#include <linux/module.h>
#include <linux/delay.h>
10
#include <linux/io.h>
11
#include <linux/iopoll.h>
12
#include <linux/sizes.h>
13
#include <linux/pm_runtime.h>
14 15

#include <sound/soc.h>
16
#include <drm/amd_asic_type.h>
17 18
#include "acp.h"

19 20
#define DRV_NAME "acp_audio_dma"

21 22 23 24 25 26 27 28 29 30 31 32
#define PLAYBACK_MIN_NUM_PERIODS    2
#define PLAYBACK_MAX_NUM_PERIODS    2
#define PLAYBACK_MAX_PERIOD_SIZE    16384
#define PLAYBACK_MIN_PERIOD_SIZE    1024
#define CAPTURE_MIN_NUM_PERIODS     2
#define CAPTURE_MAX_NUM_PERIODS     2
#define CAPTURE_MAX_PERIOD_SIZE     16384
#define CAPTURE_MIN_PERIOD_SIZE     1024

#define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
#define MIN_BUFFER MAX_BUFFER

33
#define ST_PLAYBACK_MAX_PERIOD_SIZE 4096
34 35 36 37
#define ST_CAPTURE_MAX_PERIOD_SIZE  ST_PLAYBACK_MAX_PERIOD_SIZE
#define ST_MAX_BUFFER (ST_PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
#define ST_MIN_BUFFER ST_MAX_BUFFER

38
#define DRV_NAME "acp_audio_dma"
39 40
bool bt_uart_enable = true;
EXPORT_SYMBOL(bt_uart_enable);
41

42 43 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
static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
	.info = SNDRV_PCM_INFO_INTERLEAVED |
		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
	.formats = SNDRV_PCM_FMTBIT_S16_LE |
		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
	.channels_min = 1,
	.channels_max = 8,
	.rates = SNDRV_PCM_RATE_8000_96000,
	.rate_min = 8000,
	.rate_max = 96000,
	.buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
	.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
};

static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
	.info = SNDRV_PCM_INFO_INTERLEAVED |
		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
	    SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
	.formats = SNDRV_PCM_FMTBIT_S16_LE |
		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
	.channels_min = 1,
	.channels_max = 2,
	.rates = SNDRV_PCM_RATE_8000_48000,
	.rate_min = 8000,
	.rate_max = 48000,
	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
	.periods_min = CAPTURE_MIN_NUM_PERIODS,
	.periods_max = CAPTURE_MAX_NUM_PERIODS,
};

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static const struct snd_pcm_hardware acp_st_pcm_hardware_playback = {
	.info = SNDRV_PCM_INFO_INTERLEAVED |
		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
	.formats = SNDRV_PCM_FMTBIT_S16_LE |
		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
	.channels_min = 1,
	.channels_max = 8,
	.rates = SNDRV_PCM_RATE_8000_96000,
	.rate_min = 8000,
	.rate_max = 96000,
	.buffer_bytes_max = ST_MAX_BUFFER,
	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
	.period_bytes_max = ST_PLAYBACK_MAX_PERIOD_SIZE,
	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
};

static const struct snd_pcm_hardware acp_st_pcm_hardware_capture = {
	.info = SNDRV_PCM_INFO_INTERLEAVED |
		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
	.formats = SNDRV_PCM_FMTBIT_S16_LE |
		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
	.channels_min = 1,
	.channels_max = 2,
	.rates = SNDRV_PCM_RATE_8000_48000,
	.rate_min = 8000,
	.rate_max = 48000,
	.buffer_bytes_max = ST_MAX_BUFFER,
	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
	.period_bytes_max = ST_CAPTURE_MAX_PERIOD_SIZE,
	.periods_min = CAPTURE_MIN_NUM_PERIODS,
	.periods_max = CAPTURE_MAX_NUM_PERIODS,
};

118 119 120 121 122 123 124 125 126 127
static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
{
	return readl(acp_mmio + (reg * 4));
}

static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
{
	writel(val, acp_mmio + (reg * 4));
}

128 129
/*
 * Configure a given dma channel parameters - enable/disable,
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 * number of descriptors, priority
 */
static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
				   u16 dscr_strt_idx, u16 num_dscrs,
				   enum acp_dma_priority_level priority_level)
{
	u32 dma_ctrl;

	/* disable the channel run field */
	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);

	/* program a DMA channel with first descriptor to be processed. */
	acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
			& dscr_strt_idx),
			acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);

148 149
	/*
	 * program a DMA channel with the number of descriptors to be
150
	 * processed in the transfer
151
	 */
152
	acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
153
		      acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	/* set DMA channel priority */
	acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
}

/* Initialize a dma descriptor in SRAM based on descritor information passed */
static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
					  u16 descr_idx,
					  acp_dma_dscr_transfer_t *descr_info)
{
	u32 sram_offset;

	sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));

	/* program the source base address. */
	acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
	acp_reg_write(descr_info->src,	acp_mmio, mmACP_SRBM_Targ_Idx_Data);
	/* program the destination base address. */
	acp_reg_write(sram_offset + 4,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
	acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);

	/* program the number of bytes to be transferred for this descriptor. */
	acp_reg_write(sram_offset + 8,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
	acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
static void pre_config_reset(void __iomem *acp_mmio, u16 ch_num)
{
	u32 dma_ctrl;
	int ret;

	/* clear the reset bit */
	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
	/* check the reset bit before programming configuration registers */
	ret = readl_poll_timeout(acp_mmio + ((mmACP_DMA_CNTL_0 + ch_num) * 4),
				 dma_ctrl,
				 !(dma_ctrl & ACP_DMA_CNTL_0__DMAChRst_MASK),
				 100, ACP_DMA_RESET_TIME);
	if (ret < 0)
		pr_err("Failed to clear reset of channel : %d\n", ch_num);
}

198 199
/*
 * Initialize the DMA descriptor information for transfer between
200 201 202
 * system memory <-> ACP SRAM
 */
static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
203 204 205 206
					   u32 size, int direction,
					   u32 pte_offset, u16 ch,
					   u32 sram_bank, u16 dma_dscr_idx,
					   u32 asic_type)
207 208 209 210 211 212 213
{
	u16 i;
	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];

	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
		dmadscr[i].xfer_val = 0;
		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
214
			dma_dscr_idx = dma_dscr_idx + i;
215
			dmadscr[i].dest = sram_bank + (i * (size / 2));
216
			dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
217
				+ (pte_offset * SZ_4K) + (i * (size / 2));
218 219 220
			switch (asic_type) {
			case CHIP_STONEY:
				dmadscr[i].xfer_val |=
221
				(ACP_DMA_ATTR_DAGB_GARLIC_TO_SHAREDMEM  << 16) |
222 223 224 225
				(size / 2);
				break;
			default:
				dmadscr[i].xfer_val |=
226
				(ACP_DMA_ATTR_DAGB_ONION_TO_SHAREDMEM  << 16) |
227 228
				(size / 2);
			}
229
		} else {
230
			dma_dscr_idx = dma_dscr_idx + i;
231
			dmadscr[i].src = sram_bank + (i * (size / 2));
232 233
			dmadscr[i].dest =
			ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS +
234
			(pte_offset * SZ_4K) + (i * (size / 2));
235 236 237
			switch (asic_type) {
			case CHIP_STONEY:
				dmadscr[i].xfer_val |=
238
				(ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
239 240 241 242
				(size / 2);
				break;
			default:
				dmadscr[i].xfer_val |=
243
				(ACP_DMA_ATTR_SHAREDMEM_TO_DAGB_ONION << 16) |
244 245
				(size / 2);
			}
246 247
		}
		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
248
					      &dmadscr[i]);
249
	}
250
	pre_config_reset(acp_mmio, ch);
251
	config_acp_dma_channel(acp_mmio, ch,
252 253 254
			       dma_dscr_idx - 1,
			       NUM_DSCRS_PER_CHANNEL,
			       ACP_DMA_PRIORITY_LEVEL_NORMAL);
255 256
}

257 258
/*
 * Initialize the DMA descriptor information for transfer between
259 260
 * ACP SRAM <-> I2S
 */
261
static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio, u32 size,
262 263 264
					   int direction, u32 sram_bank,
					   u16 destination, u16 ch,
					   u16 dma_dscr_idx, u32 asic_type)
265 266 267 268 269 270 271
{
	u16 i;
	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];

	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
		dmadscr[i].xfer_val = 0;
		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
272
			dma_dscr_idx = dma_dscr_idx + i;
273
			dmadscr[i].src = sram_bank  + (i * (size / 2));
274 275
			/* dmadscr[i].dest is unused by hardware. */
			dmadscr[i].dest = 0;
276
			dmadscr[i].xfer_val |= BIT(22) | (destination << 16) |
277 278
						(size / 2);
		} else {
279
			dma_dscr_idx = dma_dscr_idx + i;
280 281
			/* dmadscr[i].src is unused by hardware. */
			dmadscr[i].src = 0;
282 283
			dmadscr[i].dest =
				 sram_bank + (i * (size / 2));
284
			dmadscr[i].xfer_val |= BIT(22) |
285
				(destination << 16) | (size / 2);
286 287
		}
		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
288
					      &dmadscr[i]);
289
	}
290
	pre_config_reset(acp_mmio, ch);
291
	/* Configure the DMA channel with the above descriptore */
292
	config_acp_dma_channel(acp_mmio, ch, dma_dscr_idx - 1,
293 294
			       NUM_DSCRS_PER_CHANNEL,
			       ACP_DMA_PRIORITY_LEVEL_NORMAL);
295 296 297
}

/* Create page table entries in ACP SRAM for the allocated memory */
298
static void acp_pte_config(void __iomem *acp_mmio, dma_addr_t addr,
299 300 301 302 303 304 305 306 307 308 309
			   u16 num_of_pages, u32 pte_offset)
{
	u16 page_idx;
	u32 low;
	u32 high;
	u32 offset;

	offset	= ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
	for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
		/* Load the low address of page int ACP SRAM through SRBM */
		acp_reg_write((offset + (page_idx * 8)),
310
			      acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
311 312 313 314 315 316 317 318

		low = lower_32_bits(addr);
		high = upper_32_bits(addr);

		acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);

		/* Load the High address of page int ACP SRAM through SRBM */
		acp_reg_write((offset + (page_idx * 8) + 4),
319
			      acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
320 321 322 323 324 325

		/* page enable in ACP */
		high |= BIT(31);
		acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);

		/* Move to next physically contiguos page */
326
		addr += PAGE_SIZE;
327 328 329 330
	}
}

static void config_acp_dma(void __iomem *acp_mmio,
331
			   struct audio_substream_data *rtd,
332
			   u32 asic_type)
333
{
334 335
	u16 ch_acp_sysmem, ch_acp_i2s;

336
	acp_pte_config(acp_mmio, rtd->dma_addr, rtd->num_of_pages,
337
		       rtd->pte_offset);
338 339 340 341 342 343 344 345

	if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
		ch_acp_sysmem = rtd->ch1;
		ch_acp_i2s = rtd->ch2;
	} else {
		ch_acp_i2s = rtd->ch1;
		ch_acp_sysmem = rtd->ch2;
	}
346
	/* Configure System memory <-> ACP SRAM DMA descriptors */
347
	set_acp_sysmem_dma_descriptors(acp_mmio, rtd->size,
348
				       rtd->direction, rtd->pte_offset,
349
				       ch_acp_sysmem, rtd->sram_bank,
350
				       rtd->dma_dscr_idx_1, asic_type);
351
	/* Configure ACP SRAM <-> I2S DMA descriptors */
352
	set_acp_to_i2s_dma_descriptors(acp_mmio, rtd->size,
353
				       rtd->direction, rtd->sram_bank,
354
				       rtd->destination, ch_acp_i2s,
355
				       rtd->dma_dscr_idx_2, asic_type);
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 402 403 404 405 406 407 408 409 410 411 412
static void acp_dma_cap_channel_enable(void __iomem *acp_mmio,
				       u16 cap_channel)
{
	u32 val, ch_reg, imr_reg, res_reg;

	switch (cap_channel) {
	case CAP_CHANNEL1:
		ch_reg = mmACP_I2SMICSP_RER1;
		res_reg = mmACP_I2SMICSP_RCR1;
		imr_reg = mmACP_I2SMICSP_IMR1;
		break;
	case CAP_CHANNEL0:
	default:
		ch_reg = mmACP_I2SMICSP_RER0;
		res_reg = mmACP_I2SMICSP_RCR0;
		imr_reg = mmACP_I2SMICSP_IMR0;
		break;
	}
	val = acp_reg_read(acp_mmio,
			   mmACP_I2S_16BIT_RESOLUTION_EN);
	if (val & ACP_I2S_MIC_16BIT_RESOLUTION_EN) {
		acp_reg_write(0x0, acp_mmio, ch_reg);
		/* Set 16bit resolution on capture */
		acp_reg_write(0x2, acp_mmio, res_reg);
	}
	val = acp_reg_read(acp_mmio, imr_reg);
	val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
	val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
	acp_reg_write(val, acp_mmio, imr_reg);
	acp_reg_write(0x1, acp_mmio, ch_reg);
}

static void acp_dma_cap_channel_disable(void __iomem *acp_mmio,
					u16 cap_channel)
{
	u32 val, ch_reg, imr_reg;

	switch (cap_channel) {
	case CAP_CHANNEL1:
		imr_reg = mmACP_I2SMICSP_IMR1;
		ch_reg = mmACP_I2SMICSP_RER1;
		break;
	case CAP_CHANNEL0:
	default:
		imr_reg = mmACP_I2SMICSP_IMR0;
		ch_reg = mmACP_I2SMICSP_RER0;
		break;
	}
	val = acp_reg_read(acp_mmio, imr_reg);
	val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
	val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
	acp_reg_write(val, acp_mmio, imr_reg);
	acp_reg_write(0x0, acp_mmio, ch_reg);
}

413
/* Start a given DMA channel transfer */
414
static void acp_dma_start(void __iomem *acp_mmio, u16 ch_num, bool is_circular)
415 416 417 418 419 420 421 422 423
{
	u32 dma_ctrl;

	/* read the dma control register and disable the channel run field */
	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);

	/* Invalidating the DAGB cache */
	acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);

424 425
	/*
	 * configure the DMA channel and start the DMA transfer
426 427 428 429 430 431 432
	 * set dmachrun bit to start the transfer and enable the
	 * interrupt on completion of the dma transfer
	 */
	dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;

	switch (ch_num) {
	case ACP_TO_I2S_DMA_CH_NUM:
433
	case I2S_TO_ACP_DMA_CH_NUM:
434
	case ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM:
435
	case I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM:
436 437 438 439 440 441 442
		dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
		break;
	default:
		dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
		break;
	}

443 444 445 446 447
	/* enable for ACP to SRAM DMA channel */
	if (is_circular == true)
		dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
	else
		dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
448 449 450 451 452 453 454 455 456 457 458 459 460

	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
}

/* Stop a given DMA channel transfer */
static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
{
	u32 dma_ctrl;
	u32 dma_ch_sts;
	u32 count = ACP_DMA_RESET_TIME;

	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);

461 462
	/*
	 * clear the dma control register fields before writing zero
463
	 * in reset bit
464
	 */
465 466 467 468 469 470 471
	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;

	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
	dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);

	if (dma_ch_sts & BIT(ch_num)) {
472 473 474 475
		/*
		 * set the reset bit for this channel to stop the dma
		 *  transfer
		 */
476 477 478 479 480 481 482 483
		dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
		acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
	}

	/* check the channel status bit for some time and return the status */
	while (true) {
		dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
		if (!(dma_ch_sts & BIT(ch_num))) {
484 485 486 487
			/*
			 * clear the reset flag after successfully stopping
			 * the dma transfer and break from the loop
			 */
488 489 490
			dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;

			acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
491
				      + ch_num);
492 493 494 495 496 497 498 499 500 501 502
			break;
		}
		if (--count == 0) {
			pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
			return -ETIMEDOUT;
		}
		udelay(100);
	}
	return 0;
}

503
static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
504
				    bool power_on)
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
{
	u32 val, req_reg, sts_reg, sts_reg_mask;
	u32 loops = 1000;

	if (bank < 32) {
		req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
		sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
		sts_reg_mask = 0xFFFFFFFF;

	} else {
		bank -= 32;
		req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
		sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
		sts_reg_mask = 0x0000FFFF;
	}

	val = acp_reg_read(acp_mmio, req_reg);
	if (val & (1 << bank)) {
		/* bank is in off state */
		if (power_on == true)
			/* request to on */
			val &= ~(1 << bank);
		else
			/* request to off */
			return;
	} else {
		/* bank is in on state */
		if (power_on == false)
			/* request to off */
			val |= 1 << bank;
		else
			/* request to on */
			return;
	}
	acp_reg_write(val, acp_mmio, req_reg);

	while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
		if (!loops--) {
			pr_err("ACP SRAM bank %d state change failed\n", bank);
			break;
		}
		cpu_relax();
	}
}

550
/* Initialize and bring ACP hardware to default state. */
551
static int acp_init(void __iomem *acp_mmio, u32 asic_type)
552
{
553
	u16 bank;
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	u32 val, count, sram_pte_offset;

	/* Assert Soft reset of ACP */
	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);

	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);

	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
	while (true) {
		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
			break;
		if (--count == 0) {
			pr_err("Failed to reset ACP\n");
			return -ETIMEDOUT;
		}
		udelay(100);
	}

	/* Enable clock to ACP and wait until the clock is enabled */
	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
	val = val | ACP_CONTROL__ClkEn_MASK;
	acp_reg_write(val, acp_mmio, mmACP_CONTROL);

	count = ACP_CLOCK_EN_TIME_OUT_VALUE;

	while (true) {
		val = acp_reg_read(acp_mmio, mmACP_STATUS);
584
		if (val & (u32)0x1)
585 586 587 588 589 590 591 592 593 594 595 596 597
			break;
		if (--count == 0) {
			pr_err("Failed to reset ACP\n");
			return -ETIMEDOUT;
		}
		udelay(100);
	}

	/* Deassert the SOFT RESET flags */
	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
	val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);

598 599 600 601 602 603 604
	/* For BT instance change pins from UART to BT */
	if (!bt_uart_enable) {
		val = acp_reg_read(acp_mmio, mmACP_BT_UART_PAD_SEL);
		val |= ACP_BT_UART_PAD_SELECT_MASK;
		acp_reg_write(val, acp_mmio, mmACP_BT_UART_PAD_SEL);
	}

605 606
	/* initiailize Onion control DAGB register */
	acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
607
		      mmACP_AXI2DAGB_ONION_CNTL);
608 609 610

	/* initiailize Garlic control DAGB registers */
	acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
611
		      mmACP_AXI2DAGB_GARLIC_CNTL);
612 613 614 615 616 617 618

	sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
	acp_reg_write(sram_pte_offset,  acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
	acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
619
		      mmACP_DAGB_PAGE_SIZE_GRP_1);
620 621

	acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
622
		      mmACP_DMA_DESC_BASE_ADDR);
623 624 625 626

	/* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
	acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
	acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
627
		      acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
628

629 630
       /*
	* When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
631 632
	* Now, turn off all of them. This can't be done in 'poweron' of
	* ACP pm domain, as this requires ACP to be initialized.
633 634 635
	* For Stoney, Memory gating is disabled,i.e SRAM Banks
	* won't be turned off. The default state for SRAM banks is ON.
	* Setting SRAM bank state code skipped for STONEY platform.
636
	*/
637 638 639 640
	if (asic_type != CHIP_STONEY) {
		for (bank = 1; bank < 48; bank++)
			acp_set_sram_bank_state(acp_mmio, bank, false);
	}
641 642 643
	return 0;
}

644
/* Deinitialize ACP */
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
static int acp_deinit(void __iomem *acp_mmio)
{
	u32 val;
	u32 count;

	/* Assert Soft reset of ACP */
	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);

	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);

	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
	while (true) {
		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
			break;
		if (--count == 0) {
			pr_err("Failed to reset ACP\n");
			return -ETIMEDOUT;
		}
		udelay(100);
	}
668
	/* Disable ACP clock */
669 670 671 672 673 674 675 676
	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
	val &= ~ACP_CONTROL__ClkEn_MASK;
	acp_reg_write(val, acp_mmio, mmACP_CONTROL);

	count = ACP_CLOCK_EN_TIME_OUT_VALUE;

	while (true) {
		val = acp_reg_read(acp_mmio, mmACP_STATUS);
677
		if (!(val & (u32)0x1))
678 679 680 681 682 683 684 685 686 687 688 689 690
			break;
		if (--count == 0) {
			pr_err("Failed to reset ACP\n");
			return -ETIMEDOUT;
		}
		udelay(100);
	}
	return 0;
}

/* ACP DMA irq handler routine for playback, capture usecases */
static irqreturn_t dma_irq_handler(int irq, void *arg)
{
691
	u16 dscr_idx;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	u32 intr_flag, ext_intr_status;
	struct audio_drv_data *irq_data;
	void __iomem *acp_mmio;
	struct device *dev = arg;
	bool valid_irq = false;

	irq_data = dev_get_drvdata(dev);
	acp_mmio = irq_data->acp_mmio;

	ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
	intr_flag = (((ext_intr_status &
		      ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
		     ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));

	if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
		valid_irq = true;
708
		snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
709
		acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
710
			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
711 712
	}

713 714 715 716 717 718 719 720
	if ((intr_flag & BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) != 0) {
		valid_irq = true;
		snd_pcm_period_elapsed(irq_data->play_i2sbt_stream);
		acp_reg_write((intr_flag &
			      BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) << 16,
			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
	}

721
	if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
722
		valid_irq = true;
723 724 725 726 727 728 729 730 731
		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_14) ==
				CAPTURE_START_DMA_DESCR_CH15)
			dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
		else
			dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
		config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
				       1, 0);
		acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);

732
		snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
733
		acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
734
			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
735 736
	}

737
	if ((intr_flag & BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) != 0) {
738
		valid_irq = true;
739 740 741 742 743 744 745 746 747 748 749
		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_10) ==
			CAPTURE_START_DMA_DESCR_CH11)
			dscr_idx = CAPTURE_END_DMA_DESCR_CH10;
		else
			dscr_idx = CAPTURE_START_DMA_DESCR_CH10;
		config_acp_dma_channel(acp_mmio,
				       ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
				       dscr_idx, 1, 0);
		acp_dma_start(acp_mmio, ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
			      false);

750
		snd_pcm_period_elapsed(irq_data->capture_i2sbt_stream);
751
		acp_reg_write((intr_flag &
752
			      BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) << 16,
753 754 755
			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
	}

756 757 758 759 760 761
	if (valid_irq)
		return IRQ_HANDLED;
	else
		return IRQ_NONE;
}

762 763
static int acp_dma_open(struct snd_soc_component *component,
			struct snd_pcm_substream *substream)
764
{
765
	u16 bank;
766 767
	int ret = 0;
	struct snd_pcm_runtime *runtime = substream->runtime;
768
	struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
769 770
	struct audio_substream_data *adata =
		kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
771
	if (!adata)
772 773
		return -ENOMEM;

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		switch (intr_data->asic_type) {
		case CHIP_STONEY:
			runtime->hw = acp_st_pcm_hardware_playback;
			break;
		default:
			runtime->hw = acp_pcm_hardware_playback;
		}
	} else {
		switch (intr_data->asic_type) {
		case CHIP_STONEY:
			runtime->hw = acp_st_pcm_hardware_capture;
			break;
		default:
			runtime->hw = acp_pcm_hardware_capture;
		}
	}
791 792 793 794

	ret = snd_pcm_hw_constraint_integer(runtime,
					    SNDRV_PCM_HW_PARAM_PERIODS);
	if (ret < 0) {
795
		dev_err(component->dev, "set integer constraint failed\n");
D
Dan Carpenter 已提交
796
		kfree(adata);
797 798 799 800 801 802
		return ret;
	}

	adata->acp_mmio = intr_data->acp_mmio;
	runtime->private_data = adata;

803 804
	/*
	 * Enable ACP irq, when neither playback or capture streams are
805 806 807
	 * active by the time when a new stream is being opened.
	 * This enablement is not required for another stream, if current
	 * stream is not closed
808
	 */
809 810
	if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream &&
	    !intr_data->play_i2sbt_stream && !intr_data->capture_i2sbt_stream)
811 812
		acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);

813
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
814 815
		/*
		 * For Stoney, Memory gating is disabled,i.e SRAM Banks
816 817 818 819 820 821 822 823
		 * won't be turned off. The default state for SRAM banks is ON.
		 * Setting SRAM bank state code skipped for STONEY platform.
		 */
		if (intr_data->asic_type != CHIP_STONEY) {
			for (bank = 1; bank <= 4; bank++)
				acp_set_sram_bank_state(intr_data->acp_mmio,
							bank, true);
		}
824
	} else {
825 826 827 828 829
		if (intr_data->asic_type != CHIP_STONEY) {
			for (bank = 5; bank <= 8; bank++)
				acp_set_sram_bank_state(intr_data->acp_mmio,
							bank, true);
		}
830
	}
831 832 833 834

	return 0;
}

835 836
static int acp_dma_hw_params(struct snd_soc_component *component,
			     struct snd_pcm_substream *substream,
837 838 839
			     struct snd_pcm_hw_params *params)
{
	uint64_t size;
840
	u32 val = 0;
841 842
	struct snd_pcm_runtime *runtime;
	struct audio_substream_data *rtd;
843
	struct snd_soc_pcm_runtime *prtd = asoc_substream_to_rtd(substream);
844
	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
845 846
	struct snd_soc_card *card = prtd->card;
	struct acp_platform_info *pinfo = snd_soc_card_get_drvdata(card);
847 848 849 850 851 852 853

	runtime = substream->runtime;
	rtd = runtime->private_data;

	if (WARN_ON(!rtd))
		return -EINVAL;

854
	if (pinfo) {
855 856 857 858 859 860
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			rtd->i2s_instance = pinfo->play_i2s_instance;
		} else {
			rtd->i2s_instance = pinfo->cap_i2s_instance;
			rtd->capture_channel = pinfo->capture_channel;
		}
861
	}
862
	if (adata->asic_type == CHIP_STONEY) {
863 864
		val = acp_reg_read(adata->acp_mmio,
				   mmACP_I2S_16BIT_RESOLUTION_EN);
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			switch (rtd->i2s_instance) {
			case I2S_BT_INSTANCE:
				val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
				break;
			case I2S_SP_INSTANCE:
			default:
				val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
			}
		} else {
			switch (rtd->i2s_instance) {
			case I2S_BT_INSTANCE:
				val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
				break;
			case I2S_SP_INSTANCE:
			default:
				val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
			}
		}
884 885
		acp_reg_write(val, adata->acp_mmio,
			      mmACP_I2S_16BIT_RESOLUTION_EN);
886
	}
887 888

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
889 890 891 892 893 894 895 896 897 898 899 900 901 902
		switch (rtd->i2s_instance) {
		case I2S_BT_INSTANCE:
			rtd->pte_offset = ACP_ST_BT_PLAYBACK_PTE_OFFSET;
			rtd->ch1 = SYSRAM_TO_ACP_BT_INSTANCE_CH_NUM;
			rtd->ch2 = ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM;
			rtd->sram_bank = ACP_SRAM_BANK_3_ADDRESS;
			rtd->destination = TO_BLUETOOTH;
			rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH8;
			rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH9;
			rtd->byte_cnt_high_reg_offset =
					mmACP_I2S_BT_TRANSMIT_BYTE_CNT_HIGH;
			rtd->byte_cnt_low_reg_offset =
					mmACP_I2S_BT_TRANSMIT_BYTE_CNT_LOW;
			adata->play_i2sbt_stream = substream;
903
			break;
904
		case I2S_SP_INSTANCE:
905
		default:
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
			switch (adata->asic_type) {
			case CHIP_STONEY:
				rtd->pte_offset = ACP_ST_PLAYBACK_PTE_OFFSET;
				break;
			default:
				rtd->pte_offset = ACP_PLAYBACK_PTE_OFFSET;
			}
			rtd->ch1 = SYSRAM_TO_ACP_CH_NUM;
			rtd->ch2 = ACP_TO_I2S_DMA_CH_NUM;
			rtd->sram_bank = ACP_SRAM_BANK_1_ADDRESS;
			rtd->destination = TO_ACP_I2S_1;
			rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH12;
			rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH13;
			rtd->byte_cnt_high_reg_offset =
					mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH;
			rtd->byte_cnt_low_reg_offset =
					mmACP_I2S_TRANSMIT_BYTE_CNT_LOW;
			adata->play_i2ssp_stream = substream;
924
		}
925
	} else {
926 927 928
		switch (rtd->i2s_instance) {
		case I2S_BT_INSTANCE:
			rtd->pte_offset = ACP_ST_BT_CAPTURE_PTE_OFFSET;
929 930
			rtd->ch1 = I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM;
			rtd->ch2 = ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM;
931 932 933 934
			rtd->sram_bank = ACP_SRAM_BANK_4_ADDRESS;
			rtd->destination = FROM_BLUETOOTH;
			rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH10;
			rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH11;
935 936 937 938
			rtd->byte_cnt_high_reg_offset =
					mmACP_I2S_BT_RECEIVE_BYTE_CNT_HIGH;
			rtd->byte_cnt_low_reg_offset =
					mmACP_I2S_BT_RECEIVE_BYTE_CNT_LOW;
939
			rtd->dma_curr_dscr = mmACP_DMA_CUR_DSCR_11;
940
			adata->capture_i2sbt_stream = substream;
941
			break;
942
		case I2S_SP_INSTANCE:
943 944
		default:
			rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
945 946
			rtd->ch1 = I2S_TO_ACP_DMA_CH_NUM;
			rtd->ch2 = ACP_TO_SYSRAM_CH_NUM;
947 948 949 950 951 952 953 954 955 956 957 958
			switch (adata->asic_type) {
			case CHIP_STONEY:
				rtd->pte_offset = ACP_ST_CAPTURE_PTE_OFFSET;
				rtd->sram_bank = ACP_SRAM_BANK_2_ADDRESS;
				break;
			default:
				rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
				rtd->sram_bank = ACP_SRAM_BANK_5_ADDRESS;
			}
			rtd->destination = FROM_ACP_I2S_1;
			rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH14;
			rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH15;
959 960 961 962
			rtd->byte_cnt_high_reg_offset =
					mmACP_I2S_RECEIVED_BYTE_CNT_HIGH;
			rtd->byte_cnt_low_reg_offset =
					mmACP_I2S_RECEIVED_BYTE_CNT_LOW;
963
			rtd->dma_curr_dscr = mmACP_DMA_CUR_DSCR_15;
964
			adata->capture_i2ssp_stream = substream;
965
		}
966 967
	}

968 969
	size = params_buffer_bytes(params);

970 971
	acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
	/* Save for runtime private data */
972
	rtd->dma_addr = runtime->dma_addr;
973
	rtd->order = get_order(size);
974

975 976 977 978
	/* Fill the page table entries in ACP SRAM */
	rtd->size = size;
	rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
	rtd->direction = substream->stream;
979

980 981
	config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
	return 0;
982 983
}

984
static u64 acp_get_byte_count(struct audio_substream_data *rtd)
985
{
986
	union acp_dma_count byte_count;
987

988 989 990 991 992
	byte_count.bcount.high = acp_reg_read(rtd->acp_mmio,
					      rtd->byte_cnt_high_reg_offset);
	byte_count.bcount.low  = acp_reg_read(rtd->acp_mmio,
					      rtd->byte_cnt_low_reg_offset);
	return byte_count.bytescount;
993 994
}

995 996
static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
					 struct snd_pcm_substream *substream)
997
{
998
	u32 buffersize;
999
	u32 pos = 0;
1000
	u64 bytescount = 0;
1001
	u16 dscr;
1002
	u32 period_bytes, delay;
1003 1004 1005 1006

	struct snd_pcm_runtime *runtime = substream->runtime;
	struct audio_substream_data *rtd = runtime->private_data;

1007 1008 1009
	if (!rtd)
		return -EINVAL;

1010 1011
	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
		period_bytes = frames_to_bytes(runtime, runtime->period_size);
1012
		bytescount = acp_get_byte_count(rtd);
1013
		if (bytescount >= rtd->bytescount)
1014
			bytescount -= rtd->bytescount;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		if (bytescount < period_bytes) {
			pos = 0;
		} else {
			dscr = acp_reg_read(rtd->acp_mmio, rtd->dma_curr_dscr);
			if (dscr == rtd->dma_dscr_idx_1)
				pos = period_bytes;
			else
				pos = 0;
		}
		if (bytescount > 0) {
			delay = do_div(bytescount, period_bytes);
			runtime->delay = bytes_to_frames(runtime, delay);
		}
1028 1029 1030 1031 1032 1033 1034
	} else {
		buffersize = frames_to_bytes(runtime, runtime->buffer_size);
		bytescount = acp_get_byte_count(rtd);
		if (bytescount > rtd->bytescount)
			bytescount -= rtd->bytescount;
		pos = do_div(bytescount, buffersize);
	}
1035 1036 1037
	return bytes_to_frames(runtime, pos);
}

1038 1039
static int acp_dma_mmap(struct snd_soc_component *component,
			struct snd_pcm_substream *substream,
1040 1041 1042 1043 1044
			struct vm_area_struct *vma)
{
	return snd_pcm_lib_default_mmap(substream, vma);
}

1045 1046
static int acp_dma_prepare(struct snd_soc_component *component,
			   struct snd_pcm_substream *substream)
1047 1048 1049
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct audio_substream_data *rtd = runtime->private_data;
1050
	u16 ch_acp_sysmem, ch_acp_i2s;
1051

1052 1053
	if (!rtd)
		return -EINVAL;
1054

1055 1056 1057 1058 1059 1060 1061
	if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
		ch_acp_sysmem = rtd->ch1;
		ch_acp_i2s = rtd->ch2;
	} else {
		ch_acp_i2s = rtd->ch1;
		ch_acp_sysmem = rtd->ch2;
	}
1062
	config_acp_dma_channel(rtd->acp_mmio,
1063
			       ch_acp_sysmem,
1064 1065 1066
			       rtd->dma_dscr_idx_1,
			       NUM_DSCRS_PER_CHANNEL, 0);
	config_acp_dma_channel(rtd->acp_mmio,
1067
			       ch_acp_i2s,
1068 1069
			       rtd->dma_dscr_idx_2,
			       NUM_DSCRS_PER_CHANNEL, 0);
1070 1071 1072
	return 0;
}

1073 1074
static int acp_dma_trigger(struct snd_soc_component *component,
			   struct snd_pcm_substream *substream, int cmd)
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
{
	int ret;

	struct snd_pcm_runtime *runtime = substream->runtime;
	struct audio_substream_data *rtd = runtime->private_data;

	if (!rtd)
		return -EINVAL;
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_RESUME:
1087
		rtd->bytescount = acp_get_byte_count(rtd);
1088
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
			if (rtd->capture_channel == CAP_CHANNEL0) {
				acp_dma_cap_channel_disable(rtd->acp_mmio,
							    CAP_CHANNEL1);
				acp_dma_cap_channel_enable(rtd->acp_mmio,
							   CAP_CHANNEL0);
			}
			if (rtd->capture_channel == CAP_CHANNEL1) {
				acp_dma_cap_channel_disable(rtd->acp_mmio,
							    CAP_CHANNEL0);
				acp_dma_cap_channel_enable(rtd->acp_mmio,
							   CAP_CHANNEL1);
			}
1101 1102 1103 1104
			acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
		} else {
			acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
			acp_dma_start(rtd->acp_mmio, rtd->ch2, true);
1105 1106 1107 1108 1109 1110
		}
		ret = 0;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_SUSPEND:
D
Daniel Kurtz 已提交
1111 1112
		acp_dma_stop(rtd->acp_mmio, rtd->ch2);
		ret = acp_dma_stop(rtd->acp_mmio, rtd->ch1);
1113 1114 1115 1116 1117 1118 1119
		break;
	default:
		ret = -EINVAL;
	}
	return ret;
}

1120 1121
static int acp_dma_new(struct snd_soc_component *component,
		       struct snd_soc_pcm_runtime *rtd)
1122
{
1123
	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
1124
	struct device *parent = component->dev->parent;
1125 1126 1127

	switch (adata->asic_type) {
	case CHIP_STONEY:
1128 1129 1130 1131 1132
		snd_pcm_set_managed_buffer_all(rtd->pcm,
					       SNDRV_DMA_TYPE_DEV,
					       parent,
					       ST_MIN_BUFFER,
					       ST_MAX_BUFFER);
1133 1134
		break;
	default:
1135 1136 1137 1138 1139
		snd_pcm_set_managed_buffer_all(rtd->pcm,
					       SNDRV_DMA_TYPE_DEV,
					       parent,
					       MIN_BUFFER,
					       MAX_BUFFER);
1140 1141
		break;
	}
1142
	return 0;
1143 1144
}

1145 1146
static int acp_dma_close(struct snd_soc_component *component,
			 struct snd_pcm_substream *substream)
1147
{
1148
	u16 bank;
1149 1150
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct audio_substream_data *rtd = runtime->private_data;
1151
	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
1152

1153
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
		switch (rtd->i2s_instance) {
		case I2S_BT_INSTANCE:
			adata->play_i2sbt_stream = NULL;
			break;
		case I2S_SP_INSTANCE:
		default:
			adata->play_i2ssp_stream = NULL;
			/*
			 * For Stoney, Memory gating is disabled,i.e SRAM Banks
			 * won't be turned off. The default state for SRAM banks
			 * is ON.Setting SRAM bank state code skipped for STONEY
			 * platform. Added condition checks for Carrizo platform
			 * only.
			 */
			if (adata->asic_type != CHIP_STONEY) {
				for (bank = 1; bank <= 4; bank++)
					acp_set_sram_bank_state(adata->acp_mmio,
								bank, false);
			}
1173 1174
		}
	} else  {
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
		switch (rtd->i2s_instance) {
		case I2S_BT_INSTANCE:
			adata->capture_i2sbt_stream = NULL;
			break;
		case I2S_SP_INSTANCE:
		default:
			adata->capture_i2ssp_stream = NULL;
			if (adata->asic_type != CHIP_STONEY) {
				for (bank = 5; bank <= 8; bank++)
					acp_set_sram_bank_state(adata->acp_mmio,
								bank, false);
			}
1187
		}
1188
	}
1189

1190 1191
	/*
	 * Disable ACP irq, when the current stream is being closed and
1192
	 * another stream is also not active.
1193
	 */
1194 1195
	if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream &&
	    !adata->play_i2sbt_stream && !adata->capture_i2sbt_stream)
1196
		acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1197
	kfree(rtd);
1198 1199 1200
	return 0;
}

1201
static const struct snd_soc_component_driver acp_asoc_platform = {
1202 1203 1204 1205 1206 1207 1208 1209 1210
	.name		= DRV_NAME,
	.open		= acp_dma_open,
	.close		= acp_dma_close,
	.hw_params	= acp_dma_hw_params,
	.trigger	= acp_dma_trigger,
	.pointer	= acp_dma_pointer,
	.mmap		= acp_dma_mmap,
	.prepare	= acp_dma_prepare,
	.pcm_construct	= acp_dma_new,
1211 1212 1213 1214 1215 1216 1217
};

static int acp_audio_probe(struct platform_device *pdev)
{
	int status;
	struct audio_drv_data *audio_drv_data;
	struct resource *res;
1218
	const u32 *pdata = pdev->dev.platform_data;
1219

1220 1221 1222 1223 1224
	if (!pdata) {
		dev_err(&pdev->dev, "Missing platform data\n");
		return -ENODEV;
	}

1225
	audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
1226 1227
				      GFP_KERNEL);
	if (!audio_drv_data)
1228 1229
		return -ENOMEM;

1230
	audio_drv_data->acp_mmio = devm_platform_ioremap_resource(pdev, 0);
1231 1232
	if (IS_ERR(audio_drv_data->acp_mmio))
		return PTR_ERR(audio_drv_data->acp_mmio);
1233

1234 1235
	/*
	 * The following members gets populated in device 'open'
1236 1237 1238 1239
	 * function. Till then interrupts are disabled in 'acp_init'
	 * and device doesn't generate any interrupts.
	 */

1240 1241
	audio_drv_data->play_i2ssp_stream = NULL;
	audio_drv_data->capture_i2ssp_stream = NULL;
1242 1243
	audio_drv_data->play_i2sbt_stream = NULL;
	audio_drv_data->capture_i2sbt_stream = NULL;
1244

1245
	audio_drv_data->asic_type =  *pdata;
1246 1247 1248 1249 1250 1251 1252 1253

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
		return -ENODEV;
	}

	status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
1254
				  0, "ACP_IRQ", &pdev->dev);
1255 1256 1257 1258 1259 1260 1261 1262
	if (status) {
		dev_err(&pdev->dev, "ACP IRQ request failed\n");
		return status;
	}

	dev_set_drvdata(&pdev->dev, audio_drv_data);

	/* Initialize the ACP */
1263 1264 1265 1266 1267
	status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
	if (status) {
		dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
		return status;
	}
1268

1269
	status = devm_snd_soc_register_component(&pdev->dev,
1270
						 &acp_asoc_platform, NULL, 0);
1271 1272 1273 1274 1275
	if (status != 0) {
		dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
		return status;
	}

1276 1277 1278 1279
	pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

1280 1281 1282 1283 1284
	return status;
}

static int acp_audio_remove(struct platform_device *pdev)
{
1285
	int status;
1286 1287
	struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);

1288 1289 1290
	status = acp_deinit(adata->acp_mmio);
	if (status)
		dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
1291
	pm_runtime_disable(&pdev->dev);
1292 1293 1294 1295

	return 0;
}

1296 1297
static int acp_pcm_resume(struct device *dev)
{
1298
	u16 bank;
1299
	int status;
1300
	struct audio_substream_data *rtd;
1301 1302
	struct audio_drv_data *adata = dev_get_drvdata(dev);

1303 1304 1305 1306 1307
	status = acp_init(adata->acp_mmio, adata->asic_type);
	if (status) {
		dev_err(dev, "ACP Init failed status:%d\n", status);
		return status;
	}
1308

1309
	if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
1310 1311
		/*
		 * For Stoney, Memory gating is disabled,i.e SRAM Banks
1312 1313 1314 1315 1316 1317
		 * won't be turned off. The default state for SRAM banks is ON.
		 * Setting SRAM bank state code skipped for STONEY platform.
		 */
		if (adata->asic_type != CHIP_STONEY) {
			for (bank = 1; bank <= 4; bank++)
				acp_set_sram_bank_state(adata->acp_mmio, bank,
1318
							true);
1319
		}
1320 1321
		rtd = adata->play_i2ssp_stream->runtime->private_data;
		config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1322
	}
1323 1324
	if (adata->capture_i2ssp_stream &&
	    adata->capture_i2ssp_stream->runtime) {
1325 1326 1327
		if (adata->asic_type != CHIP_STONEY) {
			for (bank = 5; bank <= 8; bank++)
				acp_set_sram_bank_state(adata->acp_mmio, bank,
1328
							true);
1329
		}
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
		rtd =  adata->capture_i2ssp_stream->runtime->private_data;
		config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
	}
	if (adata->asic_type != CHIP_CARRIZO) {
		if (adata->play_i2sbt_stream &&
		    adata->play_i2sbt_stream->runtime) {
			rtd = adata->play_i2sbt_stream->runtime->private_data;
			config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
		}
		if (adata->capture_i2sbt_stream &&
		    adata->capture_i2sbt_stream->runtime) {
			rtd = adata->capture_i2sbt_stream->runtime->private_data;
			config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
		}
1344
	}
1345 1346 1347 1348 1349 1350
	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
	return 0;
}

static int acp_pcm_runtime_suspend(struct device *dev)
{
1351
	int status;
1352 1353
	struct audio_drv_data *adata = dev_get_drvdata(dev);

1354 1355 1356
	status = acp_deinit(adata->acp_mmio);
	if (status)
		dev_err(dev, "ACP Deinit failed status:%d\n", status);
1357 1358 1359 1360 1361 1362
	acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
	return 0;
}

static int acp_pcm_runtime_resume(struct device *dev)
{
1363
	int status;
1364 1365
	struct audio_drv_data *adata = dev_get_drvdata(dev);

1366 1367 1368 1369 1370
	status = acp_init(adata->acp_mmio, adata->asic_type);
	if (status) {
		dev_err(dev, "ACP Init failed status:%d\n", status);
		return status;
	}
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
	return 0;
}

static const struct dev_pm_ops acp_pm_ops = {
	.resume = acp_pcm_resume,
	.runtime_suspend = acp_pcm_runtime_suspend,
	.runtime_resume = acp_pcm_runtime_resume,
};

1381 1382 1383 1384
static struct platform_driver acp_dma_driver = {
	.probe = acp_audio_probe,
	.remove = acp_audio_remove,
	.driver = {
1385
		.name = DRV_NAME,
1386
		.pm = &acp_pm_ops,
1387 1388 1389 1390 1391
	},
};

module_platform_driver(acp_dma_driver);

1392
MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
1393 1394 1395
MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
MODULE_DESCRIPTION("AMD ACP PCM Driver");
MODULE_LICENSE("GPL v2");
1396
MODULE_ALIAS("platform:"DRV_NAME);