xref: /openbmc/linux/sound/soc/qcom/qdsp6/q6apm-dai.c (revision d3402925)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
3 
4 #include <linux/init.h>
5 #include <linux/err.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/slab.h>
9 #include <sound/soc.h>
10 #include <sound/soc-dapm.h>
11 #include <linux/spinlock.h>
12 #include <sound/pcm.h>
13 #include <asm/dma.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/of_device.h>
16 #include <sound/pcm_params.h>
17 #include "q6apm.h"
18 
19 #define DRV_NAME "q6apm-dai"
20 
21 #define PLAYBACK_MIN_NUM_PERIODS	2
22 #define PLAYBACK_MAX_NUM_PERIODS	8
23 #define PLAYBACK_MAX_PERIOD_SIZE	65536
24 #define PLAYBACK_MIN_PERIOD_SIZE	128
25 #define CAPTURE_MIN_NUM_PERIODS		2
26 #define CAPTURE_MAX_NUM_PERIODS		8
27 #define CAPTURE_MAX_PERIOD_SIZE		4096
28 #define CAPTURE_MIN_PERIOD_SIZE		320
29 #define BUFFER_BYTES_MAX (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE)
30 #define BUFFER_BYTES_MIN (PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE)
31 #define SID_MASK_DEFAULT	0xF
32 
33 enum stream_state {
34 	Q6APM_STREAM_IDLE = 0,
35 	Q6APM_STREAM_STOPPED,
36 	Q6APM_STREAM_RUNNING,
37 };
38 
39 struct q6apm_dai_rtd {
40 	struct snd_pcm_substream *substream;
41 	struct snd_compr_stream *cstream;
42 	struct snd_compr_params codec_param;
43 	struct snd_dma_buffer dma_buffer;
44 	phys_addr_t phys;
45 	unsigned int pcm_size;
46 	unsigned int pcm_count;
47 	unsigned int pos;       /* Buffer position */
48 	unsigned int periods;
49 	unsigned int bytes_sent;
50 	unsigned int bytes_received;
51 	unsigned int copied_total;
52 	uint16_t bits_per_sample;
53 	uint16_t source; /* Encoding source bit mask */
54 	uint16_t session_id;
55 	enum stream_state state;
56 	struct q6apm_graph *graph;
57 	spinlock_t lock;
58 };
59 
60 struct q6apm_dai_data {
61 	long long sid;
62 };
63 
64 static struct snd_pcm_hardware q6apm_dai_hardware_capture = {
65 	.info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
66 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
67 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
68 				 SNDRV_PCM_INFO_BATCH),
69 	.formats =              (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
70 	.rates =                SNDRV_PCM_RATE_8000_48000,
71 	.rate_min =             8000,
72 	.rate_max =             48000,
73 	.channels_min =         2,
74 	.channels_max =         4,
75 	.buffer_bytes_max =     CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
76 	.period_bytes_min =	CAPTURE_MIN_PERIOD_SIZE,
77 	.period_bytes_max =     CAPTURE_MAX_PERIOD_SIZE,
78 	.periods_min =          CAPTURE_MIN_NUM_PERIODS,
79 	.periods_max =          CAPTURE_MAX_NUM_PERIODS,
80 	.fifo_size =            0,
81 };
82 
83 static struct snd_pcm_hardware q6apm_dai_hardware_playback = {
84 	.info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
85 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
86 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
87 				 SNDRV_PCM_INFO_BATCH),
88 	.formats =              (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
89 	.rates =                SNDRV_PCM_RATE_8000_192000,
90 	.rate_min =             8000,
91 	.rate_max =             192000,
92 	.channels_min =         2,
93 	.channels_max =         8,
94 	.buffer_bytes_max =     (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE),
95 	.period_bytes_min =	PLAYBACK_MIN_PERIOD_SIZE,
96 	.period_bytes_max =     PLAYBACK_MAX_PERIOD_SIZE,
97 	.periods_min =          PLAYBACK_MIN_NUM_PERIODS,
98 	.periods_max =          PLAYBACK_MAX_NUM_PERIODS,
99 	.fifo_size =            0,
100 };
101 
102 static void event_handler(uint32_t opcode, uint32_t token, uint32_t *payload, void *priv)
103 {
104 	struct q6apm_dai_rtd *prtd = priv;
105 	struct snd_pcm_substream *substream = prtd->substream;
106 	unsigned long flags;
107 
108 	switch (opcode) {
109 	case APM_CLIENT_EVENT_CMD_EOS_DONE:
110 		prtd->state = Q6APM_STREAM_STOPPED;
111 		break;
112 	case APM_CLIENT_EVENT_DATA_WRITE_DONE:
113 	        spin_lock_irqsave(&prtd->lock, flags);
114 		prtd->pos += prtd->pcm_count;
115 		spin_unlock_irqrestore(&prtd->lock, flags);
116 		snd_pcm_period_elapsed(substream);
117 		if (prtd->state == Q6APM_STREAM_RUNNING)
118 			q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
119 
120 		break;
121 	case APM_CLIENT_EVENT_DATA_READ_DONE:
122 	        spin_lock_irqsave(&prtd->lock, flags);
123 		prtd->pos += prtd->pcm_count;
124 		spin_unlock_irqrestore(&prtd->lock, flags);
125 		snd_pcm_period_elapsed(substream);
126 		if (prtd->state == Q6APM_STREAM_RUNNING)
127 			q6apm_read(prtd->graph);
128 
129 		break;
130 	default:
131 		break;
132 	}
133 }
134 
135 static int q6apm_dai_prepare(struct snd_soc_component *component,
136 			     struct snd_pcm_substream *substream)
137 {
138 	struct snd_pcm_runtime *runtime = substream->runtime;
139 	struct q6apm_dai_rtd *prtd = runtime->private_data;
140 	struct audioreach_module_config cfg;
141 	struct device *dev = component->dev;
142 	struct q6apm_dai_data *pdata;
143 	int ret;
144 
145 	pdata = snd_soc_component_get_drvdata(component);
146 	if (!pdata)
147 		return -EINVAL;
148 
149 	if (!prtd || !prtd->graph) {
150 		dev_err(dev, "%s: private data null or audio client freed\n", __func__);
151 		return -EINVAL;
152 	}
153 
154 	cfg.direction = substream->stream;
155 	cfg.sample_rate = runtime->rate;
156 	cfg.num_channels = runtime->channels;
157 	cfg.bit_width = prtd->bits_per_sample;
158 
159 	if (prtd->state) {
160 		/* clear the previous setup if any  */
161 		q6apm_graph_stop(prtd->graph);
162 		q6apm_unmap_memory_regions(prtd->graph, substream->stream);
163 	}
164 
165 	prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
166 	prtd->pos = 0;
167 	/* rate and channels are sent to audio driver */
168 	ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
169 	if (ret < 0) {
170 		dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
171 		return ret;
172 	}
173 
174 	ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
175 	if (ret < 0)
176 		dev_err(dev, "%s: CMD Format block failed\n", __func__);
177 
178 	ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
179 				       (prtd->pcm_size / prtd->periods), prtd->periods);
180 
181 	if (ret < 0) {
182 		dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n",	ret);
183 		return -ENOMEM;
184 	}
185 
186 	ret = q6apm_graph_prepare(prtd->graph);
187 	if (ret) {
188 		dev_err(dev, "Failed to prepare Graph %d\n", ret);
189 		return ret;
190 	}
191 
192 	ret = q6apm_graph_start(prtd->graph);
193 	if (ret) {
194 		dev_err(dev, "Failed to Start Graph %d\n", ret);
195 		return ret;
196 	}
197 
198 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
199 		int i;
200 		/* Queue the buffers for Capture ONLY after graph is started */
201 		for (i = 0; i < runtime->periods; i++)
202 			q6apm_read(prtd->graph);
203 
204 	}
205 
206 	/* Now that graph as been prepared and started update the internal state accordingly */
207 	prtd->state = Q6APM_STREAM_RUNNING;
208 
209 	return 0;
210 }
211 
212 static int q6apm_dai_trigger(struct snd_soc_component *component,
213 			     struct snd_pcm_substream *substream, int cmd)
214 {
215 	struct snd_pcm_runtime *runtime = substream->runtime;
216 	struct q6apm_dai_rtd *prtd = runtime->private_data;
217 	int ret = 0;
218 
219 	switch (cmd) {
220 	case SNDRV_PCM_TRIGGER_START:
221 	case SNDRV_PCM_TRIGGER_RESUME:
222 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
223 		 /* start writing buffers for playback only as we already queued capture buffers */
224 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
225 			ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
226 		break;
227 	case SNDRV_PCM_TRIGGER_STOP:
228 		/* TODO support be handled via SoftPause Module */
229 		prtd->state = Q6APM_STREAM_STOPPED;
230 		break;
231 	case SNDRV_PCM_TRIGGER_SUSPEND:
232 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233 		break;
234 	default:
235 		ret = -EINVAL;
236 		break;
237 	}
238 
239 	return ret;
240 }
241 
242 static int q6apm_dai_open(struct snd_soc_component *component,
243 			  struct snd_pcm_substream *substream)
244 {
245 	struct snd_pcm_runtime *runtime = substream->runtime;
246 	struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
247 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0);
248 	struct device *dev = component->dev;
249 	struct q6apm_dai_data *pdata;
250 	struct q6apm_dai_rtd *prtd;
251 	int graph_id, ret;
252 
253 	graph_id = cpu_dai->driver->id;
254 
255 	pdata = snd_soc_component_get_drvdata(component);
256 	if (!pdata) {
257 		dev_err(dev, "Drv data not found ..\n");
258 		return -EINVAL;
259 	}
260 
261 	prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
262 	if (prtd == NULL)
263 		return -ENOMEM;
264 
265 	spin_lock_init(&prtd->lock);
266 	prtd->substream = substream;
267 	prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler, prtd, graph_id);
268 	if (IS_ERR(prtd->graph)) {
269 		dev_err(dev, "%s: Could not allocate memory\n", __func__);
270 		ret = PTR_ERR(prtd->graph);
271 		goto err;
272 	}
273 
274 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
275 		runtime->hw = q6apm_dai_hardware_playback;
276 	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
277 		runtime->hw = q6apm_dai_hardware_capture;
278 
279 	/* Ensure that buffer size is a multiple of period size */
280 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
281 	if (ret < 0) {
282 		dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
283 		goto err;
284 	}
285 
286 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
287 		ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
288 						   BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
289 		if (ret < 0) {
290 			dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
291 			goto err;
292 		}
293 	}
294 
295 	ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
296 	if (ret < 0) {
297 		dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
298 		goto err;
299 	}
300 
301 	ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
302 	if (ret < 0) {
303 		dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
304 		goto err;
305 	}
306 
307 	runtime->private_data = prtd;
308 	runtime->dma_bytes = BUFFER_BYTES_MAX;
309 	if (pdata->sid < 0)
310 		prtd->phys = substream->dma_buffer.addr;
311 	else
312 		prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
313 
314 	return 0;
315 err:
316 	kfree(prtd);
317 
318 	return ret;
319 }
320 
321 static int q6apm_dai_close(struct snd_soc_component *component,
322 			   struct snd_pcm_substream *substream)
323 {
324 	struct snd_pcm_runtime *runtime = substream->runtime;
325 	struct q6apm_dai_rtd *prtd = runtime->private_data;
326 
327 	if (prtd->state) { /* only stop graph that is started */
328 		q6apm_graph_stop(prtd->graph);
329 		q6apm_unmap_memory_regions(prtd->graph, substream->stream);
330 	}
331 
332 	q6apm_graph_close(prtd->graph);
333 	prtd->graph = NULL;
334 	kfree(prtd);
335 	runtime->private_data = NULL;
336 
337 	return 0;
338 }
339 
340 static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component,
341 					   struct snd_pcm_substream *substream)
342 {
343 	struct snd_pcm_runtime *runtime = substream->runtime;
344 	struct q6apm_dai_rtd *prtd = runtime->private_data;
345 	snd_pcm_uframes_t ptr;
346 	unsigned long flags;
347 
348 	spin_lock_irqsave(&prtd->lock, flags);
349 	if (prtd->pos == prtd->pcm_size)
350 		prtd->pos = 0;
351 
352 	ptr =  bytes_to_frames(runtime, prtd->pos);
353 	spin_unlock_irqrestore(&prtd->lock, flags);
354 
355 	return ptr;
356 }
357 
358 static int q6apm_dai_hw_params(struct snd_soc_component *component,
359 			       struct snd_pcm_substream *substream,
360 			       struct snd_pcm_hw_params *params)
361 {
362 	struct snd_pcm_runtime *runtime = substream->runtime;
363 	struct q6apm_dai_rtd *prtd = runtime->private_data;
364 
365 	prtd->pcm_size = params_buffer_bytes(params);
366 	prtd->periods = params_periods(params);
367 
368 	switch (params_format(params)) {
369 	case SNDRV_PCM_FORMAT_S16_LE:
370 		prtd->bits_per_sample = 16;
371 		break;
372 	case SNDRV_PCM_FORMAT_S24_LE:
373 		prtd->bits_per_sample = 24;
374 		break;
375 	default:
376 		return -EINVAL;
377 	}
378 
379 	return 0;
380 }
381 
382 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
383 {
384 	int size = BUFFER_BYTES_MAX;
385 
386 	return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
387 }
388 
389 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
390 	.name		= DRV_NAME,
391 	.open		= q6apm_dai_open,
392 	.close		= q6apm_dai_close,
393 	.prepare	= q6apm_dai_prepare,
394 	.pcm_construct	= q6apm_dai_pcm_new,
395 	.hw_params	= q6apm_dai_hw_params,
396 	.pointer	= q6apm_dai_pointer,
397 	.trigger	= q6apm_dai_trigger,
398 };
399 
400 static int q6apm_dai_probe(struct platform_device *pdev)
401 {
402 	struct device *dev = &pdev->dev;
403 	struct device_node *node = dev->of_node;
404 	struct q6apm_dai_data *pdata;
405 	struct of_phandle_args args;
406 	int rc;
407 
408 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
409 	if (!pdata)
410 		return -ENOMEM;
411 
412 	rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
413 	if (rc < 0)
414 		pdata->sid = -1;
415 	else
416 		pdata->sid = args.args[0] & SID_MASK_DEFAULT;
417 
418 	dev_set_drvdata(dev, pdata);
419 
420 	return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
421 }
422 
423 #ifdef CONFIG_OF
424 static const struct of_device_id q6apm_dai_device_id[] = {
425 	{ .compatible = "qcom,q6apm-dais" },
426 	{},
427 };
428 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
429 #endif
430 
431 static struct platform_driver q6apm_dai_platform_driver = {
432 	.driver = {
433 		.name = "q6apm-dai",
434 		.of_match_table = of_match_ptr(q6apm_dai_device_id),
435 	},
436 	.probe = q6apm_dai_probe,
437 };
438 module_platform_driver(q6apm_dai_platform_driver);
439 
440 MODULE_DESCRIPTION("Q6APM dai driver");
441 MODULE_LICENSE("GPL");
442