xref: /openbmc/linux/sound/soc/amd/acp/acp-platform.c (revision a8d1316a)
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 
acp_machine_select(struct acp_dev_data * adata)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 
i2s_irq_handler(int irq,void * data)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 
config_pte_for_stream(struct acp_dev_data * adata,struct acp_stream * stream)130 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 EXPORT_SYMBOL_NS_GPL(config_pte_for_stream, SND_SOC_ACP_COMMON);
147 
config_acp_dma(struct acp_dev_data * adata,struct acp_stream * stream,int size)148 void config_acp_dma(struct acp_dev_data *adata, struct acp_stream *stream, int size)
149 {
150 	struct snd_pcm_substream *substream = stream->substream;
151 	struct acp_resource *rsrc = adata->rsrc;
152 	dma_addr_t addr = substream->dma_buffer.addr;
153 	int num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT);
154 	u32 low, high, val;
155 	u16 page_idx;
156 
157 	val = stream->pte_offset;
158 
159 	for (page_idx = 0; page_idx < num_pages; page_idx++) {
160 		/* Load the low address of page int ACP SRAM through SRBM */
161 		low = lower_32_bits(addr);
162 		high = upper_32_bits(addr);
163 		writel(low, adata->acp_base + rsrc->scratch_reg_offset + val);
164 		high |= BIT(31);
165 		writel(high, adata->acp_base + rsrc->scratch_reg_offset + val + 4);
166 
167 		/* Move to next physically contiguous page */
168 		val += 8;
169 		addr += PAGE_SIZE;
170 	}
171 }
172 EXPORT_SYMBOL_NS_GPL(config_acp_dma, SND_SOC_ACP_COMMON);
173 
acp_dma_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)174 static int acp_dma_open(struct snd_soc_component *component, struct snd_pcm_substream *substream)
175 {
176 	struct snd_pcm_runtime *runtime = substream->runtime;
177 	struct device *dev = component->dev;
178 	struct acp_dev_data *adata = dev_get_drvdata(dev);
179 	struct acp_stream *stream;
180 	int ret;
181 
182 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
183 	if (!stream)
184 		return -ENOMEM;
185 
186 	stream->substream = substream;
187 
188 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
189 		runtime->hw = acp_pcm_hardware_playback;
190 	else
191 		runtime->hw = acp_pcm_hardware_capture;
192 
193 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
194 	if (ret < 0) {
195 		dev_err(component->dev, "set integer constraint failed\n");
196 		kfree(stream);
197 		return ret;
198 	}
199 	runtime->private_data = stream;
200 
201 	writel(1, ACP_EXTERNAL_INTR_ENB(adata));
202 
203 	spin_lock_irq(&adata->acp_lock);
204 	list_add_tail(&stream->list, &adata->stream_list);
205 	spin_unlock_irq(&adata->acp_lock);
206 
207 	return ret;
208 }
209 
acp_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)210 static int acp_dma_hw_params(struct snd_soc_component *component,
211 			     struct snd_pcm_substream *substream,
212 			     struct snd_pcm_hw_params *params)
213 {
214 	struct acp_dev_data *adata = snd_soc_component_get_drvdata(component);
215 	struct acp_stream *stream = substream->runtime->private_data;
216 	u64 size = params_buffer_bytes(params);
217 
218 	/* Configure ACP DMA block with params */
219 	config_pte_for_stream(adata, stream);
220 	config_acp_dma(adata, stream, size);
221 
222 	return 0;
223 }
224 
acp_dma_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)225 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
226 					 struct snd_pcm_substream *substream)
227 {
228 	struct device *dev = component->dev;
229 	struct acp_dev_data *adata = dev_get_drvdata(dev);
230 	struct acp_stream *stream = substream->runtime->private_data;
231 	u32 pos, buffersize;
232 	u64 bytescount;
233 
234 	buffersize = frames_to_bytes(substream->runtime,
235 				     substream->runtime->buffer_size);
236 
237 	bytescount = acp_get_byte_count(adata, stream->dai_id, substream->stream);
238 
239 	if (bytescount > stream->bytescount)
240 		bytescount -= stream->bytescount;
241 
242 	pos = do_div(bytescount, buffersize);
243 
244 	return bytes_to_frames(substream->runtime, pos);
245 }
246 
acp_dma_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)247 static int acp_dma_new(struct snd_soc_component *component,
248 		       struct snd_soc_pcm_runtime *rtd)
249 {
250 	struct device *parent = component->dev->parent;
251 
252 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
253 				       parent, MIN_BUFFER, MAX_BUFFER);
254 	return 0;
255 }
256 
acp_dma_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)257 static int acp_dma_close(struct snd_soc_component *component,
258 			 struct snd_pcm_substream *substream)
259 {
260 	struct device *dev = component->dev;
261 	struct acp_dev_data *adata = dev_get_drvdata(dev);
262 	struct acp_stream *stream = substream->runtime->private_data;
263 
264 	/* Remove entry from list */
265 	spin_lock_irq(&adata->acp_lock);
266 	list_del(&stream->list);
267 	spin_unlock_irq(&adata->acp_lock);
268 	kfree(stream);
269 
270 	return 0;
271 }
272 
273 static const struct snd_soc_component_driver acp_pcm_component = {
274 	.name			= DRV_NAME,
275 	.open			= acp_dma_open,
276 	.close			= acp_dma_close,
277 	.hw_params		= acp_dma_hw_params,
278 	.pointer		= acp_dma_pointer,
279 	.pcm_construct		= acp_dma_new,
280 	.legacy_dai_naming	= 1,
281 };
282 
acp_platform_register(struct device * dev)283 int acp_platform_register(struct device *dev)
284 {
285 	struct acp_dev_data *adata = dev_get_drvdata(dev);
286 	struct snd_soc_dai_driver;
287 	unsigned int status;
288 
289 	status = devm_request_irq(dev, adata->i2s_irq, i2s_irq_handler,
290 				  IRQF_SHARED, "ACP_I2S_IRQ", adata);
291 	if (status) {
292 		dev_err(dev, "ACP I2S IRQ request failed\n");
293 		return status;
294 	}
295 
296 	status = devm_snd_soc_register_component(dev, &acp_pcm_component,
297 						 adata->dai_driver,
298 						 adata->num_dai);
299 	if (status) {
300 		dev_err(dev, "Fail to register acp i2s component\n");
301 		return status;
302 	}
303 
304 	INIT_LIST_HEAD(&adata->stream_list);
305 	spin_lock_init(&adata->acp_lock);
306 
307 	return 0;
308 }
309 EXPORT_SYMBOL_NS_GPL(acp_platform_register, SND_SOC_ACP_COMMON);
310 
acp_platform_unregister(struct device * dev)311 int acp_platform_unregister(struct device *dev)
312 {
313 	struct acp_dev_data *adata = dev_get_drvdata(dev);
314 
315 	if (adata->mach_dev)
316 		platform_device_unregister(adata->mach_dev);
317 	return 0;
318 }
319 EXPORT_SYMBOL_NS_GPL(acp_platform_unregister, SND_SOC_ACP_COMMON);
320 
321 MODULE_DESCRIPTION("AMD ACP PCM Driver");
322 MODULE_LICENSE("Dual BSD/GPL");
323 MODULE_ALIAS(DRV_NAME);
324