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