blob: 816abd65a6ed3c86f365dffec1b979420b909b01 [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
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +053040#define ST_PLAYBACK_MAX_PERIOD_SIZE 4096
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -040041#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"
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +053046bool bt_uart_enable = true;
47EXPORT_SYMBOL(bt_uart_enable);
Akshu Agrawalbdd2a852017-11-08 12:24:02 -050048
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -050049static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
50 .info = SNDRV_PCM_INFO_INTERLEAVED |
51 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
52 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
53 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
54 .formats = SNDRV_PCM_FMTBIT_S16_LE |
55 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
56 .channels_min = 1,
57 .channels_max = 8,
58 .rates = SNDRV_PCM_RATE_8000_96000,
59 .rate_min = 8000,
60 .rate_max = 96000,
61 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
62 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
63 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
64 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
65 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
66};
67
68static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
69 .info = SNDRV_PCM_INFO_INTERLEAVED |
70 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
71 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
72 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
73 .formats = SNDRV_PCM_FMTBIT_S16_LE |
74 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
75 .channels_min = 1,
76 .channels_max = 2,
77 .rates = SNDRV_PCM_RATE_8000_48000,
78 .rate_min = 8000,
79 .rate_max = 48000,
80 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
81 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
82 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
83 .periods_min = CAPTURE_MIN_NUM_PERIODS,
84 .periods_max = CAPTURE_MAX_NUM_PERIODS,
85};
86
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -040087static const struct snd_pcm_hardware acp_st_pcm_hardware_playback = {
88 .info = SNDRV_PCM_INFO_INTERLEAVED |
89 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
90 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
91 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
92 .formats = SNDRV_PCM_FMTBIT_S16_LE |
93 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
94 .channels_min = 1,
95 .channels_max = 8,
96 .rates = SNDRV_PCM_RATE_8000_96000,
97 .rate_min = 8000,
98 .rate_max = 96000,
99 .buffer_bytes_max = ST_MAX_BUFFER,
100 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
101 .period_bytes_max = ST_PLAYBACK_MAX_PERIOD_SIZE,
102 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
103 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
104};
105
106static const struct snd_pcm_hardware acp_st_pcm_hardware_capture = {
107 .info = SNDRV_PCM_INFO_INTERLEAVED |
108 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
109 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
110 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
111 .formats = SNDRV_PCM_FMTBIT_S16_LE |
112 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
113 .channels_min = 1,
114 .channels_max = 2,
115 .rates = SNDRV_PCM_RATE_8000_48000,
116 .rate_min = 8000,
117 .rate_max = 48000,
118 .buffer_bytes_max = ST_MAX_BUFFER,
119 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
120 .period_bytes_max = ST_CAPTURE_MAX_PERIOD_SIZE,
121 .periods_min = CAPTURE_MIN_NUM_PERIODS,
122 .periods_max = CAPTURE_MAX_NUM_PERIODS,
123};
124
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500125static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
126{
127 return readl(acp_mmio + (reg * 4));
128}
129
130static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
131{
132 writel(val, acp_mmio + (reg * 4));
133}
134
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530135/*
136 * Configure a given dma channel parameters - enable/disable,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500137 * number of descriptors, priority
138 */
139static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
140 u16 dscr_strt_idx, u16 num_dscrs,
141 enum acp_dma_priority_level priority_level)
142{
143 u32 dma_ctrl;
144
145 /* disable the channel run field */
146 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
147 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
148 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
149
150 /* program a DMA channel with first descriptor to be processed. */
151 acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
152 & dscr_strt_idx),
153 acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);
154
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530155 /*
156 * program a DMA channel with the number of descriptors to be
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500157 * processed in the transfer
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530158 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500159 acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530160 acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500161
162 /* set DMA channel priority */
163 acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
164}
165
166/* Initialize a dma descriptor in SRAM based on descritor information passed */
167static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
168 u16 descr_idx,
169 acp_dma_dscr_transfer_t *descr_info)
170{
171 u32 sram_offset;
172
173 sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));
174
175 /* program the source base address. */
176 acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
177 acp_reg_write(descr_info->src, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
178 /* program the destination base address. */
179 acp_reg_write(sram_offset + 4, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
180 acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
181
182 /* program the number of bytes to be transferred for this descriptor. */
183 acp_reg_write(sram_offset + 8, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
184 acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
185}
186
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530187/*
188 * Initialize the DMA descriptor information for transfer between
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500189 * system memory <-> ACP SRAM
190 */
191static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530192 u32 size, int direction,
193 u32 pte_offset, u16 ch,
194 u32 sram_bank, u16 dma_dscr_idx,
195 u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500196{
197 u16 i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500198 acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
199
200 for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
201 dmadscr[i].xfer_val = 0;
202 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530203 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530204 dmadscr[i].dest = sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500205 dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530206 + (pte_offset * SZ_4K) + (i * (size / 2));
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400207 switch (asic_type) {
208 case CHIP_STONEY:
209 dmadscr[i].xfer_val |=
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530210 (ACP_DMA_ATTR_DAGB_GARLIC_TO_SHAREDMEM << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400211 (size / 2);
212 break;
213 default:
214 dmadscr[i].xfer_val |=
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530215 (ACP_DMA_ATTR_DAGB_ONION_TO_SHAREDMEM << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400216 (size / 2);
217 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500218 } else {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530219 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530220 dmadscr[i].src = sram_bank + (i * (size / 2));
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530221 dmadscr[i].dest =
222 ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS +
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530223 (pte_offset * SZ_4K) + (i * (size / 2));
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400224 switch (asic_type) {
225 case CHIP_STONEY:
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400226 dmadscr[i].xfer_val |=
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530227 (ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400228 (size / 2);
229 break;
230 default:
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400231 dmadscr[i].xfer_val |=
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{
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +0800323 u16 ch_acp_sysmem, ch_acp_i2s;
324
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530325 acp_pte_config(acp_mmio, rtd->pg, rtd->num_of_pages,
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530326 rtd->pte_offset);
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +0800327
328 if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
329 ch_acp_sysmem = rtd->ch1;
330 ch_acp_i2s = rtd->ch2;
331 } else {
332 ch_acp_i2s = rtd->ch1;
333 ch_acp_sysmem = rtd->ch2;
334 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500335 /* Configure System memory <-> ACP SRAM DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530336 set_acp_sysmem_dma_descriptors(acp_mmio, rtd->size,
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530337 rtd->direction, rtd->pte_offset,
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +0800338 ch_acp_sysmem, rtd->sram_bank,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530339 rtd->dma_dscr_idx_1, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500340 /* Configure ACP SRAM <-> I2S DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530341 set_acp_to_i2s_dma_descriptors(acp_mmio, rtd->size,
Mukunda, Vijendar18e8a402018-05-08 10:17:48 +0530342 rtd->direction, rtd->sram_bank,
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +0800343 rtd->destination, ch_acp_i2s,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530344 rtd->dma_dscr_idx_2, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500345}
346
Akshu Agrawal2718c892018-06-21 12:58:17 +0800347static void acp_dma_cap_channel_enable(void __iomem *acp_mmio,
348 u16 cap_channel)
349{
350 u32 val, ch_reg, imr_reg, res_reg;
351
352 switch (cap_channel) {
353 case CAP_CHANNEL1:
354 ch_reg = mmACP_I2SMICSP_RER1;
355 res_reg = mmACP_I2SMICSP_RCR1;
356 imr_reg = mmACP_I2SMICSP_IMR1;
357 break;
358 case CAP_CHANNEL0:
359 default:
360 ch_reg = mmACP_I2SMICSP_RER0;
361 res_reg = mmACP_I2SMICSP_RCR0;
362 imr_reg = mmACP_I2SMICSP_IMR0;
363 break;
364 }
365 val = acp_reg_read(acp_mmio,
366 mmACP_I2S_16BIT_RESOLUTION_EN);
367 if (val & ACP_I2S_MIC_16BIT_RESOLUTION_EN) {
368 acp_reg_write(0x0, acp_mmio, ch_reg);
369 /* Set 16bit resolution on capture */
370 acp_reg_write(0x2, acp_mmio, res_reg);
371 }
372 val = acp_reg_read(acp_mmio, imr_reg);
373 val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
374 val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
375 acp_reg_write(val, acp_mmio, imr_reg);
376 acp_reg_write(0x1, acp_mmio, ch_reg);
377}
378
379static void acp_dma_cap_channel_disable(void __iomem *acp_mmio,
380 u16 cap_channel)
381{
382 u32 val, ch_reg, imr_reg;
383
384 switch (cap_channel) {
385 case CAP_CHANNEL1:
386 imr_reg = mmACP_I2SMICSP_IMR1;
387 ch_reg = mmACP_I2SMICSP_RER1;
388 break;
389 case CAP_CHANNEL0:
390 default:
391 imr_reg = mmACP_I2SMICSP_IMR0;
392 ch_reg = mmACP_I2SMICSP_RER0;
393 break;
394 }
395 val = acp_reg_read(acp_mmio, imr_reg);
396 val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
397 val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
398 acp_reg_write(val, acp_mmio, imr_reg);
399 acp_reg_write(0x0, acp_mmio, ch_reg);
400}
401
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500402/* Start a given DMA channel transfer */
Akshu Agrawalbbdb7012018-08-06 12:57:14 +0530403static void acp_dma_start(void __iomem *acp_mmio, u16 ch_num, bool is_circular)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500404{
405 u32 dma_ctrl;
406
407 /* read the dma control register and disable the channel run field */
408 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
409
410 /* Invalidating the DAGB cache */
411 acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
412
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530413 /*
414 * configure the DMA channel and start the DMA transfer
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500415 * set dmachrun bit to start the transfer and enable the
416 * interrupt on completion of the dma transfer
417 */
418 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
419
420 switch (ch_num) {
421 case ACP_TO_I2S_DMA_CH_NUM:
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800422 case I2S_TO_ACP_DMA_CH_NUM:
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530423 case ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM:
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800424 case I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM:
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500425 dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
426 break;
427 default:
428 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
429 break;
430 }
431
Akshu Agrawalbbdb7012018-08-06 12:57:14 +0530432 /* enable for ACP to SRAM DMA channel */
433 if (is_circular == true)
434 dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
435 else
436 dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500437
438 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
439}
440
441/* Stop a given DMA channel transfer */
442static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
443{
444 u32 dma_ctrl;
445 u32 dma_ch_sts;
446 u32 count = ACP_DMA_RESET_TIME;
447
448 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
449
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530450 /*
451 * clear the dma control register fields before writing zero
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500452 * in reset bit
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530453 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500454 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
455 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
456
457 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
458 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
459
460 if (dma_ch_sts & BIT(ch_num)) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530461 /*
462 * set the reset bit for this channel to stop the dma
463 * transfer
464 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500465 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
466 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
467 }
468
469 /* check the channel status bit for some time and return the status */
470 while (true) {
471 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
472 if (!(dma_ch_sts & BIT(ch_num))) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530473 /*
474 * clear the reset flag after successfully stopping
475 * the dma transfer and break from the loop
476 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500477 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
478
479 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530480 + ch_num);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500481 break;
482 }
483 if (--count == 0) {
484 pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
485 return -ETIMEDOUT;
486 }
487 udelay(100);
488 }
489 return 0;
490}
491
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500492static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530493 bool power_on)
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500494{
495 u32 val, req_reg, sts_reg, sts_reg_mask;
496 u32 loops = 1000;
497
498 if (bank < 32) {
499 req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
500 sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
501 sts_reg_mask = 0xFFFFFFFF;
502
503 } else {
504 bank -= 32;
505 req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
506 sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
507 sts_reg_mask = 0x0000FFFF;
508 }
509
510 val = acp_reg_read(acp_mmio, req_reg);
511 if (val & (1 << bank)) {
512 /* bank is in off state */
513 if (power_on == true)
514 /* request to on */
515 val &= ~(1 << bank);
516 else
517 /* request to off */
518 return;
519 } else {
520 /* bank is in on state */
521 if (power_on == false)
522 /* request to off */
523 val |= 1 << bank;
524 else
525 /* request to on */
526 return;
527 }
528 acp_reg_write(val, acp_mmio, req_reg);
529
530 while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
531 if (!loops--) {
532 pr_err("ACP SRAM bank %d state change failed\n", bank);
533 break;
534 }
535 cpu_relax();
536 }
537}
538
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500539/* Initialize and bring ACP hardware to default state. */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400540static int acp_init(void __iomem *acp_mmio, u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500541{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500542 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500543 u32 val, count, sram_pte_offset;
544
545 /* Assert Soft reset of ACP */
546 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
547
548 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
549 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
550
551 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
552 while (true) {
553 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
554 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
555 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
556 break;
557 if (--count == 0) {
558 pr_err("Failed to reset ACP\n");
559 return -ETIMEDOUT;
560 }
561 udelay(100);
562 }
563
564 /* Enable clock to ACP and wait until the clock is enabled */
565 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
566 val = val | ACP_CONTROL__ClkEn_MASK;
567 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
568
569 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
570
571 while (true) {
572 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530573 if (val & (u32)0x1)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500574 break;
575 if (--count == 0) {
576 pr_err("Failed to reset ACP\n");
577 return -ETIMEDOUT;
578 }
579 udelay(100);
580 }
581
582 /* Deassert the SOFT RESET flags */
583 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
584 val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
585 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
586
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530587 /* For BT instance change pins from UART to BT */
588 if (!bt_uart_enable) {
589 val = acp_reg_read(acp_mmio, mmACP_BT_UART_PAD_SEL);
590 val |= ACP_BT_UART_PAD_SELECT_MASK;
591 acp_reg_write(val, acp_mmio, mmACP_BT_UART_PAD_SEL);
592 }
593
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500594 /* initiailize Onion control DAGB register */
595 acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530596 mmACP_AXI2DAGB_ONION_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500597
598 /* initiailize Garlic control DAGB registers */
599 acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530600 mmACP_AXI2DAGB_GARLIC_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500601
602 sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
603 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
604 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
605 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
606 acp_reg_write(sram_pte_offset, acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
607 acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530608 mmACP_DAGB_PAGE_SIZE_GRP_1);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500609
610 acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530611 mmACP_DMA_DESC_BASE_ADDR);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500612
613 /* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
614 acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
615 acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530616 acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500617
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530618 /*
619 * When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500620 * Now, turn off all of them. This can't be done in 'poweron' of
621 * ACP pm domain, as this requires ACP to be initialized.
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400622 * For Stoney, Memory gating is disabled,i.e SRAM Banks
623 * won't be turned off. The default state for SRAM banks is ON.
624 * Setting SRAM bank state code skipped for STONEY platform.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500625 */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400626 if (asic_type != CHIP_STONEY) {
627 for (bank = 1; bank < 48; bank++)
628 acp_set_sram_bank_state(acp_mmio, bank, false);
629 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500630 return 0;
631}
632
Masahiro Yamada1cce2002017-02-27 14:29:45 -0800633/* Deinitialize ACP */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500634static int acp_deinit(void __iomem *acp_mmio)
635{
636 u32 val;
637 u32 count;
638
639 /* Assert Soft reset of ACP */
640 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
641
642 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
643 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
644
645 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
646 while (true) {
647 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
648 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
649 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
650 break;
651 if (--count == 0) {
652 pr_err("Failed to reset ACP\n");
653 return -ETIMEDOUT;
654 }
655 udelay(100);
656 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530657 /* Disable ACP clock */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500658 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
659 val &= ~ACP_CONTROL__ClkEn_MASK;
660 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
661
662 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
663
664 while (true) {
665 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530666 if (!(val & (u32)0x1))
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500667 break;
668 if (--count == 0) {
669 pr_err("Failed to reset ACP\n");
670 return -ETIMEDOUT;
671 }
672 udelay(100);
673 }
674 return 0;
675}
676
677/* ACP DMA irq handler routine for playback, capture usecases */
678static irqreturn_t dma_irq_handler(int irq, void *arg)
679{
Akshu Agrawalbbdb7012018-08-06 12:57:14 +0530680 u16 dscr_idx;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500681 u32 intr_flag, ext_intr_status;
682 struct audio_drv_data *irq_data;
683 void __iomem *acp_mmio;
684 struct device *dev = arg;
685 bool valid_irq = false;
686
687 irq_data = dev_get_drvdata(dev);
688 acp_mmio = irq_data->acp_mmio;
689
690 ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
691 intr_flag = (((ext_intr_status &
692 ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
693 ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
694
695 if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
696 valid_irq = true;
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530697 snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500698 acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530699 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500700 }
701
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530702 if ((intr_flag & BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) != 0) {
703 valid_irq = true;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530704 snd_pcm_period_elapsed(irq_data->play_i2sbt_stream);
705 acp_reg_write((intr_flag &
706 BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) << 16,
707 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
708 }
709
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800710 if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500711 valid_irq = true;
Akshu Agrawalbbdb7012018-08-06 12:57:14 +0530712 if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_14) ==
713 CAPTURE_START_DMA_DESCR_CH15)
714 dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
715 else
716 dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
717 config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
718 1, 0);
719 acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);
720
Daniel Kurtz55af49ac2018-07-02 15:19:53 -0600721 snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800722 acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530723 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500724 }
725
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800726 if ((intr_flag & BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) != 0) {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530727 valid_irq = true;
Akshu Agrawalbbdb7012018-08-06 12:57:14 +0530728 if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_10) ==
729 CAPTURE_START_DMA_DESCR_CH11)
730 dscr_idx = CAPTURE_END_DMA_DESCR_CH10;
731 else
732 dscr_idx = CAPTURE_START_DMA_DESCR_CH10;
733 config_acp_dma_channel(acp_mmio,
734 ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
735 dscr_idx, 1, 0);
736 acp_dma_start(acp_mmio, ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
737 false);
738
Daniel Kurtz55af49ac2018-07-02 15:19:53 -0600739 snd_pcm_period_elapsed(irq_data->capture_i2sbt_stream);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530740 acp_reg_write((intr_flag &
Agrawal, Akshu19e023e2018-07-16 15:02:41 +0800741 BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) << 16,
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530742 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
743 }
744
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500745 if (valid_irq)
746 return IRQ_HANDLED;
747 else
748 return IRQ_NONE;
749}
750
751static int acp_dma_open(struct snd_pcm_substream *substream)
752{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500753 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500754 int ret = 0;
755 struct snd_pcm_runtime *runtime = substream->runtime;
756 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530757 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
758 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000759 struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500760 struct audio_substream_data *adata =
761 kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530762 if (!adata)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500763 return -ENOMEM;
764
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400765 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
766 switch (intr_data->asic_type) {
767 case CHIP_STONEY:
768 runtime->hw = acp_st_pcm_hardware_playback;
769 break;
770 default:
771 runtime->hw = acp_pcm_hardware_playback;
772 }
773 } else {
774 switch (intr_data->asic_type) {
775 case CHIP_STONEY:
776 runtime->hw = acp_st_pcm_hardware_capture;
777 break;
778 default:
779 runtime->hw = acp_pcm_hardware_capture;
780 }
781 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500782
783 ret = snd_pcm_hw_constraint_integer(runtime,
784 SNDRV_PCM_HW_PARAM_PERIODS);
785 if (ret < 0) {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000786 dev_err(component->dev, "set integer constraint failed\n");
Dan Carpentercde6bcd2016-01-13 15:20:02 +0300787 kfree(adata);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500788 return ret;
789 }
790
791 adata->acp_mmio = intr_data->acp_mmio;
792 runtime->private_data = adata;
793
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530794 /*
795 * Enable ACP irq, when neither playback or capture streams are
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500796 * active by the time when a new stream is being opened.
797 * This enablement is not required for another stream, if current
798 * stream is not closed
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530799 */
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530800 if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream &&
801 !intr_data->play_i2sbt_stream && !intr_data->capture_i2sbt_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500802 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
803
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500804 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530805 /*
806 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400807 * won't be turned off. The default state for SRAM banks is ON.
808 * Setting SRAM bank state code skipped for STONEY platform.
809 */
810 if (intr_data->asic_type != CHIP_STONEY) {
811 for (bank = 1; bank <= 4; bank++)
812 acp_set_sram_bank_state(intr_data->acp_mmio,
813 bank, true);
814 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500815 } else {
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400816 if (intr_data->asic_type != CHIP_STONEY) {
817 for (bank = 5; bank <= 8; bank++)
818 acp_set_sram_bank_state(intr_data->acp_mmio,
819 bank, true);
820 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500821 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500822
823 return 0;
824}
825
826static int acp_dma_hw_params(struct snd_pcm_substream *substream,
827 struct snd_pcm_hw_params *params)
828{
829 int status;
830 uint64_t size;
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530831 u32 val = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500832 struct page *pg;
833 struct snd_pcm_runtime *runtime;
834 struct audio_substream_data *rtd;
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400835 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530836 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
837 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000838 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530839 struct snd_soc_card *card = prtd->card;
840 struct acp_platform_info *pinfo = snd_soc_card_get_drvdata(card);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500841
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500842 runtime = substream->runtime;
843 rtd = runtime->private_data;
844
845 if (WARN_ON(!rtd))
846 return -EINVAL;
847
Akshu Agrawal2718c892018-06-21 12:58:17 +0800848 if (pinfo) {
Agrawal, Akshu6e56e5d2018-06-07 14:48:43 +0800849 rtd->i2s_instance = pinfo->i2s_instance;
Akshu Agrawal2718c892018-06-21 12:58:17 +0800850 rtd->capture_channel = pinfo->capture_channel;
851 }
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530852 if (adata->asic_type == CHIP_STONEY) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530853 val = acp_reg_read(adata->acp_mmio,
854 mmACP_I2S_16BIT_RESOLUTION_EN);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530855 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
856 switch (rtd->i2s_instance) {
857 case I2S_BT_INSTANCE:
858 val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
859 break;
860 case I2S_SP_INSTANCE:
861 default:
862 val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
863 }
864 } else {
865 switch (rtd->i2s_instance) {
866 case I2S_BT_INSTANCE:
867 val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
868 break;
869 case I2S_SP_INSTANCE:
870 default:
871 val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
872 }
873 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530874 acp_reg_write(val, adata->acp_mmio,
875 mmACP_I2S_16BIT_RESOLUTION_EN);
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530876 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530877
878 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530879 switch (rtd->i2s_instance) {
880 case I2S_BT_INSTANCE:
881 rtd->pte_offset = ACP_ST_BT_PLAYBACK_PTE_OFFSET;
882 rtd->ch1 = SYSRAM_TO_ACP_BT_INSTANCE_CH_NUM;
883 rtd->ch2 = ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM;
884 rtd->sram_bank = ACP_SRAM_BANK_3_ADDRESS;
885 rtd->destination = TO_BLUETOOTH;
886 rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH8;
887 rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH9;
888 rtd->byte_cnt_high_reg_offset =
889 mmACP_I2S_BT_TRANSMIT_BYTE_CNT_HIGH;
890 rtd->byte_cnt_low_reg_offset =
891 mmACP_I2S_BT_TRANSMIT_BYTE_CNT_LOW;
892 adata->play_i2sbt_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530893 break;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530894 case I2S_SP_INSTANCE:
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530895 default:
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530896 switch (adata->asic_type) {
897 case CHIP_STONEY:
898 rtd->pte_offset = ACP_ST_PLAYBACK_PTE_OFFSET;
899 break;
900 default:
901 rtd->pte_offset = ACP_PLAYBACK_PTE_OFFSET;
902 }
903 rtd->ch1 = SYSRAM_TO_ACP_CH_NUM;
904 rtd->ch2 = ACP_TO_I2S_DMA_CH_NUM;
905 rtd->sram_bank = ACP_SRAM_BANK_1_ADDRESS;
906 rtd->destination = TO_ACP_I2S_1;
907 rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH12;
908 rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH13;
909 rtd->byte_cnt_high_reg_offset =
910 mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH;
911 rtd->byte_cnt_low_reg_offset =
912 mmACP_I2S_TRANSMIT_BYTE_CNT_LOW;
913 adata->play_i2ssp_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530914 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530915 } else {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530916 switch (rtd->i2s_instance) {
917 case I2S_BT_INSTANCE:
918 rtd->pte_offset = ACP_ST_BT_CAPTURE_PTE_OFFSET;
Daniel Kurtz55af49ac2018-07-02 15:19:53 -0600919 rtd->ch1 = I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM;
920 rtd->ch2 = ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530921 rtd->sram_bank = ACP_SRAM_BANK_4_ADDRESS;
922 rtd->destination = FROM_BLUETOOTH;
923 rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH10;
924 rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH11;
925 rtd->byte_cnt_high_reg_offset =
926 mmACP_I2S_BT_RECEIVE_BYTE_CNT_HIGH;
927 rtd->byte_cnt_low_reg_offset =
928 mmACP_I2S_BT_RECEIVE_BYTE_CNT_LOW;
929 adata->capture_i2sbt_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530930 break;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530931 case I2S_SP_INSTANCE:
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530932 default:
933 rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
Daniel Kurtz55af49ac2018-07-02 15:19:53 -0600934 rtd->ch1 = I2S_TO_ACP_DMA_CH_NUM;
935 rtd->ch2 = ACP_TO_SYSRAM_CH_NUM;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530936 switch (adata->asic_type) {
937 case CHIP_STONEY:
938 rtd->pte_offset = ACP_ST_CAPTURE_PTE_OFFSET;
939 rtd->sram_bank = ACP_SRAM_BANK_2_ADDRESS;
940 break;
941 default:
942 rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
943 rtd->sram_bank = ACP_SRAM_BANK_5_ADDRESS;
944 }
945 rtd->destination = FROM_ACP_I2S_1;
946 rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH14;
947 rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH15;
948 rtd->byte_cnt_high_reg_offset =
949 mmACP_I2S_RECEIVED_BYTE_CNT_HIGH;
950 rtd->byte_cnt_low_reg_offset =
951 mmACP_I2S_RECEIVED_BYTE_CNT_LOW;
952 adata->capture_i2ssp_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530953 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530954 }
955
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500956 size = params_buffer_bytes(params);
957 status = snd_pcm_lib_malloc_pages(substream, size);
958 if (status < 0)
959 return status;
960
961 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
962 pg = virt_to_page(substream->dma_buffer.area);
963
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530964 if (pg) {
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500965 acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500966 /* Save for runtime private data */
967 rtd->pg = pg;
968 rtd->order = get_order(size);
969
970 /* Fill the page table entries in ACP SRAM */
971 rtd->pg = pg;
972 rtd->size = size;
973 rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
974 rtd->direction = substream->stream;
975
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400976 config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500977 status = 0;
978 } else {
979 status = -ENOMEM;
980 }
981 return status;
982}
983
984static int acp_dma_hw_free(struct snd_pcm_substream *substream)
985{
986 return snd_pcm_lib_free_pages(substream);
987}
988
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530989static u64 acp_get_byte_count(struct audio_substream_data *rtd)
Vijendar Mukunda61add812017-11-03 16:35:43 -0400990{
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530991 union acp_dma_count byte_count;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400992
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530993 byte_count.bcount.high = acp_reg_read(rtd->acp_mmio,
994 rtd->byte_cnt_high_reg_offset);
995 byte_count.bcount.low = acp_reg_read(rtd->acp_mmio,
996 rtd->byte_cnt_low_reg_offset);
997 return byte_count.bytescount;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400998}
999
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001000static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
1001{
Vijendar Mukunda61add812017-11-03 16:35:43 -04001002 u32 buffersize;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001003 u32 pos = 0;
Vijendar Mukunda61add812017-11-03 16:35:43 -04001004 u64 bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001005
1006 struct snd_pcm_runtime *runtime = substream->runtime;
1007 struct audio_substream_data *rtd = runtime->private_data;
1008
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301009 if (!rtd)
1010 return -EINVAL;
1011
Vijendar Mukunda61add812017-11-03 16:35:43 -04001012 buffersize = frames_to_bytes(runtime, runtime->buffer_size);
Vijendar Mukunda7f004842018-05-08 10:17:45 +05301013 bytescount = acp_get_byte_count(rtd);
Vijendar Mukunda61add812017-11-03 16:35:43 -04001014
Daniel Kurtz715cdce2018-07-02 15:19:52 -06001015 bytescount -= rtd->bytescount;
Guenter Roeck7db08b22017-11-08 16:34:54 -05001016 pos = do_div(bytescount, buffersize);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001017 return bytes_to_frames(runtime, pos);
1018}
1019
1020static int acp_dma_mmap(struct snd_pcm_substream *substream,
1021 struct vm_area_struct *vma)
1022{
1023 return snd_pcm_lib_default_mmap(substream, vma);
1024}
1025
1026static int acp_dma_prepare(struct snd_pcm_substream *substream)
1027{
1028 struct snd_pcm_runtime *runtime = substream->runtime;
1029 struct audio_substream_data *rtd = runtime->private_data;
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +08001030 u16 ch_acp_sysmem, ch_acp_i2s;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001031
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301032 if (!rtd)
1033 return -EINVAL;
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301034
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +08001035 if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1036 ch_acp_sysmem = rtd->ch1;
1037 ch_acp_i2s = rtd->ch2;
1038 } else {
1039 ch_acp_i2s = rtd->ch1;
1040 ch_acp_sysmem = rtd->ch2;
1041 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301042 config_acp_dma_channel(rtd->acp_mmio,
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +08001043 ch_acp_sysmem,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301044 rtd->dma_dscr_idx_1,
1045 NUM_DSCRS_PER_CHANNEL, 0);
1046 config_acp_dma_channel(rtd->acp_mmio,
Agrawal, Akshufa9d2f12018-07-16 15:02:40 +08001047 ch_acp_i2s,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301048 rtd->dma_dscr_idx_2,
1049 NUM_DSCRS_PER_CHANNEL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001050 return 0;
1051}
1052
1053static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
1054{
1055 int ret;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001056
1057 struct snd_pcm_runtime *runtime = substream->runtime;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001058 struct audio_substream_data *rtd = runtime->private_data;
1059
1060 if (!rtd)
1061 return -EINVAL;
1062 switch (cmd) {
1063 case SNDRV_PCM_TRIGGER_START:
1064 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1065 case SNDRV_PCM_TRIGGER_RESUME:
Daniel Kurtz1a337a12018-07-02 15:19:51 -06001066 rtd->bytescount = acp_get_byte_count(rtd);
Daniel Kurtzdf61f9f2018-07-02 15:19:55 -06001067 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Akshu Agrawal2718c892018-06-21 12:58:17 +08001068 if (rtd->capture_channel == CAP_CHANNEL0) {
1069 acp_dma_cap_channel_disable(rtd->acp_mmio,
1070 CAP_CHANNEL1);
1071 acp_dma_cap_channel_enable(rtd->acp_mmio,
1072 CAP_CHANNEL0);
1073 }
1074 if (rtd->capture_channel == CAP_CHANNEL1) {
1075 acp_dma_cap_channel_disable(rtd->acp_mmio,
1076 CAP_CHANNEL0);
1077 acp_dma_cap_channel_enable(rtd->acp_mmio,
1078 CAP_CHANNEL1);
1079 }
Akshu Agrawalbbdb7012018-08-06 12:57:14 +05301080 acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
1081 } else {
1082 acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
1083 acp_dma_start(rtd->acp_mmio, rtd->ch2, true);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001084 }
1085 ret = 0;
1086 break;
1087 case SNDRV_PCM_TRIGGER_STOP:
1088 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1089 case SNDRV_PCM_TRIGGER_SUSPEND:
Daniel Kurtz30896d32018-07-02 15:19:50 -06001090 acp_dma_stop(rtd->acp_mmio, rtd->ch2);
1091 ret = acp_dma_stop(rtd->acp_mmio, rtd->ch1);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001092 break;
1093 default:
1094 ret = -EINVAL;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001095 }
1096 return ret;
1097}
1098
1099static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
1100{
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001101 int ret;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301102 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
1103 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001104 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001105
1106 switch (adata->asic_type) {
1107 case CHIP_STONEY:
1108 ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301109 SNDRV_DMA_TYPE_DEV,
1110 NULL, ST_MIN_BUFFER,
1111 ST_MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001112 break;
1113 default:
1114 ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301115 SNDRV_DMA_TYPE_DEV,
1116 NULL, MIN_BUFFER,
1117 MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001118 break;
1119 }
1120 if (ret < 0)
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001121 dev_err(component->dev,
Colin Ian King9e6a4692018-05-01 09:20:01 +01001122 "buffer preallocation failure error:%d\n", ret);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001123 return ret;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001124}
1125
1126static int acp_dma_close(struct snd_pcm_substream *substream)
1127{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001128 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 struct audio_substream_data *rtd = runtime->private_data;
1131 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301132 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
1133 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001134 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001135
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001136 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301137 switch (rtd->i2s_instance) {
1138 case I2S_BT_INSTANCE:
1139 adata->play_i2sbt_stream = NULL;
1140 break;
1141 case I2S_SP_INSTANCE:
1142 default:
1143 adata->play_i2ssp_stream = NULL;
1144 /*
1145 * For Stoney, Memory gating is disabled,i.e SRAM Banks
1146 * won't be turned off. The default state for SRAM banks
1147 * is ON.Setting SRAM bank state code skipped for STONEY
1148 * platform. Added condition checks for Carrizo platform
1149 * only.
1150 */
1151 if (adata->asic_type != CHIP_STONEY) {
1152 for (bank = 1; bank <= 4; bank++)
1153 acp_set_sram_bank_state(adata->acp_mmio,
1154 bank, false);
1155 }
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001156 }
1157 } else {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301158 switch (rtd->i2s_instance) {
1159 case I2S_BT_INSTANCE:
1160 adata->capture_i2sbt_stream = NULL;
1161 break;
1162 case I2S_SP_INSTANCE:
1163 default:
1164 adata->capture_i2ssp_stream = NULL;
1165 if (adata->asic_type != CHIP_STONEY) {
1166 for (bank = 5; bank <= 8; bank++)
1167 acp_set_sram_bank_state(adata->acp_mmio,
1168 bank, false);
1169 }
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001170 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001171 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001172
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301173 /*
1174 * Disable ACP irq, when the current stream is being closed and
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001175 * another stream is also not active.
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301176 */
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301177 if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream &&
1178 !adata->play_i2sbt_stream && !adata->capture_i2sbt_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001179 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
Mukunda, Vijendarcac6f592018-05-08 10:17:49 +05301180 kfree(rtd);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001181 return 0;
1182}
1183
Julia Lawall115c7252016-09-08 02:35:23 +02001184static const struct snd_pcm_ops acp_dma_ops = {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001185 .open = acp_dma_open,
1186 .close = acp_dma_close,
1187 .ioctl = snd_pcm_lib_ioctl,
1188 .hw_params = acp_dma_hw_params,
1189 .hw_free = acp_dma_hw_free,
1190 .trigger = acp_dma_trigger,
1191 .pointer = acp_dma_pointer,
1192 .mmap = acp_dma_mmap,
1193 .prepare = acp_dma_prepare,
1194};
1195
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301196static const struct snd_soc_component_driver acp_asoc_platform = {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001197 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001198 .ops = &acp_dma_ops,
1199 .pcm_new = acp_dma_new,
1200};
1201
1202static int acp_audio_probe(struct platform_device *pdev)
1203{
1204 int status;
1205 struct audio_drv_data *audio_drv_data;
1206 struct resource *res;
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001207 const u32 *pdata = pdev->dev.platform_data;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001208
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001209 if (!pdata) {
1210 dev_err(&pdev->dev, "Missing platform data\n");
1211 return -ENODEV;
1212 }
1213
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001214 audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301215 GFP_KERNEL);
1216 if (!audio_drv_data)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001217 return -ENOMEM;
1218
1219 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1220 audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001221 if (IS_ERR(audio_drv_data->acp_mmio))
1222 return PTR_ERR(audio_drv_data->acp_mmio);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001223
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301224 /*
1225 * The following members gets populated in device 'open'
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001226 * function. Till then interrupts are disabled in 'acp_init'
1227 * and device doesn't generate any interrupts.
1228 */
1229
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301230 audio_drv_data->play_i2ssp_stream = NULL;
1231 audio_drv_data->capture_i2ssp_stream = NULL;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301232 audio_drv_data->play_i2sbt_stream = NULL;
1233 audio_drv_data->capture_i2sbt_stream = NULL;
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301234
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001235 audio_drv_data->asic_type = *pdata;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001236
1237 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1238 if (!res) {
1239 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
1240 return -ENODEV;
1241 }
1242
1243 status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301244 0, "ACP_IRQ", &pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001245 if (status) {
1246 dev_err(&pdev->dev, "ACP IRQ request failed\n");
1247 return status;
1248 }
1249
1250 dev_set_drvdata(&pdev->dev, audio_drv_data);
1251
1252 /* Initialize the ACP */
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301253 status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
1254 if (status) {
1255 dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
1256 return status;
1257 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001258
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001259 status = devm_snd_soc_register_component(&pdev->dev,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301260 &acp_asoc_platform, NULL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001261 if (status != 0) {
1262 dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
1263 return status;
1264 }
1265
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001266 pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
1267 pm_runtime_use_autosuspend(&pdev->dev);
1268 pm_runtime_enable(&pdev->dev);
1269
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001270 return status;
1271}
1272
1273static int acp_audio_remove(struct platform_device *pdev)
1274{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301275 int status;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001276 struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
1277
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301278 status = acp_deinit(adata->acp_mmio);
1279 if (status)
1280 dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001281 pm_runtime_disable(&pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001282
1283 return 0;
1284}
1285
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001286static int acp_pcm_resume(struct device *dev)
1287{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001288 u16 bank;
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301289 int status;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301290 struct audio_substream_data *rtd;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001291 struct audio_drv_data *adata = dev_get_drvdata(dev);
1292
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301293 status = acp_init(adata->acp_mmio, adata->asic_type);
1294 if (status) {
1295 dev_err(dev, "ACP Init failed status:%d\n", status);
1296 return status;
1297 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001298
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301299 if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301300 /*
1301 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001302 * won't be turned off. The default state for SRAM banks is ON.
1303 * Setting SRAM bank state code skipped for STONEY platform.
1304 */
1305 if (adata->asic_type != CHIP_STONEY) {
1306 for (bank = 1; bank <= 4; bank++)
1307 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301308 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001309 }
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301310 rtd = adata->play_i2ssp_stream->runtime->private_data;
1311 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001312 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301313 if (adata->capture_i2ssp_stream &&
1314 adata->capture_i2ssp_stream->runtime) {
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001315 if (adata->asic_type != CHIP_STONEY) {
1316 for (bank = 5; bank <= 8; bank++)
1317 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301318 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001319 }
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301320 rtd = adata->capture_i2ssp_stream->runtime->private_data;
1321 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1322 }
1323 if (adata->asic_type != CHIP_CARRIZO) {
1324 if (adata->play_i2sbt_stream &&
1325 adata->play_i2sbt_stream->runtime) {
1326 rtd = adata->play_i2sbt_stream->runtime->private_data;
1327 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1328 }
1329 if (adata->capture_i2sbt_stream &&
1330 adata->capture_i2sbt_stream->runtime) {
1331 rtd = adata->capture_i2sbt_stream->runtime->private_data;
1332 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1333 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001334 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001335 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1336 return 0;
1337}
1338
1339static int acp_pcm_runtime_suspend(struct device *dev)
1340{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301341 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001342 struct audio_drv_data *adata = dev_get_drvdata(dev);
1343
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301344 status = acp_deinit(adata->acp_mmio);
1345 if (status)
1346 dev_err(dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001347 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1348 return 0;
1349}
1350
1351static int acp_pcm_runtime_resume(struct device *dev)
1352{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301353 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001354 struct audio_drv_data *adata = dev_get_drvdata(dev);
1355
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301356 status = acp_init(adata->acp_mmio, adata->asic_type);
1357 if (status) {
1358 dev_err(dev, "ACP Init failed status:%d\n", status);
1359 return status;
1360 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001361 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1362 return 0;
1363}
1364
1365static const struct dev_pm_ops acp_pm_ops = {
1366 .resume = acp_pcm_resume,
1367 .runtime_suspend = acp_pcm_runtime_suspend,
1368 .runtime_resume = acp_pcm_runtime_resume,
1369};
1370
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001371static struct platform_driver acp_dma_driver = {
1372 .probe = acp_audio_probe,
1373 .remove = acp_audio_remove,
1374 .driver = {
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001375 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001376 .pm = &acp_pm_ops,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001377 },
1378};
1379
1380module_platform_driver(acp_dma_driver);
1381
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001382MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001383MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
1384MODULE_DESCRIPTION("AMD ACP PCM Driver");
1385MODULE_LICENSE("GPL v2");
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001386MODULE_ALIAS("platform:"DRV_NAME);