1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2021 Advanced Micro Devices, Inc.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9
10 /*
11 * Generic interface for ACP audio blck PCM component
12 */
13
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/io.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-dai.h>
21 #include <linux/dma-mapping.h>
22
23 #include "amd.h"
24 #include "../mach-config.h"
25 #include "acp-mach.h"
26
27 #define DRV_NAME "acp_i2s_dma"
28
29 static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
30 .info = SNDRV_PCM_INFO_INTERLEAVED |
31 SNDRV_PCM_INFO_BLOCK_TRANSFER |
32 SNDRV_PCM_INFO_BATCH |
33 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
34 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
35 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
36 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
37 SNDRV_PCM_FMTBIT_S32_LE,
38 .channels_min = 2,
39 .channels_max = 8,
40 .rates = SNDRV_PCM_RATE_8000_96000,
41 .rate_min = 8000,
42 .rate_max = 96000,
43 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
44 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
45 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
46 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
47 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
48 };
49
50 static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
51 .info = SNDRV_PCM_INFO_INTERLEAVED |
52 SNDRV_PCM_INFO_BLOCK_TRANSFER |
53 SNDRV_PCM_INFO_BATCH |
54 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
55 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
56 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
57 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
58 SNDRV_PCM_FMTBIT_S32_LE,
59 .channels_min = 2,
60 .channels_max = 2,
61 .rates = SNDRV_PCM_RATE_8000_48000,
62 .rate_min = 8000,
63 .rate_max = 48000,
64 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
65 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
66 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
67 .periods_min = CAPTURE_MIN_NUM_PERIODS,
68 .periods_max = CAPTURE_MAX_NUM_PERIODS,
69 };
70
71 static const struct snd_pcm_hardware acp6x_pcm_hardware_playback = {
72 .info = SNDRV_PCM_INFO_INTERLEAVED |
73 SNDRV_PCM_INFO_BLOCK_TRANSFER |
74 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
75 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
76 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
77 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
78 SNDRV_PCM_FMTBIT_S32_LE,
79 .channels_min = 2,
80 .channels_max = 32,
81 .rates = SNDRV_PCM_RATE_8000_192000,
82 .rate_min = 8000,
83 .rate_max = 192000,
84 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
85 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
86 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
87 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
88 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
89 };
90
91 static const struct snd_pcm_hardware acp6x_pcm_hardware_capture = {
92 .info = SNDRV_PCM_INFO_INTERLEAVED |
93 SNDRV_PCM_INFO_BLOCK_TRANSFER |
94 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
95 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
96 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
97 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
98 SNDRV_PCM_FMTBIT_S32_LE,
99 .channels_min = 2,
100 .channels_max = 32,
101 .rates = SNDRV_PCM_RATE_8000_192000,
102 .rate_min = 8000,
103 .rate_max = 192000,
104 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
105 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
106 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
107 .periods_min = CAPTURE_MIN_NUM_PERIODS,
108 .periods_max = CAPTURE_MAX_NUM_PERIODS,
109 };
110
acp_machine_select(struct acp_dev_data * adata)111 int acp_machine_select(struct acp_dev_data *adata)
112 {
113 struct snd_soc_acpi_mach *mach;
114 int size, platform;
115
116 if (adata->flag == FLAG_AMD_LEGACY_ONLY_DMIC) {
117 platform = adata->platform;
118 adata->mach_dev = platform_device_register_data(adata->dev, "acp-pdm-mach",
119 PLATFORM_DEVID_NONE, &platform,
120 sizeof(platform));
121 } else {
122 size = sizeof(*adata->machines);
123 mach = snd_soc_acpi_find_machine(adata->machines);
124 if (!mach) {
125 dev_err(adata->dev, "warning: No matching ASoC machine driver found\n");
126 return -EINVAL;
127 }
128 adata->mach_dev = platform_device_register_data(adata->dev, mach->drv_name,
129 PLATFORM_DEVID_NONE, mach, size);
130 }
131 if (IS_ERR(adata->mach_dev))
132 dev_warn(adata->dev, "Unable to register Machine device\n");
133 return 0;
134 }
135 EXPORT_SYMBOL_NS_GPL(acp_machine_select, SND_SOC_ACP_COMMON);
136
i2s_irq_handler(int irq,void * data)137 static irqreturn_t i2s_irq_handler(int irq, void *data)
138 {
139 struct acp_dev_data *adata = data;
140 struct acp_resource *rsrc = adata->rsrc;
141 struct acp_stream *stream;
142 u16 i2s_flag = 0;
143 u32 ext_intr_stat, ext_intr_stat1;
144
145 if (!adata)
146 return IRQ_NONE;
147
148 if (adata->rsrc->no_of_ctrls == 2)
149 ext_intr_stat1 = readl(ACP_EXTERNAL_INTR_STAT(adata, (rsrc->irqp_used - 1)));
150
151 ext_intr_stat = readl(ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used));
152
153 spin_lock(&adata->acp_lock);
154 list_for_each_entry(stream, &adata->stream_list, list) {
155 if (ext_intr_stat & stream->irq_bit) {
156 writel(stream->irq_bit,
157 ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used));
158 snd_pcm_period_elapsed(stream->substream);
159 i2s_flag = 1;
160 }
161 if (adata->rsrc->no_of_ctrls == 2) {
162 if (ext_intr_stat1 & stream->irq_bit) {
163 writel(stream->irq_bit, ACP_EXTERNAL_INTR_STAT(adata,
164 (rsrc->irqp_used - 1)));
165 snd_pcm_period_elapsed(stream->substream);
166 i2s_flag = 1;
167 }
168 }
169 }
170 spin_unlock(&adata->acp_lock);
171 if (i2s_flag)
172 return IRQ_HANDLED;
173
174 return IRQ_NONE;
175 }
176
config_pte_for_stream(struct acp_dev_data * adata,struct acp_stream * stream)177 void config_pte_for_stream(struct acp_dev_data *adata, struct acp_stream *stream)
178 {
179 struct acp_resource *rsrc = adata->rsrc;
180 u32 reg_val;
181
182 reg_val = rsrc->sram_pte_offset;
183 stream->reg_offset = 0x02000000;
184
185 writel((reg_val + GRP1_OFFSET) | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_1);
186 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_1);
187
188 writel((reg_val + GRP2_OFFSET) | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_2);
189 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_2);
190
191 writel(reg_val | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_5);
192 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_5);
193
194 writel(0x01, adata->acp_base + ACPAXI2AXI_ATU_CTRL);
195 }
196 EXPORT_SYMBOL_NS_GPL(config_pte_for_stream, SND_SOC_ACP_COMMON);
197
config_acp_dma(struct acp_dev_data * adata,struct acp_stream * stream,int size)198 void config_acp_dma(struct acp_dev_data *adata, struct acp_stream *stream, int size)
199 {
200 struct snd_pcm_substream *substream = stream->substream;
201 struct acp_resource *rsrc = adata->rsrc;
202 dma_addr_t addr = substream->dma_buffer.addr;
203 int num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
204 u32 low, high, val;
205 u16 page_idx;
206
207 switch (adata->platform) {
208 case ACP70:
209 case ACP71:
210 switch (stream->dai_id) {
211 case I2S_SP_INSTANCE:
212 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK)
213 val = 0x0;
214 else
215 val = 0x1000;
216 break;
217 case I2S_BT_INSTANCE:
218 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK)
219 val = 0x2000;
220 else
221 val = 0x3000;
222 break;
223 case I2S_HS_INSTANCE:
224 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK)
225 val = 0x4000;
226 else
227 val = 0x5000;
228 break;
229 case DMIC_INSTANCE:
230 val = 0x6000;
231 break;
232 default:
233 dev_err(adata->dev, "Invalid dai id %x\n", stream->dai_id);
234 return;
235 }
236 break;
237 default:
238 val = stream->pte_offset;
239 break;
240 }
241
242 for (page_idx = 0; page_idx < num_pages; page_idx++) {
243 /* Load the low address of page int ACP SRAM through SRBM */
244 low = lower_32_bits(addr);
245 high = upper_32_bits(addr);
246 writel(low, adata->acp_base + rsrc->scratch_reg_offset + val);
247 high |= BIT(31);
248 writel(high, adata->acp_base + rsrc->scratch_reg_offset + val + 4);
249
250 /* Move to next physically contiguous page */
251 val += 8;
252 addr += PAGE_SIZE;
253 }
254 }
255 EXPORT_SYMBOL_NS_GPL(config_acp_dma, SND_SOC_ACP_COMMON);
256
acp_dma_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)257 static int acp_dma_open(struct snd_soc_component *component, struct snd_pcm_substream *substream)
258 {
259 struct snd_pcm_runtime *runtime = substream->runtime;
260 struct device *dev = component->dev;
261 struct acp_dev_data *adata = dev_get_drvdata(dev);
262 struct acp_chip_info *chip;
263 struct acp_stream *stream;
264 int ret;
265
266 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
267 if (!stream)
268 return -ENOMEM;
269
270 stream->substream = substream;
271 chip = dev_get_platdata(dev);
272 switch (chip->acp_rev) {
273 case ACP63_DEV:
274 case ACP70_DEV:
275 case ACP71_DEV:
276 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
277 runtime->hw = acp6x_pcm_hardware_playback;
278 else
279 runtime->hw = acp6x_pcm_hardware_capture;
280 break;
281 default:
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
283 runtime->hw = acp_pcm_hardware_playback;
284 else
285 runtime->hw = acp_pcm_hardware_capture;
286 break;
287 }
288
289 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, DMA_SIZE);
290 if (ret) {
291 dev_err(component->dev, "set hw constraint HW_PARAM_PERIOD_BYTES failed\n");
292 kfree(stream);
293 return ret;
294 }
295
296 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, DMA_SIZE);
297 if (ret) {
298 dev_err(component->dev, "set hw constraint HW_PARAM_BUFFER_BYTES failed\n");
299 kfree(stream);
300 return ret;
301 }
302
303 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
304 if (ret < 0) {
305 dev_err(component->dev, "set integer constraint failed\n");
306 kfree(stream);
307 return ret;
308 }
309 runtime->private_data = stream;
310
311 writel(1, ACP_EXTERNAL_INTR_ENB(adata));
312
313 spin_lock_irq(&adata->acp_lock);
314 list_add_tail(&stream->list, &adata->stream_list);
315 spin_unlock_irq(&adata->acp_lock);
316
317 return ret;
318 }
319
acp_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)320 static int acp_dma_hw_params(struct snd_soc_component *component,
321 struct snd_pcm_substream *substream,
322 struct snd_pcm_hw_params *params)
323 {
324 struct acp_dev_data *adata = snd_soc_component_get_drvdata(component);
325 struct acp_stream *stream = substream->runtime->private_data;
326 u64 size = params_buffer_bytes(params);
327
328 /* Configure ACP DMA block with params */
329 config_pte_for_stream(adata, stream);
330 config_acp_dma(adata, stream, size);
331
332 return 0;
333 }
334
acp_dma_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)335 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
336 struct snd_pcm_substream *substream)
337 {
338 struct device *dev = component->dev;
339 struct acp_dev_data *adata = dev_get_drvdata(dev);
340 struct acp_stream *stream = substream->runtime->private_data;
341 u32 pos, buffersize;
342 u64 bytescount;
343
344 buffersize = frames_to_bytes(substream->runtime,
345 substream->runtime->buffer_size);
346
347 bytescount = acp_get_byte_count(adata, stream->dai_id, substream->stream);
348
349 if (bytescount > stream->bytescount)
350 bytescount -= stream->bytescount;
351
352 pos = do_div(bytescount, buffersize);
353
354 return bytes_to_frames(substream->runtime, pos);
355 }
356
acp_dma_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)357 static int acp_dma_new(struct snd_soc_component *component,
358 struct snd_soc_pcm_runtime *rtd)
359 {
360 struct device *parent = component->dev->parent;
361
362 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
363 parent, MIN_BUFFER, MAX_BUFFER);
364 return 0;
365 }
366
acp_dma_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)367 static int acp_dma_close(struct snd_soc_component *component,
368 struct snd_pcm_substream *substream)
369 {
370 struct device *dev = component->dev;
371 struct acp_dev_data *adata = dev_get_drvdata(dev);
372 struct acp_stream *stream = substream->runtime->private_data;
373
374 /* Remove entry from list */
375 spin_lock_irq(&adata->acp_lock);
376 list_del(&stream->list);
377 spin_unlock_irq(&adata->acp_lock);
378 kfree(stream);
379
380 return 0;
381 }
382
383 static const struct snd_soc_component_driver acp_pcm_component = {
384 .name = DRV_NAME,
385 .open = acp_dma_open,
386 .close = acp_dma_close,
387 .hw_params = acp_dma_hw_params,
388 .pointer = acp_dma_pointer,
389 .pcm_construct = acp_dma_new,
390 .legacy_dai_naming = 1,
391 };
392
acp_platform_register(struct device * dev)393 int acp_platform_register(struct device *dev)
394 {
395 struct acp_dev_data *adata = dev_get_drvdata(dev);
396 struct snd_soc_dai_driver;
397 unsigned int status;
398
399 status = devm_request_irq(dev, adata->i2s_irq, i2s_irq_handler,
400 IRQF_SHARED, "ACP_I2S_IRQ", adata);
401 if (status) {
402 dev_err(dev, "ACP I2S IRQ request failed\n");
403 return status;
404 }
405
406 status = devm_snd_soc_register_component(dev, &acp_pcm_component,
407 adata->dai_driver,
408 adata->num_dai);
409 if (status) {
410 dev_err(dev, "Fail to register acp i2s component\n");
411 return status;
412 }
413
414 INIT_LIST_HEAD(&adata->stream_list);
415 spin_lock_init(&adata->acp_lock);
416
417 return 0;
418 }
419 EXPORT_SYMBOL_NS_GPL(acp_platform_register, SND_SOC_ACP_COMMON);
420
acp_platform_unregister(struct device * dev)421 int acp_platform_unregister(struct device *dev)
422 {
423 struct acp_dev_data *adata = dev_get_drvdata(dev);
424
425 if (adata->mach_dev)
426 platform_device_unregister(adata->mach_dev);
427 return 0;
428 }
429 EXPORT_SYMBOL_NS_GPL(acp_platform_unregister, SND_SOC_ACP_COMMON);
430
431 MODULE_DESCRIPTION("AMD ACP PCM Driver");
432 MODULE_LICENSE("Dual BSD/GPL");
433 MODULE_ALIAS(DRV_NAME);
434