xref: /openbmc/linux/sound/soc/qcom/qdsp6/q6apm-dai.c (revision 64cf26f0)
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 	prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
151 	prtd->pos = 0;
152 	/* rate and channels are sent to audio driver */
153 	ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
154 	if (ret < 0) {
155 		dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
156 		return ret;
157 	}
158 
159 	ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
160 	if (ret < 0)
161 		dev_err(dev, "%s: CMD Format block failed\n", __func__);
162 
163 	ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
164 				       (prtd->pcm_size / prtd->periods), prtd->periods);
165 
166 	if (ret < 0) {
167 		dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n",	ret);
168 		return -ENOMEM;
169 	}
170 
171 	ret = q6apm_graph_prepare(prtd->graph);
172 	if (ret) {
173 		dev_err(dev, "Failed to prepare Graph %d\n", ret);
174 		return ret;
175 	}
176 
177 	ret = q6apm_graph_start(prtd->graph);
178 	if (ret) {
179 		dev_err(dev, "Failed to Start Graph %d\n", ret);
180 		return ret;
181 	}
182 
183 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
184 		int i;
185 		/* Queue the buffers for Capture ONLY after graph is started */
186 		for (i = 0; i < runtime->periods; i++)
187 			q6apm_read(prtd->graph);
188 
189 	}
190 
191 	/* Now that graph as been prepared and started update the internal state accordingly */
192 	prtd->state = Q6APM_STREAM_RUNNING;
193 
194 	return 0;
195 }
196 
197 static int q6apm_dai_trigger(struct snd_soc_component *component,
198 			     struct snd_pcm_substream *substream, int cmd)
199 {
200 	struct snd_pcm_runtime *runtime = substream->runtime;
201 	struct q6apm_dai_rtd *prtd = runtime->private_data;
202 	int ret = 0;
203 
204 	switch (cmd) {
205 	case SNDRV_PCM_TRIGGER_START:
206 	case SNDRV_PCM_TRIGGER_RESUME:
207 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
208 		 /* start writing buffers for playback only as we already queued capture buffers */
209 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
210 			ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
211 		break;
212 	case SNDRV_PCM_TRIGGER_STOP:
213 		/* TODO support be handled via SoftPause Module */
214 		prtd->state = Q6APM_STREAM_STOPPED;
215 		break;
216 	case SNDRV_PCM_TRIGGER_SUSPEND:
217 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
218 		break;
219 	default:
220 		ret = -EINVAL;
221 		break;
222 	}
223 
224 	return ret;
225 }
226 
227 static int q6apm_dai_open(struct snd_soc_component *component,
228 			  struct snd_pcm_substream *substream)
229 {
230 	struct snd_pcm_runtime *runtime = substream->runtime;
231 	struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
232 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(soc_prtd, 0);
233 	struct device *dev = component->dev;
234 	struct q6apm_dai_data *pdata;
235 	struct q6apm_dai_rtd *prtd;
236 	int graph_id, ret;
237 
238 	graph_id = cpu_dai->driver->id;
239 
240 	pdata = snd_soc_component_get_drvdata(component);
241 	if (!pdata) {
242 		dev_err(dev, "Drv data not found ..\n");
243 		return -EINVAL;
244 	}
245 
246 	prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
247 	if (prtd == NULL)
248 		return -ENOMEM;
249 
250 	prtd->substream = substream;
251 	prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler, prtd, graph_id);
252 	if (IS_ERR(prtd->graph)) {
253 		dev_err(dev, "%s: Could not allocate memory\n", __func__);
254 		ret = PTR_ERR(prtd->graph);
255 		goto err;
256 	}
257 
258 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
259 		runtime->hw = q6apm_dai_hardware_playback;
260 	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
261 		runtime->hw = q6apm_dai_hardware_capture;
262 
263 	/* Ensure that buffer size is a multiple of period size */
264 	ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
265 	if (ret < 0) {
266 		dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
267 		goto err;
268 	}
269 
270 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
271 		ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
272 						   BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
273 		if (ret < 0) {
274 			dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
275 			goto err;
276 		}
277 	}
278 
279 	ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
280 	if (ret < 0) {
281 		dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
282 		goto err;
283 	}
284 
285 	ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
286 	if (ret < 0) {
287 		dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
288 		goto err;
289 	}
290 
291 	runtime->private_data = prtd;
292 	runtime->dma_bytes = BUFFER_BYTES_MAX;
293 	if (pdata->sid < 0)
294 		prtd->phys = substream->dma_buffer.addr;
295 	else
296 		prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
297 
298 	return 0;
299 err:
300 	kfree(prtd);
301 
302 	return ret;
303 }
304 
305 static int q6apm_dai_close(struct snd_soc_component *component,
306 			   struct snd_pcm_substream *substream)
307 {
308 	struct snd_pcm_runtime *runtime = substream->runtime;
309 	struct q6apm_dai_rtd *prtd = runtime->private_data;
310 
311 	q6apm_graph_stop(prtd->graph);
312 	q6apm_unmap_memory_regions(prtd->graph, substream->stream);
313 	q6apm_graph_close(prtd->graph);
314 	prtd->graph = NULL;
315 	kfree(prtd);
316 	runtime->private_data = NULL;
317 
318 	return 0;
319 }
320 
321 static snd_pcm_uframes_t q6apm_dai_pointer(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->pos == prtd->pcm_size)
328 		prtd->pos = 0;
329 
330 	return bytes_to_frames(runtime, prtd->pos);
331 }
332 
333 static int q6apm_dai_hw_params(struct snd_soc_component *component,
334 			       struct snd_pcm_substream *substream,
335 			       struct snd_pcm_hw_params *params)
336 {
337 	struct snd_pcm_runtime *runtime = substream->runtime;
338 	struct q6apm_dai_rtd *prtd = runtime->private_data;
339 
340 	prtd->pcm_size = params_buffer_bytes(params);
341 	prtd->periods = params_periods(params);
342 
343 	switch (params_format(params)) {
344 	case SNDRV_PCM_FORMAT_S16_LE:
345 		prtd->bits_per_sample = 16;
346 		break;
347 	case SNDRV_PCM_FORMAT_S24_LE:
348 		prtd->bits_per_sample = 24;
349 		break;
350 	default:
351 		return -EINVAL;
352 	}
353 
354 	return 0;
355 }
356 
357 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
358 {
359 	int size = BUFFER_BYTES_MAX;
360 
361 	return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
362 }
363 
364 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
365 	.name		= DRV_NAME,
366 	.open		= q6apm_dai_open,
367 	.close		= q6apm_dai_close,
368 	.prepare	= q6apm_dai_prepare,
369 	.pcm_construct	= q6apm_dai_pcm_new,
370 	.hw_params	= q6apm_dai_hw_params,
371 	.pointer	= q6apm_dai_pointer,
372 	.trigger	= q6apm_dai_trigger,
373 };
374 
375 static int q6apm_dai_probe(struct platform_device *pdev)
376 {
377 	struct device *dev = &pdev->dev;
378 	struct device_node *node = dev->of_node;
379 	struct q6apm_dai_data *pdata;
380 	struct of_phandle_args args;
381 	int rc;
382 
383 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
384 	if (!pdata)
385 		return -ENOMEM;
386 
387 	rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
388 	if (rc < 0)
389 		pdata->sid = -1;
390 	else
391 		pdata->sid = args.args[0] & SID_MASK_DEFAULT;
392 
393 	dev_set_drvdata(dev, pdata);
394 
395 	return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
396 }
397 
398 #ifdef CONFIG_OF
399 static const struct of_device_id q6apm_dai_device_id[] = {
400 	{ .compatible = "qcom,q6apm-dais" },
401 	{},
402 };
403 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
404 #endif
405 
406 static struct platform_driver q6apm_dai_platform_driver = {
407 	.driver = {
408 		.name = "q6apm-dai",
409 		.of_match_table = of_match_ptr(q6apm_dai_device_id),
410 	},
411 	.probe = q6apm_dai_probe,
412 };
413 module_platform_driver(q6apm_dai_platform_driver);
414 
415 MODULE_DESCRIPTION("Q6APM dai driver");
416 MODULE_LICENSE("GPL");
417