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