blob: 3c3d398d0d0b1c2a5861ccccb8df1a3e01544b73 [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 |=
227 BIT(22) |
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530228 (ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400229 (size / 2);
230 break;
231 default:
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400232 dmadscr[i].xfer_val |=
233 BIT(22) |
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530234 (ACP_DMA_ATTR_SHAREDMEM_TO_DAGB_ONION << 16) |
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400235 (size / 2);
236 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500237 }
238 config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530239 &dmadscr[i]);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500240 }
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530241 config_acp_dma_channel(acp_mmio, ch,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530242 dma_dscr_idx - 1,
243 NUM_DSCRS_PER_CHANNEL,
244 ACP_DMA_PRIORITY_LEVEL_NORMAL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500245}
246
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530247/*
248 * Initialize the DMA descriptor information for transfer between
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500249 * ACP SRAM <-> I2S
250 */
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530251static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio, u32 size,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530252 int direction, u32 sram_bank,
253 u16 destination, u16 ch,
254 u16 dma_dscr_idx, u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500255{
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500256 u16 i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500257 acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
258
259 for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
260 dmadscr[i].xfer_val = 0;
261 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530262 dma_dscr_idx = dma_dscr_idx + i;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530263 dmadscr[i].src = sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500264 /* dmadscr[i].dest is unused by hardware. */
265 dmadscr[i].dest = 0;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530266 dmadscr[i].xfer_val |= BIT(22) | (destination << 16) |
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500267 (size / 2);
268 } else {
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530269 dma_dscr_idx = dma_dscr_idx + i;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500270 /* dmadscr[i].src is unused by hardware. */
271 dmadscr[i].src = 0;
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530272 dmadscr[i].dest =
273 sram_bank + (i * (size / 2));
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500274 dmadscr[i].xfer_val |= BIT(22) |
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530275 (destination << 16) | (size / 2);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500276 }
277 config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530278 &dmadscr[i]);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500279 }
280 /* Configure the DMA channel with the above descriptore */
Mukunda, Vijendar4376a862018-02-16 13:03:47 +0530281 config_acp_dma_channel(acp_mmio, ch, dma_dscr_idx - 1,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530282 NUM_DSCRS_PER_CHANNEL,
283 ACP_DMA_PRIORITY_LEVEL_NORMAL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500284}
285
286/* Create page table entries in ACP SRAM for the allocated memory */
287static void acp_pte_config(void __iomem *acp_mmio, struct page *pg,
288 u16 num_of_pages, u32 pte_offset)
289{
290 u16 page_idx;
291 u64 addr;
292 u32 low;
293 u32 high;
294 u32 offset;
295
296 offset = ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
297 for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
298 /* Load the low address of page int ACP SRAM through SRBM */
299 acp_reg_write((offset + (page_idx * 8)),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530300 acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500301 addr = page_to_phys(pg);
302
303 low = lower_32_bits(addr);
304 high = upper_32_bits(addr);
305
306 acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
307
308 /* Load the High address of page int ACP SRAM through SRBM */
309 acp_reg_write((offset + (page_idx * 8) + 4),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530310 acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500311
312 /* page enable in ACP */
313 high |= BIT(31);
314 acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
315
316 /* Move to next physically contiguos page */
317 pg++;
318 }
319}
320
321static void config_acp_dma(void __iomem *acp_mmio,
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530322 struct audio_substream_data *rtd,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530323 u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500324{
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);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500327 /* Configure System memory <-> ACP SRAM DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530328 set_acp_sysmem_dma_descriptors(acp_mmio, rtd->size,
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530329 rtd->direction, rtd->pte_offset,
Mukunda, Vijendar18e8a402018-05-08 10:17:48 +0530330 rtd->ch1, rtd->sram_bank,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530331 rtd->dma_dscr_idx_1, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500332 /* Configure ACP SRAM <-> I2S DMA descriptors */
Mukunda, Vijendar8349b7f2018-04-26 16:45:47 +0530333 set_acp_to_i2s_dma_descriptors(acp_mmio, rtd->size,
Mukunda, Vijendar18e8a402018-05-08 10:17:48 +0530334 rtd->direction, rtd->sram_bank,
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530335 rtd->destination, rtd->ch2,
336 rtd->dma_dscr_idx_2, asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500337}
338
Akshu Agrawal2718c892018-06-21 12:58:17 +0800339static void acp_dma_cap_channel_enable(void __iomem *acp_mmio,
340 u16 cap_channel)
341{
342 u32 val, ch_reg, imr_reg, res_reg;
343
344 switch (cap_channel) {
345 case CAP_CHANNEL1:
346 ch_reg = mmACP_I2SMICSP_RER1;
347 res_reg = mmACP_I2SMICSP_RCR1;
348 imr_reg = mmACP_I2SMICSP_IMR1;
349 break;
350 case CAP_CHANNEL0:
351 default:
352 ch_reg = mmACP_I2SMICSP_RER0;
353 res_reg = mmACP_I2SMICSP_RCR0;
354 imr_reg = mmACP_I2SMICSP_IMR0;
355 break;
356 }
357 val = acp_reg_read(acp_mmio,
358 mmACP_I2S_16BIT_RESOLUTION_EN);
359 if (val & ACP_I2S_MIC_16BIT_RESOLUTION_EN) {
360 acp_reg_write(0x0, acp_mmio, ch_reg);
361 /* Set 16bit resolution on capture */
362 acp_reg_write(0x2, acp_mmio, res_reg);
363 }
364 val = acp_reg_read(acp_mmio, imr_reg);
365 val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
366 val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
367 acp_reg_write(val, acp_mmio, imr_reg);
368 acp_reg_write(0x1, acp_mmio, ch_reg);
369}
370
371static void acp_dma_cap_channel_disable(void __iomem *acp_mmio,
372 u16 cap_channel)
373{
374 u32 val, ch_reg, imr_reg;
375
376 switch (cap_channel) {
377 case CAP_CHANNEL1:
378 imr_reg = mmACP_I2SMICSP_IMR1;
379 ch_reg = mmACP_I2SMICSP_RER1;
380 break;
381 case CAP_CHANNEL0:
382 default:
383 imr_reg = mmACP_I2SMICSP_IMR0;
384 ch_reg = mmACP_I2SMICSP_RER0;
385 break;
386 }
387 val = acp_reg_read(acp_mmio, imr_reg);
388 val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
389 val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
390 acp_reg_write(val, acp_mmio, imr_reg);
391 acp_reg_write(0x0, acp_mmio, ch_reg);
392}
393
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500394/* Start a given DMA channel transfer */
Agrawal, Akshu6b116df2018-05-28 11:48:22 +0800395static void acp_dma_start(void __iomem *acp_mmio, u16 ch_num)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500396{
397 u32 dma_ctrl;
398
399 /* read the dma control register and disable the channel run field */
400 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
401
402 /* Invalidating the DAGB cache */
403 acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
404
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530405 /*
406 * configure the DMA channel and start the DMA transfer
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500407 * set dmachrun bit to start the transfer and enable the
408 * interrupt on completion of the dma transfer
409 */
410 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
411
412 switch (ch_num) {
413 case ACP_TO_I2S_DMA_CH_NUM:
414 case ACP_TO_SYSRAM_CH_NUM:
415 case I2S_TO_ACP_DMA_CH_NUM:
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530416 case ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM:
417 case ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM:
418 case I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM:
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500419 dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
420 break;
421 default:
422 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
423 break;
424 }
425
Agrawal, Akshu6b116df2018-05-28 11:48:22 +0800426 /* circular for both DMA channel */
427 dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500428
429 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
430}
431
432/* Stop a given DMA channel transfer */
433static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
434{
435 u32 dma_ctrl;
436 u32 dma_ch_sts;
437 u32 count = ACP_DMA_RESET_TIME;
438
439 dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
440
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530441 /*
442 * clear the dma control register fields before writing zero
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500443 * in reset bit
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530444 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500445 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
446 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
447
448 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
449 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
450
451 if (dma_ch_sts & BIT(ch_num)) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530452 /*
453 * set the reset bit for this channel to stop the dma
454 * transfer
455 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500456 dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
457 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
458 }
459
460 /* check the channel status bit for some time and return the status */
461 while (true) {
462 dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
463 if (!(dma_ch_sts & BIT(ch_num))) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530464 /*
465 * clear the reset flag after successfully stopping
466 * the dma transfer and break from the loop
467 */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500468 dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
469
470 acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530471 + ch_num);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500472 break;
473 }
474 if (--count == 0) {
475 pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
476 return -ETIMEDOUT;
477 }
478 udelay(100);
479 }
480 return 0;
481}
482
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500483static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530484 bool power_on)
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500485{
486 u32 val, req_reg, sts_reg, sts_reg_mask;
487 u32 loops = 1000;
488
489 if (bank < 32) {
490 req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
491 sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
492 sts_reg_mask = 0xFFFFFFFF;
493
494 } else {
495 bank -= 32;
496 req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
497 sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
498 sts_reg_mask = 0x0000FFFF;
499 }
500
501 val = acp_reg_read(acp_mmio, req_reg);
502 if (val & (1 << bank)) {
503 /* bank is in off state */
504 if (power_on == true)
505 /* request to on */
506 val &= ~(1 << bank);
507 else
508 /* request to off */
509 return;
510 } else {
511 /* bank is in on state */
512 if (power_on == false)
513 /* request to off */
514 val |= 1 << bank;
515 else
516 /* request to on */
517 return;
518 }
519 acp_reg_write(val, acp_mmio, req_reg);
520
521 while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
522 if (!loops--) {
523 pr_err("ACP SRAM bank %d state change failed\n", bank);
524 break;
525 }
526 cpu_relax();
527 }
528}
529
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500530/* Initialize and bring ACP hardware to default state. */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400531static int acp_init(void __iomem *acp_mmio, u32 asic_type)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500532{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500533 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500534 u32 val, count, sram_pte_offset;
535
536 /* Assert Soft reset of ACP */
537 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
538
539 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
540 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
541
542 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
543 while (true) {
544 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
545 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
546 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
547 break;
548 if (--count == 0) {
549 pr_err("Failed to reset ACP\n");
550 return -ETIMEDOUT;
551 }
552 udelay(100);
553 }
554
555 /* Enable clock to ACP and wait until the clock is enabled */
556 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
557 val = val | ACP_CONTROL__ClkEn_MASK;
558 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
559
560 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
561
562 while (true) {
563 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530564 if (val & (u32)0x1)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500565 break;
566 if (--count == 0) {
567 pr_err("Failed to reset ACP\n");
568 return -ETIMEDOUT;
569 }
570 udelay(100);
571 }
572
573 /* Deassert the SOFT RESET flags */
574 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
575 val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
576 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
577
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530578 /* For BT instance change pins from UART to BT */
579 if (!bt_uart_enable) {
580 val = acp_reg_read(acp_mmio, mmACP_BT_UART_PAD_SEL);
581 val |= ACP_BT_UART_PAD_SELECT_MASK;
582 acp_reg_write(val, acp_mmio, mmACP_BT_UART_PAD_SEL);
583 }
584
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500585 /* initiailize Onion control DAGB register */
586 acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530587 mmACP_AXI2DAGB_ONION_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500588
589 /* initiailize Garlic control DAGB registers */
590 acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530591 mmACP_AXI2DAGB_GARLIC_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500592
593 sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
594 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
595 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
596 ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
597 acp_reg_write(sram_pte_offset, acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
598 acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530599 mmACP_DAGB_PAGE_SIZE_GRP_1);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500600
601 acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530602 mmACP_DMA_DESC_BASE_ADDR);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500603
604 /* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
605 acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
606 acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530607 acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500608
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530609 /*
610 * When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500611 * Now, turn off all of them. This can't be done in 'poweron' of
612 * ACP pm domain, as this requires ACP to be initialized.
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400613 * For Stoney, Memory gating is disabled,i.e SRAM Banks
614 * won't be turned off. The default state for SRAM banks is ON.
615 * Setting SRAM bank state code skipped for STONEY platform.
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500616 */
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400617 if (asic_type != CHIP_STONEY) {
618 for (bank = 1; bank < 48; bank++)
619 acp_set_sram_bank_state(acp_mmio, bank, false);
620 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500621 return 0;
622}
623
Masahiro Yamada1cce2002017-02-27 14:29:45 -0800624/* Deinitialize ACP */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500625static int acp_deinit(void __iomem *acp_mmio)
626{
627 u32 val;
628 u32 count;
629
630 /* Assert Soft reset of ACP */
631 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
632
633 val |= ACP_SOFT_RESET__SoftResetAud_MASK;
634 acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
635
636 count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
637 while (true) {
638 val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
639 if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
640 (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
641 break;
642 if (--count == 0) {
643 pr_err("Failed to reset ACP\n");
644 return -ETIMEDOUT;
645 }
646 udelay(100);
647 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530648 /* Disable ACP clock */
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500649 val = acp_reg_read(acp_mmio, mmACP_CONTROL);
650 val &= ~ACP_CONTROL__ClkEn_MASK;
651 acp_reg_write(val, acp_mmio, mmACP_CONTROL);
652
653 count = ACP_CLOCK_EN_TIME_OUT_VALUE;
654
655 while (true) {
656 val = acp_reg_read(acp_mmio, mmACP_STATUS);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530657 if (!(val & (u32)0x1))
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500658 break;
659 if (--count == 0) {
660 pr_err("Failed to reset ACP\n");
661 return -ETIMEDOUT;
662 }
663 udelay(100);
664 }
665 return 0;
666}
667
668/* ACP DMA irq handler routine for playback, capture usecases */
669static irqreturn_t dma_irq_handler(int irq, void *arg)
670{
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500671 u32 intr_flag, ext_intr_status;
672 struct audio_drv_data *irq_data;
673 void __iomem *acp_mmio;
674 struct device *dev = arg;
675 bool valid_irq = false;
676
677 irq_data = dev_get_drvdata(dev);
678 acp_mmio = irq_data->acp_mmio;
679
680 ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
681 intr_flag = (((ext_intr_status &
682 ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
683 ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
684
685 if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
686 valid_irq = true;
Mukunda, Vijendare21358c2018-02-16 13:03:46 +0530687 snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500688 acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530689 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500690 }
691
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530692 if ((intr_flag & BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) != 0) {
693 valid_irq = true;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530694 snd_pcm_period_elapsed(irq_data->play_i2sbt_stream);
695 acp_reg_write((intr_flag &
696 BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) << 16,
697 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
698 }
699
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500700 if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
701 valid_irq = true;
Agrawal, Akshu6b116df2018-05-28 11:48:22 +0800702 snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500703 acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530704 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500705 }
706
707 if ((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) != 0) {
708 valid_irq = true;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500709 acp_reg_write((intr_flag & BIT(ACP_TO_SYSRAM_CH_NUM)) << 16,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530710 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500711 }
712
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530713 if ((intr_flag & BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) != 0) {
714 valid_irq = true;
Agrawal, Akshu6b116df2018-05-28 11:48:22 +0800715 snd_pcm_period_elapsed(irq_data->capture_i2sbt_stream);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530716 acp_reg_write((intr_flag &
717 BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) << 16,
718 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
719 }
720
721 if ((intr_flag & BIT(ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM)) != 0) {
722 valid_irq = true;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530723 acp_reg_write((intr_flag &
724 BIT(ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM)) << 16,
725 acp_mmio, mmACP_EXTERNAL_INTR_STAT);
726 }
727
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500728 if (valid_irq)
729 return IRQ_HANDLED;
730 else
731 return IRQ_NONE;
732}
733
734static int acp_dma_open(struct snd_pcm_substream *substream)
735{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500736 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500737 int ret = 0;
738 struct snd_pcm_runtime *runtime = substream->runtime;
739 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530740 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
741 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000742 struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500743 struct audio_substream_data *adata =
744 kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530745 if (!adata)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500746 return -ENOMEM;
747
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -0400748 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
749 switch (intr_data->asic_type) {
750 case CHIP_STONEY:
751 runtime->hw = acp_st_pcm_hardware_playback;
752 break;
753 default:
754 runtime->hw = acp_pcm_hardware_playback;
755 }
756 } else {
757 switch (intr_data->asic_type) {
758 case CHIP_STONEY:
759 runtime->hw = acp_st_pcm_hardware_capture;
760 break;
761 default:
762 runtime->hw = acp_pcm_hardware_capture;
763 }
764 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500765
766 ret = snd_pcm_hw_constraint_integer(runtime,
767 SNDRV_PCM_HW_PARAM_PERIODS);
768 if (ret < 0) {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000769 dev_err(component->dev, "set integer constraint failed\n");
Dan Carpentercde6bcd2016-01-13 15:20:02 +0300770 kfree(adata);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500771 return ret;
772 }
773
774 adata->acp_mmio = intr_data->acp_mmio;
775 runtime->private_data = adata;
776
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530777 /*
778 * Enable ACP irq, when neither playback or capture streams are
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500779 * active by the time when a new stream is being opened.
780 * This enablement is not required for another stream, if current
781 * stream is not closed
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530782 */
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530783 if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream &&
784 !intr_data->play_i2sbt_stream && !intr_data->capture_i2sbt_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500785 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
786
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500787 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530788 /*
789 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400790 * won't be turned off. The default state for SRAM banks is ON.
791 * Setting SRAM bank state code skipped for STONEY platform.
792 */
793 if (intr_data->asic_type != CHIP_STONEY) {
794 for (bank = 1; bank <= 4; bank++)
795 acp_set_sram_bank_state(intr_data->acp_mmio,
796 bank, true);
797 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500798 } else {
Vijendar Mukunda607b39e2017-10-18 12:13:57 -0400799 if (intr_data->asic_type != CHIP_STONEY) {
800 for (bank = 5; bank <= 8; bank++)
801 acp_set_sram_bank_state(intr_data->acp_mmio,
802 bank, true);
803 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500804 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500805
806 return 0;
807}
808
809static int acp_dma_hw_params(struct snd_pcm_substream *substream,
810 struct snd_pcm_hw_params *params)
811{
812 int status;
813 uint64_t size;
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530814 u32 val = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500815 struct page *pg;
816 struct snd_pcm_runtime *runtime;
817 struct audio_substream_data *rtd;
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400818 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530819 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
820 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +0000821 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530822 struct snd_soc_card *card = prtd->card;
823 struct acp_platform_info *pinfo = snd_soc_card_get_drvdata(card);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500824
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500825 runtime = substream->runtime;
826 rtd = runtime->private_data;
827
828 if (WARN_ON(!rtd))
829 return -EINVAL;
830
Akshu Agrawal2718c892018-06-21 12:58:17 +0800831 if (pinfo) {
Agrawal, Akshu6e56e5d2018-06-07 14:48:43 +0800832 rtd->i2s_instance = pinfo->i2s_instance;
Akshu Agrawal2718c892018-06-21 12:58:17 +0800833 rtd->capture_channel = pinfo->capture_channel;
834 }
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530835 if (adata->asic_type == CHIP_STONEY) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530836 val = acp_reg_read(adata->acp_mmio,
837 mmACP_I2S_16BIT_RESOLUTION_EN);
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530838 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
839 switch (rtd->i2s_instance) {
840 case I2S_BT_INSTANCE:
841 val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
842 break;
843 case I2S_SP_INSTANCE:
844 default:
845 val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
846 }
847 } else {
848 switch (rtd->i2s_instance) {
849 case I2S_BT_INSTANCE:
850 val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
851 break;
852 case I2S_SP_INSTANCE:
853 default:
854 val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
855 }
856 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530857 acp_reg_write(val, adata->acp_mmio,
858 mmACP_I2S_16BIT_RESOLUTION_EN);
Vijendar Mukundaa37d48e2018-03-09 21:13:02 +0530859 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530860
861 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530862 switch (rtd->i2s_instance) {
863 case I2S_BT_INSTANCE:
864 rtd->pte_offset = ACP_ST_BT_PLAYBACK_PTE_OFFSET;
865 rtd->ch1 = SYSRAM_TO_ACP_BT_INSTANCE_CH_NUM;
866 rtd->ch2 = ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM;
867 rtd->sram_bank = ACP_SRAM_BANK_3_ADDRESS;
868 rtd->destination = TO_BLUETOOTH;
869 rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH8;
870 rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH9;
871 rtd->byte_cnt_high_reg_offset =
872 mmACP_I2S_BT_TRANSMIT_BYTE_CNT_HIGH;
873 rtd->byte_cnt_low_reg_offset =
874 mmACP_I2S_BT_TRANSMIT_BYTE_CNT_LOW;
875 adata->play_i2sbt_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530876 break;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530877 case I2S_SP_INSTANCE:
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530878 default:
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530879 switch (adata->asic_type) {
880 case CHIP_STONEY:
881 rtd->pte_offset = ACP_ST_PLAYBACK_PTE_OFFSET;
882 break;
883 default:
884 rtd->pte_offset = ACP_PLAYBACK_PTE_OFFSET;
885 }
886 rtd->ch1 = SYSRAM_TO_ACP_CH_NUM;
887 rtd->ch2 = ACP_TO_I2S_DMA_CH_NUM;
888 rtd->sram_bank = ACP_SRAM_BANK_1_ADDRESS;
889 rtd->destination = TO_ACP_I2S_1;
890 rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH12;
891 rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH13;
892 rtd->byte_cnt_high_reg_offset =
893 mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH;
894 rtd->byte_cnt_low_reg_offset =
895 mmACP_I2S_TRANSMIT_BYTE_CNT_LOW;
896 adata->play_i2ssp_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530897 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530898 } else {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530899 switch (rtd->i2s_instance) {
900 case I2S_BT_INSTANCE:
901 rtd->pte_offset = ACP_ST_BT_CAPTURE_PTE_OFFSET;
902 rtd->ch1 = ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM;
903 rtd->ch2 = I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM;
904 rtd->sram_bank = ACP_SRAM_BANK_4_ADDRESS;
905 rtd->destination = FROM_BLUETOOTH;
906 rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH10;
907 rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH11;
908 rtd->byte_cnt_high_reg_offset =
909 mmACP_I2S_BT_RECEIVE_BYTE_CNT_HIGH;
910 rtd->byte_cnt_low_reg_offset =
911 mmACP_I2S_BT_RECEIVE_BYTE_CNT_LOW;
912 adata->capture_i2sbt_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530913 break;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530914 case I2S_SP_INSTANCE:
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530915 default:
916 rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +0530917 rtd->ch1 = ACP_TO_SYSRAM_CH_NUM;
918 rtd->ch2 = I2S_TO_ACP_DMA_CH_NUM;
919 switch (adata->asic_type) {
920 case CHIP_STONEY:
921 rtd->pte_offset = ACP_ST_CAPTURE_PTE_OFFSET;
922 rtd->sram_bank = ACP_SRAM_BANK_2_ADDRESS;
923 break;
924 default:
925 rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
926 rtd->sram_bank = ACP_SRAM_BANK_5_ADDRESS;
927 }
928 rtd->destination = FROM_ACP_I2S_1;
929 rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH14;
930 rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH15;
931 rtd->byte_cnt_high_reg_offset =
932 mmACP_I2S_RECEIVED_BYTE_CNT_HIGH;
933 rtd->byte_cnt_low_reg_offset =
934 mmACP_I2S_RECEIVED_BYTE_CNT_LOW;
935 adata->capture_i2ssp_stream = substream;
Mukunda, Vijendare188c522018-05-08 10:17:47 +0530936 }
Vijendar Mukunda8769bb52018-05-08 10:17:44 +0530937 }
938
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500939 size = params_buffer_bytes(params);
940 status = snd_pcm_lib_malloc_pages(substream, size);
941 if (status < 0)
942 return status;
943
944 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
945 pg = virt_to_page(substream->dma_buffer.area);
946
Mukunda, Vijendar13838c12018-04-17 10:29:52 +0530947 if (pg) {
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -0500948 acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500949 /* Save for runtime private data */
950 rtd->pg = pg;
951 rtd->order = get_order(size);
952
953 /* Fill the page table entries in ACP SRAM */
954 rtd->pg = pg;
955 rtd->size = size;
956 rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
957 rtd->direction = substream->stream;
958
Vijendar Mukundaaac89742017-10-18 12:13:58 -0400959 config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500960 status = 0;
961 } else {
962 status = -ENOMEM;
963 }
964 return status;
965}
966
967static int acp_dma_hw_free(struct snd_pcm_substream *substream)
968{
969 return snd_pcm_lib_free_pages(substream);
970}
971
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530972static u64 acp_get_byte_count(struct audio_substream_data *rtd)
Vijendar Mukunda61add812017-11-03 16:35:43 -0400973{
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530974 union acp_dma_count byte_count;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400975
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530976 byte_count.bcount.high = acp_reg_read(rtd->acp_mmio,
977 rtd->byte_cnt_high_reg_offset);
978 byte_count.bcount.low = acp_reg_read(rtd->acp_mmio,
979 rtd->byte_cnt_low_reg_offset);
980 return byte_count.bytescount;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400981}
982
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500983static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
984{
Vijendar Mukunda61add812017-11-03 16:35:43 -0400985 u32 buffersize;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500986 u32 pos = 0;
Vijendar Mukunda61add812017-11-03 16:35:43 -0400987 u64 bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -0500988
989 struct snd_pcm_runtime *runtime = substream->runtime;
990 struct audio_substream_data *rtd = runtime->private_data;
991
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +0530992 if (!rtd)
993 return -EINVAL;
994
Vijendar Mukunda61add812017-11-03 16:35:43 -0400995 buffersize = frames_to_bytes(runtime, runtime->buffer_size);
Vijendar Mukunda7f004842018-05-08 10:17:45 +0530996 bytescount = acp_get_byte_count(rtd);
Vijendar Mukunda61add812017-11-03 16:35:43 -0400997
Vijendar Mukunda9af89372018-05-08 10:17:46 +0530998 if (bytescount > rtd->bytescount)
999 bytescount -= rtd->bytescount;
Guenter Roeck7db08b22017-11-08 16:34:54 -05001000 pos = do_div(bytescount, buffersize);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001001 return bytes_to_frames(runtime, pos);
1002}
1003
1004static int acp_dma_mmap(struct snd_pcm_substream *substream,
1005 struct vm_area_struct *vma)
1006{
1007 return snd_pcm_lib_default_mmap(substream, vma);
1008}
1009
1010static int acp_dma_prepare(struct snd_pcm_substream *substream)
1011{
1012 struct snd_pcm_runtime *runtime = substream->runtime;
1013 struct audio_substream_data *rtd = runtime->private_data;
1014
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301015 if (!rtd)
1016 return -EINVAL;
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301017
1018 config_acp_dma_channel(rtd->acp_mmio,
1019 rtd->ch1,
1020 rtd->dma_dscr_idx_1,
1021 NUM_DSCRS_PER_CHANNEL, 0);
1022 config_acp_dma_channel(rtd->acp_mmio,
1023 rtd->ch2,
1024 rtd->dma_dscr_idx_2,
1025 NUM_DSCRS_PER_CHANNEL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001026 return 0;
1027}
1028
1029static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
1030{
1031 int ret;
Vijendar Mukunda61add812017-11-03 16:35:43 -04001032 u64 bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001033
1034 struct snd_pcm_runtime *runtime = substream->runtime;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001035 struct audio_substream_data *rtd = runtime->private_data;
1036
1037 if (!rtd)
1038 return -EINVAL;
1039 switch (cmd) {
1040 case SNDRV_PCM_TRIGGER_START:
1041 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1042 case SNDRV_PCM_TRIGGER_RESUME:
Vijendar Mukunda7f004842018-05-08 10:17:45 +05301043 bytescount = acp_get_byte_count(rtd);
Vijendar Mukunda9af89372018-05-08 10:17:46 +05301044 if (rtd->bytescount == 0)
1045 rtd->bytescount = bytescount;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001046 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Agrawal, Akshu6b116df2018-05-28 11:48:22 +08001047 acp_dma_start(rtd->acp_mmio, rtd->ch1);
1048 acp_dma_start(rtd->acp_mmio, rtd->ch2);
1049 } else {
Akshu Agrawal2718c892018-06-21 12:58:17 +08001050 if (rtd->capture_channel == CAP_CHANNEL0) {
1051 acp_dma_cap_channel_disable(rtd->acp_mmio,
1052 CAP_CHANNEL1);
1053 acp_dma_cap_channel_enable(rtd->acp_mmio,
1054 CAP_CHANNEL0);
1055 }
1056 if (rtd->capture_channel == CAP_CHANNEL1) {
1057 acp_dma_cap_channel_disable(rtd->acp_mmio,
1058 CAP_CHANNEL0);
1059 acp_dma_cap_channel_enable(rtd->acp_mmio,
1060 CAP_CHANNEL1);
1061 }
Agrawal, Akshu6b116df2018-05-28 11:48:22 +08001062 acp_dma_start(rtd->acp_mmio, rtd->ch2);
1063 acp_dma_start(rtd->acp_mmio, rtd->ch1);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001064 }
1065 ret = 0;
1066 break;
1067 case SNDRV_PCM_TRIGGER_STOP:
1068 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1069 case SNDRV_PCM_TRIGGER_SUSPEND:
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301070 /* For playback, non circular dma should be stopped first
1071 * i.e Sysram to acp dma transfer channel(rtd->ch1) should be
1072 * stopped before stopping cirular dma which is acp sram to i2s
1073 * fifo dma transfer channel(rtd->ch2). Where as in Capture
1074 * scenario, i2s fifo to acp sram dma channel(rtd->ch2) stopped
1075 * first before stopping acp sram to sysram which is circular
1076 * dma(rtd->ch1).
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001077 */
Vijendar Mukunda61add812017-11-03 16:35:43 -04001078 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301079 acp_dma_stop(rtd->acp_mmio, rtd->ch1);
1080 ret = acp_dma_stop(rtd->acp_mmio, rtd->ch2);
Vijendar Mukunda61add812017-11-03 16:35:43 -04001081 } else {
Vijendar Mukunda8769bb52018-05-08 10:17:44 +05301082 acp_dma_stop(rtd->acp_mmio, rtd->ch2);
1083 ret = acp_dma_stop(rtd->acp_mmio, rtd->ch1);
Vijendar Mukunda61add812017-11-03 16:35:43 -04001084 }
Vijendar Mukunda9af89372018-05-08 10:17:46 +05301085 rtd->bytescount = 0;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001086 break;
1087 default:
1088 ret = -EINVAL;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001089 }
1090 return ret;
1091}
1092
1093static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
1094{
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001095 int ret;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301096 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
1097 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001098 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001099
1100 switch (adata->asic_type) {
1101 case CHIP_STONEY:
1102 ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301103 SNDRV_DMA_TYPE_DEV,
1104 NULL, ST_MIN_BUFFER,
1105 ST_MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001106 break;
1107 default:
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, MIN_BUFFER,
1111 MAX_BUFFER);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001112 break;
1113 }
1114 if (ret < 0)
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001115 dev_err(component->dev,
Colin Ian King9e6a4692018-05-01 09:20:01 +01001116 "buffer preallocation failure error:%d\n", ret);
Vijendar Mukunda9c7d6fa2017-10-18 12:13:59 -04001117 return ret;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001118}
1119
1120static int acp_dma_close(struct snd_pcm_substream *substream)
1121{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001122 u16 bank;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001123 struct snd_pcm_runtime *runtime = substream->runtime;
1124 struct audio_substream_data *rtd = runtime->private_data;
1125 struct snd_soc_pcm_runtime *prtd = substream->private_data;
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301126 struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
1127 DRV_NAME);
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001128 struct audio_drv_data *adata = dev_get_drvdata(component->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001129
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301131 switch (rtd->i2s_instance) {
1132 case I2S_BT_INSTANCE:
1133 adata->play_i2sbt_stream = NULL;
1134 break;
1135 case I2S_SP_INSTANCE:
1136 default:
1137 adata->play_i2ssp_stream = NULL;
1138 /*
1139 * For Stoney, Memory gating is disabled,i.e SRAM Banks
1140 * won't be turned off. The default state for SRAM banks
1141 * is ON.Setting SRAM bank state code skipped for STONEY
1142 * platform. Added condition checks for Carrizo platform
1143 * only.
1144 */
1145 if (adata->asic_type != CHIP_STONEY) {
1146 for (bank = 1; bank <= 4; bank++)
1147 acp_set_sram_bank_state(adata->acp_mmio,
1148 bank, false);
1149 }
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001150 }
1151 } else {
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301152 switch (rtd->i2s_instance) {
1153 case I2S_BT_INSTANCE:
1154 adata->capture_i2sbt_stream = NULL;
1155 break;
1156 case I2S_SP_INSTANCE:
1157 default:
1158 adata->capture_i2ssp_stream = NULL;
1159 if (adata->asic_type != CHIP_STONEY) {
1160 for (bank = 5; bank <= 8; bank++)
1161 acp_set_sram_bank_state(adata->acp_mmio,
1162 bank, false);
1163 }
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001164 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001165 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001166
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301167 /*
1168 * Disable ACP irq, when the current stream is being closed and
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001169 * another stream is also not active.
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301170 */
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301171 if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream &&
1172 !adata->play_i2sbt_stream && !adata->capture_i2sbt_stream)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001173 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
Mukunda, Vijendarcac6f592018-05-08 10:17:49 +05301174 kfree(rtd);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001175 return 0;
1176}
1177
Julia Lawall115c7252016-09-08 02:35:23 +02001178static const struct snd_pcm_ops acp_dma_ops = {
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001179 .open = acp_dma_open,
1180 .close = acp_dma_close,
1181 .ioctl = snd_pcm_lib_ioctl,
1182 .hw_params = acp_dma_hw_params,
1183 .hw_free = acp_dma_hw_free,
1184 .trigger = acp_dma_trigger,
1185 .pointer = acp_dma_pointer,
1186 .mmap = acp_dma_mmap,
1187 .prepare = acp_dma_prepare,
1188};
1189
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301190static const struct snd_soc_component_driver acp_asoc_platform = {
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001191 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001192 .ops = &acp_dma_ops,
1193 .pcm_new = acp_dma_new,
1194};
1195
1196static int acp_audio_probe(struct platform_device *pdev)
1197{
1198 int status;
1199 struct audio_drv_data *audio_drv_data;
1200 struct resource *res;
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001201 const u32 *pdata = pdev->dev.platform_data;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001202
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001203 if (!pdata) {
1204 dev_err(&pdev->dev, "Missing platform data\n");
1205 return -ENODEV;
1206 }
1207
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001208 audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301209 GFP_KERNEL);
1210 if (!audio_drv_data)
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001211 return -ENOMEM;
1212
1213 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1214 audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
Guenter Roeckfdaa4512017-11-20 20:27:56 -08001215 if (IS_ERR(audio_drv_data->acp_mmio))
1216 return PTR_ERR(audio_drv_data->acp_mmio);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001217
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301218 /*
1219 * The following members gets populated in device 'open'
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001220 * function. Till then interrupts are disabled in 'acp_init'
1221 * and device doesn't generate any interrupts.
1222 */
1223
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301224 audio_drv_data->play_i2ssp_stream = NULL;
1225 audio_drv_data->capture_i2ssp_stream = NULL;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301226 audio_drv_data->play_i2sbt_stream = NULL;
1227 audio_drv_data->capture_i2sbt_stream = NULL;
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301228
Vijendar Mukundaa1b16aa2017-10-09 16:36:08 -04001229 audio_drv_data->asic_type = *pdata;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001230
1231 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1232 if (!res) {
1233 dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
1234 return -ENODEV;
1235 }
1236
1237 status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301238 0, "ACP_IRQ", &pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001239 if (status) {
1240 dev_err(&pdev->dev, "ACP IRQ request failed\n");
1241 return status;
1242 }
1243
1244 dev_set_drvdata(&pdev->dev, audio_drv_data);
1245
1246 /* Initialize the ACP */
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301247 status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
1248 if (status) {
1249 dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
1250 return status;
1251 }
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001252
Kuninori Morimotoa1042a42018-01-29 02:44:23 +00001253 status = devm_snd_soc_register_component(&pdev->dev,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301254 &acp_asoc_platform, NULL, 0);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001255 if (status != 0) {
1256 dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
1257 return status;
1258 }
1259
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001260 pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
1261 pm_runtime_use_autosuspend(&pdev->dev);
1262 pm_runtime_enable(&pdev->dev);
1263
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001264 return status;
1265}
1266
1267static int acp_audio_remove(struct platform_device *pdev)
1268{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301269 int status;
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001270 struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
1271
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301272 status = acp_deinit(adata->acp_mmio);
1273 if (status)
1274 dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001275 pm_runtime_disable(&pdev->dev);
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001276
1277 return 0;
1278}
1279
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001280static int acp_pcm_resume(struct device *dev)
1281{
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001282 u16 bank;
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301283 int status;
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301284 struct audio_substream_data *rtd;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001285 struct audio_drv_data *adata = dev_get_drvdata(dev);
1286
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301287 status = acp_init(adata->acp_mmio, adata->asic_type);
1288 if (status) {
1289 dev_err(dev, "ACP Init failed status:%d\n", status);
1290 return status;
1291 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001292
Mukunda, Vijendare21358c2018-02-16 13:03:46 +05301293 if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301294 /*
1295 * For Stoney, Memory gating is disabled,i.e SRAM Banks
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001296 * won't be turned off. The default state for SRAM banks is ON.
1297 * Setting SRAM bank state code skipped for STONEY platform.
1298 */
1299 if (adata->asic_type != CHIP_STONEY) {
1300 for (bank = 1; bank <= 4; bank++)
1301 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301302 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001303 }
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301304 rtd = adata->play_i2ssp_stream->runtime->private_data;
1305 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001306 }
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301307 if (adata->capture_i2ssp_stream &&
1308 adata->capture_i2ssp_stream->runtime) {
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001309 if (adata->asic_type != CHIP_STONEY) {
1310 for (bank = 5; bank <= 8; bank++)
1311 acp_set_sram_bank_state(adata->acp_mmio, bank,
Mukunda, Vijendar13838c12018-04-17 10:29:52 +05301312 true);
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001313 }
Mukunda, Vijendarccfbb4f2018-05-08 10:17:53 +05301314 rtd = adata->capture_i2ssp_stream->runtime->private_data;
1315 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1316 }
1317 if (adata->asic_type != CHIP_CARRIZO) {
1318 if (adata->play_i2sbt_stream &&
1319 adata->play_i2sbt_stream->runtime) {
1320 rtd = adata->play_i2sbt_stream->runtime->private_data;
1321 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1322 }
1323 if (adata->capture_i2sbt_stream &&
1324 adata->capture_i2sbt_stream->runtime) {
1325 rtd = adata->capture_i2sbt_stream->runtime->private_data;
1326 config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
1327 }
Maruthi Srinivas Bayyavarapuc36d9b32016-01-08 18:22:11 -05001328 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001329 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1330 return 0;
1331}
1332
1333static int acp_pcm_runtime_suspend(struct device *dev)
1334{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301335 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001336 struct audio_drv_data *adata = dev_get_drvdata(dev);
1337
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301338 status = acp_deinit(adata->acp_mmio);
1339 if (status)
1340 dev_err(dev, "ACP Deinit failed status:%d\n", status);
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001341 acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1342 return 0;
1343}
1344
1345static int acp_pcm_runtime_resume(struct device *dev)
1346{
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301347 int status;
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001348 struct audio_drv_data *adata = dev_get_drvdata(dev);
1349
Mukunda, Vijendar7afa5352017-12-04 20:46:24 +05301350 status = acp_init(adata->acp_mmio, adata->asic_type);
1351 if (status) {
1352 dev_err(dev, "ACP Init failed status:%d\n", status);
1353 return status;
1354 }
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001355 acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
1356 return 0;
1357}
1358
1359static const struct dev_pm_ops acp_pm_ops = {
1360 .resume = acp_pcm_resume,
1361 .runtime_suspend = acp_pcm_runtime_suspend,
1362 .runtime_resume = acp_pcm_runtime_resume,
1363};
1364
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001365static struct platform_driver acp_dma_driver = {
1366 .probe = acp_audio_probe,
1367 .remove = acp_audio_remove,
1368 .driver = {
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001369 .name = DRV_NAME,
Maruthi Srinivas Bayyavarapu1927da92016-01-08 18:22:10 -05001370 .pm = &acp_pm_ops,
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001371 },
1372};
1373
1374module_platform_driver(acp_dma_driver);
1375
Vijendar Mukunda607b39e2017-10-18 12:13:57 -04001376MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
Maruthi Srinivas Bayyavarapu7c313352016-01-08 18:22:09 -05001377MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
1378MODULE_DESCRIPTION("AMD ACP PCM Driver");
1379MODULE_LICENSE("GPL v2");
Akshu Agrawalbdd2a852017-11-08 12:24:02 -05001380MODULE_ALIAS("platform:"DRV_NAME);