xref: /openbmc/linux/sound/soc/amd/acp/acp-platform.c (revision 58d33131)
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/pm_runtime.h>
22 #include <linux/dma-mapping.h>
23 
24 #include "amd.h"
25 
26 #define DRV_NAME "acp_i2s_dma"
27 
28 static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
29 	.info = SNDRV_PCM_INFO_INTERLEAVED |
30 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
31 		SNDRV_PCM_INFO_BATCH |
32 		SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
33 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
34 	.formats = SNDRV_PCM_FMTBIT_S16_LE |  SNDRV_PCM_FMTBIT_S8 |
35 		   SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
36 		   SNDRV_PCM_FMTBIT_S32_LE,
37 	.channels_min = 2,
38 	.channels_max = 8,
39 	.rates = SNDRV_PCM_RATE_8000_96000,
40 	.rate_min = 8000,
41 	.rate_max = 96000,
42 	.buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
43 	.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
44 	.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
45 	.periods_min = PLAYBACK_MIN_NUM_PERIODS,
46 	.periods_max = PLAYBACK_MAX_NUM_PERIODS,
47 };
48 
49 static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
50 	.info = SNDRV_PCM_INFO_INTERLEAVED |
51 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
52 		SNDRV_PCM_INFO_BATCH |
53 		SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
54 		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
55 	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 |
56 		   SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE |
57 		   SNDRV_PCM_FMTBIT_S32_LE,
58 	.channels_min = 2,
59 	.channels_max = 2,
60 	.rates = SNDRV_PCM_RATE_8000_48000,
61 	.rate_min = 8000,
62 	.rate_max = 48000,
63 	.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
64 	.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
65 	.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
66 	.periods_min = CAPTURE_MIN_NUM_PERIODS,
67 	.periods_max = CAPTURE_MAX_NUM_PERIODS,
68 };
69 
70 int acp_machine_select(struct acp_dev_data *adata)
71 {
72 	struct snd_soc_acpi_mach *mach;
73 	int size;
74 
75 	size = sizeof(*adata->machines);
76 	mach = snd_soc_acpi_find_machine(adata->machines);
77 	if (!mach) {
78 		dev_err(adata->dev, "warning: No matching ASoC machine driver found\n");
79 		return -EINVAL;
80 	}
81 
82 	adata->mach_dev = platform_device_register_data(adata->dev, mach->drv_name,
83 							PLATFORM_DEVID_NONE, mach, size);
84 	if (IS_ERR(adata->mach_dev))
85 		dev_warn(adata->dev, "Unable to register Machine device\n");
86 
87 	return 0;
88 }
89 EXPORT_SYMBOL_NS_GPL(acp_machine_select, SND_SOC_ACP_COMMON);
90 
91 static irqreturn_t i2s_irq_handler(int irq, void *data)
92 {
93 	struct acp_dev_data *adata = data;
94 	struct acp_resource *rsrc = adata->rsrc;
95 	struct acp_stream *stream;
96 	u16 i2s_flag = 0;
97 	u32 ext_intr_stat, ext_intr_stat1, i;
98 
99 	if (!adata)
100 		return IRQ_NONE;
101 
102 	if (adata->rsrc->no_of_ctrls == 2)
103 		ext_intr_stat1 = readl(ACP_EXTERNAL_INTR_STAT(adata, (rsrc->irqp_used - 1)));
104 
105 	ext_intr_stat = readl(ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used));
106 
107 	for (i = 0; i < ACP_MAX_STREAM; i++) {
108 		stream = adata->stream[i];
109 		if (stream && (ext_intr_stat & stream->irq_bit)) {
110 			writel(stream->irq_bit,
111 			       ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used));
112 			snd_pcm_period_elapsed(stream->substream);
113 			i2s_flag = 1;
114 			break;
115 		}
116 		if (adata->rsrc->no_of_ctrls == 2) {
117 			if (stream && (ext_intr_stat1 & stream->irq_bit)) {
118 				writel(stream->irq_bit, ACP_EXTERNAL_INTR_STAT(adata,
119 				       (rsrc->irqp_used - 1)));
120 				snd_pcm_period_elapsed(stream->substream);
121 				i2s_flag = 1;
122 				break;
123 			}
124 		}
125 	}
126 	if (i2s_flag)
127 		return IRQ_HANDLED;
128 
129 	return IRQ_NONE;
130 }
131 
132 static void config_pte_for_stream(struct acp_dev_data *adata, struct acp_stream *stream)
133 {
134 	struct acp_resource *rsrc = adata->rsrc;
135 	u32 pte_reg, pte_size, reg_val;
136 
137 	/* Use ATU base Group5 */
138 	pte_reg = ACPAXI2AXI_ATU_BASE_ADDR_GRP_5;
139 	pte_size =  ACPAXI2AXI_ATU_PAGE_SIZE_GRP_5;
140 	stream->reg_offset = 0x02000000;
141 
142 	/* Group Enable */
143 	reg_val = rsrc->sram_pte_offset;
144 	writel(reg_val | BIT(31), adata->acp_base + pte_reg);
145 	writel(PAGE_SIZE_4K_ENABLE,  adata->acp_base + pte_size);
146 	writel(0x01, adata->acp_base + ACPAXI2AXI_ATU_CTRL);
147 }
148 
149 static void config_acp_dma(struct acp_dev_data *adata, int cpu_id, int size)
150 {
151 	struct acp_stream *stream = adata->stream[cpu_id];
152 	struct snd_pcm_substream *substream = stream->substream;
153 	struct acp_resource *rsrc = adata->rsrc;
154 	dma_addr_t addr = substream->dma_buffer.addr;
155 	int num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
156 	u32 low, high, val;
157 	u16 page_idx;
158 
159 	val = stream->pte_offset;
160 
161 	for (page_idx = 0; page_idx < num_pages; page_idx++) {
162 		/* Load the low address of page int ACP SRAM through SRBM */
163 		low = lower_32_bits(addr);
164 		high = upper_32_bits(addr);
165 		writel(low, adata->acp_base + rsrc->scratch_reg_offset + val);
166 		high |= BIT(31);
167 		writel(high, adata->acp_base + rsrc->scratch_reg_offset + val + 4);
168 
169 		/* Move to next physically contiguous page */
170 		val += 8;
171 		addr += PAGE_SIZE;
172 	}
173 }
174 
175 static int acp_dma_open(struct snd_soc_component *component, struct snd_pcm_substream *substream)
176 {
177 	struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
178 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_runtime, 0);
179 	struct snd_pcm_runtime *runtime = substream->runtime;
180 	struct device *dev = component->dev;
181 	struct acp_dev_data *adata = dev_get_drvdata(dev);
182 	struct acp_stream *stream;
183 	int stream_id = cpu_dai->driver->id * 2 + substream->stream;
184 	int ret;
185 
186 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
187 	if (!stream)
188 		return -ENOMEM;
189 
190 	stream->substream = substream;
191 	adata->stream[stream_id] = stream;
192 
193 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
194 		runtime->hw = acp_pcm_hardware_playback;
195 	else
196 		runtime->hw = acp_pcm_hardware_capture;
197 
198 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
199 	if (ret < 0) {
200 		dev_err(component->dev, "set integer constraint failed\n");
201 		kfree(stream);
202 		return ret;
203 	}
204 	runtime->private_data = stream;
205 
206 	writel(1, ACP_EXTERNAL_INTR_ENB(adata));
207 
208 	return ret;
209 }
210 
211 static int acp_dma_hw_params(struct snd_soc_component *component,
212 			     struct snd_pcm_substream *substream,
213 			     struct snd_pcm_hw_params *params)
214 {
215 	struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
216 	struct acp_dev_data *adata = snd_soc_component_get_drvdata(component);
217 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_runtime, 0);
218 	struct acp_stream *stream = substream->runtime->private_data;
219 	int stream_id = cpu_dai->driver->id * 2 + substream->stream;
220 	u64 size = params_buffer_bytes(params);
221 
222 	/* Configure ACP DMA block with params */
223 	config_pte_for_stream(adata, stream);
224 	config_acp_dma(adata, stream_id, size);
225 
226 	return 0;
227 }
228 
229 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
230 					 struct snd_pcm_substream *substream)
231 {
232 	struct device *dev = component->dev;
233 	struct acp_dev_data *adata = dev_get_drvdata(dev);
234 	struct acp_stream *stream = substream->runtime->private_data;
235 	u32 pos, buffersize;
236 	u64 bytescount;
237 
238 	buffersize = frames_to_bytes(substream->runtime,
239 				     substream->runtime->buffer_size);
240 
241 	bytescount = acp_get_byte_count(adata, stream->dai_id, substream->stream);
242 
243 	if (bytescount > stream->bytescount)
244 		bytescount -= stream->bytescount;
245 
246 	pos = do_div(bytescount, buffersize);
247 
248 	return bytes_to_frames(substream->runtime, pos);
249 }
250 
251 static int acp_dma_new(struct snd_soc_component *component,
252 		       struct snd_soc_pcm_runtime *rtd)
253 {
254 	struct device *parent = component->dev->parent;
255 
256 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
257 				       parent, MIN_BUFFER, MAX_BUFFER);
258 	return 0;
259 }
260 
261 static int acp_dma_close(struct snd_soc_component *component,
262 			 struct snd_pcm_substream *substream)
263 {
264 	struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
265 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_runtime, 0);
266 	struct device *dev = component->dev;
267 	struct acp_dev_data *adata = dev_get_drvdata(dev);
268 	struct acp_stream *stream;
269 	int stream_id = cpu_dai->driver->id * 2 + substream->stream;
270 
271 	stream = adata->stream[stream_id];
272 	kfree(stream);
273 	adata->stream[stream_id] = NULL;
274 
275 	return 0;
276 }
277 
278 static const struct snd_soc_component_driver acp_pcm_component = {
279 	.name			= DRV_NAME,
280 	.open			= acp_dma_open,
281 	.close			= acp_dma_close,
282 	.hw_params		= acp_dma_hw_params,
283 	.pointer		= acp_dma_pointer,
284 	.pcm_construct		= acp_dma_new,
285 	.legacy_dai_naming	= 1,
286 };
287 
288 int acp_platform_register(struct device *dev)
289 {
290 	struct acp_dev_data *adata = dev_get_drvdata(dev);
291 	struct snd_soc_dai_driver;
292 	unsigned int status;
293 
294 	status = devm_request_irq(dev, adata->i2s_irq, i2s_irq_handler,
295 				  IRQF_SHARED, "ACP_I2S_IRQ", adata);
296 	if (status) {
297 		dev_err(dev, "ACP I2S IRQ request failed\n");
298 		return status;
299 	}
300 
301 	status = devm_snd_soc_register_component(dev, &acp_pcm_component,
302 						 adata->dai_driver,
303 						 adata->num_dai);
304 	if (status) {
305 		dev_err(dev, "Fail to register acp i2s component\n");
306 		return status;
307 	}
308 	return 0;
309 }
310 EXPORT_SYMBOL_NS_GPL(acp_platform_register, SND_SOC_ACP_COMMON);
311 
312 int acp_platform_unregister(struct device *dev)
313 {
314 	struct acp_dev_data *adata = dev_get_drvdata(dev);
315 
316 	if (adata->mach_dev)
317 		platform_device_unregister(adata->mach_dev);
318 	return 0;
319 }
320 EXPORT_SYMBOL_NS_GPL(acp_platform_unregister, SND_SOC_ACP_COMMON);
321 
322 MODULE_DESCRIPTION("AMD ACP PCM Driver");
323 MODULE_LICENSE("Dual BSD/GPL");
324 MODULE_ALIAS(DRV_NAME);
325