blob: e6d59f47ed00d169de3eeb22f92609c23678aea3 [file] [log] [blame]
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001/*
2 * AMD ALSA SoC PCM Driver for ACP 2.x
3 *
4 * Copyright 2014-2015 Advanced Micro Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/module.h>
17#include <linux/delay.h>
Guenter Roeck7cb1dc82016-01-11 02:41:05 -080018#include <linux/io.h>
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050019#include <linux/sizes.h>
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -050020#include <linux/pm_runtime.h>
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050021
22#include <sound/soc.h>
Vijendar Mukunda607b39e2017-10-18 12:13:57 -040023#include <drm/amd_asic_type.h>
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050024#include "acp.h"
25
Kuninori Morimotoa1042a42018-01-29 02:44:23 +000026#define DRV_NAME "acp_audio_dma"
27
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050028#define PLAYBACK_MIN_NUM_PERIODS 2
29#define PLAYBACK_MAX_NUM_PERIODS 2
30#define PLAYBACK_MAX_PERIOD_SIZE 16384
31#define PLAYBACK_MIN_PERIOD_SIZE 1024
32#define CAPTURE_MIN_NUM_PERIODS 2
33#define CAPTURE_MAX_NUM_PERIODS 2
34#define CAPTURE_MAX_PERIOD_SIZE 16384
35#define CAPTURE_MIN_PERIOD_SIZE 1024
36
37#define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
38#define MIN_BUFFER MAX_BUFFER
39
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -040040#define ST_PLAYBACK_MAX_PERIOD_SIZE 8192
41#define ST_CAPTURE_MAX_PERIOD_SIZE ST_PLAYBACK_MAX_PERIOD_SIZE
42#define ST_MAX_BUFFER (ST_PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
43#define ST_MIN_BUFFER ST_MAX_BUFFER
44
Akshu Agrawalbdd2a852017-11-08 12:24:02 -050045#define DRV_NAME "acp_audio_dma"
46
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050047static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
48 .info = SNDRV_PCM_INFO_INTERLEAVED |
49 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
50 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
51 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
52 .formats = SNDRV_PCM_FMTBIT_S16_LE |
53 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
54 .channels_min = 1,
55 .channels_max = 8,
56 .rates = SNDRV_PCM_RATE_8000_96000,
57 .rate_min = 8000,
58 .rate_max = 96000,
59 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
60 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
61 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
62 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
63 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
64};
65
66static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
67 .info = SNDRV_PCM_INFO_INTERLEAVED |
68 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
69 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
70 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
71 .formats = SNDRV_PCM_FMTBIT_S16_LE |
72 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
73 .channels_min = 1,
74 .channels_max = 2,
75 .rates = SNDRV_PCM_RATE_8000_48000,
76 .rate_min = 8000,
77 .rate_max = 48000,
78 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
79 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
80 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
81 .periods_min = CAPTURE_MIN_NUM_PERIODS,
82 .periods_max = CAPTURE_MAX_NUM_PERIODS,
83};
84
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -040085static const struct snd_pcm_hardware acp_st_pcm_hardware_playback = {
86 .info = SNDRV_PCM_INFO_INTERLEAVED |
87 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
88 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
89 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
90 .formats = SNDRV_PCM_FMTBIT_S16_LE |
91 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
92 .channels_min = 1,
93 .channels_max = 8,
94 .rates = SNDRV_PCM_RATE_8000_96000,
95 .rate_min = 8000,
96 .rate_max = 96000,
97 .buffer_bytes_max = ST_MAX_BUFFER,
98 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
99 .period_bytes_max = ST_PLAYBACK_MAX_PERIOD_SIZE,
100 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
101 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
102};
103
104static const struct snd_pcm_hardware acp_st_pcm_hardware_capture = {
105 .info = SNDRV_PCM_INFO_INTERLEAVED |
106 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
107 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
108 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
109 .formats = SNDRV_PCM_FMTBIT_S16_LE |
110 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
111 .channels_min = 1,
112 .channels_max = 2,
113 .rates = SNDRV_PCM_RATE_8000_48000,
114 .rate_min = 8000,
115 .rate_max = 48000,
116 .buffer_bytes_max = ST_MAX_BUFFER,
117 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
118 .period_bytes_max = ST_CAPTURE_MAX_PERIOD_SIZE,
119 .periods_min = CAPTURE_MIN_NUM_PERIODS,
120 .periods_max = CAPTURE_MAX_NUM_PERIODS,
121};
122
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500123static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
124{
125 return readl(acp_mmio + (reg * 4));
126}
127
128static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
129{
130 writel(val, acp_mmio + (reg * 4));
131}
132
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530133/*
134 * Configure a given dma channel parameters - enable/disable,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500135 * number of descriptors, priority
136 */
137static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
138 u16 dscr_strt_idx, u16 num_dscrs,
139 enum acp_dma_priority_level priority_level)
140{
141 u32 dma_ctrl;
142
143 /* disable the channel run field */
144 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
145 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
146 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
147
148 /* program a DMA channel with first descriptor to be processed. */
149 acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
150 & dscr_strt_idx),
151 acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);
152
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530153 /*
154 * program a DMA channel with the number of descriptors to be
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500155 * processed in the transfer
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530156 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500157 acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530158 acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500159
160 /* set DMA channel priority */
161 acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
162}
163
164/* Initialize a dma descriptor in SRAM based on descritor information passed */
165static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
166 u16 descr_idx,
167 acp_dma_dscr_transfer_t *descr_info)
168{
169 u32 sram_offset;
170
171 sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));
172
173 /* program the source base address. */
174 acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
175 acp_reg_write(descr_info->src, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
176 /* program the destination base address. */
177 acp_reg_write(sram_offset + 4, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
178 acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
179
180 /* program the number of bytes to be transferred for this descriptor. */
181 acp_reg_write(sram_offset + 8, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
182 acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
183}
184
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530185/*
186 * Initialize the DMA descriptor information for transfer between
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500187 * system memory <-> ACP SRAM
188 */
189static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530190 u32 size, int direction,
191 u32 pte_offset, u16 ch,
192 u32 sram_bank, u16 dma_dscr_idx,
193 u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500194{
195 u16 i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500196 acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
197
198 for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
199 dmadscr[i].xfer_val = 0;
200 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530201 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530202 dmadscr[i].dest = sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500203 dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530204 + (pte_offset * SZ_4K) + (i * (size / 2));
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400205 switch (asic_type) {
206 case CHIP_STONEY:
207 dmadscr[i].xfer_val |=
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530208 (ACP_DMA_ATTR_DAGB_GARLIC_TO_SHAREDMEM << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400209 (size / 2);
210 break;
211 default:
212 dmadscr[i].xfer_val |=
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530213 (ACP_DMA_ATTR_DAGB_ONION_TO_SHAREDMEM << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400214 (size / 2);
215 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500216 } else {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530217 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530218 dmadscr[i].src = sram_bank + (i * (size / 2));
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530219 dmadscr[i].dest =
220 ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS +
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530221 (pte_offset * SZ_4K) + (i * (size / 2));
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400222 switch (asic_type) {
223 case CHIP_STONEY:
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400224 dmadscr[i].xfer_val |=
225 BIT(22) |
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530226 (ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400227 (size / 2);
228 break;
229 default:
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400230 dmadscr[i].xfer_val |=
231 BIT(22) |
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530232 (ACP_DMA_ATTR_SHAREDMEM_TO_DAGB_ONION << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400233 (size / 2);
234 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500235 }
236 config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530237 &dmadscr[i]);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500238 }
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530239 config_acp_dma_channel(acp_mmio, ch,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530240 dma_dscr_idx - 1,
241 NUM_DSCRS_PER_CHANNEL,
242 ACP_DMA_PRIORITY_LEVEL_NORMAL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500243}
244
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530245/*
246 * Initialize the DMA descriptor information for transfer between
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500247 * ACP SRAM <-> I2S
248 */
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530249static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio, u32 size,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530250 int direction, u32 sram_bank,
251 u16 destination, u16 ch,
252 u16 dma_dscr_idx, u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500253{
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500254 u16 i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500255 acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
256
257 for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
258 dmadscr[i].xfer_val = 0;
259 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530260 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530261 dmadscr[i].src = sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500262 /* dmadscr[i].dest is unused by hardware. */
263 dmadscr[i].dest = 0;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530264 dmadscr[i].xfer_val |= BIT(22) | (destination << 16) |
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500265 (size / 2);
266 } else {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530267 dma_dscr_idx = dma_dscr_idx + i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500268 /* dmadscr[i].src is unused by hardware. */
269 dmadscr[i].src = 0;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530270 dmadscr[i].dest =
271 sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500272 dmadscr[i].xfer_val |= BIT(22) |
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530273 (destination << 16) | (size / 2);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500274 }
275 config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530276 &dmadscr[i]);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500277 }
278 /* Configure the DMA channel with the above descriptore */
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530279 config_acp_dma_channel(acp_mmio, ch, dma_dscr_idx - 1,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530280 NUM_DSCRS_PER_CHANNEL,
281 ACP_DMA_PRIORITY_LEVEL_NORMAL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500282}
283
284/* Create page table entries in ACP SRAM for the allocated memory */
285static void acp_pte_config(void __iomem *acp_mmio, struct page *pg,
286 u16 num_of_pages, u32 pte_offset)
287{
288 u16 page_idx;
289 u64 addr;
290 u32 low;
291 u32 high;
292 u32 offset;
293
294 offset = ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
295 for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
296 /* Load the low address of page int ACP SRAM through SRBM */
297 acp_reg_write((offset + (page_idx * 8)),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530298 acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500299 addr = page_to_phys(pg);
300
301 low = lower_32_bits(addr);
302 high = upper_32_bits(addr);
303
304 acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
305
306 /* Load the High address of page int ACP SRAM through SRBM */
307 acp_reg_write((offset + (page_idx * 8) + 4),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530308 acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500309
310 /* page enable in ACP */
311 high |= BIT(31);
312 acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
313
314 /* Move to next physically contiguos page */
315 pg++;
316 }
317}
318
319static void config_acp_dma(void __iomem *acp_mmio,
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530320 struct audio_substream_data *rtd,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530321 u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500322{
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530323 u32 pte_offset, sram_bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500324
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530325 if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500326 pte_offset = ACP_PLAYBACK_PTE_OFFSET;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530327 sram_bank = ACP_SHARED_RAM_BANK_1_ADDRESS;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530328 } else {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500329 pte_offset = ACP_CAPTURE_PTE_OFFSET;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530330 switch (asic_type) {
331 case CHIP_STONEY:
332 sram_bank = ACP_SHARED_RAM_BANK_3_ADDRESS;
333 break;
334 default:
335 sram_bank = ACP_SHARED_RAM_BANK_5_ADDRESS;
336 }
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530337 }
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530338 acp_pte_config(acp_mmio, rtd->pg, rtd->num_of_pages,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530339 pte_offset);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500340 /* Configure System memory <-> ACP SRAM DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530341 set_acp_sysmem_dma_descriptors(acp_mmio, rtd->size,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530342 rtd->direction, pte_offset,
343 rtd->ch1, sram_bank,
344 rtd->dma_dscr_idx_1, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500345 /* Configure ACP SRAM <-> I2S DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530346 set_acp_to_i2s_dma_descriptors(acp_mmio, rtd->size,
347 rtd->direction, sram_bank,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530348 rtd->destination, rtd->ch2,
349 rtd->dma_dscr_idx_2, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500350}
351
352/* Start a given DMA channel transfer */
353static void acp_dma_start(void __iomem *acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530354 u16 ch_num, bool is_circular)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500355{
356 u32 dma_ctrl;
357
358 /* read the dma control register and disable the channel run field */
359 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
360
361 /* Invalidating the DAGB cache */
362 acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
363
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530364 /*
365 * configure the DMA channel and start the DMA transfer
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500366 * set dmachrun bit to start the transfer and enable the
367 * interrupt on completion of the dma transfer
368 */
369 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
370
371 switch (ch_num) {
372 case ACP_TO_I2S_DMA_CH_NUM:
373 case ACP_TO_SYSRAM_CH_NUM:
374 case I2S_TO_ACP_DMA_CH_NUM:
375 dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
376 break;
377 default:
378 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
379 break;
380 }
381
382 /* enable for ACP SRAM to/from I2S DMA channel */
383 if (is_circular == true)
384 dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
385 else
386 dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
387
388 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
389}
390
391/* Stop a given DMA channel transfer */
392static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
393{
394 u32 dma_ctrl;
395 u32 dma_ch_sts;
396 u32 count = ACP_DMA_RESET_TIME;
397
398 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
399
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530400 /*
401 * clear the dma control register fields before writing zero
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500402 * in reset bit
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530403 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500404 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
405 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
406
407 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
408 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
409
410 if (dma_ch_sts & BIT(ch_num)) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530411 /*
412 * set the reset bit for this channel to stop the dma
413 * transfer
414 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500415 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
416 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
417 }
418
419 /* check the channel status bit for some time and return the status */
420 while (true) {
421 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
422 if (!(dma_ch_sts & BIT(ch_num))) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530423 /*
424 * clear the reset flag after successfully stopping
425 * the dma transfer and break from the loop
426 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500427 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
428
429 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530430 + ch_num);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500431 break;
432 }
433 if (--count == 0) {
434 pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
435 return -ETIMEDOUT;
436 }
437 udelay(100);
438 }
439 return 0;
440}
441
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500442static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530443 bool power_on)
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500444{
445 u32 val, req_reg, sts_reg, sts_reg_mask;
446 u32 loops = 1000;
447
448 if (bank < 32) {
449 req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
450 sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
451 sts_reg_mask = 0xFFFFFFFF;
452
453 } else {
454 bank -= 32;
455 req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
456 sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
457 sts_reg_mask = 0x0000FFFF;
458 }
459
460 val = acp_reg_read(acp_mmio, req_reg);
461 if (val & (1 << bank)) {
462 /* bank is in off state */
463 if (power_on == true)
464 /* request to on */
465 val &= ~(1 << bank);
466 else
467 /* request to off */
468 return;
469 } else {
470 /* bank is in on state */
471 if (power_on == false)
472 /* request to off */
473 val |= 1 << bank;
474 else
475 /* request to on */
476 return;
477 }
478 acp_reg_write(val, acp_mmio, req_reg);
479
480 while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
481 if (!loops--) {
482 pr_err("ACP SRAM bank %d state change failed\n", bank);
483 break;
484 }
485 cpu_relax();
486 }
487}
488
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500489/* Initialize and bring ACP hardware to default state. */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400490static int acp_init(void __iomem *acp_mmio, u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500491{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500492 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500493 u32 val, count, sram_pte_offset;
494
495 /* Assert Soft reset of ACP */
496 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
497
498 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
499 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
500
501 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
502 while (true) {
503 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
504 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
505 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
506 break;
507 if (--count == 0) {
508 pr_err("Failed to reset ACP\n");
509 return -ETIMEDOUT;
510 }
511 udelay(100);
512 }
513
514 /* Enable clock to ACP and wait until the clock is enabled */
515 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
516 val = val | ACP_CONTROL__ClkEn_MASK;
517 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
518
519 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
520
521 while (true) {
522 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530523 if (val & (u32)0x1)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500524 break;
525 if (--count == 0) {
526 pr_err("Failed to reset ACP\n");
527 return -ETIMEDOUT;
528 }
529 udelay(100);
530 }
531
532 /* Deassert the SOFT RESET flags */
533 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
534 val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
535 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
536
537 /* initiailize Onion control DAGB register */
538 acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530539 mmACP_AXI2DAGB_ONION_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500540
541 /* initiailize Garlic control DAGB registers */
542 acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530543 mmACP_AXI2DAGB_GARLIC_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500544
545 sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
546 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
547 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
548 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
549 acp_reg_write(sram_pte_offset, acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
550 acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530551 mmACP_DAGB_PAGE_SIZE_GRP_1);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500552
553 acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530554 mmACP_DMA_DESC_BASE_ADDR);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500555
556 /* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
557 acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
558 acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530559 acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500560
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530561 /*
562 * When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500563 * Now, turn off all of them. This can't be done in 'poweron' of
564 * ACP pm domain, as this requires ACP to be initialized.
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400565 * For Stoney, Memory gating is disabled,i.e SRAM Banks
566 * won't be turned off. The default state for SRAM banks is ON.
567 * Setting SRAM bank state code skipped for STONEY platform.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500568 */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400569 if (asic_type != CHIP_STONEY) {
570 for (bank = 1; bank < 48; bank++)
571 acp_set_sram_bank_state(acp_mmio, bank, false);
572 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500573 return 0;
574}
575
Masahiro Yamada1cce2002017-02-27 14:29:45 -0800576/* Deinitialize ACP */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500577static int acp_deinit(void __iomem *acp_mmio)
578{
579 u32 val;
580 u32 count;
581
582 /* Assert Soft reset of ACP */
583 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
584
585 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
586 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
587
588 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
589 while (true) {
590 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
591 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
592 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
593 break;
594 if (--count == 0) {
595 pr_err("Failed to reset ACP\n");
596 return -ETIMEDOUT;
597 }
598 udelay(100);
599 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530600 /* Disable ACP clock */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500601 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
602 val &= ~ACP_CONTROL__ClkEn_MASK;
603 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
604
605 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
606
607 while (true) {
608 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530609 if (!(val & (u32)0x1))
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500610 break;
611 if (--count == 0) {
612 pr_err("Failed to reset ACP\n");
613 return -ETIMEDOUT;
614 }
615 udelay(100);
616 }
617 return 0;
618}
619
620/* ACP DMA irq handler routine for playback, capture usecases */
621static irqreturn_t dma_irq_handler(int irq, void *arg)
622{
623 u16 dscr_idx;
624 u32 intr_flag, ext_intr_status;
625 struct audio_drv_data *irq_data;
626 void __iomem *acp_mmio;
627 struct device *dev = arg;
628 bool valid_irq = false;
629
630 irq_data = dev_get_drvdata(dev);
631 acp_mmio = irq_data->acp_mmio;
632
633 ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
634 intr_flag = (((ext_intr_status &
635 ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
636 ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
637
638 if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
639 valid_irq = true;
640 if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_13) ==
641 PLAYBACK_START_DMA_DESCR_CH13)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500642 dscr_idx = PLAYBACK_END_DMA_DESCR_CH12;
Vijendar Mukunda31c45b32017-11-09 12:35:52 -0500643 else
644 dscr_idx = PLAYBACK_START_DMA_DESCR_CH12;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500645 config_acp_dma_channel(acp_mmio, SYSRAM_TO_ACP_CH_NUM, dscr_idx,
646 1, 0);
647 acp_dma_start(acp_mmio, SYSRAM_TO_ACP_CH_NUM, false);
648
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530649 snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500650
651 acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530652 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500653 }
654
655 if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
656 valid_irq = true;
657 if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_15) ==
658 CAPTURE_START_DMA_DESCR_CH15)
659 dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
660 else
661 dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
662 config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
663 1, 0);
664 acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);
665
666 acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530667 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500668 }
669
670 if ((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) != 0) {
671 valid_irq = true;
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530672 snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500673 acp_reg_write((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530674 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500675 }
676
677 if (valid_irq)
678 return IRQ_HANDLED;
679 else
680 return IRQ_NONE;
681}
682
683static int acp_dma_open(struct snd_pcm_substream *substream)
684{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500685 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500686 int ret = 0;
687 struct snd_pcm_runtime *runtime = substream->runtime;
688 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530689 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
690 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000691 struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500692 struct audio_substream_data *adata =
693 kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530694 if (!adata)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500695 return -ENOMEM;
696
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400697 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
698 switch (intr_data->asic_type) {
699 case CHIP_STONEY:
700 runtime->hw = acp_st_pcm_hardware_playback;
701 break;
702 default:
703 runtime->hw = acp_pcm_hardware_playback;
704 }
705 } else {
706 switch (intr_data->asic_type) {
707 case CHIP_STONEY:
708 runtime->hw = acp_st_pcm_hardware_capture;
709 break;
710 default:
711 runtime->hw = acp_pcm_hardware_capture;
712 }
713 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500714
715 ret = snd_pcm_hw_constraint_integer(runtime,
716 SNDRV_PCM_HW_PARAM_PERIODS);
717 if (ret < 0) {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000718 dev_err(component->dev, "set integer constraint failed\n");
Dan Carpentercde6bcd2016-01-13 15:20:02 +0300719 kfree(adata);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500720 return ret;
721 }
722
723 adata->acp_mmio = intr_data->acp_mmio;
724 runtime->private_data = adata;
725
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530726 /*
727 * Enable ACP irq, when neither playback or capture streams are
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500728 * active by the time when a new stream is being opened.
729 * This enablement is not required for another stream, if current
730 * stream is not closed
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530731 */
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530732 if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500733 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
734
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500735 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530736 intr_data->play_i2ssp_stream = substream;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530737 /*
738 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400739 * won't be turned off. The default state for SRAM banks is ON.
740 * Setting SRAM bank state code skipped for STONEY platform.
741 */
742 if (intr_data->asic_type != CHIP_STONEY) {
743 for (bank = 1; bank <= 4; bank++)
744 acp_set_sram_bank_state(intr_data->acp_mmio,
745 bank, true);
746 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500747 } else {
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530748 intr_data->capture_i2ssp_stream = substream;
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400749 if (intr_data->asic_type != CHIP_STONEY) {
750 for (bank = 5; bank <= 8; bank++)
751 acp_set_sram_bank_state(intr_data->acp_mmio,
752 bank, true);
753 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500754 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500755
756 return 0;
757}
758
759static int acp_dma_hw_params(struct snd_pcm_substream *substream,
760 struct snd_pcm_hw_params *params)
761{
762 int status;
763 uint64_t size;
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530764 u32 val = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500765 struct page *pg;
766 struct snd_pcm_runtime *runtime;
767 struct audio_substream_data *rtd;
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400768 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530769 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
770 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000771 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500772
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500773 runtime = substream->runtime;
774 rtd = runtime->private_data;
775
776 if (WARN_ON(!rtd))
777 return -EINVAL;
778
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530779 if (adata->asic_type == CHIP_STONEY) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530780 val = acp_reg_read(adata->acp_mmio,
781 mmACP_I2S_16BIT_RESOLUTION_EN);
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
783 val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
784 else
785 val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530786 acp_reg_write(val, adata->acp_mmio,
787 mmACP_I2S_16BIT_RESOLUTION_EN);
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530788 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530789
790 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
791 rtd->ch1 = SYSRAM_TO_ACP_CH_NUM;
792 rtd->ch2 = ACP_TO_I2S_DMA_CH_NUM;
793 rtd->destination = TO_ACP_I2S_1;
794 rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH12;
795 rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH13;
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530796 rtd->byte_cnt_high_reg_offset =
797 mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH;
798 rtd->byte_cnt_low_reg_offset = mmACP_I2S_TRANSMIT_BYTE_CNT_LOW;
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530799 } else {
800 rtd->ch1 = ACP_TO_SYSRAM_CH_NUM;
801 rtd->ch2 = I2S_TO_ACP_DMA_CH_NUM;
802 rtd->destination = FROM_ACP_I2S_1;
803 rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH14;
804 rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH15;
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530805 rtd->byte_cnt_high_reg_offset =
806 mmACP_I2S_RECEIVED_BYTE_CNT_HIGH;
807 rtd->byte_cnt_low_reg_offset = mmACP_I2S_RECEIVED_BYTE_CNT_LOW;
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530808 }
809
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500810 size = params_buffer_bytes(params);
811 status = snd_pcm_lib_malloc_pages(substream, size);
812 if (status < 0)
813 return status;
814
815 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
816 pg = virt_to_page(substream->dma_buffer.area);
817
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530818 if (pg) {
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500819 acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500820 /* Save for runtime private data */
821 rtd->pg = pg;
822 rtd->order = get_order(size);
823
824 /* Fill the page table entries in ACP SRAM */
825 rtd->pg = pg;
826 rtd->size = size;
827 rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
828 rtd->direction = substream->stream;
829
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400830 config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500831 status = 0;
832 } else {
833 status = -ENOMEM;
834 }
835 return status;
836}
837
838static int acp_dma_hw_free(struct snd_pcm_substream *substream)
839{
840 return snd_pcm_lib_free_pages(substream);
841}
842
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530843static u64 acp_get_byte_count(struct audio_substream_data *rtd)
Vijendar Mukunda61add812017-11-03 16:35:43 -0400844{
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530845 union acp_dma_count byte_count;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400846
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530847 byte_count.bcount.high = acp_reg_read(rtd->acp_mmio,
848 rtd->byte_cnt_high_reg_offset);
849 byte_count.bcount.low = acp_reg_read(rtd->acp_mmio,
850 rtd->byte_cnt_low_reg_offset);
851 return byte_count.bytescount;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400852}
853
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500854static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
855{
Vijendar Mukunda61add812017-11-03 16:35:43 -0400856 u32 buffersize;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500857 u32 pos = 0;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400858 u64 bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500859
860 struct snd_pcm_runtime *runtime = substream->runtime;
861 struct audio_substream_data *rtd = runtime->private_data;
862
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +0530863 if (!rtd)
864 return -EINVAL;
865
Vijendar Mukunda61add812017-11-03 16:35:43 -0400866 buffersize = frames_to_bytes(runtime, runtime->buffer_size);
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530867 bytescount = acp_get_byte_count(rtd);
Vijendar Mukunda61add812017-11-03 16:35:43 -0400868
Vijendar Mukunda9af89372018-05-08 10:17:46 +0530869 if (bytescount > rtd->bytescount)
870 bytescount -= rtd->bytescount;
Guenter Roeck7db08b22017-11-08 16:34:54 -0500871 pos = do_div(bytescount, buffersize);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500872 return bytes_to_frames(runtime, pos);
873}
874
875static int acp_dma_mmap(struct snd_pcm_substream *substream,
876 struct vm_area_struct *vma)
877{
878 return snd_pcm_lib_default_mmap(substream, vma);
879}
880
881static int acp_dma_prepare(struct snd_pcm_substream *substream)
882{
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct audio_substream_data *rtd = runtime->private_data;
885
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +0530886 if (!rtd)
887 return -EINVAL;
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530888
889 config_acp_dma_channel(rtd->acp_mmio,
890 rtd->ch1,
891 rtd->dma_dscr_idx_1,
892 NUM_DSCRS_PER_CHANNEL, 0);
893 config_acp_dma_channel(rtd->acp_mmio,
894 rtd->ch2,
895 rtd->dma_dscr_idx_2,
896 NUM_DSCRS_PER_CHANNEL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500897 return 0;
898}
899
900static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
901{
902 int ret;
Vijendar Mukunda31c45b32017-11-09 12:35:52 -0500903 u32 loops = 4000;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400904 u64 bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500905
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_soc_pcm_runtime *prtd = substream->private_data;
908 struct audio_substream_data *rtd = runtime->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530909 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
910 DRV_NAME);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500911
912 if (!rtd)
913 return -EINVAL;
914 switch (cmd) {
915 case SNDRV_PCM_TRIGGER_START:
916 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
917 case SNDRV_PCM_TRIGGER_RESUME:
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530918 bytescount = acp_get_byte_count(rtd);
Vijendar Mukunda9af89372018-05-08 10:17:46 +0530919 if (rtd->bytescount == 0)
920 rtd->bytescount = bytescount;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500921 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530922 acp_dma_start(rtd->acp_mmio, rtd->ch1, false);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500923 while (acp_reg_read(rtd->acp_mmio, mmACP_DMA_CH_STS) &
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530924 BIT(rtd->ch1)) {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500925 if (!loops--) {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000926 dev_err(component->dev,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500927 "acp dma start timeout\n");
928 return -ETIMEDOUT;
929 }
930 cpu_relax();
931 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500932 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530933 acp_dma_start(rtd->acp_mmio, rtd->ch2, true);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500934 ret = 0;
935 break;
936 case SNDRV_PCM_TRIGGER_STOP:
937 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
938 case SNDRV_PCM_TRIGGER_SUSPEND:
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530939 /* For playback, non circular dma should be stopped first
940 * i.e Sysram to acp dma transfer channel(rtd->ch1) should be
941 * stopped before stopping cirular dma which is acp sram to i2s
942 * fifo dma transfer channel(rtd->ch2). Where as in Capture
943 * scenario, i2s fifo to acp sram dma channel(rtd->ch2) stopped
944 * first before stopping acp sram to sysram which is circular
945 * dma(rtd->ch1).
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500946 */
Vijendar Mukunda61add812017-11-03 16:35:43 -0400947 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530948 acp_dma_stop(rtd->acp_mmio, rtd->ch1);
949 ret = acp_dma_stop(rtd->acp_mmio, rtd->ch2);
Vijendar Mukunda61add812017-11-03 16:35:43 -0400950 } else {
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530951 acp_dma_stop(rtd->acp_mmio, rtd->ch2);
952 ret = acp_dma_stop(rtd->acp_mmio, rtd->ch1);
Vijendar Mukunda61add812017-11-03 16:35:43 -0400953 }
Vijendar Mukunda9af89372018-05-08 10:17:46 +0530954 rtd->bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500955 break;
956 default:
957 ret = -EINVAL;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500958 }
959 return ret;
960}
961
962static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
963{
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400964 int ret;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530965 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
966 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000967 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400968
969 switch (adata->asic_type) {
970 case CHIP_STONEY:
971 ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530972 SNDRV_DMA_TYPE_DEV,
973 NULL, ST_MIN_BUFFER,
974 ST_MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400975 break;
976 default:
977 ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530978 SNDRV_DMA_TYPE_DEV,
979 NULL, MIN_BUFFER,
980 MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400981 break;
982 }
983 if (ret < 0)
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000984 dev_err(component->dev,
Colin Ian King9e6a4692018-05-01 09:20:01 +0100985 "buffer preallocation failure error:%d\n", ret);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400986 return ret;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500987}
988
989static int acp_dma_close(struct snd_pcm_substream *substream)
990{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500991 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500992 struct snd_pcm_runtime *runtime = substream->runtime;
993 struct audio_substream_data *rtd = runtime->private_data;
994 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530995 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
996 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000997 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500998
999 kfree(rtd);
1000
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001001 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301002 adata->play_i2ssp_stream = NULL;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301003 /*
1004 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001005 * won't be turned off. The default state for SRAM banks is ON.
1006 * Setting SRAM bank state code skipped for STONEY platform.
1007 * added condition checks for Carrizo platform only
1008 */
1009 if (adata->asic_type != CHIP_STONEY) {
1010 for (bank = 1; bank <= 4; bank++)
1011 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301012 false);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001013 }
1014 } else {
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301015 adata->capture_i2ssp_stream = NULL;
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001016 if (adata->asic_type != CHIP_STONEY) {
1017 for (bank = 5; bank <= 8; bank++)
1018 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301019 false);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001020 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001021 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001022
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301023 /*
1024 * Disable ACP irq, when the current stream is being closed and
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001025 * another stream is also not active.
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301026 */
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301027 if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001028 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1029
1030 return 0;
1031}
1032
Julia Lawall115c7252016-09-08 02:35:23 +02001033static const struct snd_pcm_ops acp_dma_ops = {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001034 .open = acp_dma_open,
1035 .close = acp_dma_close,
1036 .ioctl = snd_pcm_lib_ioctl,
1037 .hw_params = acp_dma_hw_params,
1038 .hw_free = acp_dma_hw_free,
1039 .trigger = acp_dma_trigger,
1040 .pointer = acp_dma_pointer,
1041 .mmap = acp_dma_mmap,
1042 .prepare = acp_dma_prepare,
1043};
1044
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301045static const struct snd_soc_component_driver acp_asoc_platform = {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001046 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001047 .ops = &acp_dma_ops,
1048 .pcm_new = acp_dma_new,
1049};
1050
1051static int acp_audio_probe(struct platform_device *pdev)
1052{
1053 int status;
1054 struct audio_drv_data *audio_drv_data;
1055 struct resource *res;
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001056 const u32 *pdata = pdev->dev.platform_data;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001057
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001058 if (!pdata) {
1059 dev_err(&pdev->dev, "Missing platform data\n");
1060 return -ENODEV;
1061 }
1062
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001063 audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301064 GFP_KERNEL);
1065 if (!audio_drv_data)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001066 return -ENOMEM;
1067
1068 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1069 audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001070 if (IS_ERR(audio_drv_data->acp_mmio))
1071 return PTR_ERR(audio_drv_data->acp_mmio);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001072
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301073 /*
1074 * The following members gets populated in device 'open'
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001075 * function. Till then interrupts are disabled in 'acp_init'
1076 * and device doesn't generate any interrupts.
1077 */
1078
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301079 audio_drv_data->play_i2ssp_stream = NULL;
1080 audio_drv_data->capture_i2ssp_stream = NULL;
1081
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001082 audio_drv_data->asic_type = *pdata;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001083
1084 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1085 if (!res) {
1086 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
1087 return -ENODEV;
1088 }
1089
1090 status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301091 0, "ACP_IRQ", &pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001092 if (status) {
1093 dev_err(&pdev->dev, "ACP IRQ request failed\n");
1094 return status;
1095 }
1096
1097 dev_set_drvdata(&pdev->dev, audio_drv_data);
1098
1099 /* Initialize the ACP */
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301100 status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
1101 if (status) {
1102 dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
1103 return status;
1104 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001105
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001106 status = devm_snd_soc_register_component(&pdev->dev,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301107 &acp_asoc_platform, NULL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001108 if (status != 0) {
1109 dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
1110 return status;
1111 }
1112
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001113 pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
1114 pm_runtime_use_autosuspend(&pdev->dev);
1115 pm_runtime_enable(&pdev->dev);
1116
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001117 return status;
1118}
1119
1120static int acp_audio_remove(struct platform_device *pdev)
1121{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301122 int status;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001123 struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
1124
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301125 status = acp_deinit(adata->acp_mmio);
1126 if (status)
1127 dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001128 pm_runtime_disable(&pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001129
1130 return 0;
1131}
1132
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001133static int acp_pcm_resume(struct device *dev)
1134{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001135 u16 bank;
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301136 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001137 struct audio_drv_data *adata = dev_get_drvdata(dev);
1138
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301139 status = acp_init(adata->acp_mmio, adata->asic_type);
1140 if (status) {
1141 dev_err(dev, "ACP Init failed status:%d\n", status);
1142 return status;
1143 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001144
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301145 if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301146 /*
1147 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001148 * won't be turned off. The default state for SRAM banks is ON.
1149 * Setting SRAM bank state code skipped for STONEY platform.
1150 */
1151 if (adata->asic_type != CHIP_STONEY) {
1152 for (bank = 1; bank <= 4; bank++)
1153 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301154 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001155 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001156 config_acp_dma(adata->acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301157 adata->play_i2ssp_stream->runtime->private_data,
1158 adata->asic_type);
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001159 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301160 if (adata->capture_i2ssp_stream &&
1161 adata->capture_i2ssp_stream->runtime) {
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001162 if (adata->asic_type != CHIP_STONEY) {
1163 for (bank = 5; bank <= 8; bank++)
1164 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301165 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001166 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001167 config_acp_dma(adata->acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301168 adata->capture_i2ssp_stream->runtime->private_data,
1169 adata->asic_type);
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001170 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001171 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1172 return 0;
1173}
1174
1175static int acp_pcm_runtime_suspend(struct device *dev)
1176{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301177 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001178 struct audio_drv_data *adata = dev_get_drvdata(dev);
1179
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301180 status = acp_deinit(adata->acp_mmio);
1181 if (status)
1182 dev_err(dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001183 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1184 return 0;
1185}
1186
1187static int acp_pcm_runtime_resume(struct device *dev)
1188{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301189 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001190 struct audio_drv_data *adata = dev_get_drvdata(dev);
1191
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301192 status = acp_init(adata->acp_mmio, adata->asic_type);
1193 if (status) {
1194 dev_err(dev, "ACP Init failed status:%d\n", status);
1195 return status;
1196 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001197 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1198 return 0;
1199}
1200
1201static const struct dev_pm_ops acp_pm_ops = {
1202 .resume = acp_pcm_resume,
1203 .runtime_suspend = acp_pcm_runtime_suspend,
1204 .runtime_resume = acp_pcm_runtime_resume,
1205};
1206
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001207static struct platform_driver acp_dma_driver = {
1208 .probe = acp_audio_probe,
1209 .remove = acp_audio_remove,
1210 .driver = {
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001211 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001212 .pm = &acp_pm_ops,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001213 },
1214};
1215
1216module_platform_driver(acp_dma_driver);
1217
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001218MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001219MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
1220MODULE_DESCRIPTION("AMD ACP PCM Driver");
1221MODULE_LICENSE("GPL v2");
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001222MODULE_ALIAS("platform:"DRV_NAME);