xref: /linux/sound/soc/amd/acp-pcm-dma.c (revision 13838c11c31e764d8143fdfcccea47691afd5ff2)
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>
187cb1dc81SGuenter Roeck #include <linux/io.h>
197c31335aSMaruthi Srinivas Bayyavarapu #include <linux/sizes.h>
201927da93SMaruthi Srinivas Bayyavarapu #include <linux/pm_runtime.h>
217c31335aSMaruthi Srinivas Bayyavarapu 
227c31335aSMaruthi Srinivas Bayyavarapu #include <sound/soc.h>
23607b39efSVijendar Mukunda #include <drm/amd_asic_type.h>
247c31335aSMaruthi Srinivas Bayyavarapu #include "acp.h"
257c31335aSMaruthi Srinivas Bayyavarapu 
26a1042a42SKuninori Morimoto #define DRV_NAME "acp_audio_dma"
27a1042a42SKuninori Morimoto 
287c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MIN_NUM_PERIODS    2
297c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MAX_NUM_PERIODS    2
307c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MAX_PERIOD_SIZE    16384
317c31335aSMaruthi Srinivas Bayyavarapu #define PLAYBACK_MIN_PERIOD_SIZE    1024
327c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MIN_NUM_PERIODS     2
337c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MAX_NUM_PERIODS     2
347c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MAX_PERIOD_SIZE     16384
357c31335aSMaruthi Srinivas Bayyavarapu #define CAPTURE_MIN_PERIOD_SIZE     1024
367c31335aSMaruthi Srinivas Bayyavarapu 
377c31335aSMaruthi Srinivas Bayyavarapu #define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
387c31335aSMaruthi Srinivas Bayyavarapu #define MIN_BUFFER MAX_BUFFER
397c31335aSMaruthi Srinivas Bayyavarapu 
409c7d6fabSVijendar Mukunda #define ST_PLAYBACK_MAX_PERIOD_SIZE 8192
419c7d6fabSVijendar Mukunda #define ST_CAPTURE_MAX_PERIOD_SIZE  ST_PLAYBACK_MAX_PERIOD_SIZE
429c7d6fabSVijendar Mukunda #define ST_MAX_BUFFER (ST_PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
439c7d6fabSVijendar Mukunda #define ST_MIN_BUFFER ST_MAX_BUFFER
449c7d6fabSVijendar Mukunda 
45bdd2a858SAkshu Agrawal #define DRV_NAME "acp_audio_dma"
46bdd2a858SAkshu Agrawal 
477c31335aSMaruthi Srinivas Bayyavarapu static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
487c31335aSMaruthi Srinivas Bayyavarapu 	.info = SNDRV_PCM_INFO_INTERLEAVED |
497c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
507c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
517c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
527c31335aSMaruthi Srinivas Bayyavarapu 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
537c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
547c31335aSMaruthi Srinivas Bayyavarapu 	.channels_min = 1,
557c31335aSMaruthi Srinivas Bayyavarapu 	.channels_max = 8,
567c31335aSMaruthi Srinivas Bayyavarapu 	.rates = SNDRV_PCM_RATE_8000_96000,
577c31335aSMaruthi Srinivas Bayyavarapu 	.rate_min = 8000,
587c31335aSMaruthi Srinivas Bayyavarapu 	.rate_max = 96000,
597c31335aSMaruthi Srinivas Bayyavarapu 	.buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
607c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
617c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
627c31335aSMaruthi Srinivas Bayyavarapu 	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
637c31335aSMaruthi Srinivas Bayyavarapu 	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
647c31335aSMaruthi Srinivas Bayyavarapu };
657c31335aSMaruthi Srinivas Bayyavarapu 
667c31335aSMaruthi Srinivas Bayyavarapu static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
677c31335aSMaruthi Srinivas Bayyavarapu 	.info = SNDRV_PCM_INFO_INTERLEAVED |
687c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
697c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
707c31335aSMaruthi Srinivas Bayyavarapu 	    SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
717c31335aSMaruthi Srinivas Bayyavarapu 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
727c31335aSMaruthi Srinivas Bayyavarapu 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
737c31335aSMaruthi Srinivas Bayyavarapu 	.channels_min = 1,
747c31335aSMaruthi Srinivas Bayyavarapu 	.channels_max = 2,
757c31335aSMaruthi Srinivas Bayyavarapu 	.rates = SNDRV_PCM_RATE_8000_48000,
767c31335aSMaruthi Srinivas Bayyavarapu 	.rate_min = 8000,
777c31335aSMaruthi Srinivas Bayyavarapu 	.rate_max = 48000,
787c31335aSMaruthi Srinivas Bayyavarapu 	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
797c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
807c31335aSMaruthi Srinivas Bayyavarapu 	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
817c31335aSMaruthi Srinivas Bayyavarapu 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
827c31335aSMaruthi Srinivas Bayyavarapu 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
837c31335aSMaruthi Srinivas Bayyavarapu };
847c31335aSMaruthi Srinivas Bayyavarapu 
859c7d6fabSVijendar Mukunda static const struct snd_pcm_hardware acp_st_pcm_hardware_playback = {
869c7d6fabSVijendar Mukunda 	.info = SNDRV_PCM_INFO_INTERLEAVED |
879c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
889c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
899c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
909c7d6fabSVijendar Mukunda 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
919c7d6fabSVijendar Mukunda 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
929c7d6fabSVijendar Mukunda 	.channels_min = 1,
939c7d6fabSVijendar Mukunda 	.channels_max = 8,
949c7d6fabSVijendar Mukunda 	.rates = SNDRV_PCM_RATE_8000_96000,
959c7d6fabSVijendar Mukunda 	.rate_min = 8000,
969c7d6fabSVijendar Mukunda 	.rate_max = 96000,
979c7d6fabSVijendar Mukunda 	.buffer_bytes_max = ST_MAX_BUFFER,
989c7d6fabSVijendar Mukunda 	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
999c7d6fabSVijendar Mukunda 	.period_bytes_max = ST_PLAYBACK_MAX_PERIOD_SIZE,
1009c7d6fabSVijendar Mukunda 	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
1019c7d6fabSVijendar Mukunda 	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
1029c7d6fabSVijendar Mukunda };
1039c7d6fabSVijendar Mukunda 
1049c7d6fabSVijendar Mukunda static const struct snd_pcm_hardware acp_st_pcm_hardware_capture = {
1059c7d6fabSVijendar Mukunda 	.info = SNDRV_PCM_INFO_INTERLEAVED |
1069c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
1079c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
1089c7d6fabSVijendar Mukunda 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
1099c7d6fabSVijendar Mukunda 	.formats = SNDRV_PCM_FMTBIT_S16_LE |
1109c7d6fabSVijendar Mukunda 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
1119c7d6fabSVijendar Mukunda 	.channels_min = 1,
1129c7d6fabSVijendar Mukunda 	.channels_max = 2,
1139c7d6fabSVijendar Mukunda 	.rates = SNDRV_PCM_RATE_8000_48000,
1149c7d6fabSVijendar Mukunda 	.rate_min = 8000,
1159c7d6fabSVijendar Mukunda 	.rate_max = 48000,
1169c7d6fabSVijendar Mukunda 	.buffer_bytes_max = ST_MAX_BUFFER,
1179c7d6fabSVijendar Mukunda 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
1189c7d6fabSVijendar Mukunda 	.period_bytes_max = ST_CAPTURE_MAX_PERIOD_SIZE,
1199c7d6fabSVijendar Mukunda 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
1209c7d6fabSVijendar Mukunda 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
1219c7d6fabSVijendar Mukunda };
1229c7d6fabSVijendar Mukunda 
1237c31335aSMaruthi Srinivas Bayyavarapu static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
1247c31335aSMaruthi Srinivas Bayyavarapu {
1257c31335aSMaruthi Srinivas Bayyavarapu 	return readl(acp_mmio + (reg * 4));
1267c31335aSMaruthi Srinivas Bayyavarapu }
1277c31335aSMaruthi Srinivas Bayyavarapu 
1287c31335aSMaruthi Srinivas Bayyavarapu static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
1297c31335aSMaruthi Srinivas Bayyavarapu {
1307c31335aSMaruthi Srinivas Bayyavarapu 	writel(val, acp_mmio + (reg * 4));
1317c31335aSMaruthi Srinivas Bayyavarapu }
1327c31335aSMaruthi Srinivas Bayyavarapu 
133*13838c11SMukunda, Vijendar /*
134*13838c11SMukunda, Vijendar  * Configure a given dma channel parameters - enable/disable,
1357c31335aSMaruthi Srinivas Bayyavarapu  * number of descriptors, priority
1367c31335aSMaruthi Srinivas Bayyavarapu  */
1377c31335aSMaruthi Srinivas Bayyavarapu static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
1387c31335aSMaruthi Srinivas Bayyavarapu 				   u16 dscr_strt_idx, u16 num_dscrs,
1397c31335aSMaruthi Srinivas Bayyavarapu 				   enum acp_dma_priority_level priority_level)
1407c31335aSMaruthi Srinivas Bayyavarapu {
1417c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
1427c31335aSMaruthi Srinivas Bayyavarapu 
1437c31335aSMaruthi Srinivas Bayyavarapu 	/* disable the channel run field */
1447c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
1457c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
1467c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
1477c31335aSMaruthi Srinivas Bayyavarapu 
1487c31335aSMaruthi Srinivas Bayyavarapu 	/* program a DMA channel with first descriptor to be processed. */
1497c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
1507c31335aSMaruthi Srinivas Bayyavarapu 			& dscr_strt_idx),
1517c31335aSMaruthi Srinivas Bayyavarapu 			acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);
1527c31335aSMaruthi Srinivas Bayyavarapu 
153*13838c11SMukunda, Vijendar 	/*
154*13838c11SMukunda, Vijendar 	 * program a DMA channel with the number of descriptors to be
1557c31335aSMaruthi Srinivas Bayyavarapu 	 * processed in the transfer
1567c31335aSMaruthi Srinivas Bayyavarapu 	 */
1577c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
1587c31335aSMaruthi Srinivas Bayyavarapu 		      acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
1597c31335aSMaruthi Srinivas Bayyavarapu 
1607c31335aSMaruthi Srinivas Bayyavarapu 	/* set DMA channel priority */
1617c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
1627c31335aSMaruthi Srinivas Bayyavarapu }
1637c31335aSMaruthi Srinivas Bayyavarapu 
1647c31335aSMaruthi Srinivas Bayyavarapu /* Initialize a dma descriptor in SRAM based on descritor information passed */
1657c31335aSMaruthi Srinivas Bayyavarapu static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
1667c31335aSMaruthi Srinivas Bayyavarapu 					  u16 descr_idx,
1677c31335aSMaruthi Srinivas Bayyavarapu 					  acp_dma_dscr_transfer_t *descr_info)
1687c31335aSMaruthi Srinivas Bayyavarapu {
1697c31335aSMaruthi Srinivas Bayyavarapu 	u32 sram_offset;
1707c31335aSMaruthi Srinivas Bayyavarapu 
1717c31335aSMaruthi Srinivas Bayyavarapu 	sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));
1727c31335aSMaruthi Srinivas Bayyavarapu 
1737c31335aSMaruthi Srinivas Bayyavarapu 	/* program the source base address. */
1747c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1757c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->src,	acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1767c31335aSMaruthi Srinivas Bayyavarapu 	/* program the destination base address. */
1777c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset + 4,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1787c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1797c31335aSMaruthi Srinivas Bayyavarapu 
1807c31335aSMaruthi Srinivas Bayyavarapu 	/* program the number of bytes to be transferred for this descriptor. */
1817c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_offset + 8,	acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
1827c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
1837c31335aSMaruthi Srinivas Bayyavarapu }
1847c31335aSMaruthi Srinivas Bayyavarapu 
185*13838c11SMukunda, Vijendar /*
186*13838c11SMukunda, Vijendar  * Initialize the DMA descriptor information for transfer between
1877c31335aSMaruthi Srinivas Bayyavarapu  * system memory <-> ACP SRAM
1887c31335aSMaruthi Srinivas Bayyavarapu  */
1897c31335aSMaruthi Srinivas Bayyavarapu static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
190*13838c11SMukunda, Vijendar 					   u32 size, int direction,
191*13838c11SMukunda, Vijendar 					   u32 pte_offset, u16 ch,
192*13838c11SMukunda, Vijendar 					   u32 sram_bank, u16 dma_dscr_idx,
193*13838c11SMukunda, Vijendar 					   u32 asic_type)
1947c31335aSMaruthi Srinivas Bayyavarapu {
1957c31335aSMaruthi Srinivas Bayyavarapu 	u16 i;
1967c31335aSMaruthi Srinivas Bayyavarapu 	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
1977c31335aSMaruthi Srinivas Bayyavarapu 
1987c31335aSMaruthi Srinivas Bayyavarapu 	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
1997c31335aSMaruthi Srinivas Bayyavarapu 		dmadscr[i].xfer_val = 0;
2007c31335aSMaruthi Srinivas Bayyavarapu 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
2014376a86cSMukunda, Vijendar 			dma_dscr_idx = dma_dscr_idx + i;
2024376a86cSMukunda, Vijendar 			dmadscr[i].dest = sram_bank + (i * (size / 2));
2037c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
2047c31335aSMaruthi Srinivas Bayyavarapu 				+ (pte_offset * SZ_4K) + (i * (size / 2));
205aac89748SVijendar Mukunda 			switch (asic_type) {
206aac89748SVijendar Mukunda 			case CHIP_STONEY:
207aac89748SVijendar Mukunda 				dmadscr[i].xfer_val |=
208*13838c11SMukunda, Vijendar 				(ACP_DMA_ATTR_DAGB_GARLIC_TO_SHAREDMEM  << 16) |
209aac89748SVijendar Mukunda 				(size / 2);
210aac89748SVijendar Mukunda 				break;
211aac89748SVijendar Mukunda 			default:
2127c31335aSMaruthi Srinivas Bayyavarapu 				dmadscr[i].xfer_val |=
213*13838c11SMukunda, Vijendar 				(ACP_DMA_ATTR_DAGB_ONION_TO_SHAREDMEM  << 16) |
2147c31335aSMaruthi Srinivas Bayyavarapu 				(size / 2);
215aac89748SVijendar Mukunda 			}
2167c31335aSMaruthi Srinivas Bayyavarapu 		} else {
2174376a86cSMukunda, Vijendar 			dma_dscr_idx = dma_dscr_idx + i;
2184376a86cSMukunda, Vijendar 			dmadscr[i].src = sram_bank + (i * (size / 2));
219aac89748SVijendar Mukunda 			dmadscr[i].dest =
220aac89748SVijendar Mukunda 			ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS +
221aac89748SVijendar Mukunda 			(pte_offset * SZ_4K) + (i * (size / 2));
2224376a86cSMukunda, Vijendar 			switch (asic_type) {
2234376a86cSMukunda, Vijendar 			case CHIP_STONEY:
224aac89748SVijendar Mukunda 				dmadscr[i].xfer_val |=
225aac89748SVijendar Mukunda 				BIT(22) |
226*13838c11SMukunda, Vijendar 				(ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
227aac89748SVijendar Mukunda 				(size / 2);
228aac89748SVijendar Mukunda 				break;
229aac89748SVijendar Mukunda 			default:
2307c31335aSMaruthi Srinivas Bayyavarapu 				dmadscr[i].xfer_val |=
2317c31335aSMaruthi Srinivas Bayyavarapu 				BIT(22) |
232*13838c11SMukunda, Vijendar 				(ACP_DMA_ATTR_SHAREDMEM_TO_DAGB_ONION << 16) |
2337c31335aSMaruthi Srinivas Bayyavarapu 				(size / 2);
2347c31335aSMaruthi Srinivas Bayyavarapu 			}
235aac89748SVijendar Mukunda 		}
2367c31335aSMaruthi Srinivas Bayyavarapu 		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
2377c31335aSMaruthi Srinivas Bayyavarapu 					      &dmadscr[i]);
2387c31335aSMaruthi Srinivas Bayyavarapu 	}
2394376a86cSMukunda, Vijendar 	config_acp_dma_channel(acp_mmio, ch,
2404376a86cSMukunda, Vijendar 			       dma_dscr_idx - 1,
2417c31335aSMaruthi Srinivas Bayyavarapu 			       NUM_DSCRS_PER_CHANNEL,
2427c31335aSMaruthi Srinivas Bayyavarapu 			       ACP_DMA_PRIORITY_LEVEL_NORMAL);
2437c31335aSMaruthi Srinivas Bayyavarapu }
2447c31335aSMaruthi Srinivas Bayyavarapu 
245*13838c11SMukunda, Vijendar /*
246*13838c11SMukunda, Vijendar  * Initialize the DMA descriptor information for transfer between
2477c31335aSMaruthi Srinivas Bayyavarapu  * ACP SRAM <-> I2S
2487c31335aSMaruthi Srinivas Bayyavarapu  */
2494376a86cSMukunda, Vijendar static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio, u32 size,
2504376a86cSMukunda, Vijendar 					   int direction, u32 sram_bank,
2514376a86cSMukunda, Vijendar 					   u16 destination, u16 ch,
2524376a86cSMukunda, Vijendar 					   u16 dma_dscr_idx, u32 asic_type)
2537c31335aSMaruthi Srinivas Bayyavarapu {
2547c31335aSMaruthi Srinivas Bayyavarapu 	u16 i;
2557c31335aSMaruthi Srinivas Bayyavarapu 	acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
2567c31335aSMaruthi Srinivas Bayyavarapu 
2577c31335aSMaruthi Srinivas Bayyavarapu 	for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
2587c31335aSMaruthi Srinivas Bayyavarapu 		dmadscr[i].xfer_val = 0;
2597c31335aSMaruthi Srinivas Bayyavarapu 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
2604376a86cSMukunda, Vijendar 			dma_dscr_idx = dma_dscr_idx + i;
2614376a86cSMukunda, Vijendar 			dmadscr[i].src = sram_bank  + (i * (size / 2));
2627c31335aSMaruthi Srinivas Bayyavarapu 			/* dmadscr[i].dest is unused by hardware. */
2637c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].dest = 0;
2644376a86cSMukunda, Vijendar 			dmadscr[i].xfer_val |= BIT(22) | (destination << 16) |
2657c31335aSMaruthi Srinivas Bayyavarapu 						(size / 2);
2667c31335aSMaruthi Srinivas Bayyavarapu 		} else {
2674376a86cSMukunda, Vijendar 			dma_dscr_idx = dma_dscr_idx + i;
2687c31335aSMaruthi Srinivas Bayyavarapu 			/* dmadscr[i].src is unused by hardware. */
2697c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].src = 0;
270aac89748SVijendar Mukunda 			dmadscr[i].dest =
2714376a86cSMukunda, Vijendar 				 sram_bank + (i * (size / 2));
2727c31335aSMaruthi Srinivas Bayyavarapu 			dmadscr[i].xfer_val |= BIT(22) |
2734376a86cSMukunda, Vijendar 				(destination << 16) | (size / 2);
2747c31335aSMaruthi Srinivas Bayyavarapu 		}
2757c31335aSMaruthi Srinivas Bayyavarapu 		config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
2767c31335aSMaruthi Srinivas Bayyavarapu 					      &dmadscr[i]);
2777c31335aSMaruthi Srinivas Bayyavarapu 	}
2787c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure the DMA channel with the above descriptore */
2794376a86cSMukunda, Vijendar 	config_acp_dma_channel(acp_mmio, ch, dma_dscr_idx - 1,
2807c31335aSMaruthi Srinivas Bayyavarapu 			       NUM_DSCRS_PER_CHANNEL,
2817c31335aSMaruthi Srinivas Bayyavarapu 			       ACP_DMA_PRIORITY_LEVEL_NORMAL);
2827c31335aSMaruthi Srinivas Bayyavarapu }
2837c31335aSMaruthi Srinivas Bayyavarapu 
2847c31335aSMaruthi Srinivas Bayyavarapu /* Create page table entries in ACP SRAM for the allocated memory */
2857c31335aSMaruthi Srinivas Bayyavarapu static void acp_pte_config(void __iomem *acp_mmio, struct page *pg,
2867c31335aSMaruthi Srinivas Bayyavarapu 			   u16 num_of_pages, u32 pte_offset)
2877c31335aSMaruthi Srinivas Bayyavarapu {
2887c31335aSMaruthi Srinivas Bayyavarapu 	u16 page_idx;
2897c31335aSMaruthi Srinivas Bayyavarapu 	u64 addr;
2907c31335aSMaruthi Srinivas Bayyavarapu 	u32 low;
2917c31335aSMaruthi Srinivas Bayyavarapu 	u32 high;
2927c31335aSMaruthi Srinivas Bayyavarapu 	u32 offset;
2937c31335aSMaruthi Srinivas Bayyavarapu 
2947c31335aSMaruthi Srinivas Bayyavarapu 	offset	= ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
2957c31335aSMaruthi Srinivas Bayyavarapu 	for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
2967c31335aSMaruthi Srinivas Bayyavarapu 		/* Load the low address of page int ACP SRAM through SRBM */
2977c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((offset + (page_idx * 8)),
2987c31335aSMaruthi Srinivas Bayyavarapu 			      acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
2997c31335aSMaruthi Srinivas Bayyavarapu 		addr = page_to_phys(pg);
3007c31335aSMaruthi Srinivas Bayyavarapu 
3017c31335aSMaruthi Srinivas Bayyavarapu 		low = lower_32_bits(addr);
3027c31335aSMaruthi Srinivas Bayyavarapu 		high = upper_32_bits(addr);
3037c31335aSMaruthi Srinivas Bayyavarapu 
3047c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
3057c31335aSMaruthi Srinivas Bayyavarapu 
3067c31335aSMaruthi Srinivas Bayyavarapu 		/* Load the High address of page int ACP SRAM through SRBM */
3077c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((offset + (page_idx * 8) + 4),
3087c31335aSMaruthi Srinivas Bayyavarapu 			      acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
3097c31335aSMaruthi Srinivas Bayyavarapu 
3107c31335aSMaruthi Srinivas Bayyavarapu 		/* page enable in ACP */
3117c31335aSMaruthi Srinivas Bayyavarapu 		high |= BIT(31);
3127c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
3137c31335aSMaruthi Srinivas Bayyavarapu 
3147c31335aSMaruthi Srinivas Bayyavarapu 		/* Move to next physically contiguos page */
3157c31335aSMaruthi Srinivas Bayyavarapu 		pg++;
3167c31335aSMaruthi Srinivas Bayyavarapu 	}
3177c31335aSMaruthi Srinivas Bayyavarapu }
3187c31335aSMaruthi Srinivas Bayyavarapu 
3197c31335aSMaruthi Srinivas Bayyavarapu static void config_acp_dma(void __iomem *acp_mmio,
320aac89748SVijendar Mukunda 			   struct audio_substream_data *audio_config,
321aac89748SVijendar Mukunda 			   u32 asic_type)
3227c31335aSMaruthi Srinivas Bayyavarapu {
3234376a86cSMukunda, Vijendar 	u32 pte_offset, sram_bank;
3244376a86cSMukunda, Vijendar 	u16 ch1, ch2, destination, dma_dscr_idx;
3257c31335aSMaruthi Srinivas Bayyavarapu 
3264376a86cSMukunda, Vijendar 	if (audio_config->direction == SNDRV_PCM_STREAM_PLAYBACK) {
3277c31335aSMaruthi Srinivas Bayyavarapu 		pte_offset = ACP_PLAYBACK_PTE_OFFSET;
3284376a86cSMukunda, Vijendar 		ch1 = SYSRAM_TO_ACP_CH_NUM;
3294376a86cSMukunda, Vijendar 		ch2 = ACP_TO_I2S_DMA_CH_NUM;
3304376a86cSMukunda, Vijendar 		sram_bank = ACP_SHARED_RAM_BANK_1_ADDRESS;
3314376a86cSMukunda, Vijendar 		destination = TO_ACP_I2S_1;
3324376a86cSMukunda, Vijendar 
3334376a86cSMukunda, Vijendar 	} else {
3347c31335aSMaruthi Srinivas Bayyavarapu 		pte_offset = ACP_CAPTURE_PTE_OFFSET;
3354376a86cSMukunda, Vijendar 		ch1 = SYSRAM_TO_ACP_CH_NUM;
3364376a86cSMukunda, Vijendar 		ch2 = ACP_TO_I2S_DMA_CH_NUM;
3374376a86cSMukunda, Vijendar 		switch (asic_type) {
3384376a86cSMukunda, Vijendar 		case CHIP_STONEY:
3394376a86cSMukunda, Vijendar 			sram_bank = ACP_SHARED_RAM_BANK_3_ADDRESS;
3404376a86cSMukunda, Vijendar 			break;
3414376a86cSMukunda, Vijendar 		default:
3424376a86cSMukunda, Vijendar 			sram_bank = ACP_SHARED_RAM_BANK_5_ADDRESS;
3434376a86cSMukunda, Vijendar 		}
3444376a86cSMukunda, Vijendar 		destination = FROM_ACP_I2S_1;
3454376a86cSMukunda, Vijendar 	}
3467c31335aSMaruthi Srinivas Bayyavarapu 
3477c31335aSMaruthi Srinivas Bayyavarapu 	acp_pte_config(acp_mmio, audio_config->pg, audio_config->num_of_pages,
3487c31335aSMaruthi Srinivas Bayyavarapu 		       pte_offset);
3494376a86cSMukunda, Vijendar 	if (audio_config->direction == SNDRV_PCM_STREAM_PLAYBACK)
3504376a86cSMukunda, Vijendar 		dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH12;
3514376a86cSMukunda, Vijendar 	else
3524376a86cSMukunda, Vijendar 		dma_dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
3537c31335aSMaruthi Srinivas Bayyavarapu 
3547c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure System memory <-> ACP SRAM DMA descriptors */
3557c31335aSMaruthi Srinivas Bayyavarapu 	set_acp_sysmem_dma_descriptors(acp_mmio, audio_config->size,
356*13838c11SMukunda, Vijendar 				       audio_config->direction, pte_offset, ch1,
357*13838c11SMukunda, Vijendar 				       sram_bank, dma_dscr_idx, asic_type);
3587c31335aSMaruthi Srinivas Bayyavarapu 
3594376a86cSMukunda, Vijendar 	if (audio_config->direction == SNDRV_PCM_STREAM_PLAYBACK)
3604376a86cSMukunda, Vijendar 		dma_dscr_idx = PLAYBACK_START_DMA_DESCR_CH13;
3614376a86cSMukunda, Vijendar 	else
3624376a86cSMukunda, Vijendar 		dma_dscr_idx = CAPTURE_START_DMA_DESCR_CH15;
3637c31335aSMaruthi Srinivas Bayyavarapu 	/* Configure ACP SRAM <-> I2S DMA descriptors */
3647c31335aSMaruthi Srinivas Bayyavarapu 	set_acp_to_i2s_dma_descriptors(acp_mmio, audio_config->size,
3654376a86cSMukunda, Vijendar 				       audio_config->direction, sram_bank,
3664376a86cSMukunda, Vijendar 				       destination, ch2, dma_dscr_idx,
3674376a86cSMukunda, Vijendar 				       asic_type);
3687c31335aSMaruthi Srinivas Bayyavarapu }
3697c31335aSMaruthi Srinivas Bayyavarapu 
3707c31335aSMaruthi Srinivas Bayyavarapu /* Start a given DMA channel transfer */
3717c31335aSMaruthi Srinivas Bayyavarapu static void acp_dma_start(void __iomem *acp_mmio,
3727c31335aSMaruthi Srinivas Bayyavarapu 			  u16 ch_num, bool is_circular)
3737c31335aSMaruthi Srinivas Bayyavarapu {
3747c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
3757c31335aSMaruthi Srinivas Bayyavarapu 
3767c31335aSMaruthi Srinivas Bayyavarapu 	/* read the dma control register and disable the channel run field */
3777c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
3787c31335aSMaruthi Srinivas Bayyavarapu 
3797c31335aSMaruthi Srinivas Bayyavarapu 	/* Invalidating the DAGB cache */
3807c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
3817c31335aSMaruthi Srinivas Bayyavarapu 
382*13838c11SMukunda, Vijendar 	/*
383*13838c11SMukunda, Vijendar 	 * configure the DMA channel and start the DMA transfer
3847c31335aSMaruthi Srinivas Bayyavarapu 	 * set dmachrun bit to start the transfer and enable the
3857c31335aSMaruthi Srinivas Bayyavarapu 	 * interrupt on completion of the dma transfer
3867c31335aSMaruthi Srinivas Bayyavarapu 	 */
3877c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
3887c31335aSMaruthi Srinivas Bayyavarapu 
3897c31335aSMaruthi Srinivas Bayyavarapu 	switch (ch_num) {
3907c31335aSMaruthi Srinivas Bayyavarapu 	case ACP_TO_I2S_DMA_CH_NUM:
3917c31335aSMaruthi Srinivas Bayyavarapu 	case ACP_TO_SYSRAM_CH_NUM:
3927c31335aSMaruthi Srinivas Bayyavarapu 	case I2S_TO_ACP_DMA_CH_NUM:
3937c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
3947c31335aSMaruthi Srinivas Bayyavarapu 		break;
3957c31335aSMaruthi Srinivas Bayyavarapu 	default:
3967c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
3977c31335aSMaruthi Srinivas Bayyavarapu 		break;
3987c31335aSMaruthi Srinivas Bayyavarapu 	}
3997c31335aSMaruthi Srinivas Bayyavarapu 
4007c31335aSMaruthi Srinivas Bayyavarapu 	/* enable  for ACP SRAM to/from I2S DMA channel */
4017c31335aSMaruthi Srinivas Bayyavarapu 	if (is_circular == true)
4027c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
4037c31335aSMaruthi Srinivas Bayyavarapu 	else
4047c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
4057c31335aSMaruthi Srinivas Bayyavarapu 
4067c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
4077c31335aSMaruthi Srinivas Bayyavarapu }
4087c31335aSMaruthi Srinivas Bayyavarapu 
4097c31335aSMaruthi Srinivas Bayyavarapu /* Stop a given DMA channel transfer */
4107c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
4117c31335aSMaruthi Srinivas Bayyavarapu {
4127c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ctrl;
4137c31335aSMaruthi Srinivas Bayyavarapu 	u32 dma_ch_sts;
4147c31335aSMaruthi Srinivas Bayyavarapu 	u32 count = ACP_DMA_RESET_TIME;
4157c31335aSMaruthi Srinivas Bayyavarapu 
4167c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
4177c31335aSMaruthi Srinivas Bayyavarapu 
418*13838c11SMukunda, Vijendar 	/*
419*13838c11SMukunda, Vijendar 	 * clear the dma control register fields before writing zero
4207c31335aSMaruthi Srinivas Bayyavarapu 	 * in reset bit
4217c31335aSMaruthi Srinivas Bayyavarapu 	 */
4227c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
4237c31335aSMaruthi Srinivas Bayyavarapu 	dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
4247c31335aSMaruthi Srinivas Bayyavarapu 
4257c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
4267c31335aSMaruthi Srinivas Bayyavarapu 	dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
4277c31335aSMaruthi Srinivas Bayyavarapu 
4287c31335aSMaruthi Srinivas Bayyavarapu 	if (dma_ch_sts & BIT(ch_num)) {
429*13838c11SMukunda, Vijendar 		/*
430*13838c11SMukunda, Vijendar 		 * set the reset bit for this channel to stop the dma
4317c31335aSMaruthi Srinivas Bayyavarapu 		 *  transfer
4327c31335aSMaruthi Srinivas Bayyavarapu 		 */
4337c31335aSMaruthi Srinivas Bayyavarapu 		dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
4347c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
4357c31335aSMaruthi Srinivas Bayyavarapu 	}
4367c31335aSMaruthi Srinivas Bayyavarapu 
4377c31335aSMaruthi Srinivas Bayyavarapu 	/* check the channel status bit for some time and return the status */
4387c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
4397c31335aSMaruthi Srinivas Bayyavarapu 		dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
4407c31335aSMaruthi Srinivas Bayyavarapu 		if (!(dma_ch_sts & BIT(ch_num))) {
441*13838c11SMukunda, Vijendar 			/*
442*13838c11SMukunda, Vijendar 			 * clear the reset flag after successfully stopping
4437c31335aSMaruthi Srinivas Bayyavarapu 			 * the dma transfer and break from the loop
4447c31335aSMaruthi Srinivas Bayyavarapu 			 */
4457c31335aSMaruthi Srinivas Bayyavarapu 			dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
4467c31335aSMaruthi Srinivas Bayyavarapu 
4477c31335aSMaruthi Srinivas Bayyavarapu 			acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
4487c31335aSMaruthi Srinivas Bayyavarapu 				      + ch_num);
4497c31335aSMaruthi Srinivas Bayyavarapu 			break;
4507c31335aSMaruthi Srinivas Bayyavarapu 		}
4517c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
4527c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
4537c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
4547c31335aSMaruthi Srinivas Bayyavarapu 		}
4557c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
4567c31335aSMaruthi Srinivas Bayyavarapu 	}
4577c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
4587c31335aSMaruthi Srinivas Bayyavarapu }
4597c31335aSMaruthi Srinivas Bayyavarapu 
460c36d9b3fSMaruthi Srinivas Bayyavarapu static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
461c36d9b3fSMaruthi Srinivas Bayyavarapu 				    bool power_on)
462c36d9b3fSMaruthi Srinivas Bayyavarapu {
463c36d9b3fSMaruthi Srinivas Bayyavarapu 	u32 val, req_reg, sts_reg, sts_reg_mask;
464c36d9b3fSMaruthi Srinivas Bayyavarapu 	u32 loops = 1000;
465c36d9b3fSMaruthi Srinivas Bayyavarapu 
466c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (bank < 32) {
467c36d9b3fSMaruthi Srinivas Bayyavarapu 		req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
468c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
469c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg_mask = 0xFFFFFFFF;
470c36d9b3fSMaruthi Srinivas Bayyavarapu 
471c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
472c36d9b3fSMaruthi Srinivas Bayyavarapu 		bank -= 32;
473c36d9b3fSMaruthi Srinivas Bayyavarapu 		req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
474c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
475c36d9b3fSMaruthi Srinivas Bayyavarapu 		sts_reg_mask = 0x0000FFFF;
476c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
477c36d9b3fSMaruthi Srinivas Bayyavarapu 
478c36d9b3fSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, req_reg);
479c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (val & (1 << bank)) {
480c36d9b3fSMaruthi Srinivas Bayyavarapu 		/* bank is in off state */
481c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (power_on == true)
482c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to on */
483c36d9b3fSMaruthi Srinivas Bayyavarapu 			val &= ~(1 << bank);
484c36d9b3fSMaruthi Srinivas Bayyavarapu 		else
485c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to off */
486c36d9b3fSMaruthi Srinivas Bayyavarapu 			return;
487c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
488c36d9b3fSMaruthi Srinivas Bayyavarapu 		/* bank is in on state */
489c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (power_on == false)
490c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to off */
491c36d9b3fSMaruthi Srinivas Bayyavarapu 			val |= 1 << bank;
492c36d9b3fSMaruthi Srinivas Bayyavarapu 		else
493c36d9b3fSMaruthi Srinivas Bayyavarapu 			/* request to on */
494c36d9b3fSMaruthi Srinivas Bayyavarapu 			return;
495c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
496c36d9b3fSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, req_reg);
497c36d9b3fSMaruthi Srinivas Bayyavarapu 
498c36d9b3fSMaruthi Srinivas Bayyavarapu 	while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
499c36d9b3fSMaruthi Srinivas Bayyavarapu 		if (!loops--) {
500c36d9b3fSMaruthi Srinivas Bayyavarapu 			pr_err("ACP SRAM bank %d state change failed\n", bank);
501c36d9b3fSMaruthi Srinivas Bayyavarapu 			break;
502c36d9b3fSMaruthi Srinivas Bayyavarapu 		}
503c36d9b3fSMaruthi Srinivas Bayyavarapu 		cpu_relax();
504c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
505c36d9b3fSMaruthi Srinivas Bayyavarapu }
506c36d9b3fSMaruthi Srinivas Bayyavarapu 
5077c31335aSMaruthi Srinivas Bayyavarapu /* Initialize and bring ACP hardware to default state. */
508607b39efSVijendar Mukunda static int acp_init(void __iomem *acp_mmio, u32 asic_type)
5097c31335aSMaruthi Srinivas Bayyavarapu {
510c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
5117c31335aSMaruthi Srinivas Bayyavarapu 	u32 val, count, sram_pte_offset;
5127c31335aSMaruthi Srinivas Bayyavarapu 
5137c31335aSMaruthi Srinivas Bayyavarapu 	/* Assert Soft reset of ACP */
5147c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
5157c31335aSMaruthi Srinivas Bayyavarapu 
5167c31335aSMaruthi Srinivas Bayyavarapu 	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
5177c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
5187c31335aSMaruthi Srinivas Bayyavarapu 
5197c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
5207c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
5217c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
5227c31335aSMaruthi Srinivas Bayyavarapu 		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
5237c31335aSMaruthi Srinivas Bayyavarapu 		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
5247c31335aSMaruthi Srinivas Bayyavarapu 			break;
5257c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
5267c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
5277c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
5287c31335aSMaruthi Srinivas Bayyavarapu 		}
5297c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
5307c31335aSMaruthi Srinivas Bayyavarapu 	}
5317c31335aSMaruthi Srinivas Bayyavarapu 
5327c31335aSMaruthi Srinivas Bayyavarapu 	/* Enable clock to ACP and wait until the clock is enabled */
5337c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
5347c31335aSMaruthi Srinivas Bayyavarapu 	val = 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 
5507c31335aSMaruthi Srinivas Bayyavarapu 	/* Deassert the SOFT RESET flags */
5517c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
5527c31335aSMaruthi Srinivas Bayyavarapu 	val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
5537c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
5547c31335aSMaruthi Srinivas Bayyavarapu 
5557c31335aSMaruthi Srinivas Bayyavarapu 	/* initiailize Onion control DAGB register */
5567c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
5577c31335aSMaruthi Srinivas Bayyavarapu 		      mmACP_AXI2DAGB_ONION_CNTL);
5587c31335aSMaruthi Srinivas Bayyavarapu 
5597c31335aSMaruthi Srinivas Bayyavarapu 	/* initiailize Garlic control DAGB registers */
5607c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
5617c31335aSMaruthi Srinivas Bayyavarapu 		      mmACP_AXI2DAGB_GARLIC_CNTL);
5627c31335aSMaruthi Srinivas Bayyavarapu 
5637c31335aSMaruthi Srinivas Bayyavarapu 	sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
5647c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
5657c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
5667c31335aSMaruthi Srinivas Bayyavarapu 			ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
5677c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(sram_pte_offset,  acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
5687c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
5697c31335aSMaruthi Srinivas Bayyavarapu 		      mmACP_DAGB_PAGE_SIZE_GRP_1);
5707c31335aSMaruthi Srinivas Bayyavarapu 
5717c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
5727c31335aSMaruthi Srinivas Bayyavarapu 		      mmACP_DMA_DESC_BASE_ADDR);
5737c31335aSMaruthi Srinivas Bayyavarapu 
5747c31335aSMaruthi Srinivas Bayyavarapu 	/* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
5757c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
5767c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
5777c31335aSMaruthi Srinivas Bayyavarapu 		      acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
5787c31335aSMaruthi Srinivas Bayyavarapu 
579*13838c11SMukunda, Vijendar        /*
580*13838c11SMukunda, Vijendar 	* When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
581c36d9b3fSMaruthi Srinivas Bayyavarapu 	* Now, turn off all of them. This can't be done in 'poweron' of
582c36d9b3fSMaruthi Srinivas Bayyavarapu 	* ACP pm domain, as this requires ACP to be initialized.
583607b39efSVijendar Mukunda 	* For Stoney, Memory gating is disabled,i.e SRAM Banks
584607b39efSVijendar Mukunda 	* won't be turned off. The default state for SRAM banks is ON.
585607b39efSVijendar Mukunda 	* Setting SRAM bank state code skipped for STONEY platform.
586c36d9b3fSMaruthi Srinivas Bayyavarapu 	*/
587607b39efSVijendar Mukunda 	if (asic_type != CHIP_STONEY) {
588c36d9b3fSMaruthi Srinivas Bayyavarapu 		for (bank = 1; bank < 48; bank++)
589c36d9b3fSMaruthi Srinivas Bayyavarapu 			acp_set_sram_bank_state(acp_mmio, bank, false);
590607b39efSVijendar Mukunda 	}
5917c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
5927c31335aSMaruthi Srinivas Bayyavarapu }
5937c31335aSMaruthi Srinivas Bayyavarapu 
5941cce2000SMasahiro Yamada /* Deinitialize ACP */
5957c31335aSMaruthi Srinivas Bayyavarapu static int acp_deinit(void __iomem *acp_mmio)
5967c31335aSMaruthi Srinivas Bayyavarapu {
5977c31335aSMaruthi Srinivas Bayyavarapu 	u32 val;
5987c31335aSMaruthi Srinivas Bayyavarapu 	u32 count;
5997c31335aSMaruthi Srinivas Bayyavarapu 
6007c31335aSMaruthi Srinivas Bayyavarapu 	/* Assert Soft reset of ACP */
6017c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
6027c31335aSMaruthi Srinivas Bayyavarapu 
6037c31335aSMaruthi Srinivas Bayyavarapu 	val |= ACP_SOFT_RESET__SoftResetAud_MASK;
6047c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
6057c31335aSMaruthi Srinivas Bayyavarapu 
6067c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
6077c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
6087c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
6097c31335aSMaruthi Srinivas Bayyavarapu 		if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
6107c31335aSMaruthi Srinivas Bayyavarapu 		    (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
6117c31335aSMaruthi Srinivas Bayyavarapu 			break;
6127c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
6137c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
6147c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
6157c31335aSMaruthi Srinivas Bayyavarapu 		}
6167c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
6177c31335aSMaruthi Srinivas Bayyavarapu 	}
618*13838c11SMukunda, Vijendar 	/* Disable ACP clock */
6197c31335aSMaruthi Srinivas Bayyavarapu 	val = acp_reg_read(acp_mmio, mmACP_CONTROL);
6207c31335aSMaruthi Srinivas Bayyavarapu 	val &= ~ACP_CONTROL__ClkEn_MASK;
6217c31335aSMaruthi Srinivas Bayyavarapu 	acp_reg_write(val, acp_mmio, mmACP_CONTROL);
6227c31335aSMaruthi Srinivas Bayyavarapu 
6237c31335aSMaruthi Srinivas Bayyavarapu 	count = ACP_CLOCK_EN_TIME_OUT_VALUE;
6247c31335aSMaruthi Srinivas Bayyavarapu 
6257c31335aSMaruthi Srinivas Bayyavarapu 	while (true) {
6267c31335aSMaruthi Srinivas Bayyavarapu 		val = acp_reg_read(acp_mmio, mmACP_STATUS);
6277c31335aSMaruthi Srinivas Bayyavarapu 		if (!(val & (u32)0x1))
6287c31335aSMaruthi Srinivas Bayyavarapu 			break;
6297c31335aSMaruthi Srinivas Bayyavarapu 		if (--count == 0) {
6307c31335aSMaruthi Srinivas Bayyavarapu 			pr_err("Failed to reset ACP\n");
6317c31335aSMaruthi Srinivas Bayyavarapu 			return -ETIMEDOUT;
6327c31335aSMaruthi Srinivas Bayyavarapu 		}
6337c31335aSMaruthi Srinivas Bayyavarapu 		udelay(100);
6347c31335aSMaruthi Srinivas Bayyavarapu 	}
6357c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
6367c31335aSMaruthi Srinivas Bayyavarapu }
6377c31335aSMaruthi Srinivas Bayyavarapu 
6387c31335aSMaruthi Srinivas Bayyavarapu /* ACP DMA irq handler routine for playback, capture usecases */
6397c31335aSMaruthi Srinivas Bayyavarapu static irqreturn_t dma_irq_handler(int irq, void *arg)
6407c31335aSMaruthi Srinivas Bayyavarapu {
6417c31335aSMaruthi Srinivas Bayyavarapu 	u16 dscr_idx;
6427c31335aSMaruthi Srinivas Bayyavarapu 	u32 intr_flag, ext_intr_status;
6437c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *irq_data;
6447c31335aSMaruthi Srinivas Bayyavarapu 	void __iomem *acp_mmio;
6457c31335aSMaruthi Srinivas Bayyavarapu 	struct device *dev = arg;
6467c31335aSMaruthi Srinivas Bayyavarapu 	bool valid_irq = false;
6477c31335aSMaruthi Srinivas Bayyavarapu 
6487c31335aSMaruthi Srinivas Bayyavarapu 	irq_data = dev_get_drvdata(dev);
6497c31335aSMaruthi Srinivas Bayyavarapu 	acp_mmio = irq_data->acp_mmio;
6507c31335aSMaruthi Srinivas Bayyavarapu 
6517c31335aSMaruthi Srinivas Bayyavarapu 	ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6527c31335aSMaruthi Srinivas Bayyavarapu 	intr_flag = (((ext_intr_status &
6537c31335aSMaruthi Srinivas Bayyavarapu 		      ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
6547c31335aSMaruthi Srinivas Bayyavarapu 		     ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
6557c31335aSMaruthi Srinivas Bayyavarapu 
6567c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
6577c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
6587c31335aSMaruthi Srinivas Bayyavarapu 		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_13) ==
6597c31335aSMaruthi Srinivas Bayyavarapu 				PLAYBACK_START_DMA_DESCR_CH13)
6607c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = PLAYBACK_END_DMA_DESCR_CH12;
66131c45b3eSVijendar Mukunda 		else
66231c45b3eSVijendar Mukunda 			dscr_idx = PLAYBACK_START_DMA_DESCR_CH12;
6637c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, SYSRAM_TO_ACP_CH_NUM, dscr_idx,
6647c31335aSMaruthi Srinivas Bayyavarapu 				       1, 0);
6657c31335aSMaruthi Srinivas Bayyavarapu 		acp_dma_start(acp_mmio, SYSRAM_TO_ACP_CH_NUM, false);
6667c31335aSMaruthi Srinivas Bayyavarapu 
667e21358c4SMukunda, Vijendar 		snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
6687c31335aSMaruthi Srinivas Bayyavarapu 
6697c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
6707c31335aSMaruthi Srinivas Bayyavarapu 			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6717c31335aSMaruthi Srinivas Bayyavarapu 	}
6727c31335aSMaruthi Srinivas Bayyavarapu 
6737c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
6747c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
6757c31335aSMaruthi Srinivas Bayyavarapu 		if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_15) ==
6767c31335aSMaruthi Srinivas Bayyavarapu 				CAPTURE_START_DMA_DESCR_CH15)
6777c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
6787c31335aSMaruthi Srinivas Bayyavarapu 		else
6797c31335aSMaruthi Srinivas Bayyavarapu 			dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
6807c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
6817c31335aSMaruthi Srinivas Bayyavarapu 				       1, 0);
6827c31335aSMaruthi Srinivas Bayyavarapu 		acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);
6837c31335aSMaruthi Srinivas Bayyavarapu 
6847c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
6857c31335aSMaruthi Srinivas Bayyavarapu 			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6867c31335aSMaruthi Srinivas Bayyavarapu 	}
6877c31335aSMaruthi Srinivas Bayyavarapu 
6887c31335aSMaruthi Srinivas Bayyavarapu 	if ((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) != 0) {
6897c31335aSMaruthi Srinivas Bayyavarapu 		valid_irq = true;
690e21358c4SMukunda, Vijendar 		snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
6917c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) << 16,
6927c31335aSMaruthi Srinivas Bayyavarapu 			      acp_mmio, mmACP_EXTERNAL_INTR_STAT);
6937c31335aSMaruthi Srinivas Bayyavarapu 	}
6947c31335aSMaruthi Srinivas Bayyavarapu 
6957c31335aSMaruthi Srinivas Bayyavarapu 	if (valid_irq)
6967c31335aSMaruthi Srinivas Bayyavarapu 		return IRQ_HANDLED;
6977c31335aSMaruthi Srinivas Bayyavarapu 	else
6987c31335aSMaruthi Srinivas Bayyavarapu 		return IRQ_NONE;
6997c31335aSMaruthi Srinivas Bayyavarapu }
7007c31335aSMaruthi Srinivas Bayyavarapu 
7017c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_open(struct snd_pcm_substream *substream)
7027c31335aSMaruthi Srinivas Bayyavarapu {
703c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
7047c31335aSMaruthi Srinivas Bayyavarapu 	int ret = 0;
7057c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
7067c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
707*13838c11SMukunda, Vijendar 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
708*13838c11SMukunda, Vijendar 								    DRV_NAME);
709a1042a42SKuninori Morimoto 	struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
7107c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *adata =
7117c31335aSMaruthi Srinivas Bayyavarapu 		kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
712*13838c11SMukunda, Vijendar 	if (!adata)
7137c31335aSMaruthi Srinivas Bayyavarapu 		return -ENOMEM;
7147c31335aSMaruthi Srinivas Bayyavarapu 
7159c7d6fabSVijendar Mukunda 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
7169c7d6fabSVijendar Mukunda 		switch (intr_data->asic_type) {
7179c7d6fabSVijendar Mukunda 		case CHIP_STONEY:
7189c7d6fabSVijendar Mukunda 			runtime->hw = acp_st_pcm_hardware_playback;
7199c7d6fabSVijendar Mukunda 			break;
7209c7d6fabSVijendar Mukunda 		default:
7217c31335aSMaruthi Srinivas Bayyavarapu 			runtime->hw = acp_pcm_hardware_playback;
7229c7d6fabSVijendar Mukunda 		}
7239c7d6fabSVijendar Mukunda 	} else {
7249c7d6fabSVijendar Mukunda 		switch (intr_data->asic_type) {
7259c7d6fabSVijendar Mukunda 		case CHIP_STONEY:
7269c7d6fabSVijendar Mukunda 			runtime->hw = acp_st_pcm_hardware_capture;
7279c7d6fabSVijendar Mukunda 			break;
7289c7d6fabSVijendar Mukunda 		default:
7297c31335aSMaruthi Srinivas Bayyavarapu 			runtime->hw = acp_pcm_hardware_capture;
7309c7d6fabSVijendar Mukunda 		}
7319c7d6fabSVijendar Mukunda 	}
7327c31335aSMaruthi Srinivas Bayyavarapu 
7337c31335aSMaruthi Srinivas Bayyavarapu 	ret = snd_pcm_hw_constraint_integer(runtime,
7347c31335aSMaruthi Srinivas Bayyavarapu 					    SNDRV_PCM_HW_PARAM_PERIODS);
7357c31335aSMaruthi Srinivas Bayyavarapu 	if (ret < 0) {
736a1042a42SKuninori Morimoto 		dev_err(component->dev, "set integer constraint failed\n");
737cde6bcd5SDan Carpenter 		kfree(adata);
7387c31335aSMaruthi Srinivas Bayyavarapu 		return ret;
7397c31335aSMaruthi Srinivas Bayyavarapu 	}
7407c31335aSMaruthi Srinivas Bayyavarapu 
7417c31335aSMaruthi Srinivas Bayyavarapu 	adata->acp_mmio = intr_data->acp_mmio;
7427c31335aSMaruthi Srinivas Bayyavarapu 	runtime->private_data = adata;
7437c31335aSMaruthi Srinivas Bayyavarapu 
744*13838c11SMukunda, Vijendar 	/*
745*13838c11SMukunda, Vijendar 	 * Enable ACP irq, when neither playback or capture streams are
7467c31335aSMaruthi Srinivas Bayyavarapu 	 * active by the time when a new stream is being opened.
7477c31335aSMaruthi Srinivas Bayyavarapu 	 * This enablement is not required for another stream, if current
7487c31335aSMaruthi Srinivas Bayyavarapu 	 * stream is not closed
7497c31335aSMaruthi Srinivas Bayyavarapu 	 */
750e21358c4SMukunda, Vijendar 	if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream)
7517c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
7527c31335aSMaruthi Srinivas Bayyavarapu 
753c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
754e21358c4SMukunda, Vijendar 		intr_data->play_i2ssp_stream = substream;
755*13838c11SMukunda, Vijendar 		/*
756*13838c11SMukunda, Vijendar 		 * For Stoney, Memory gating is disabled,i.e SRAM Banks
757607b39efSVijendar Mukunda 		 * won't be turned off. The default state for SRAM banks is ON.
758607b39efSVijendar Mukunda 		 * Setting SRAM bank state code skipped for STONEY platform.
759607b39efSVijendar Mukunda 		 */
760607b39efSVijendar Mukunda 		if (intr_data->asic_type != CHIP_STONEY) {
761c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 1; bank <= 4; bank++)
762607b39efSVijendar Mukunda 				acp_set_sram_bank_state(intr_data->acp_mmio,
763607b39efSVijendar Mukunda 							bank, true);
764607b39efSVijendar Mukunda 		}
765c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else {
766e21358c4SMukunda, Vijendar 		intr_data->capture_i2ssp_stream = substream;
767607b39efSVijendar Mukunda 		if (intr_data->asic_type != CHIP_STONEY) {
768c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 5; bank <= 8; bank++)
769607b39efSVijendar Mukunda 				acp_set_sram_bank_state(intr_data->acp_mmio,
770607b39efSVijendar Mukunda 							bank, true);
771607b39efSVijendar Mukunda 		}
772c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
7737c31335aSMaruthi Srinivas Bayyavarapu 
7747c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
7757c31335aSMaruthi Srinivas Bayyavarapu }
7767c31335aSMaruthi Srinivas Bayyavarapu 
7777c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_hw_params(struct snd_pcm_substream *substream,
7787c31335aSMaruthi Srinivas Bayyavarapu 			     struct snd_pcm_hw_params *params)
7797c31335aSMaruthi Srinivas Bayyavarapu {
7807c31335aSMaruthi Srinivas Bayyavarapu 	int status;
7817c31335aSMaruthi Srinivas Bayyavarapu 	uint64_t size;
782a37d48e3SVijendar Mukunda 	u32 val = 0;
7837c31335aSMaruthi Srinivas Bayyavarapu 	struct page *pg;
7847c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime;
7857c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd;
786aac89748SVijendar Mukunda 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
787*13838c11SMukunda, Vijendar 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
788*13838c11SMukunda, Vijendar 								    DRV_NAME);
789a1042a42SKuninori Morimoto 	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
7907c31335aSMaruthi Srinivas Bayyavarapu 
7917c31335aSMaruthi Srinivas Bayyavarapu 	runtime = substream->runtime;
7927c31335aSMaruthi Srinivas Bayyavarapu 	rtd = runtime->private_data;
7937c31335aSMaruthi Srinivas Bayyavarapu 
7947c31335aSMaruthi Srinivas Bayyavarapu 	if (WARN_ON(!rtd))
7957c31335aSMaruthi Srinivas Bayyavarapu 		return -EINVAL;
7967c31335aSMaruthi Srinivas Bayyavarapu 
797a37d48e3SVijendar Mukunda 	if (adata->asic_type == CHIP_STONEY) {
798*13838c11SMukunda, Vijendar 		val = acp_reg_read(adata->acp_mmio,
799*13838c11SMukunda, Vijendar 				   mmACP_I2S_16BIT_RESOLUTION_EN);
800a37d48e3SVijendar Mukunda 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
801a37d48e3SVijendar Mukunda 			val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
802a37d48e3SVijendar Mukunda 		else
803a37d48e3SVijendar Mukunda 			val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
804*13838c11SMukunda, Vijendar 		acp_reg_write(val, adata->acp_mmio,
805*13838c11SMukunda, Vijendar 			      mmACP_I2S_16BIT_RESOLUTION_EN);
806a37d48e3SVijendar Mukunda 	}
8077c31335aSMaruthi Srinivas Bayyavarapu 	size = params_buffer_bytes(params);
8087c31335aSMaruthi Srinivas Bayyavarapu 	status = snd_pcm_lib_malloc_pages(substream, size);
8097c31335aSMaruthi Srinivas Bayyavarapu 	if (status < 0)
8107c31335aSMaruthi Srinivas Bayyavarapu 		return status;
8117c31335aSMaruthi Srinivas Bayyavarapu 
8127c31335aSMaruthi Srinivas Bayyavarapu 	memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
8137c31335aSMaruthi Srinivas Bayyavarapu 	pg = virt_to_page(substream->dma_buffer.area);
8147c31335aSMaruthi Srinivas Bayyavarapu 
815*13838c11SMukunda, Vijendar 	if (pg) {
816c36d9b3fSMaruthi Srinivas Bayyavarapu 		acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
8177c31335aSMaruthi Srinivas Bayyavarapu 		/* Save for runtime private data */
8187c31335aSMaruthi Srinivas Bayyavarapu 		rtd->pg = pg;
8197c31335aSMaruthi Srinivas Bayyavarapu 		rtd->order = get_order(size);
8207c31335aSMaruthi Srinivas Bayyavarapu 
8217c31335aSMaruthi Srinivas Bayyavarapu 		/* Fill the page table entries in ACP SRAM */
8227c31335aSMaruthi Srinivas Bayyavarapu 		rtd->pg = pg;
8237c31335aSMaruthi Srinivas Bayyavarapu 		rtd->size = size;
8247c31335aSMaruthi Srinivas Bayyavarapu 		rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
8257c31335aSMaruthi Srinivas Bayyavarapu 		rtd->direction = substream->stream;
8267c31335aSMaruthi Srinivas Bayyavarapu 
827aac89748SVijendar Mukunda 		config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
8287c31335aSMaruthi Srinivas Bayyavarapu 		status = 0;
8297c31335aSMaruthi Srinivas Bayyavarapu 	} else {
8307c31335aSMaruthi Srinivas Bayyavarapu 		status = -ENOMEM;
8317c31335aSMaruthi Srinivas Bayyavarapu 	}
8327c31335aSMaruthi Srinivas Bayyavarapu 	return status;
8337c31335aSMaruthi Srinivas Bayyavarapu }
8347c31335aSMaruthi Srinivas Bayyavarapu 
8357c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_hw_free(struct snd_pcm_substream *substream)
8367c31335aSMaruthi Srinivas Bayyavarapu {
8377c31335aSMaruthi Srinivas Bayyavarapu 	return snd_pcm_lib_free_pages(substream);
8387c31335aSMaruthi Srinivas Bayyavarapu }
8397c31335aSMaruthi Srinivas Bayyavarapu 
84061add814SVijendar Mukunda static u64 acp_get_byte_count(void __iomem *acp_mmio, int stream)
84161add814SVijendar Mukunda {
84261add814SVijendar Mukunda 	union acp_dma_count playback_dma_count;
84361add814SVijendar Mukunda 	union acp_dma_count capture_dma_count;
84461add814SVijendar Mukunda 	u64 bytescount = 0;
84561add814SVijendar Mukunda 
84661add814SVijendar Mukunda 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
84761add814SVijendar Mukunda 		playback_dma_count.bcount.high = acp_reg_read(acp_mmio,
84861add814SVijendar Mukunda 					mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH);
84961add814SVijendar Mukunda 		playback_dma_count.bcount.low  = acp_reg_read(acp_mmio,
85061add814SVijendar Mukunda 					mmACP_I2S_TRANSMIT_BYTE_CNT_LOW);
85161add814SVijendar Mukunda 		bytescount = playback_dma_count.bytescount;
85261add814SVijendar Mukunda 	} else {
85361add814SVijendar Mukunda 		capture_dma_count.bcount.high = acp_reg_read(acp_mmio,
85461add814SVijendar Mukunda 					mmACP_I2S_RECEIVED_BYTE_CNT_HIGH);
85561add814SVijendar Mukunda 		capture_dma_count.bcount.low  = acp_reg_read(acp_mmio,
85661add814SVijendar Mukunda 					mmACP_I2S_RECEIVED_BYTE_CNT_LOW);
85761add814SVijendar Mukunda 		bytescount = capture_dma_count.bytescount;
85861add814SVijendar Mukunda 	}
85961add814SVijendar Mukunda 	return bytescount;
86061add814SVijendar Mukunda }
86161add814SVijendar Mukunda 
8627c31335aSMaruthi Srinivas Bayyavarapu static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
8637c31335aSMaruthi Srinivas Bayyavarapu {
86461add814SVijendar Mukunda 	u32 buffersize;
8657c31335aSMaruthi Srinivas Bayyavarapu 	u32 pos = 0;
86661add814SVijendar Mukunda 	u64 bytescount = 0;
8677c31335aSMaruthi Srinivas Bayyavarapu 
8687c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
8697c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
8707c31335aSMaruthi Srinivas Bayyavarapu 
8717afa535eSMukunda, Vijendar 	if (!rtd)
8727afa535eSMukunda, Vijendar 		return -EINVAL;
8737afa535eSMukunda, Vijendar 
87461add814SVijendar Mukunda 	buffersize = frames_to_bytes(runtime, runtime->buffer_size);
87561add814SVijendar Mukunda 	bytescount = acp_get_byte_count(rtd->acp_mmio, substream->stream);
87661add814SVijendar Mukunda 
8777c31335aSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
878e21358c4SMukunda, Vijendar 		if (bytescount > rtd->i2ssp_renderbytescount)
879e21358c4SMukunda, Vijendar 			bytescount = bytescount - rtd->i2ssp_renderbytescount;
8807c31335aSMaruthi Srinivas Bayyavarapu 	} else {
881e21358c4SMukunda, Vijendar 		if (bytescount > rtd->i2ssp_capturebytescount)
882e21358c4SMukunda, Vijendar 			bytescount = bytescount - rtd->i2ssp_capturebytescount;
8837c31335aSMaruthi Srinivas Bayyavarapu 	}
8847db08b2cSGuenter Roeck 	pos = do_div(bytescount, buffersize);
8857c31335aSMaruthi Srinivas Bayyavarapu 	return bytes_to_frames(runtime, pos);
8867c31335aSMaruthi Srinivas Bayyavarapu }
8877c31335aSMaruthi Srinivas Bayyavarapu 
8887c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_mmap(struct snd_pcm_substream *substream,
8897c31335aSMaruthi Srinivas Bayyavarapu 			struct vm_area_struct *vma)
8907c31335aSMaruthi Srinivas Bayyavarapu {
8917c31335aSMaruthi Srinivas Bayyavarapu 	return snd_pcm_lib_default_mmap(substream, vma);
8927c31335aSMaruthi Srinivas Bayyavarapu }
8937c31335aSMaruthi Srinivas Bayyavarapu 
8947c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_prepare(struct snd_pcm_substream *substream)
8957c31335aSMaruthi Srinivas Bayyavarapu {
8967c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
8977c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
8987c31335aSMaruthi Srinivas Bayyavarapu 
8997afa535eSMukunda, Vijendar 	if (!rtd)
9007afa535eSMukunda, Vijendar 		return -EINVAL;
9017c31335aSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
9027c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, SYSRAM_TO_ACP_CH_NUM,
9037c31335aSMaruthi Srinivas Bayyavarapu 				       PLAYBACK_START_DMA_DESCR_CH12,
9047c31335aSMaruthi Srinivas Bayyavarapu 				       NUM_DSCRS_PER_CHANNEL, 0);
9057c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, ACP_TO_I2S_DMA_CH_NUM,
9067c31335aSMaruthi Srinivas Bayyavarapu 				       PLAYBACK_START_DMA_DESCR_CH13,
9077c31335aSMaruthi Srinivas Bayyavarapu 				       NUM_DSCRS_PER_CHANNEL, 0);
9087c31335aSMaruthi Srinivas Bayyavarapu 	} else {
9097c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, ACP_TO_SYSRAM_CH_NUM,
9107c31335aSMaruthi Srinivas Bayyavarapu 				       CAPTURE_START_DMA_DESCR_CH14,
9117c31335aSMaruthi Srinivas Bayyavarapu 				       NUM_DSCRS_PER_CHANNEL, 0);
9127c31335aSMaruthi Srinivas Bayyavarapu 		config_acp_dma_channel(rtd->acp_mmio, I2S_TO_ACP_DMA_CH_NUM,
9137c31335aSMaruthi Srinivas Bayyavarapu 				       CAPTURE_START_DMA_DESCR_CH15,
9147c31335aSMaruthi Srinivas Bayyavarapu 				       NUM_DSCRS_PER_CHANNEL, 0);
9157c31335aSMaruthi Srinivas Bayyavarapu 	}
9167c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
9177c31335aSMaruthi Srinivas Bayyavarapu }
9187c31335aSMaruthi Srinivas Bayyavarapu 
9197c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
9207c31335aSMaruthi Srinivas Bayyavarapu {
9217c31335aSMaruthi Srinivas Bayyavarapu 	int ret;
92231c45b3eSVijendar Mukunda 	u32 loops = 4000;
92361add814SVijendar Mukunda 	u64 bytescount = 0;
9247c31335aSMaruthi Srinivas Bayyavarapu 
9257c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
9267c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
9277c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
928*13838c11SMukunda, Vijendar 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
929*13838c11SMukunda, Vijendar 								    DRV_NAME);
9307c31335aSMaruthi Srinivas Bayyavarapu 
9317c31335aSMaruthi Srinivas Bayyavarapu 	if (!rtd)
9327c31335aSMaruthi Srinivas Bayyavarapu 		return -EINVAL;
9337c31335aSMaruthi Srinivas Bayyavarapu 	switch (cmd) {
9347c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_START:
9357c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
9367c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_RESUME:
93761add814SVijendar Mukunda 		bytescount = acp_get_byte_count(rtd->acp_mmio,
93861add814SVijendar Mukunda 						substream->stream);
9397c31335aSMaruthi Srinivas Bayyavarapu 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
940e21358c4SMukunda, Vijendar 			if (rtd->i2ssp_renderbytescount == 0)
941e21358c4SMukunda, Vijendar 				rtd->i2ssp_renderbytescount = bytescount;
9427c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
9437c31335aSMaruthi Srinivas Bayyavarapu 				      SYSRAM_TO_ACP_CH_NUM, false);
9447c31335aSMaruthi Srinivas Bayyavarapu 			while (acp_reg_read(rtd->acp_mmio, mmACP_DMA_CH_STS) &
9457c31335aSMaruthi Srinivas Bayyavarapu 						BIT(SYSRAM_TO_ACP_CH_NUM)) {
9467c31335aSMaruthi Srinivas Bayyavarapu 				if (!loops--) {
947a1042a42SKuninori Morimoto 					dev_err(component->dev,
9487c31335aSMaruthi Srinivas Bayyavarapu 						"acp dma start timeout\n");
9497c31335aSMaruthi Srinivas Bayyavarapu 					return -ETIMEDOUT;
9507c31335aSMaruthi Srinivas Bayyavarapu 				}
9517c31335aSMaruthi Srinivas Bayyavarapu 				cpu_relax();
9527c31335aSMaruthi Srinivas Bayyavarapu 			}
9537c31335aSMaruthi Srinivas Bayyavarapu 
9547c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
9557c31335aSMaruthi Srinivas Bayyavarapu 				      ACP_TO_I2S_DMA_CH_NUM, true);
9567c31335aSMaruthi Srinivas Bayyavarapu 
9577c31335aSMaruthi Srinivas Bayyavarapu 		} else {
958e21358c4SMukunda, Vijendar 			if (rtd->i2ssp_capturebytescount == 0)
959e21358c4SMukunda, Vijendar 				rtd->i2ssp_capturebytescount = bytescount;
9607c31335aSMaruthi Srinivas Bayyavarapu 			acp_dma_start(rtd->acp_mmio,
9617c31335aSMaruthi Srinivas Bayyavarapu 				      I2S_TO_ACP_DMA_CH_NUM, true);
9627c31335aSMaruthi Srinivas Bayyavarapu 		}
9637c31335aSMaruthi Srinivas Bayyavarapu 		ret = 0;
9647c31335aSMaruthi Srinivas Bayyavarapu 		break;
9657c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_STOP:
9667c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
9677c31335aSMaruthi Srinivas Bayyavarapu 	case SNDRV_PCM_TRIGGER_SUSPEND:
968*13838c11SMukunda, Vijendar 		/*
969*13838c11SMukunda, Vijendar 		 * Need to stop only circular DMA channels :
9707c31335aSMaruthi Srinivas Bayyavarapu 		 * ACP_TO_I2S_DMA_CH_NUM / I2S_TO_ACP_DMA_CH_NUM. Non-circular
9717c31335aSMaruthi Srinivas Bayyavarapu 		 * channels will stopped automatically after its transfer
9727c31335aSMaruthi Srinivas Bayyavarapu 		 * completes : SYSRAM_TO_ACP_CH_NUM / ACP_TO_SYSRAM_CH_NUM
9737c31335aSMaruthi Srinivas Bayyavarapu 		 */
97461add814SVijendar Mukunda 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
9757c31335aSMaruthi Srinivas Bayyavarapu 			ret = acp_dma_stop(rtd->acp_mmio,
97617aa9521SVijendar Mukunda 					   SYSRAM_TO_ACP_CH_NUM);
97717aa9521SVijendar Mukunda 			ret = acp_dma_stop(rtd->acp_mmio,
9787c31335aSMaruthi Srinivas Bayyavarapu 					   ACP_TO_I2S_DMA_CH_NUM);
979e21358c4SMukunda, Vijendar 			rtd->i2ssp_renderbytescount = 0;
98061add814SVijendar Mukunda 		} else {
9817c31335aSMaruthi Srinivas Bayyavarapu 			ret = acp_dma_stop(rtd->acp_mmio,
9827c31335aSMaruthi Srinivas Bayyavarapu 					   I2S_TO_ACP_DMA_CH_NUM);
98317aa9521SVijendar Mukunda 			ret = acp_dma_stop(rtd->acp_mmio,
98417aa9521SVijendar Mukunda 					   ACP_TO_SYSRAM_CH_NUM);
985e21358c4SMukunda, Vijendar 			rtd->i2ssp_capturebytescount = 0;
98661add814SVijendar Mukunda 		}
9877c31335aSMaruthi Srinivas Bayyavarapu 		break;
9887c31335aSMaruthi Srinivas Bayyavarapu 	default:
9897c31335aSMaruthi Srinivas Bayyavarapu 		ret = -EINVAL;
9907c31335aSMaruthi Srinivas Bayyavarapu 	}
9917c31335aSMaruthi Srinivas Bayyavarapu 	return ret;
9927c31335aSMaruthi Srinivas Bayyavarapu }
9937c31335aSMaruthi Srinivas Bayyavarapu 
9947c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
9957c31335aSMaruthi Srinivas Bayyavarapu {
9969c7d6fabSVijendar Mukunda 	int ret;
997*13838c11SMukunda, Vijendar 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
998*13838c11SMukunda, Vijendar 								    DRV_NAME);
999a1042a42SKuninori Morimoto 	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
10009c7d6fabSVijendar Mukunda 
10019c7d6fabSVijendar Mukunda 	switch (adata->asic_type) {
10029c7d6fabSVijendar Mukunda 	case CHIP_STONEY:
10039c7d6fabSVijendar Mukunda 		ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
10049c7d6fabSVijendar Mukunda 							    SNDRV_DMA_TYPE_DEV,
10059c7d6fabSVijendar Mukunda 							    NULL, ST_MIN_BUFFER,
10069c7d6fabSVijendar Mukunda 							    ST_MAX_BUFFER);
10079c7d6fabSVijendar Mukunda 		break;
10089c7d6fabSVijendar Mukunda 	default:
10099c7d6fabSVijendar Mukunda 		ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
10107c31335aSMaruthi Srinivas Bayyavarapu 							    SNDRV_DMA_TYPE_DEV,
10117c31335aSMaruthi Srinivas Bayyavarapu 							    NULL, MIN_BUFFER,
10127c31335aSMaruthi Srinivas Bayyavarapu 							    MAX_BUFFER);
10139c7d6fabSVijendar Mukunda 		break;
10149c7d6fabSVijendar Mukunda 	}
10159c7d6fabSVijendar Mukunda 	if (ret < 0)
1016a1042a42SKuninori Morimoto 		dev_err(component->dev,
10179c7d6fabSVijendar Mukunda 			"buffer preallocation failer error:%d\n", ret);
10189c7d6fabSVijendar Mukunda 	return ret;
10197c31335aSMaruthi Srinivas Bayyavarapu }
10207c31335aSMaruthi Srinivas Bayyavarapu 
10217c31335aSMaruthi Srinivas Bayyavarapu static int acp_dma_close(struct snd_pcm_substream *substream)
10227c31335aSMaruthi Srinivas Bayyavarapu {
1023c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
10247c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_pcm_runtime *runtime = substream->runtime;
10257c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_substream_data *rtd = runtime->private_data;
10267c31335aSMaruthi Srinivas Bayyavarapu 	struct snd_soc_pcm_runtime *prtd = substream->private_data;
1027*13838c11SMukunda, Vijendar 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
1028*13838c11SMukunda, Vijendar 								    DRV_NAME);
1029a1042a42SKuninori Morimoto 	struct audio_drv_data *adata = dev_get_drvdata(component->dev);
10307c31335aSMaruthi Srinivas Bayyavarapu 
10317c31335aSMaruthi Srinivas Bayyavarapu 	kfree(rtd);
10327c31335aSMaruthi Srinivas Bayyavarapu 
1033c36d9b3fSMaruthi Srinivas Bayyavarapu 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1034e21358c4SMukunda, Vijendar 		adata->play_i2ssp_stream = NULL;
1035*13838c11SMukunda, Vijendar 		/*
1036*13838c11SMukunda, Vijendar 		 * For Stoney, Memory gating is disabled,i.e SRAM Banks
1037607b39efSVijendar Mukunda 		 * won't be turned off. The default state for SRAM banks is ON.
1038607b39efSVijendar Mukunda 		 * Setting SRAM bank state code skipped for STONEY platform.
1039607b39efSVijendar Mukunda 		 * added condition checks for Carrizo platform only
1040607b39efSVijendar Mukunda 		 */
1041607b39efSVijendar Mukunda 		if (adata->asic_type != CHIP_STONEY) {
1042c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 1; bank <= 4; bank++)
1043c36d9b3fSMaruthi Srinivas Bayyavarapu 				acp_set_sram_bank_state(adata->acp_mmio, bank,
1044c36d9b3fSMaruthi Srinivas Bayyavarapu 							false);
1045607b39efSVijendar Mukunda 		}
1046c36d9b3fSMaruthi Srinivas Bayyavarapu 	} else  {
1047e21358c4SMukunda, Vijendar 		adata->capture_i2ssp_stream = NULL;
1048607b39efSVijendar Mukunda 		if (adata->asic_type != CHIP_STONEY) {
1049c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 5; bank <= 8; bank++)
1050c36d9b3fSMaruthi Srinivas Bayyavarapu 				acp_set_sram_bank_state(adata->acp_mmio, bank,
1051c36d9b3fSMaruthi Srinivas Bayyavarapu 							false);
1052c36d9b3fSMaruthi Srinivas Bayyavarapu 		}
1053607b39efSVijendar Mukunda 	}
10547c31335aSMaruthi Srinivas Bayyavarapu 
1055*13838c11SMukunda, Vijendar 	/*
1056*13838c11SMukunda, Vijendar 	 * Disable ACP irq, when the current stream is being closed and
10577c31335aSMaruthi Srinivas Bayyavarapu 	 * another stream is also not active.
10587c31335aSMaruthi Srinivas Bayyavarapu 	 */
1059e21358c4SMukunda, Vijendar 	if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream)
10607c31335aSMaruthi Srinivas Bayyavarapu 		acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
10617c31335aSMaruthi Srinivas Bayyavarapu 
10627c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
10637c31335aSMaruthi Srinivas Bayyavarapu }
10647c31335aSMaruthi Srinivas Bayyavarapu 
1065115c7254SJulia Lawall static const struct snd_pcm_ops acp_dma_ops = {
10667c31335aSMaruthi Srinivas Bayyavarapu 	.open = acp_dma_open,
10677c31335aSMaruthi Srinivas Bayyavarapu 	.close = acp_dma_close,
10687c31335aSMaruthi Srinivas Bayyavarapu 	.ioctl = snd_pcm_lib_ioctl,
10697c31335aSMaruthi Srinivas Bayyavarapu 	.hw_params = acp_dma_hw_params,
10707c31335aSMaruthi Srinivas Bayyavarapu 	.hw_free = acp_dma_hw_free,
10717c31335aSMaruthi Srinivas Bayyavarapu 	.trigger = acp_dma_trigger,
10727c31335aSMaruthi Srinivas Bayyavarapu 	.pointer = acp_dma_pointer,
10737c31335aSMaruthi Srinivas Bayyavarapu 	.mmap = acp_dma_mmap,
10747c31335aSMaruthi Srinivas Bayyavarapu 	.prepare = acp_dma_prepare,
10757c31335aSMaruthi Srinivas Bayyavarapu };
10767c31335aSMaruthi Srinivas Bayyavarapu 
1077*13838c11SMukunda, Vijendar static const struct snd_soc_component_driver acp_asoc_platform = {
1078a1042a42SKuninori Morimoto 	.name = DRV_NAME,
10797c31335aSMaruthi Srinivas Bayyavarapu 	.ops = &acp_dma_ops,
10807c31335aSMaruthi Srinivas Bayyavarapu 	.pcm_new = acp_dma_new,
10817c31335aSMaruthi Srinivas Bayyavarapu };
10827c31335aSMaruthi Srinivas Bayyavarapu 
10837c31335aSMaruthi Srinivas Bayyavarapu static int acp_audio_probe(struct platform_device *pdev)
10847c31335aSMaruthi Srinivas Bayyavarapu {
10857c31335aSMaruthi Srinivas Bayyavarapu 	int status;
10867c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *audio_drv_data;
10877c31335aSMaruthi Srinivas Bayyavarapu 	struct resource *res;
1088a1b16aaaSVijendar Mukunda 	const u32 *pdata = pdev->dev.platform_data;
10897c31335aSMaruthi Srinivas Bayyavarapu 
1090fdaa4511SGuenter Roeck 	if (!pdata) {
1091fdaa4511SGuenter Roeck 		dev_err(&pdev->dev, "Missing platform data\n");
1092fdaa4511SGuenter Roeck 		return -ENODEV;
1093fdaa4511SGuenter Roeck 	}
1094fdaa4511SGuenter Roeck 
10957c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
10967c31335aSMaruthi Srinivas Bayyavarapu 				      GFP_KERNEL);
1097*13838c11SMukunda, Vijendar 	if (!audio_drv_data)
10987c31335aSMaruthi Srinivas Bayyavarapu 		return -ENOMEM;
10997c31335aSMaruthi Srinivas Bayyavarapu 
11007c31335aSMaruthi Srinivas Bayyavarapu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11017c31335aSMaruthi Srinivas Bayyavarapu 	audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
1102fdaa4511SGuenter Roeck 	if (IS_ERR(audio_drv_data->acp_mmio))
1103fdaa4511SGuenter Roeck 		return PTR_ERR(audio_drv_data->acp_mmio);
11047c31335aSMaruthi Srinivas Bayyavarapu 
1105*13838c11SMukunda, Vijendar 	/*
1106*13838c11SMukunda, Vijendar 	 * The following members gets populated in device 'open'
11077c31335aSMaruthi Srinivas Bayyavarapu 	 * function. Till then interrupts are disabled in 'acp_init'
11087c31335aSMaruthi Srinivas Bayyavarapu 	 * and device doesn't generate any interrupts.
11097c31335aSMaruthi Srinivas Bayyavarapu 	 */
11107c31335aSMaruthi Srinivas Bayyavarapu 
1111e21358c4SMukunda, Vijendar 	audio_drv_data->play_i2ssp_stream = NULL;
1112e21358c4SMukunda, Vijendar 	audio_drv_data->capture_i2ssp_stream = NULL;
1113e21358c4SMukunda, Vijendar 
1114a1b16aaaSVijendar Mukunda 	audio_drv_data->asic_type =  *pdata;
11157c31335aSMaruthi Srinivas Bayyavarapu 
11167c31335aSMaruthi Srinivas Bayyavarapu 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
11177c31335aSMaruthi Srinivas Bayyavarapu 	if (!res) {
11187c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
11197c31335aSMaruthi Srinivas Bayyavarapu 		return -ENODEV;
11207c31335aSMaruthi Srinivas Bayyavarapu 	}
11217c31335aSMaruthi Srinivas Bayyavarapu 
11227c31335aSMaruthi Srinivas Bayyavarapu 	status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
11237c31335aSMaruthi Srinivas Bayyavarapu 				  0, "ACP_IRQ", &pdev->dev);
11247c31335aSMaruthi Srinivas Bayyavarapu 	if (status) {
11257c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "ACP IRQ request failed\n");
11267c31335aSMaruthi Srinivas Bayyavarapu 		return status;
11277c31335aSMaruthi Srinivas Bayyavarapu 	}
11287c31335aSMaruthi Srinivas Bayyavarapu 
11297c31335aSMaruthi Srinivas Bayyavarapu 	dev_set_drvdata(&pdev->dev, audio_drv_data);
11307c31335aSMaruthi Srinivas Bayyavarapu 
11317c31335aSMaruthi Srinivas Bayyavarapu 	/* Initialize the ACP */
11327afa535eSMukunda, Vijendar 	status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
11337afa535eSMukunda, Vijendar 	if (status) {
11347afa535eSMukunda, Vijendar 		dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
11357afa535eSMukunda, Vijendar 		return status;
11367afa535eSMukunda, Vijendar 	}
11377c31335aSMaruthi Srinivas Bayyavarapu 
1138a1042a42SKuninori Morimoto 	status = devm_snd_soc_register_component(&pdev->dev,
1139a1042a42SKuninori Morimoto 						 &acp_asoc_platform, NULL, 0);
11407c31335aSMaruthi Srinivas Bayyavarapu 	if (status != 0) {
11417c31335aSMaruthi Srinivas Bayyavarapu 		dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
11427c31335aSMaruthi Srinivas Bayyavarapu 		return status;
11437c31335aSMaruthi Srinivas Bayyavarapu 	}
11447c31335aSMaruthi Srinivas Bayyavarapu 
11451927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
11461927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_use_autosuspend(&pdev->dev);
11471927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_enable(&pdev->dev);
11481927da93SMaruthi Srinivas Bayyavarapu 
11497c31335aSMaruthi Srinivas Bayyavarapu 	return status;
11507c31335aSMaruthi Srinivas Bayyavarapu }
11517c31335aSMaruthi Srinivas Bayyavarapu 
11527c31335aSMaruthi Srinivas Bayyavarapu static int acp_audio_remove(struct platform_device *pdev)
11537c31335aSMaruthi Srinivas Bayyavarapu {
11547afa535eSMukunda, Vijendar 	int status;
11557c31335aSMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
11567c31335aSMaruthi Srinivas Bayyavarapu 
11577afa535eSMukunda, Vijendar 	status = acp_deinit(adata->acp_mmio);
11587afa535eSMukunda, Vijendar 	if (status)
11597afa535eSMukunda, Vijendar 		dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
11601927da93SMaruthi Srinivas Bayyavarapu 	pm_runtime_disable(&pdev->dev);
11617c31335aSMaruthi Srinivas Bayyavarapu 
11627c31335aSMaruthi Srinivas Bayyavarapu 	return 0;
11637c31335aSMaruthi Srinivas Bayyavarapu }
11647c31335aSMaruthi Srinivas Bayyavarapu 
11651927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_resume(struct device *dev)
11661927da93SMaruthi Srinivas Bayyavarapu {
1167c36d9b3fSMaruthi Srinivas Bayyavarapu 	u16 bank;
11687afa535eSMukunda, Vijendar 	int status;
11691927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
11701927da93SMaruthi Srinivas Bayyavarapu 
11717afa535eSMukunda, Vijendar 	status = acp_init(adata->acp_mmio, adata->asic_type);
11727afa535eSMukunda, Vijendar 	if (status) {
11737afa535eSMukunda, Vijendar 		dev_err(dev, "ACP Init failed status:%d\n", status);
11747afa535eSMukunda, Vijendar 		return status;
11757afa535eSMukunda, Vijendar 	}
11761927da93SMaruthi Srinivas Bayyavarapu 
1177e21358c4SMukunda, Vijendar 	if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
1178*13838c11SMukunda, Vijendar 		/*
1179*13838c11SMukunda, Vijendar 		 * For Stoney, Memory gating is disabled,i.e SRAM Banks
1180607b39efSVijendar Mukunda 		 * won't be turned off. The default state for SRAM banks is ON.
1181607b39efSVijendar Mukunda 		 * Setting SRAM bank state code skipped for STONEY platform.
1182607b39efSVijendar Mukunda 		 */
1183607b39efSVijendar Mukunda 		if (adata->asic_type != CHIP_STONEY) {
1184c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 1; bank <= 4; bank++)
1185c36d9b3fSMaruthi Srinivas Bayyavarapu 				acp_set_sram_bank_state(adata->acp_mmio, bank,
1186c36d9b3fSMaruthi Srinivas Bayyavarapu 							true);
1187607b39efSVijendar Mukunda 		}
11881927da93SMaruthi Srinivas Bayyavarapu 		config_acp_dma(adata->acp_mmio,
1189e21358c4SMukunda, Vijendar 			       adata->play_i2ssp_stream->runtime->private_data,
1190aac89748SVijendar Mukunda 			       adata->asic_type);
1191c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
1192*13838c11SMukunda, Vijendar 	if (adata->capture_i2ssp_stream &&
1193*13838c11SMukunda, Vijendar 	    adata->capture_i2ssp_stream->runtime) {
1194607b39efSVijendar Mukunda 		if (adata->asic_type != CHIP_STONEY) {
1195c36d9b3fSMaruthi Srinivas Bayyavarapu 			for (bank = 5; bank <= 8; bank++)
1196c36d9b3fSMaruthi Srinivas Bayyavarapu 				acp_set_sram_bank_state(adata->acp_mmio, bank,
1197c36d9b3fSMaruthi Srinivas Bayyavarapu 							true);
1198607b39efSVijendar Mukunda 		}
11991927da93SMaruthi Srinivas Bayyavarapu 		config_acp_dma(adata->acp_mmio,
1200e21358c4SMukunda, Vijendar 			       adata->capture_i2ssp_stream->runtime->private_data,
1201aac89748SVijendar Mukunda 			       adata->asic_type);
1202c36d9b3fSMaruthi Srinivas Bayyavarapu 	}
12031927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
12041927da93SMaruthi Srinivas Bayyavarapu 	return 0;
12051927da93SMaruthi Srinivas Bayyavarapu }
12061927da93SMaruthi Srinivas Bayyavarapu 
12071927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_runtime_suspend(struct device *dev)
12081927da93SMaruthi Srinivas Bayyavarapu {
12097afa535eSMukunda, Vijendar 	int status;
12101927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
12111927da93SMaruthi Srinivas Bayyavarapu 
12127afa535eSMukunda, Vijendar 	status = acp_deinit(adata->acp_mmio);
12137afa535eSMukunda, Vijendar 	if (status)
12147afa535eSMukunda, Vijendar 		dev_err(dev, "ACP Deinit failed status:%d\n", status);
12151927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
12161927da93SMaruthi Srinivas Bayyavarapu 	return 0;
12171927da93SMaruthi Srinivas Bayyavarapu }
12181927da93SMaruthi Srinivas Bayyavarapu 
12191927da93SMaruthi Srinivas Bayyavarapu static int acp_pcm_runtime_resume(struct device *dev)
12201927da93SMaruthi Srinivas Bayyavarapu {
12217afa535eSMukunda, Vijendar 	int status;
12221927da93SMaruthi Srinivas Bayyavarapu 	struct audio_drv_data *adata = dev_get_drvdata(dev);
12231927da93SMaruthi Srinivas Bayyavarapu 
12247afa535eSMukunda, Vijendar 	status = acp_init(adata->acp_mmio, adata->asic_type);
12257afa535eSMukunda, Vijendar 	if (status) {
12267afa535eSMukunda, Vijendar 		dev_err(dev, "ACP Init failed status:%d\n", status);
12277afa535eSMukunda, Vijendar 		return status;
12287afa535eSMukunda, Vijendar 	}
12291927da93SMaruthi Srinivas Bayyavarapu 	acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
12301927da93SMaruthi Srinivas Bayyavarapu 	return 0;
12311927da93SMaruthi Srinivas Bayyavarapu }
12321927da93SMaruthi Srinivas Bayyavarapu 
12331927da93SMaruthi Srinivas Bayyavarapu static const struct dev_pm_ops acp_pm_ops = {
12341927da93SMaruthi Srinivas Bayyavarapu 	.resume = acp_pcm_resume,
12351927da93SMaruthi Srinivas Bayyavarapu 	.runtime_suspend = acp_pcm_runtime_suspend,
12361927da93SMaruthi Srinivas Bayyavarapu 	.runtime_resume = acp_pcm_runtime_resume,
12371927da93SMaruthi Srinivas Bayyavarapu };
12381927da93SMaruthi Srinivas Bayyavarapu 
12397c31335aSMaruthi Srinivas Bayyavarapu static struct platform_driver acp_dma_driver = {
12407c31335aSMaruthi Srinivas Bayyavarapu 	.probe = acp_audio_probe,
12417c31335aSMaruthi Srinivas Bayyavarapu 	.remove = acp_audio_remove,
12427c31335aSMaruthi Srinivas Bayyavarapu 	.driver = {
1243bdd2a858SAkshu Agrawal 		.name = DRV_NAME,
12441927da93SMaruthi Srinivas Bayyavarapu 		.pm = &acp_pm_ops,
12457c31335aSMaruthi Srinivas Bayyavarapu 	},
12467c31335aSMaruthi Srinivas Bayyavarapu };
12477c31335aSMaruthi Srinivas Bayyavarapu 
12487c31335aSMaruthi Srinivas Bayyavarapu module_platform_driver(acp_dma_driver);
12497c31335aSMaruthi Srinivas Bayyavarapu 
1250607b39efSVijendar Mukunda MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
12517c31335aSMaruthi Srinivas Bayyavarapu MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
12527c31335aSMaruthi Srinivas Bayyavarapu MODULE_DESCRIPTION("AMD ACP PCM Driver");
12537c31335aSMaruthi Srinivas Bayyavarapu MODULE_LICENSE("GPL v2");
1254bdd2a858SAkshu Agrawal MODULE_ALIAS("platform:"DRV_NAME);
1255