xref: /openbmc/linux/sound/soc/amd/acp/acp-platform.c (revision 979ac5ef)
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;
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 	spin_lock(&adata->acp_lock);
108 	list_for_each_entry(stream, &adata->stream_list, list) {
109 		if (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 		}
115 		if (adata->rsrc->no_of_ctrls == 2) {
116 			if (ext_intr_stat1 & stream->irq_bit) {
117 				writel(stream->irq_bit, ACP_EXTERNAL_INTR_STAT(adata,
118 				       (rsrc->irqp_used - 1)));
119 				snd_pcm_period_elapsed(stream->substream);
120 				i2s_flag = 1;
121 			}
122 		}
123 	}
124 	spin_unlock(&adata->acp_lock);
125 	if (i2s_flag)
126 		return IRQ_HANDLED;
127 
128 	return IRQ_NONE;
129 }
130 
131 static void config_pte_for_stream(struct acp_dev_data *adata, struct acp_stream *stream)
132 {
133 	struct acp_resource *rsrc = adata->rsrc;
134 	u32 pte_reg, pte_size, reg_val;
135 
136 	/* Use ATU base Group5 */
137 	pte_reg = ACPAXI2AXI_ATU_BASE_ADDR_GRP_5;
138 	pte_size =  ACPAXI2AXI_ATU_PAGE_SIZE_GRP_5;
139 	stream->reg_offset = 0x02000000;
140 
141 	/* Group Enable */
142 	reg_val = rsrc->sram_pte_offset;
143 	writel(reg_val | BIT(31), adata->acp_base + pte_reg);
144 	writel(PAGE_SIZE_4K_ENABLE,  adata->acp_base + pte_size);
145 	writel(0x01, adata->acp_base + ACPAXI2AXI_ATU_CTRL);
146 }
147 
148 static 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 
173 static int acp_dma_open(struct snd_soc_component *component, struct snd_pcm_substream *substream)
174 {
175 	struct snd_pcm_runtime *runtime = substream->runtime;
176 	struct device *dev = component->dev;
177 	struct acp_dev_data *adata = dev_get_drvdata(dev);
178 	struct acp_stream *stream;
179 	int ret;
180 
181 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
182 	if (!stream)
183 		return -ENOMEM;
184 
185 	stream->substream = substream;
186 
187 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
188 		runtime->hw = acp_pcm_hardware_playback;
189 	else
190 		runtime->hw = acp_pcm_hardware_capture;
191 
192 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
193 	if (ret < 0) {
194 		dev_err(component->dev, "set integer constraint failed\n");
195 		kfree(stream);
196 		return ret;
197 	}
198 	runtime->private_data = stream;
199 
200 	writel(1, ACP_EXTERNAL_INTR_ENB(adata));
201 
202 	spin_lock_irq(&adata->acp_lock);
203 	list_add_tail(&stream->list, &adata->stream_list);
204 	spin_unlock_irq(&adata->acp_lock);
205 
206 	return ret;
207 }
208 
209 static int acp_dma_hw_params(struct snd_soc_component *component,
210 			     struct snd_pcm_substream *substream,
211 			     struct snd_pcm_hw_params *params)
212 {
213 	struct acp_dev_data *adata = snd_soc_component_get_drvdata(component);
214 	struct acp_stream *stream = substream->runtime->private_data;
215 	u64 size = params_buffer_bytes(params);
216 
217 	/* Configure ACP DMA block with params */
218 	config_pte_for_stream(adata, stream);
219 	config_acp_dma(adata, stream, size);
220 
221 	return 0;
222 }
223 
224 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
225 					 struct snd_pcm_substream *substream)
226 {
227 	struct device *dev = component->dev;
228 	struct acp_dev_data *adata = dev_get_drvdata(dev);
229 	struct acp_stream *stream = substream->runtime->private_data;
230 	u32 pos, buffersize;
231 	u64 bytescount;
232 
233 	buffersize = frames_to_bytes(substream->runtime,
234 				     substream->runtime->buffer_size);
235 
236 	bytescount = acp_get_byte_count(adata, stream->dai_id, substream->stream);
237 
238 	if (bytescount > stream->bytescount)
239 		bytescount -= stream->bytescount;
240 
241 	pos = do_div(bytescount, buffersize);
242 
243 	return bytes_to_frames(substream->runtime, pos);
244 }
245 
246 static int acp_dma_new(struct snd_soc_component *component,
247 		       struct snd_soc_pcm_runtime *rtd)
248 {
249 	struct device *parent = component->dev->parent;
250 
251 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
252 				       parent, MIN_BUFFER, MAX_BUFFER);
253 	return 0;
254 }
255 
256 static int acp_dma_close(struct snd_soc_component *component,
257 			 struct snd_pcm_substream *substream)
258 {
259 	struct device *dev = component->dev;
260 	struct acp_dev_data *adata = dev_get_drvdata(dev);
261 	struct acp_stream *stream = substream->runtime->private_data;
262 
263 	/* Remove entry from list */
264 	spin_lock_irq(&adata->acp_lock);
265 	list_del(&stream->list);
266 	spin_unlock_irq(&adata->acp_lock);
267 	kfree(stream);
268 
269 	return 0;
270 }
271 
272 static const struct snd_soc_component_driver acp_pcm_component = {
273 	.name			= DRV_NAME,
274 	.open			= acp_dma_open,
275 	.close			= acp_dma_close,
276 	.hw_params		= acp_dma_hw_params,
277 	.pointer		= acp_dma_pointer,
278 	.pcm_construct		= acp_dma_new,
279 	.legacy_dai_naming	= 1,
280 };
281 
282 int acp_platform_register(struct device *dev)
283 {
284 	struct acp_dev_data *adata = dev_get_drvdata(dev);
285 	struct snd_soc_dai_driver;
286 	unsigned int status;
287 
288 	status = devm_request_irq(dev, adata->i2s_irq, i2s_irq_handler,
289 				  IRQF_SHARED, "ACP_I2S_IRQ", adata);
290 	if (status) {
291 		dev_err(dev, "ACP I2S IRQ request failed\n");
292 		return status;
293 	}
294 
295 	status = devm_snd_soc_register_component(dev, &acp_pcm_component,
296 						 adata->dai_driver,
297 						 adata->num_dai);
298 	if (status) {
299 		dev_err(dev, "Fail to register acp i2s component\n");
300 		return status;
301 	}
302 
303 	INIT_LIST_HEAD(&adata->stream_list);
304 	spin_lock_init(&adata->acp_lock);
305 
306 	return 0;
307 }
308 EXPORT_SYMBOL_NS_GPL(acp_platform_register, SND_SOC_ACP_COMMON);
309 
310 int acp_platform_unregister(struct device *dev)
311 {
312 	struct acp_dev_data *adata = dev_get_drvdata(dev);
313 
314 	if (adata->mach_dev)
315 		platform_device_unregister(adata->mach_dev);
316 	return 0;
317 }
318 EXPORT_SYMBOL_NS_GPL(acp_platform_unregister, SND_SOC_ACP_COMMON);
319 
320 MODULE_DESCRIPTION("AMD ACP PCM Driver");
321 MODULE_LICENSE("Dual BSD/GPL");
322 MODULE_ALIAS(DRV_NAME);
323