xref: /linux/sound/soc/amd/acp-pcm-dma.c (revision c36d9b3f6de7c6aefed5fdf6ad752773bdafa60c)
17c31335aSMaruthi Srinivas Bayyavarapu /*
27c31335aSMaruthi Srinivas Bayyavarapu  * AMD ALSA SoC PCM Driver for ACP 2.x
37c31335aSMaruthi Srinivas Bayyavarapu  *
47c31335aSMaruthi Srinivas Bayyavarapu  * Copyright 2014-2015 Advanced Micro Devices, Inc.
57c31335aSMaruthi Srinivas Bayyavarapu  *
67c31335aSMaruthi Srinivas Bayyavarapu  * This program is free software; you can redistribute it and/or modify it
77c31335aSMaruthi Srinivas Bayyavarapu  * under the terms and conditions of the GNU General Public License,
87c31335aSMaruthi Srinivas Bayyavarapu  * version 2, as published by the Free Software Foundation.
97c31335aSMaruthi Srinivas Bayyavarapu  *
107c31335aSMaruthi Srinivas Bayyavarapu  * This program is distributed in the hope it will be useful, but WITHOUT
117c31335aSMaruthi Srinivas Bayyavarapu  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
127c31335aSMaruthi Srinivas Bayyavarapu  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
137c31335aSMaruthi Srinivas Bayyavarapu  * more details.
147c31335aSMaruthi Srinivas Bayyavarapu  */
157c31335aSMaruthi Srinivas Bayyavarapu 
167c31335aSMaruthi Srinivas Bayyavarapu #include <linux/module.h>
177c31335aSMaruthi Srinivas Bayyavarapu #include <linux/delay.h>
187c31335aSMaruthi Srinivas Bayyavarapu #include <linux/sizes.h>
191927da93SMaruthi Srinivas Bayyavarapu #include <linux/pm_runtime.h>
207c31335aSMaruthi Srinivas Bayyavarapu 
217c31335aSMaruthi Srinivas Bayyavarapu #include <sound/soc.h>
227c31335aSMaruthi Srinivas Bayyavarapu 
237c31335aSMaruthi Srinivas Bayyavarapu #include "acp.h"
247c31335aSMaruthi Srinivas Bayyavarapu 
257c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MIN_NUM_PERIODS    2
267c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MAX_NUM_PERIODS    2
277c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MAX_PERIOD_SIZE    16384
287c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MIN_PERIOD_SIZE    1024
297c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MIN_NUM_PERIODS     2
307c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MAX_NUM_PERIODS     2
317c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MAX_PERIOD_SIZE     16384
327c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MIN_PERIOD_SIZE     1024
337c31335aSMaruthi Srinivas Bayyavarapu 
347c31335aSMaruthi Srinivas Bayyavarapu #define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
357c31335aSMaruthi Srinivas Bayyavarapu #define MIN_BUFFER MAX_BUFFER
367c31335aSMaruthi Srinivas Bayyavarapu 
377c31335aSMaruthi Srinivas Bayyavarapu static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
387c31335aSMaruthi Srinivas Bayyavarapu 	.info = SNDRV_PCM_INFO_INTERLEAVED |
397c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
407c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
417c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
427c31335aSMaruthi Srinivas Bayyavarapu 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
437c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
447c31335aSMaruthi Srinivas Bayyavarapu 	.channels_min = 1,
457c31335aSMaruthi Srinivas Bayyavarapu 	.channels_max = 8,
467c31335aSMaruthi Srinivas Bayyavarapu 	.rates = SNDRV_PCM_RATE_8000_96000,
477c31335aSMaruthi Srinivas Bayyavarapu 	.rate_min = 8000,
487c31335aSMaruthi Srinivas Bayyavarapu 	.rate_max = 96000,
497c31335aSMaruthi Srinivas Bayyavarapu 	.buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
507c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
517c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
527c31335aSMaruthi Srinivas Bayyavarapu 	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
537c31335aSMaruthi Srinivas Bayyavarapu 	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
547c31335aSMaruthi Srinivas Bayyavarapu };
557c31335aSMaruthi Srinivas Bayyavarapu 
567c31335aSMaruthi Srinivas Bayyavarapu static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
577c31335aSMaruthi Srinivas Bayyavarapu 	.info = SNDRV_PCM_INFO_INTERLEAVED |
587c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
597c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
607c31335aSMaruthi Srinivas Bayyavarapu 	    SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
617c31335aSMaruthi Srinivas Bayyavarapu 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
627c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
637c31335aSMaruthi Srinivas Bayyavarapu 	.channels_min = 1,
647c31335aSMaruthi Srinivas Bayyavarapu 	.channels_max = 2,
657c31335aSMaruthi Srinivas Bayyavarapu 	.rates = SNDRV_PCM_RATE_8000_48000,
667c31335aSMaruthi Srinivas Bayyavarapu 	.rate_min = 8000,
677c31335aSMaruthi Srinivas Bayyavarapu 	.rate_max = 48000,
687c31335aSMaruthi Srinivas Bayyavarapu 	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
697c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
707c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
717c31335aSMaruthi Srinivas Bayyavarapu 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
727c31335aSMaruthi Srinivas Bayyavarapu 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
737c31335aSMaruthi Srinivas Bayyavarapu };
747c31335aSMaruthi Srinivas Bayyavarapu 
757c31335aSMaruthi Srinivas Bayyavarapu struct audio_drv_data {
767c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_substream *play_stream;
777c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_substream *capture_stream;
787c31335aSMaruthi Srinivas Bayyavarapu 	void __iomem *acp_mmio;
797c31335aSMaruthi Srinivas Bayyavarapu };
807c31335aSMaruthi Srinivas Bayyavarapu 
817c31335aSMaruthi Srinivas Bayyavarapu static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
827c31335aSMaruthi Srinivas Bayyavarapu {
837c31335aSMaruthi Srinivas Bayyavarapu 	return readl(acp_mmio + (reg * 4));
847c31335aSMaruthi Srinivas Bayyavarapu }
857c31335aSMaruthi Srinivas Bayyavarapu 
867c31335aSMaruthi Srinivas Bayyavarapu static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
877c31335aSMaruthi Srinivas Bayyavarapu {
887c31335aSMaruthi Srinivas Bayyavarapu 	writel(val, acp_mmio + (reg * 4));
897c31335aSMaruthi Srinivas Bayyavarapu }
907c31335aSMaruthi Srinivas Bayyavarapu 
917c31335aSMaruthi Srinivas Bayyavarapu /* Configure a given dma channel parameters - enable/disble,
927c31335aSMaruthi Srinivas Bayyavarapu  * number of descriptors, priority
937c31335aSMaruthi Srinivas Bayyavarapu  */
947c31335aSMaruthi Srinivas Bayyavarapu static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
957c31335aSMaruthi Srinivas Bayyavarapu 				   u16 dscr_strt_idx, u16 num_dscrs,
967c31335aSMaruthi Srinivas Bayyavarapu 				   enum acp_dma_priority_level priority_level)
977c31335aSMaruthi Srinivas Bayyavarapu {
987c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
997c31335aSMaruthi Srinivas Bayyavarapu 
1007c31335aSMaruthi Srinivas Bayyavarapu 	/* disable the channel run field */
1017c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
1027c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
1037c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
1047c31335aSMaruthi Srinivas Bayyavarapu 
1057c31335aSMaruthi Srinivas Bayyavarapu 	/* program a DMA channel with first descriptor to be processed. */
1067c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
1077c31335aSMaruthi Srinivas Bayyavarapu 			& dscr_strt_idx),
1087c31335aSMaruthi Srinivas Bayyavarapu 			acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);
1097c31335aSMaruthi Srinivas Bayyavarapu 
1107c31335aSMaruthi Srinivas Bayyavarapu 	/* program a DMA channel with the number of descriptors to be
1117c31335aSMaruthi Srinivas Bayyavarapu 	 * processed in the transfer
1127c31335aSMaruthi Srinivas Bayyavarapu 	*/
1137c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
1147c31335aSMaruthi Srinivas Bayyavarapu 		acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
1157c31335aSMaruthi Srinivas Bayyavarapu 
1167c31335aSMaruthi Srinivas Bayyavarapu 	/* set DMA channel priority */
1177c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
1187c31335aSMaruthi Srinivas Bayyavarapu }
1197c31335aSMaruthi Srinivas Bayyavarapu 
1207c31335aSMaruthi Srinivas Bayyavarapu /* Initialize a dma descriptor in SRAM based on descritor information passed */
1217c31335aSMaruthi Srinivas Bayyavarapu static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
1227c31335aSMaruthi Srinivas Bayyavarapu 					  u16 descr_idx,
1237c31335aSMaruthi Srinivas Bayyavarapu 					  acp_dma_dscr_transfer_t *descr_info)
1247c31335aSMaruthi Srinivas Bayyavarapu {
1257c31335aSMaruthi Srinivas Bayyavarapu 	u32 sram_offset;
1267c31335aSMaruthi Srinivas Bayyavarapu 
1277c31335aSMaruthi Srinivas Bayyavarapu 	sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));
1287c31335aSMaruthi Srinivas Bayyavarapu 
1297c31335aSMaruthi Srinivas Bayyavarapu 	/* program the source base address. */
1307c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1317c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->src,	acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1327c31335aSMaruthi Srinivas Bayyavarapu 	/* program the destination base address. */
1337c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset + 4,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1347c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1357c31335aSMaruthi Srinivas Bayyavarapu 
1367c31335aSMaruthi Srinivas Bayyavarapu 	/* program the number of bytes to be transferred for this descriptor. */
1377c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset + 8,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1387c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1397c31335aSMaruthi Srinivas Bayyavarapu }
1407c31335aSMaruthi Srinivas Bayyavarapu 
1417c31335aSMaruthi Srinivas Bayyavarapu /* Initialize the DMA descriptor information for transfer between
1427c31335aSMaruthi Srinivas Bayyavarapu  * system memory <-> ACP SRAM
1437c31335aSMaruthi Srinivas Bayyavarapu  */
1447c31335aSMaruthi Srinivas Bayyavarapu static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
1457c31335aSMaruthi Srinivas Bayyavarapu 					   u32 size, int direction,
1467c31335aSMaruthi Srinivas Bayyavarapu 					   u32 pte_offset)
1477c31335aSMaruthi Srinivas Bayyavarapu {
1487c31335aSMaruthi Srinivas Bayyavarapu 	u16 i;
1497c31335aSMaruthi Srinivas Bayyavarapu 	u16 dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH12;
1507c31335aSMaruthi Srinivas Bayyavarapu 	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
1517c31335aSMaruthi Srinivas Bayyavarapu 
1527c31335aSMaruthi Srinivas Bayyavarapu 	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
1537c31335aSMaruthi Srinivas Bayyavarapu 		dmadscr[i].xfer_val = 0;
1547c31335aSMaruthi Srinivas Bayyavarapu 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1557c31335aSMaruthi Srinivas Bayyavarapu 			dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH12 + i;
1567c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].dest = ACP_SHARED_RAM_BANK_1_ADDRESS +
1577c31335aSMaruthi Srinivas Bayyavarapu 					(size / 2) - (i * (size/2));
1587c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
1597c31335aSMaruthi Srinivas Bayyavarapu 				+ (pte_offset * SZ_4K) + (i * (size/2));
1607c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].xfer_val |=
1617c31335aSMaruthi Srinivas Bayyavarapu 			(ACP_DMA_ATTRIBUTES_DAGB_ONION_TO_SHAREDMEM << 16) |
1627c31335aSMaruthi Srinivas Bayyavarapu 			(size / 2);
1637c31335aSMaruthi Srinivas Bayyavarapu 		} else {
1647c31335aSMaruthi Srinivas Bayyavarapu 			dma_dscr_idx = CAPTURE_START_DMA_DESCR_CH14 + i;
1657c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = ACP_SHARED_RAM_BANK_5_ADDRESS +
1667c31335aSMaruthi Srinivas Bayyavarapu 					(i * (size/2));
1677c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].dest = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
1687c31335aSMaruthi Srinivas Bayyavarapu 						+ (pte_offset * SZ_4K) +
1697c31335aSMaruthi Srinivas Bayyavarapu 						(i * (size/2));
1707c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].xfer_val |=
1717c31335aSMaruthi Srinivas Bayyavarapu 			BIT(22) |
1727c31335aSMaruthi Srinivas Bayyavarapu 			(ACP_DMA_ATTRIBUTES_SHAREDMEM_TO_DAGB_ONION << 16) |
1737c31335aSMaruthi Srinivas Bayyavarapu 			(size / 2);
1747c31335aSMaruthi Srinivas Bayyavarapu 		}
1757c31335aSMaruthi Srinivas Bayyavarapu 		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
1767c31335aSMaruthi Srinivas Bayyavarapu 						&dmadscr[i]);
1777c31335aSMaruthi Srinivas Bayyavarapu 	}
1787c31335aSMaruthi Srinivas Bayyavarapu 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1797c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, SYSRAM_TO_ACP_CH_NUM,
1807c31335aSMaruthi Srinivas Bayyavarapu 					PLAYBACK_START_DMA_DESCR_CH12,
1817c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL,
1827c31335aSMaruthi Srinivas Bayyavarapu 					ACP_DMA_PRIORITY_LEVEL_NORMAL);
1837c31335aSMaruthi Srinivas Bayyavarapu 	else
1847c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM,
1857c31335aSMaruthi Srinivas Bayyavarapu 					CAPTURE_START_DMA_DESCR_CH14,
1867c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL,
1877c31335aSMaruthi Srinivas Bayyavarapu 					ACP_DMA_PRIORITY_LEVEL_NORMAL);
1887c31335aSMaruthi Srinivas Bayyavarapu }
1897c31335aSMaruthi Srinivas Bayyavarapu 
1907c31335aSMaruthi Srinivas Bayyavarapu /* Initialize the DMA descriptor information for transfer between
1917c31335aSMaruthi Srinivas Bayyavarapu  * ACP SRAM <-> I2S
1927c31335aSMaruthi Srinivas Bayyavarapu  */
1937c31335aSMaruthi Srinivas Bayyavarapu static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio,
1947c31335aSMaruthi Srinivas Bayyavarapu 					   u32 size, int direction)
1957c31335aSMaruthi Srinivas Bayyavarapu {
1967c31335aSMaruthi Srinivas Bayyavarapu 
1977c31335aSMaruthi Srinivas Bayyavarapu 	u16 i;
1987c31335aSMaruthi Srinivas Bayyavarapu 	u16 dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH13;
1997c31335aSMaruthi Srinivas Bayyavarapu 	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
2007c31335aSMaruthi Srinivas Bayyavarapu 
2017c31335aSMaruthi Srinivas Bayyavarapu 	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
2027c31335aSMaruthi Srinivas Bayyavarapu 		dmadscr[i].xfer_val = 0;
2037c31335aSMaruthi Srinivas Bayyavarapu 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
2047c31335aSMaruthi Srinivas Bayyavarapu 			dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH13 + i;
2057c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = ACP_SHARED_RAM_BANK_1_ADDRESS +
2067c31335aSMaruthi Srinivas Bayyavarapu 					 (i * (size/2));
2077c31335aSMaruthi Srinivas Bayyavarapu 			/* dmadscr[i].dest is unused by hardware. */
2087c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].dest = 0;
2097c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].xfer_val |= BIT(22) | (TO_ACP_I2S_1 << 16) |
2107c31335aSMaruthi Srinivas Bayyavarapu 						(size / 2);
2117c31335aSMaruthi Srinivas Bayyavarapu 		} else {
2127c31335aSMaruthi Srinivas Bayyavarapu 			dma_dscr_idx = CAPTURE_START_DMA_DESCR_CH15 + i;
2137c31335aSMaruthi Srinivas Bayyavarapu 			/* dmadscr[i].src is unused by hardware. */
2147c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = 0;
2157c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].dest = ACP_SHARED_RAM_BANK_5_ADDRESS +
2167c31335aSMaruthi Srinivas Bayyavarapu 					(i * (size / 2));
2177c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].xfer_val |= BIT(22) |
2187c31335aSMaruthi Srinivas Bayyavarapu 					(FROM_ACP_I2S_1 << 16) | (size / 2);
2197c31335aSMaruthi Srinivas Bayyavarapu 		}
2207c31335aSMaruthi Srinivas Bayyavarapu 		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
2217c31335aSMaruthi Srinivas Bayyavarapu 						&dmadscr[i]);
2227c31335aSMaruthi Srinivas Bayyavarapu 	}
2237c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure the DMA channel with the above descriptore */
2247c31335aSMaruthi Srinivas Bayyavarapu 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
2257c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, ACP_TO_I2S_DMA_CH_NUM,
2267c31335aSMaruthi Srinivas Bayyavarapu 					PLAYBACK_START_DMA_DESCR_CH13,
2277c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL,
2287c31335aSMaruthi Srinivas Bayyavarapu 					ACP_DMA_PRIORITY_LEVEL_NORMAL);
2297c31335aSMaruthi Srinivas Bayyavarapu 	else
2307c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, I2S_TO_ACP_DMA_CH_NUM,
2317c31335aSMaruthi Srinivas Bayyavarapu 					CAPTURE_START_DMA_DESCR_CH15,
2327c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL,
2337c31335aSMaruthi Srinivas Bayyavarapu 					ACP_DMA_PRIORITY_LEVEL_NORMAL);
2347c31335aSMaruthi Srinivas Bayyavarapu }
2357c31335aSMaruthi Srinivas Bayyavarapu 
2367c31335aSMaruthi Srinivas Bayyavarapu /* Create page table entries in ACP SRAM for the allocated memory */
2377c31335aSMaruthi Srinivas Bayyavarapu static void acp_pte_config(void __iomem *acp_mmio, struct page *pg,
2387c31335aSMaruthi Srinivas Bayyavarapu 			   u16 num_of_pages, u32 pte_offset)
2397c31335aSMaruthi Srinivas Bayyavarapu {
2407c31335aSMaruthi Srinivas Bayyavarapu 	u16 page_idx;
2417c31335aSMaruthi Srinivas Bayyavarapu 	u64 addr;
2427c31335aSMaruthi Srinivas Bayyavarapu 	u32 low;
2437c31335aSMaruthi Srinivas Bayyavarapu 	u32 high;
2447c31335aSMaruthi Srinivas Bayyavarapu 	u32 offset;
2457c31335aSMaruthi Srinivas Bayyavarapu 
2467c31335aSMaruthi Srinivas Bayyavarapu 	offset	= ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
2477c31335aSMaruthi Srinivas Bayyavarapu 	for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
2487c31335aSMaruthi Srinivas Bayyavarapu 		/* Load the low address of page int ACP SRAM through SRBM */
2497c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((offset + (page_idx * 8)),
2507c31335aSMaruthi Srinivas Bayyavarapu 			acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
2517c31335aSMaruthi Srinivas Bayyavarapu 		addr = page_to_phys(pg);
2527c31335aSMaruthi Srinivas Bayyavarapu 
2537c31335aSMaruthi Srinivas Bayyavarapu 		low = lower_32_bits(addr);
2547c31335aSMaruthi Srinivas Bayyavarapu 		high = upper_32_bits(addr);
2557c31335aSMaruthi Srinivas Bayyavarapu 
2567c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
2577c31335aSMaruthi Srinivas Bayyavarapu 
2587c31335aSMaruthi Srinivas Bayyavarapu 		/* Load the High address of page int ACP SRAM through SRBM */
2597c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((offset + (page_idx * 8) + 4),
2607c31335aSMaruthi Srinivas Bayyavarapu 			acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
2617c31335aSMaruthi Srinivas Bayyavarapu 
2627c31335aSMaruthi Srinivas Bayyavarapu 		/* page enable in ACP */
2637c31335aSMaruthi Srinivas Bayyavarapu 		high |= BIT(31);
2647c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
2657c31335aSMaruthi Srinivas Bayyavarapu 
2667c31335aSMaruthi Srinivas Bayyavarapu 		/* Move to next physically contiguos page */
2677c31335aSMaruthi Srinivas Bayyavarapu 		pg++;
2687c31335aSMaruthi Srinivas Bayyavarapu 	}
2697c31335aSMaruthi Srinivas Bayyavarapu }
2707c31335aSMaruthi Srinivas Bayyavarapu 
2717c31335aSMaruthi Srinivas Bayyavarapu static void config_acp_dma(void __iomem *acp_mmio,
2727c31335aSMaruthi Srinivas Bayyavarapu 			   struct audio_substream_data *audio_config)
2737c31335aSMaruthi Srinivas Bayyavarapu {
2747c31335aSMaruthi Srinivas Bayyavarapu 	u32 pte_offset;
2757c31335aSMaruthi Srinivas Bayyavarapu 
2767c31335aSMaruthi Srinivas Bayyavarapu 	if (audio_config->direction == SNDRV_PCM_STREAM_PLAYBACK)
2777c31335aSMaruthi Srinivas Bayyavarapu 		pte_offset = ACP_PLAYBACK_PTE_OFFSET;
2787c31335aSMaruthi Srinivas Bayyavarapu 	else
2797c31335aSMaruthi Srinivas Bayyavarapu 		pte_offset = ACP_CAPTURE_PTE_OFFSET;
2807c31335aSMaruthi Srinivas Bayyavarapu 
2817c31335aSMaruthi Srinivas Bayyavarapu 	acp_pte_config(acp_mmio, audio_config->pg, audio_config->num_of_pages,
2827c31335aSMaruthi Srinivas Bayyavarapu 			pte_offset);
2837c31335aSMaruthi Srinivas Bayyavarapu 
2847c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure System memory <-> ACP SRAM DMA descriptors */
2857c31335aSMaruthi Srinivas Bayyavarapu 	set_acp_sysmem_dma_descriptors(acp_mmio, audio_config->size,
2867c31335aSMaruthi Srinivas Bayyavarapu 				       audio_config->direction, pte_offset);
2877c31335aSMaruthi Srinivas Bayyavarapu 
2887c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure ACP SRAM <-> I2S DMA descriptors */
2897c31335aSMaruthi Srinivas Bayyavarapu 	set_acp_to_i2s_dma_descriptors(acp_mmio, audio_config->size,
2907c31335aSMaruthi Srinivas Bayyavarapu 					audio_config->direction);
2917c31335aSMaruthi Srinivas Bayyavarapu }
2927c31335aSMaruthi Srinivas Bayyavarapu 
2937c31335aSMaruthi Srinivas Bayyavarapu /* Start a given DMA channel transfer */
2947c31335aSMaruthi Srinivas Bayyavarapu static void acp_dma_start(void __iomem *acp_mmio,
2957c31335aSMaruthi Srinivas Bayyavarapu 			 u16 ch_num, bool is_circular)
2967c31335aSMaruthi Srinivas Bayyavarapu {
2977c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
2987c31335aSMaruthi Srinivas Bayyavarapu 
2997c31335aSMaruthi Srinivas Bayyavarapu 	/* read the dma control register and disable the channel run field */
3007c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3017c31335aSMaruthi Srinivas Bayyavarapu 
3027c31335aSMaruthi Srinivas Bayyavarapu 	/* Invalidating the DAGB cache */
3037c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
3047c31335aSMaruthi Srinivas Bayyavarapu 
3057c31335aSMaruthi Srinivas Bayyavarapu 	/* configure the DMA channel and start the DMA transfer
3067c31335aSMaruthi Srinivas Bayyavarapu 	 * set dmachrun bit to start the transfer and enable the
3077c31335aSMaruthi Srinivas Bayyavarapu 	 * interrupt on completion of the dma transfer
3087c31335aSMaruthi Srinivas Bayyavarapu 	 */
3097c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
3107c31335aSMaruthi Srinivas Bayyavarapu 
3117c31335aSMaruthi Srinivas Bayyavarapu 	switch (ch_num) {
3127c31335aSMaruthi Srinivas Bayyavarapu 	case ACP_TO_I2S_DMA_CH_NUM:
3137c31335aSMaruthi Srinivas Bayyavarapu 	case ACP_TO_SYSRAM_CH_NUM:
3147c31335aSMaruthi Srinivas Bayyavarapu 	case I2S_TO_ACP_DMA_CH_NUM:
3157c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
3167c31335aSMaruthi Srinivas Bayyavarapu 		break;
3177c31335aSMaruthi Srinivas Bayyavarapu 	default:
3187c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
3197c31335aSMaruthi Srinivas Bayyavarapu 		break;
3207c31335aSMaruthi Srinivas Bayyavarapu 	}
3217c31335aSMaruthi Srinivas Bayyavarapu 
3227c31335aSMaruthi Srinivas Bayyavarapu 	/* enable  for ACP SRAM to/from I2S DMA channel */
3237c31335aSMaruthi Srinivas Bayyavarapu 	if (is_circular == true)
3247c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
3257c31335aSMaruthi Srinivas Bayyavarapu 	else
3267c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
3277c31335aSMaruthi Srinivas Bayyavarapu 
3287c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3297c31335aSMaruthi Srinivas Bayyavarapu }
3307c31335aSMaruthi Srinivas Bayyavarapu 
3317c31335aSMaruthi Srinivas Bayyavarapu /* Stop a given DMA channel transfer */
3327c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
3337c31335aSMaruthi Srinivas Bayyavarapu {
3347c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
3357c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ch_sts;
3367c31335aSMaruthi Srinivas Bayyavarapu 	u32 count = ACP_DMA_RESET_TIME;
3377c31335aSMaruthi Srinivas Bayyavarapu 
3387c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3397c31335aSMaruthi Srinivas Bayyavarapu 
3407c31335aSMaruthi Srinivas Bayyavarapu 	/* clear the dma control register fields before writing zero
3417c31335aSMaruthi Srinivas Bayyavarapu 	 * in reset bit
3427c31335aSMaruthi Srinivas Bayyavarapu 	*/
3437c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
3447c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
3457c31335aSMaruthi Srinivas Bayyavarapu 
3467c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3477c31335aSMaruthi Srinivas Bayyavarapu 	dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
3487c31335aSMaruthi Srinivas Bayyavarapu 
3497c31335aSMaruthi Srinivas Bayyavarapu 	if (dma_ch_sts & BIT(ch_num)) {
3507c31335aSMaruthi Srinivas Bayyavarapu 		/* set the reset bit for this channel to stop the dma
3517c31335aSMaruthi Srinivas Bayyavarapu 		*  transfer
3527c31335aSMaruthi Srinivas Bayyavarapu 		*/
3537c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
3547c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3557c31335aSMaruthi Srinivas Bayyavarapu 	}
3567c31335aSMaruthi Srinivas Bayyavarapu 
3577c31335aSMaruthi Srinivas Bayyavarapu 	/* check the channel status bit for some time and return the status */
3587c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
3597c31335aSMaruthi Srinivas Bayyavarapu 		dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
3607c31335aSMaruthi Srinivas Bayyavarapu 		if (!(dma_ch_sts & BIT(ch_num))) {
3617c31335aSMaruthi Srinivas Bayyavarapu 			/* clear the reset flag after successfully stopping
3627c31335aSMaruthi Srinivas Bayyavarapu 			* the dma transfer and break from the loop
3637c31335aSMaruthi Srinivas Bayyavarapu 			*/
3647c31335aSMaruthi Srinivas Bayyavarapu 			dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
3657c31335aSMaruthi Srinivas Bayyavarapu 
3667c31335aSMaruthi Srinivas Bayyavarapu 			acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
3677c31335aSMaruthi Srinivas Bayyavarapu 								+ ch_num);
3687c31335aSMaruthi Srinivas Bayyavarapu 			break;
3697c31335aSMaruthi Srinivas Bayyavarapu 		}
3707c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
3717c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
3727c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
3737c31335aSMaruthi Srinivas Bayyavarapu 		}
3747c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
3757c31335aSMaruthi Srinivas Bayyavarapu 	}
3767c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
3777c31335aSMaruthi Srinivas Bayyavarapu }
3787c31335aSMaruthi Srinivas Bayyavarapu 
379*c36d9b3fSMaruthi Srinivas Bayyavarapu static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
380*c36d9b3fSMaruthi Srinivas Bayyavarapu 					bool power_on)
381*c36d9b3fSMaruthi Srinivas Bayyavarapu {
382*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u32 val, req_reg, sts_reg, sts_reg_mask;
383*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u32 loops = 1000;
384*c36d9b3fSMaruthi Srinivas Bayyavarapu 
385*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (bank < 32) {
386*c36d9b3fSMaruthi Srinivas Bayyavarapu 		req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
387*c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
388*c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg_mask = 0xFFFFFFFF;
389*c36d9b3fSMaruthi Srinivas Bayyavarapu 
390*c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
391*c36d9b3fSMaruthi Srinivas Bayyavarapu 		bank -= 32;
392*c36d9b3fSMaruthi Srinivas Bayyavarapu 		req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
393*c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
394*c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg_mask = 0x0000FFFF;
395*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
396*c36d9b3fSMaruthi Srinivas Bayyavarapu 
397*c36d9b3fSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, req_reg);
398*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (val & (1 << bank)) {
399*c36d9b3fSMaruthi Srinivas Bayyavarapu 		/* bank is in off state */
400*c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (power_on == true)
401*c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to on */
402*c36d9b3fSMaruthi Srinivas Bayyavarapu 			val &= ~(1 << bank);
403*c36d9b3fSMaruthi Srinivas Bayyavarapu 		else
404*c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to off */
405*c36d9b3fSMaruthi Srinivas Bayyavarapu 			return;
406*c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
407*c36d9b3fSMaruthi Srinivas Bayyavarapu 		/* bank is in on state */
408*c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (power_on == false)
409*c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to off */
410*c36d9b3fSMaruthi Srinivas Bayyavarapu 			val |= 1 << bank;
411*c36d9b3fSMaruthi Srinivas Bayyavarapu 		else
412*c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to on */
413*c36d9b3fSMaruthi Srinivas Bayyavarapu 			return;
414*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
415*c36d9b3fSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, req_reg);
416*c36d9b3fSMaruthi Srinivas Bayyavarapu 
417*c36d9b3fSMaruthi Srinivas Bayyavarapu 	while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
418*c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (!loops--) {
419*c36d9b3fSMaruthi Srinivas Bayyavarapu 			pr_err("ACP SRAM bank %d state change failed\n", bank);
420*c36d9b3fSMaruthi Srinivas Bayyavarapu 			break;
421*c36d9b3fSMaruthi Srinivas Bayyavarapu 		}
422*c36d9b3fSMaruthi Srinivas Bayyavarapu 		cpu_relax();
423*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
424*c36d9b3fSMaruthi Srinivas Bayyavarapu }
425*c36d9b3fSMaruthi Srinivas Bayyavarapu 
4267c31335aSMaruthi Srinivas Bayyavarapu /* Initialize and bring ACP hardware to default state. */
4277c31335aSMaruthi Srinivas Bayyavarapu static int acp_init(void __iomem *acp_mmio)
4287c31335aSMaruthi Srinivas Bayyavarapu {
429*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
4307c31335aSMaruthi Srinivas Bayyavarapu 	u32 val, count, sram_pte_offset;
4317c31335aSMaruthi Srinivas Bayyavarapu 
4327c31335aSMaruthi Srinivas Bayyavarapu 	/* Assert Soft reset of ACP */
4337c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
4347c31335aSMaruthi Srinivas Bayyavarapu 
4357c31335aSMaruthi Srinivas Bayyavarapu 	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
4367c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
4377c31335aSMaruthi Srinivas Bayyavarapu 
4387c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
4397c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
4407c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
4417c31335aSMaruthi Srinivas Bayyavarapu 		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
4427c31335aSMaruthi Srinivas Bayyavarapu 		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
4437c31335aSMaruthi Srinivas Bayyavarapu 			break;
4447c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
4457c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
4467c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
4477c31335aSMaruthi Srinivas Bayyavarapu 		}
4487c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
4497c31335aSMaruthi Srinivas Bayyavarapu 	}
4507c31335aSMaruthi Srinivas Bayyavarapu 
4517c31335aSMaruthi Srinivas Bayyavarapu 	/* Enable clock to ACP and wait until the clock is enabled */
4527c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
4537c31335aSMaruthi Srinivas Bayyavarapu 	val = val | ACP_CONTROL__ClkEn_MASK;
4547c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_CONTROL);
4557c31335aSMaruthi Srinivas Bayyavarapu 
4567c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_CLOCK_EN_TIME_OUT_VALUE;
4577c31335aSMaruthi Srinivas Bayyavarapu 
4587c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
4597c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_STATUS);
4607c31335aSMaruthi Srinivas Bayyavarapu 		if (val & (u32) 0x1)
4617c31335aSMaruthi Srinivas Bayyavarapu 			break;
4627c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
4637c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
4647c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
4657c31335aSMaruthi Srinivas Bayyavarapu 		}
4667c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
4677c31335aSMaruthi Srinivas Bayyavarapu 	}
4687c31335aSMaruthi Srinivas Bayyavarapu 
4697c31335aSMaruthi Srinivas Bayyavarapu 	/* Deassert the SOFT RESET flags */
4707c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
4717c31335aSMaruthi Srinivas Bayyavarapu 	val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
4727c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
4737c31335aSMaruthi Srinivas Bayyavarapu 
4747c31335aSMaruthi Srinivas Bayyavarapu 	/* initiailize Onion control DAGB register */
4757c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
4767c31335aSMaruthi Srinivas Bayyavarapu 			mmACP_AXI2DAGB_ONION_CNTL);
4777c31335aSMaruthi Srinivas Bayyavarapu 
4787c31335aSMaruthi Srinivas Bayyavarapu 	/* initiailize Garlic control DAGB registers */
4797c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
4807c31335aSMaruthi Srinivas Bayyavarapu 			mmACP_AXI2DAGB_GARLIC_CNTL);
4817c31335aSMaruthi Srinivas Bayyavarapu 
4827c31335aSMaruthi Srinivas Bayyavarapu 	sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
4837c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
4847c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
4857c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
4867c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_pte_offset,  acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
4877c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
4887c31335aSMaruthi Srinivas Bayyavarapu 			mmACP_DAGB_PAGE_SIZE_GRP_1);
4897c31335aSMaruthi Srinivas Bayyavarapu 
4907c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
4917c31335aSMaruthi Srinivas Bayyavarapu 			mmACP_DMA_DESC_BASE_ADDR);
4927c31335aSMaruthi Srinivas Bayyavarapu 
4937c31335aSMaruthi Srinivas Bayyavarapu 	/* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
4947c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
4957c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
4967c31335aSMaruthi Srinivas Bayyavarapu 		acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
4977c31335aSMaruthi Srinivas Bayyavarapu 
498*c36d9b3fSMaruthi Srinivas Bayyavarapu        /* When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
499*c36d9b3fSMaruthi Srinivas Bayyavarapu 	* Now, turn off all of them. This can't be done in 'poweron' of
500*c36d9b3fSMaruthi Srinivas Bayyavarapu 	* ACP pm domain, as this requires ACP to be initialized.
501*c36d9b3fSMaruthi Srinivas Bayyavarapu 	*/
502*c36d9b3fSMaruthi Srinivas Bayyavarapu 	for (bank = 1; bank < 48; bank++)
503*c36d9b3fSMaruthi Srinivas Bayyavarapu 		acp_set_sram_bank_state(acp_mmio, bank, false);
504*c36d9b3fSMaruthi Srinivas Bayyavarapu 
5057c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
5067c31335aSMaruthi Srinivas Bayyavarapu }
5077c31335aSMaruthi Srinivas Bayyavarapu 
5087c31335aSMaruthi Srinivas Bayyavarapu /* Deintialize ACP */
5097c31335aSMaruthi Srinivas Bayyavarapu static int acp_deinit(void __iomem *acp_mmio)
5107c31335aSMaruthi Srinivas Bayyavarapu {
5117c31335aSMaruthi Srinivas Bayyavarapu 	u32 val;
5127c31335aSMaruthi Srinivas Bayyavarapu 	u32 count;
5137c31335aSMaruthi Srinivas Bayyavarapu 
5147c31335aSMaruthi Srinivas Bayyavarapu 	/* Assert Soft reset of ACP */
5157c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
5167c31335aSMaruthi Srinivas Bayyavarapu 
5177c31335aSMaruthi Srinivas Bayyavarapu 	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
5187c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
5197c31335aSMaruthi Srinivas Bayyavarapu 
5207c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
5217c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
5227c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
5237c31335aSMaruthi Srinivas Bayyavarapu 		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
5247c31335aSMaruthi Srinivas Bayyavarapu 		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
5257c31335aSMaruthi Srinivas Bayyavarapu 			break;
5267c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
5277c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
5287c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
5297c31335aSMaruthi Srinivas Bayyavarapu 		}
5307c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
5317c31335aSMaruthi Srinivas Bayyavarapu 	}
5327c31335aSMaruthi Srinivas Bayyavarapu 	/** Disable ACP clock */
5337c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
5347c31335aSMaruthi Srinivas Bayyavarapu 	val &= ~ACP_CONTROL__ClkEn_MASK;
5357c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_CONTROL);
5367c31335aSMaruthi Srinivas Bayyavarapu 
5377c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_CLOCK_EN_TIME_OUT_VALUE;
5387c31335aSMaruthi Srinivas Bayyavarapu 
5397c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
5407c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_STATUS);
5417c31335aSMaruthi Srinivas Bayyavarapu 		if (!(val & (u32) 0x1))
5427c31335aSMaruthi Srinivas Bayyavarapu 			break;
5437c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
5447c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
5457c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
5467c31335aSMaruthi Srinivas Bayyavarapu 		}
5477c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
5487c31335aSMaruthi Srinivas Bayyavarapu 	}
5497c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
5507c31335aSMaruthi Srinivas Bayyavarapu }
5517c31335aSMaruthi Srinivas Bayyavarapu 
5527c31335aSMaruthi Srinivas Bayyavarapu /* ACP DMA irq handler routine for playback, capture usecases */
5537c31335aSMaruthi Srinivas Bayyavarapu static irqreturn_t dma_irq_handler(int irq, void *arg)
5547c31335aSMaruthi Srinivas Bayyavarapu {
5557c31335aSMaruthi Srinivas Bayyavarapu 	u16 dscr_idx;
5567c31335aSMaruthi Srinivas Bayyavarapu 	u32 intr_flag, ext_intr_status;
5577c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *irq_data;
5587c31335aSMaruthi Srinivas Bayyavarapu 	void __iomem *acp_mmio;
5597c31335aSMaruthi Srinivas Bayyavarapu 	struct device *dev = arg;
5607c31335aSMaruthi Srinivas Bayyavarapu 	bool valid_irq = false;
5617c31335aSMaruthi Srinivas Bayyavarapu 
5627c31335aSMaruthi Srinivas Bayyavarapu 	irq_data = dev_get_drvdata(dev);
5637c31335aSMaruthi Srinivas Bayyavarapu 	acp_mmio = irq_data->acp_mmio;
5647c31335aSMaruthi Srinivas Bayyavarapu 
5657c31335aSMaruthi Srinivas Bayyavarapu 	ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
5667c31335aSMaruthi Srinivas Bayyavarapu 	intr_flag = (((ext_intr_status &
5677c31335aSMaruthi Srinivas Bayyavarapu 		      ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
5687c31335aSMaruthi Srinivas Bayyavarapu 		     ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
5697c31335aSMaruthi Srinivas Bayyavarapu 
5707c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
5717c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
5727c31335aSMaruthi Srinivas Bayyavarapu 		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_13) ==
5737c31335aSMaruthi Srinivas Bayyavarapu 				PLAYBACK_START_DMA_DESCR_CH13)
5747c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = PLAYBACK_START_DMA_DESCR_CH12;
5757c31335aSMaruthi Srinivas Bayyavarapu 		else
5767c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = PLAYBACK_END_DMA_DESCR_CH12;
5777c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, SYSRAM_TO_ACP_CH_NUM, dscr_idx,
5787c31335aSMaruthi Srinivas Bayyavarapu 				       1, 0);
5797c31335aSMaruthi Srinivas Bayyavarapu 		acp_dma_start(acp_mmio, SYSRAM_TO_ACP_CH_NUM, false);
5807c31335aSMaruthi Srinivas Bayyavarapu 
5817c31335aSMaruthi Srinivas Bayyavarapu 		snd_pcm_period_elapsed(irq_data->play_stream);
5827c31335aSMaruthi Srinivas Bayyavarapu 
5837c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
5847c31335aSMaruthi Srinivas Bayyavarapu 				acp_mmio, mmACP_EXTERNAL_INTR_STAT);
5857c31335aSMaruthi Srinivas Bayyavarapu 	}
5867c31335aSMaruthi Srinivas Bayyavarapu 
5877c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
5887c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
5897c31335aSMaruthi Srinivas Bayyavarapu 		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_15) ==
5907c31335aSMaruthi Srinivas Bayyavarapu 				CAPTURE_START_DMA_DESCR_CH15)
5917c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
5927c31335aSMaruthi Srinivas Bayyavarapu 		else
5937c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
5947c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
5957c31335aSMaruthi Srinivas Bayyavarapu 				       1, 0);
5967c31335aSMaruthi Srinivas Bayyavarapu 		acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);
5977c31335aSMaruthi Srinivas Bayyavarapu 
5987c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
5997c31335aSMaruthi Srinivas Bayyavarapu 				acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6007c31335aSMaruthi Srinivas Bayyavarapu 	}
6017c31335aSMaruthi Srinivas Bayyavarapu 
6027c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) != 0) {
6037c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
6047c31335aSMaruthi Srinivas Bayyavarapu 		snd_pcm_period_elapsed(irq_data->capture_stream);
6057c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) << 16,
6067c31335aSMaruthi Srinivas Bayyavarapu 				acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6077c31335aSMaruthi Srinivas Bayyavarapu 	}
6087c31335aSMaruthi Srinivas Bayyavarapu 
6097c31335aSMaruthi Srinivas Bayyavarapu 	if (valid_irq)
6107c31335aSMaruthi Srinivas Bayyavarapu 		return IRQ_HANDLED;
6117c31335aSMaruthi Srinivas Bayyavarapu 	else
6127c31335aSMaruthi Srinivas Bayyavarapu 		return IRQ_NONE;
6137c31335aSMaruthi Srinivas Bayyavarapu }
6147c31335aSMaruthi Srinivas Bayyavarapu 
6157c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_open(struct snd_pcm_substream *substream)
6167c31335aSMaruthi Srinivas Bayyavarapu {
617*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
6187c31335aSMaruthi Srinivas Bayyavarapu 	int ret = 0;
6197c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
6207c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
6217c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *intr_data = dev_get_drvdata(prtd->platform->dev);
6227c31335aSMaruthi Srinivas Bayyavarapu 
6237c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *adata =
6247c31335aSMaruthi Srinivas Bayyavarapu 		kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
6257c31335aSMaruthi Srinivas Bayyavarapu 	if (adata == NULL)
6267c31335aSMaruthi Srinivas Bayyavarapu 		return -ENOMEM;
6277c31335aSMaruthi Srinivas Bayyavarapu 
6287c31335aSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6297c31335aSMaruthi Srinivas Bayyavarapu 		runtime->hw = acp_pcm_hardware_playback;
6307c31335aSMaruthi Srinivas Bayyavarapu 	else
6317c31335aSMaruthi Srinivas Bayyavarapu 		runtime->hw = acp_pcm_hardware_capture;
6327c31335aSMaruthi Srinivas Bayyavarapu 
6337c31335aSMaruthi Srinivas Bayyavarapu 	ret = snd_pcm_hw_constraint_integer(runtime,
6347c31335aSMaruthi Srinivas Bayyavarapu 					    SNDRV_PCM_HW_PARAM_PERIODS);
6357c31335aSMaruthi Srinivas Bayyavarapu 	if (ret < 0) {
6367c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(prtd->platform->dev, "set integer constraint failed\n");
6377c31335aSMaruthi Srinivas Bayyavarapu 		return ret;
6387c31335aSMaruthi Srinivas Bayyavarapu 	}
6397c31335aSMaruthi Srinivas Bayyavarapu 
6407c31335aSMaruthi Srinivas Bayyavarapu 	adata->acp_mmio = intr_data->acp_mmio;
6417c31335aSMaruthi Srinivas Bayyavarapu 	runtime->private_data = adata;
6427c31335aSMaruthi Srinivas Bayyavarapu 
6437c31335aSMaruthi Srinivas Bayyavarapu 	/* Enable ACP irq, when neither playback or capture streams are
6447c31335aSMaruthi Srinivas Bayyavarapu 	 * active by the time when a new stream is being opened.
6457c31335aSMaruthi Srinivas Bayyavarapu 	 * This enablement is not required for another stream, if current
6467c31335aSMaruthi Srinivas Bayyavarapu 	 * stream is not closed
6477c31335aSMaruthi Srinivas Bayyavarapu 	*/
6487c31335aSMaruthi Srinivas Bayyavarapu 	if (!intr_data->play_stream && !intr_data->capture_stream)
6497c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
6507c31335aSMaruthi Srinivas Bayyavarapu 
651*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
6527c31335aSMaruthi Srinivas Bayyavarapu 		intr_data->play_stream = substream;
653*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 1; bank <= 4; bank++)
654*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(intr_data->acp_mmio, bank,
655*c36d9b3fSMaruthi Srinivas Bayyavarapu 						true);
656*c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
6577c31335aSMaruthi Srinivas Bayyavarapu 		intr_data->capture_stream = substream;
658*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 5; bank <= 8; bank++)
659*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(intr_data->acp_mmio, bank,
660*c36d9b3fSMaruthi Srinivas Bayyavarapu 						true);
661*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
6627c31335aSMaruthi Srinivas Bayyavarapu 
6637c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
6647c31335aSMaruthi Srinivas Bayyavarapu }
6657c31335aSMaruthi Srinivas Bayyavarapu 
6667c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_hw_params(struct snd_pcm_substream *substream,
6677c31335aSMaruthi Srinivas Bayyavarapu 			     struct snd_pcm_hw_params *params)
6687c31335aSMaruthi Srinivas Bayyavarapu {
6697c31335aSMaruthi Srinivas Bayyavarapu 	int status;
6707c31335aSMaruthi Srinivas Bayyavarapu 	uint64_t size;
6717c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_dma_buffer *dma_buffer;
6727c31335aSMaruthi Srinivas Bayyavarapu 	struct page *pg;
6737c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime;
6747c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd;
6757c31335aSMaruthi Srinivas Bayyavarapu 
6767c31335aSMaruthi Srinivas Bayyavarapu 	dma_buffer = &substream->dma_buffer;
6777c31335aSMaruthi Srinivas Bayyavarapu 
6787c31335aSMaruthi Srinivas Bayyavarapu 	runtime = substream->runtime;
6797c31335aSMaruthi Srinivas Bayyavarapu 	rtd = runtime->private_data;
6807c31335aSMaruthi Srinivas Bayyavarapu 
6817c31335aSMaruthi Srinivas Bayyavarapu 	if (WARN_ON(!rtd))
6827c31335aSMaruthi Srinivas Bayyavarapu 		return -EINVAL;
6837c31335aSMaruthi Srinivas Bayyavarapu 
6847c31335aSMaruthi Srinivas Bayyavarapu 	size = params_buffer_bytes(params);
6857c31335aSMaruthi Srinivas Bayyavarapu 	status = snd_pcm_lib_malloc_pages(substream, size);
6867c31335aSMaruthi Srinivas Bayyavarapu 	if (status < 0)
6877c31335aSMaruthi Srinivas Bayyavarapu 		return status;
6887c31335aSMaruthi Srinivas Bayyavarapu 
6897c31335aSMaruthi Srinivas Bayyavarapu 	memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
6907c31335aSMaruthi Srinivas Bayyavarapu 	pg = virt_to_page(substream->dma_buffer.area);
6917c31335aSMaruthi Srinivas Bayyavarapu 
6927c31335aSMaruthi Srinivas Bayyavarapu 	if (pg != NULL) {
693*c36d9b3fSMaruthi Srinivas Bayyavarapu 		acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
6947c31335aSMaruthi Srinivas Bayyavarapu 		/* Save for runtime private data */
6957c31335aSMaruthi Srinivas Bayyavarapu 		rtd->pg = pg;
6967c31335aSMaruthi Srinivas Bayyavarapu 		rtd->order = get_order(size);
6977c31335aSMaruthi Srinivas Bayyavarapu 
6987c31335aSMaruthi Srinivas Bayyavarapu 		/* Fill the page table entries in ACP SRAM */
6997c31335aSMaruthi Srinivas Bayyavarapu 		rtd->pg = pg;
7007c31335aSMaruthi Srinivas Bayyavarapu 		rtd->size = size;
7017c31335aSMaruthi Srinivas Bayyavarapu 		rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
7027c31335aSMaruthi Srinivas Bayyavarapu 		rtd->direction = substream->stream;
7037c31335aSMaruthi Srinivas Bayyavarapu 
7047c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma(rtd->acp_mmio, rtd);
7057c31335aSMaruthi Srinivas Bayyavarapu 		status = 0;
7067c31335aSMaruthi Srinivas Bayyavarapu 	} else {
7077c31335aSMaruthi Srinivas Bayyavarapu 		status = -ENOMEM;
7087c31335aSMaruthi Srinivas Bayyavarapu 	}
7097c31335aSMaruthi Srinivas Bayyavarapu 	return status;
7107c31335aSMaruthi Srinivas Bayyavarapu }
7117c31335aSMaruthi Srinivas Bayyavarapu 
7127c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_hw_free(struct snd_pcm_substream *substream)
7137c31335aSMaruthi Srinivas Bayyavarapu {
7147c31335aSMaruthi Srinivas Bayyavarapu 	return snd_pcm_lib_free_pages(substream);
7157c31335aSMaruthi Srinivas Bayyavarapu }
7167c31335aSMaruthi Srinivas Bayyavarapu 
7177c31335aSMaruthi Srinivas Bayyavarapu static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
7187c31335aSMaruthi Srinivas Bayyavarapu {
7197c31335aSMaruthi Srinivas Bayyavarapu 	u16 dscr;
7207c31335aSMaruthi Srinivas Bayyavarapu 	u32 mul, dma_config, period_bytes;
7217c31335aSMaruthi Srinivas Bayyavarapu 	u32 pos = 0;
7227c31335aSMaruthi Srinivas Bayyavarapu 
7237c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
7247c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
7257c31335aSMaruthi Srinivas Bayyavarapu 
7267c31335aSMaruthi Srinivas Bayyavarapu 	period_bytes = frames_to_bytes(runtime, runtime->period_size);
7277c31335aSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
7287c31335aSMaruthi Srinivas Bayyavarapu 		dscr = acp_reg_read(rtd->acp_mmio, mmACP_DMA_CUR_DSCR_13);
7297c31335aSMaruthi Srinivas Bayyavarapu 
7307c31335aSMaruthi Srinivas Bayyavarapu 		if (dscr == PLAYBACK_START_DMA_DESCR_CH13)
7317c31335aSMaruthi Srinivas Bayyavarapu 			mul = 0;
7327c31335aSMaruthi Srinivas Bayyavarapu 		else
7337c31335aSMaruthi Srinivas Bayyavarapu 			mul = 1;
7347c31335aSMaruthi Srinivas Bayyavarapu 		pos =  (mul * period_bytes);
7357c31335aSMaruthi Srinivas Bayyavarapu 	} else {
7367c31335aSMaruthi Srinivas Bayyavarapu 		dma_config = acp_reg_read(rtd->acp_mmio, mmACP_DMA_CNTL_14);
7377c31335aSMaruthi Srinivas Bayyavarapu 		if (dma_config != 0) {
7387c31335aSMaruthi Srinivas Bayyavarapu 			dscr = acp_reg_read(rtd->acp_mmio,
7397c31335aSMaruthi Srinivas Bayyavarapu 						mmACP_DMA_CUR_DSCR_14);
7407c31335aSMaruthi Srinivas Bayyavarapu 			if (dscr == CAPTURE_START_DMA_DESCR_CH14)
7417c31335aSMaruthi Srinivas Bayyavarapu 				mul = 1;
7427c31335aSMaruthi Srinivas Bayyavarapu 			else
7437c31335aSMaruthi Srinivas Bayyavarapu 				mul = 2;
7447c31335aSMaruthi Srinivas Bayyavarapu 			pos = (mul * period_bytes);
7457c31335aSMaruthi Srinivas Bayyavarapu 		}
7467c31335aSMaruthi Srinivas Bayyavarapu 
7477c31335aSMaruthi Srinivas Bayyavarapu 		if (pos >= (2 * period_bytes))
7487c31335aSMaruthi Srinivas Bayyavarapu 			pos = 0;
7497c31335aSMaruthi Srinivas Bayyavarapu 
7507c31335aSMaruthi Srinivas Bayyavarapu 	}
7517c31335aSMaruthi Srinivas Bayyavarapu 	return bytes_to_frames(runtime, pos);
7527c31335aSMaruthi Srinivas Bayyavarapu }
7537c31335aSMaruthi Srinivas Bayyavarapu 
7547c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_mmap(struct snd_pcm_substream *substream,
7557c31335aSMaruthi Srinivas Bayyavarapu 			struct vm_area_struct *vma)
7567c31335aSMaruthi Srinivas Bayyavarapu {
7577c31335aSMaruthi Srinivas Bayyavarapu 	return snd_pcm_lib_default_mmap(substream, vma);
7587c31335aSMaruthi Srinivas Bayyavarapu }
7597c31335aSMaruthi Srinivas Bayyavarapu 
7607c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_prepare(struct snd_pcm_substream *substream)
7617c31335aSMaruthi Srinivas Bayyavarapu {
7627c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
7637c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
7647c31335aSMaruthi Srinivas Bayyavarapu 
7657c31335aSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
7667c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, SYSRAM_TO_ACP_CH_NUM,
7677c31335aSMaruthi Srinivas Bayyavarapu 					PLAYBACK_START_DMA_DESCR_CH12,
7687c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL, 0);
7697c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, ACP_TO_I2S_DMA_CH_NUM,
7707c31335aSMaruthi Srinivas Bayyavarapu 					PLAYBACK_START_DMA_DESCR_CH13,
7717c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL, 0);
7727c31335aSMaruthi Srinivas Bayyavarapu 		/* Fill ACP SRAM (2 periods) with zeros from System RAM
7737c31335aSMaruthi Srinivas Bayyavarapu 		 * which is zero-ed in hw_params
7747c31335aSMaruthi Srinivas Bayyavarapu 		*/
7757c31335aSMaruthi Srinivas Bayyavarapu 		acp_dma_start(rtd->acp_mmio, SYSRAM_TO_ACP_CH_NUM, false);
7767c31335aSMaruthi Srinivas Bayyavarapu 
7777c31335aSMaruthi Srinivas Bayyavarapu 		/* ACP SRAM (2 periods of buffer size) is intially filled with
7787c31335aSMaruthi Srinivas Bayyavarapu 		 * zeros. Before rendering starts, 2nd half of SRAM will be
7797c31335aSMaruthi Srinivas Bayyavarapu 		 * filled with valid audio data DMA'ed from first half of system
7807c31335aSMaruthi Srinivas Bayyavarapu 		 * RAM and 1st half of SRAM will be filled with Zeros. This is
7817c31335aSMaruthi Srinivas Bayyavarapu 		 * the initial scenario when redering starts from SRAM. Later
7827c31335aSMaruthi Srinivas Bayyavarapu 		 * on, 2nd half of system memory will be DMA'ed to 1st half of
7837c31335aSMaruthi Srinivas Bayyavarapu 		 * SRAM, 1st half of system memory will be DMA'ed to 2nd half of
7847c31335aSMaruthi Srinivas Bayyavarapu 		 * SRAM in ping-pong way till rendering stops.
7857c31335aSMaruthi Srinivas Bayyavarapu 		*/
7867c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, SYSRAM_TO_ACP_CH_NUM,
7877c31335aSMaruthi Srinivas Bayyavarapu 					PLAYBACK_START_DMA_DESCR_CH12,
7887c31335aSMaruthi Srinivas Bayyavarapu 					1, 0);
7897c31335aSMaruthi Srinivas Bayyavarapu 	} else {
7907c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, ACP_TO_SYSRAM_CH_NUM,
7917c31335aSMaruthi Srinivas Bayyavarapu 					CAPTURE_START_DMA_DESCR_CH14,
7927c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL, 0);
7937c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, I2S_TO_ACP_DMA_CH_NUM,
7947c31335aSMaruthi Srinivas Bayyavarapu 					CAPTURE_START_DMA_DESCR_CH15,
7957c31335aSMaruthi Srinivas Bayyavarapu 					NUM_DSCRS_PER_CHANNEL, 0);
7967c31335aSMaruthi Srinivas Bayyavarapu 	}
7977c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
7987c31335aSMaruthi Srinivas Bayyavarapu }
7997c31335aSMaruthi Srinivas Bayyavarapu 
8007c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
8017c31335aSMaruthi Srinivas Bayyavarapu {
8027c31335aSMaruthi Srinivas Bayyavarapu 	int ret;
8037c31335aSMaruthi Srinivas Bayyavarapu 	u32 loops = 1000;
8047c31335aSMaruthi Srinivas Bayyavarapu 
8057c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
8067c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
8077c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
8087c31335aSMaruthi Srinivas Bayyavarapu 
8097c31335aSMaruthi Srinivas Bayyavarapu 	if (!rtd)
8107c31335aSMaruthi Srinivas Bayyavarapu 		return -EINVAL;
8117c31335aSMaruthi Srinivas Bayyavarapu 	switch (cmd) {
8127c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_START:
8137c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
8147c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_RESUME:
8157c31335aSMaruthi Srinivas Bayyavarapu 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
8167c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
8177c31335aSMaruthi Srinivas Bayyavarapu 						SYSRAM_TO_ACP_CH_NUM, false);
8187c31335aSMaruthi Srinivas Bayyavarapu 			while (acp_reg_read(rtd->acp_mmio, mmACP_DMA_CH_STS) &
8197c31335aSMaruthi Srinivas Bayyavarapu 						BIT(SYSRAM_TO_ACP_CH_NUM)) {
8207c31335aSMaruthi Srinivas Bayyavarapu 				if (!loops--) {
8217c31335aSMaruthi Srinivas Bayyavarapu 					dev_err(prtd->platform->dev,
8227c31335aSMaruthi Srinivas Bayyavarapu 						"acp dma start timeout\n");
8237c31335aSMaruthi Srinivas Bayyavarapu 					return -ETIMEDOUT;
8247c31335aSMaruthi Srinivas Bayyavarapu 				}
8257c31335aSMaruthi Srinivas Bayyavarapu 				cpu_relax();
8267c31335aSMaruthi Srinivas Bayyavarapu 			}
8277c31335aSMaruthi Srinivas Bayyavarapu 
8287c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
8297c31335aSMaruthi Srinivas Bayyavarapu 					ACP_TO_I2S_DMA_CH_NUM, true);
8307c31335aSMaruthi Srinivas Bayyavarapu 
8317c31335aSMaruthi Srinivas Bayyavarapu 		} else {
8327c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
8337c31335aSMaruthi Srinivas Bayyavarapu 					    I2S_TO_ACP_DMA_CH_NUM, true);
8347c31335aSMaruthi Srinivas Bayyavarapu 		}
8357c31335aSMaruthi Srinivas Bayyavarapu 		ret = 0;
8367c31335aSMaruthi Srinivas Bayyavarapu 		break;
8377c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_STOP:
8387c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
8397c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_SUSPEND:
8407c31335aSMaruthi Srinivas Bayyavarapu 		/* Need to stop only circular DMA channels :
8417c31335aSMaruthi Srinivas Bayyavarapu 		 * ACP_TO_I2S_DMA_CH_NUM / I2S_TO_ACP_DMA_CH_NUM. Non-circular
8427c31335aSMaruthi Srinivas Bayyavarapu 		 * channels will stopped automatically after its transfer
8437c31335aSMaruthi Srinivas Bayyavarapu 		 * completes : SYSRAM_TO_ACP_CH_NUM / ACP_TO_SYSRAM_CH_NUM
8447c31335aSMaruthi Srinivas Bayyavarapu 		 */
8457c31335aSMaruthi Srinivas Bayyavarapu 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
8467c31335aSMaruthi Srinivas Bayyavarapu 			ret = acp_dma_stop(rtd->acp_mmio,
8477c31335aSMaruthi Srinivas Bayyavarapu 					ACP_TO_I2S_DMA_CH_NUM);
8487c31335aSMaruthi Srinivas Bayyavarapu 		else
8497c31335aSMaruthi Srinivas Bayyavarapu 			ret = acp_dma_stop(rtd->acp_mmio,
8507c31335aSMaruthi Srinivas Bayyavarapu 					I2S_TO_ACP_DMA_CH_NUM);
8517c31335aSMaruthi Srinivas Bayyavarapu 		break;
8527c31335aSMaruthi Srinivas Bayyavarapu 	default:
8537c31335aSMaruthi Srinivas Bayyavarapu 		ret = -EINVAL;
8547c31335aSMaruthi Srinivas Bayyavarapu 
8557c31335aSMaruthi Srinivas Bayyavarapu 	}
8567c31335aSMaruthi Srinivas Bayyavarapu 	return ret;
8577c31335aSMaruthi Srinivas Bayyavarapu }
8587c31335aSMaruthi Srinivas Bayyavarapu 
8597c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
8607c31335aSMaruthi Srinivas Bayyavarapu {
8617c31335aSMaruthi Srinivas Bayyavarapu 	return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
8627c31335aSMaruthi Srinivas Bayyavarapu 							SNDRV_DMA_TYPE_DEV,
8637c31335aSMaruthi Srinivas Bayyavarapu 							NULL, MIN_BUFFER,
8647c31335aSMaruthi Srinivas Bayyavarapu 							MAX_BUFFER);
8657c31335aSMaruthi Srinivas Bayyavarapu }
8667c31335aSMaruthi Srinivas Bayyavarapu 
8677c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_close(struct snd_pcm_substream *substream)
8687c31335aSMaruthi Srinivas Bayyavarapu {
869*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
8707c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
8717c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
8727c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
8737c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(prtd->platform->dev);
8747c31335aSMaruthi Srinivas Bayyavarapu 
8757c31335aSMaruthi Srinivas Bayyavarapu 	kfree(rtd);
8767c31335aSMaruthi Srinivas Bayyavarapu 
877*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
8787c31335aSMaruthi Srinivas Bayyavarapu 		adata->play_stream = NULL;
879*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 1; bank <= 4; bank++)
880*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(adata->acp_mmio, bank,
881*c36d9b3fSMaruthi Srinivas Bayyavarapu 						false);
882*c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
8837c31335aSMaruthi Srinivas Bayyavarapu 		adata->capture_stream = NULL;
884*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 5; bank <= 8; bank++)
885*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(adata->acp_mmio, bank,
886*c36d9b3fSMaruthi Srinivas Bayyavarapu 						false);
887*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
8887c31335aSMaruthi Srinivas Bayyavarapu 
8897c31335aSMaruthi Srinivas Bayyavarapu 	/* Disable ACP irq, when the current stream is being closed and
8907c31335aSMaruthi Srinivas Bayyavarapu 	 * another stream is also not active.
8917c31335aSMaruthi Srinivas Bayyavarapu 	*/
8927c31335aSMaruthi Srinivas Bayyavarapu 	if (!adata->play_stream && !adata->capture_stream)
8937c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
8947c31335aSMaruthi Srinivas Bayyavarapu 
8957c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
8967c31335aSMaruthi Srinivas Bayyavarapu }
8977c31335aSMaruthi Srinivas Bayyavarapu 
8987c31335aSMaruthi Srinivas Bayyavarapu static struct snd_pcm_ops acp_dma_ops = {
8997c31335aSMaruthi Srinivas Bayyavarapu 	.open = acp_dma_open,
9007c31335aSMaruthi Srinivas Bayyavarapu 	.close = acp_dma_close,
9017c31335aSMaruthi Srinivas Bayyavarapu 	.ioctl = snd_pcm_lib_ioctl,
9027c31335aSMaruthi Srinivas Bayyavarapu 	.hw_params = acp_dma_hw_params,
9037c31335aSMaruthi Srinivas Bayyavarapu 	.hw_free = acp_dma_hw_free,
9047c31335aSMaruthi Srinivas Bayyavarapu 	.trigger = acp_dma_trigger,
9057c31335aSMaruthi Srinivas Bayyavarapu 	.pointer = acp_dma_pointer,
9067c31335aSMaruthi Srinivas Bayyavarapu 	.mmap = acp_dma_mmap,
9077c31335aSMaruthi Srinivas Bayyavarapu 	.prepare = acp_dma_prepare,
9087c31335aSMaruthi Srinivas Bayyavarapu };
9097c31335aSMaruthi Srinivas Bayyavarapu 
9107c31335aSMaruthi Srinivas Bayyavarapu static struct snd_soc_platform_driver acp_asoc_platform = {
9117c31335aSMaruthi Srinivas Bayyavarapu 	.ops = &acp_dma_ops,
9127c31335aSMaruthi Srinivas Bayyavarapu 	.pcm_new = acp_dma_new,
9137c31335aSMaruthi Srinivas Bayyavarapu };
9147c31335aSMaruthi Srinivas Bayyavarapu 
9157c31335aSMaruthi Srinivas Bayyavarapu static int acp_audio_probe(struct platform_device *pdev)
9167c31335aSMaruthi Srinivas Bayyavarapu {
9177c31335aSMaruthi Srinivas Bayyavarapu 	int status;
9187c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *audio_drv_data;
9197c31335aSMaruthi Srinivas Bayyavarapu 	struct resource *res;
9207c31335aSMaruthi Srinivas Bayyavarapu 
9217c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
9227c31335aSMaruthi Srinivas Bayyavarapu 					GFP_KERNEL);
9237c31335aSMaruthi Srinivas Bayyavarapu 	if (audio_drv_data == NULL)
9247c31335aSMaruthi Srinivas Bayyavarapu 		return -ENOMEM;
9257c31335aSMaruthi Srinivas Bayyavarapu 
9267c31335aSMaruthi Srinivas Bayyavarapu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9277c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
9287c31335aSMaruthi Srinivas Bayyavarapu 
9297c31335aSMaruthi Srinivas Bayyavarapu 	/* The following members gets populated in device 'open'
9307c31335aSMaruthi Srinivas Bayyavarapu 	 * function. Till then interrupts are disabled in 'acp_init'
9317c31335aSMaruthi Srinivas Bayyavarapu 	 * and device doesn't generate any interrupts.
9327c31335aSMaruthi Srinivas Bayyavarapu 	 */
9337c31335aSMaruthi Srinivas Bayyavarapu 
9347c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data->play_stream = NULL;
9357c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data->capture_stream = NULL;
9367c31335aSMaruthi Srinivas Bayyavarapu 
9377c31335aSMaruthi Srinivas Bayyavarapu 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
9387c31335aSMaruthi Srinivas Bayyavarapu 	if (!res) {
9397c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
9407c31335aSMaruthi Srinivas Bayyavarapu 		return -ENODEV;
9417c31335aSMaruthi Srinivas Bayyavarapu 	}
9427c31335aSMaruthi Srinivas Bayyavarapu 
9437c31335aSMaruthi Srinivas Bayyavarapu 	status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
9447c31335aSMaruthi Srinivas Bayyavarapu 					0, "ACP_IRQ", &pdev->dev);
9457c31335aSMaruthi Srinivas Bayyavarapu 	if (status) {
9467c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "ACP IRQ request failed\n");
9477c31335aSMaruthi Srinivas Bayyavarapu 		return status;
9487c31335aSMaruthi Srinivas Bayyavarapu 	}
9497c31335aSMaruthi Srinivas Bayyavarapu 
9507c31335aSMaruthi Srinivas Bayyavarapu 	dev_set_drvdata(&pdev->dev, audio_drv_data);
9517c31335aSMaruthi Srinivas Bayyavarapu 
9527c31335aSMaruthi Srinivas Bayyavarapu 	/* Initialize the ACP */
9537c31335aSMaruthi Srinivas Bayyavarapu 	acp_init(audio_drv_data->acp_mmio);
9547c31335aSMaruthi Srinivas Bayyavarapu 
9557c31335aSMaruthi Srinivas Bayyavarapu 	status = snd_soc_register_platform(&pdev->dev, &acp_asoc_platform);
9567c31335aSMaruthi Srinivas Bayyavarapu 	if (status != 0) {
9577c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
9587c31335aSMaruthi Srinivas Bayyavarapu 		return status;
9597c31335aSMaruthi Srinivas Bayyavarapu 	}
9607c31335aSMaruthi Srinivas Bayyavarapu 
9611927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
9621927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_use_autosuspend(&pdev->dev);
9631927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_enable(&pdev->dev);
9641927da93SMaruthi Srinivas Bayyavarapu 
9657c31335aSMaruthi Srinivas Bayyavarapu 	return status;
9667c31335aSMaruthi Srinivas Bayyavarapu }
9677c31335aSMaruthi Srinivas Bayyavarapu 
9687c31335aSMaruthi Srinivas Bayyavarapu static int acp_audio_remove(struct platform_device *pdev)
9697c31335aSMaruthi Srinivas Bayyavarapu {
9707c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
9717c31335aSMaruthi Srinivas Bayyavarapu 
9727c31335aSMaruthi Srinivas Bayyavarapu 	acp_deinit(adata->acp_mmio);
9737c31335aSMaruthi Srinivas Bayyavarapu 	snd_soc_unregister_platform(&pdev->dev);
9741927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_disable(&pdev->dev);
9757c31335aSMaruthi Srinivas Bayyavarapu 
9767c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
9777c31335aSMaruthi Srinivas Bayyavarapu }
9787c31335aSMaruthi Srinivas Bayyavarapu 
9791927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_resume(struct device *dev)
9801927da93SMaruthi Srinivas Bayyavarapu {
981*c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
9821927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
9831927da93SMaruthi Srinivas Bayyavarapu 
9841927da93SMaruthi Srinivas Bayyavarapu 	acp_init(adata->acp_mmio);
9851927da93SMaruthi Srinivas Bayyavarapu 
986*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (adata->play_stream && adata->play_stream->runtime) {
987*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 1; bank <= 4; bank++)
988*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(adata->acp_mmio, bank,
989*c36d9b3fSMaruthi Srinivas Bayyavarapu 						true);
9901927da93SMaruthi Srinivas Bayyavarapu 		config_acp_dma(adata->acp_mmio,
9911927da93SMaruthi Srinivas Bayyavarapu 				adata->play_stream->runtime->private_data);
992*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
993*c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (adata->capture_stream && adata->capture_stream->runtime) {
994*c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 5; bank <= 8; bank++)
995*c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(adata->acp_mmio, bank,
996*c36d9b3fSMaruthi Srinivas Bayyavarapu 						true);
9971927da93SMaruthi Srinivas Bayyavarapu 		config_acp_dma(adata->acp_mmio,
9981927da93SMaruthi Srinivas Bayyavarapu 				adata->capture_stream->runtime->private_data);
999*c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
10001927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
10011927da93SMaruthi Srinivas Bayyavarapu 	return 0;
10021927da93SMaruthi Srinivas Bayyavarapu }
10031927da93SMaruthi Srinivas Bayyavarapu 
10041927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_runtime_suspend(struct device *dev)
10051927da93SMaruthi Srinivas Bayyavarapu {
10061927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
10071927da93SMaruthi Srinivas Bayyavarapu 
10081927da93SMaruthi Srinivas Bayyavarapu 	acp_deinit(adata->acp_mmio);
10091927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
10101927da93SMaruthi Srinivas Bayyavarapu 	return 0;
10111927da93SMaruthi Srinivas Bayyavarapu }
10121927da93SMaruthi Srinivas Bayyavarapu 
10131927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_runtime_resume(struct device *dev)
10141927da93SMaruthi Srinivas Bayyavarapu {
10151927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
10161927da93SMaruthi Srinivas Bayyavarapu 
10171927da93SMaruthi Srinivas Bayyavarapu 	acp_init(adata->acp_mmio);
10181927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
10191927da93SMaruthi Srinivas Bayyavarapu 	return 0;
10201927da93SMaruthi Srinivas Bayyavarapu }
10211927da93SMaruthi Srinivas Bayyavarapu 
10221927da93SMaruthi Srinivas Bayyavarapu static const struct dev_pm_ops acp_pm_ops = {
10231927da93SMaruthi Srinivas Bayyavarapu 	.resume = acp_pcm_resume,
10241927da93SMaruthi Srinivas Bayyavarapu 	.runtime_suspend = acp_pcm_runtime_suspend,
10251927da93SMaruthi Srinivas Bayyavarapu 	.runtime_resume = acp_pcm_runtime_resume,
10261927da93SMaruthi Srinivas Bayyavarapu };
10271927da93SMaruthi Srinivas Bayyavarapu 
10287c31335aSMaruthi Srinivas Bayyavarapu static struct platform_driver acp_dma_driver = {
10297c31335aSMaruthi Srinivas Bayyavarapu 	.probe = acp_audio_probe,
10307c31335aSMaruthi Srinivas Bayyavarapu 	.remove = acp_audio_remove,
10317c31335aSMaruthi Srinivas Bayyavarapu 	.driver = {
10327c31335aSMaruthi Srinivas Bayyavarapu 		.name = "acp_audio_dma",
10331927da93SMaruthi Srinivas Bayyavarapu 		.pm = &acp_pm_ops,
10347c31335aSMaruthi Srinivas Bayyavarapu 	},
10357c31335aSMaruthi Srinivas Bayyavarapu };
10367c31335aSMaruthi Srinivas Bayyavarapu 
10377c31335aSMaruthi Srinivas Bayyavarapu module_platform_driver(acp_dma_driver);
10387c31335aSMaruthi Srinivas Bayyavarapu 
10397c31335aSMaruthi Srinivas Bayyavarapu MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
10407c31335aSMaruthi Srinivas Bayyavarapu MODULE_DESCRIPTION("AMD ACP PCM Driver");
10417c31335aSMaruthi Srinivas Bayyavarapu MODULE_LICENSE("GPL v2");
10427c31335aSMaruthi Srinivas Bayyavarapu MODULE_ALIAS("platform:acp-dma-audio");
1043