1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
4 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/slab.h>
10 #include <sound/soc-dapm.h>
11 #include <sound/pcm.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/of_device.h>
15 #include <sound/pcm_params.h>
18 #define DRV_NAME "q6apm-dai"
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
33 Q6APM_STREAM_IDLE = 0,
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;
44 unsigned int pcm_size;
45 unsigned int pcm_count;
46 unsigned int pos; /* Buffer position */
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 */
54 enum stream_state state;
55 struct q6apm_graph *graph;
58 struct q6apm_dai_data {
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,
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,
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,
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,
98 static void event_handler(uint32_t opcode, uint32_t token, uint32_t *payload, void *priv)
100 struct q6apm_dai_rtd *prtd = priv;
101 struct snd_pcm_substream *substream = prtd->substream;
104 case APM_CLIENT_EVENT_CMD_EOS_DONE:
105 prtd->state = Q6APM_STREAM_STOPPED;
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);
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);
126 static int q6apm_dai_prepare(struct snd_soc_component *component,
127 struct snd_pcm_substream *substream)
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;
136 pdata = snd_soc_component_get_drvdata(component);
140 if (!prtd || !prtd->graph) {
141 dev_err(dev, "%s: private data null or audio client freed\n", __func__);
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;
150 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
152 /* rate and channels are sent to audio driver */
153 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
155 dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
159 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
161 dev_err(dev, "%s: CMD Format block failed\n", __func__);
163 ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
164 (prtd->pcm_size / prtd->periods), prtd->periods);
167 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
171 ret = q6apm_graph_prepare(prtd->graph);
173 dev_err(dev, "Failed to prepare Graph %d\n", ret);
177 ret = q6apm_graph_start(prtd->graph);
179 dev_err(dev, "Failed to Start Graph %d\n", ret);
183 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
185 /* Queue the buffers for Capture ONLY after graph is started */
186 for (i = 0; i < runtime->periods; i++)
187 q6apm_read(prtd->graph);
191 /* Now that graph as been prepared and started update the internal state accordingly */
192 prtd->state = Q6APM_STREAM_RUNNING;
197 static int q6apm_dai_trigger(struct snd_soc_component *component,
198 struct snd_pcm_substream *substream, int cmd)
200 struct snd_pcm_runtime *runtime = substream->runtime;
201 struct q6apm_dai_rtd *prtd = runtime->private_data;
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);
212 case SNDRV_PCM_TRIGGER_STOP:
213 /* TODO support be handled via SoftPause Module */
214 prtd->state = Q6APM_STREAM_STOPPED;
216 case SNDRV_PCM_TRIGGER_SUSPEND:
217 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
227 static int q6apm_dai_open(struct snd_soc_component *component,
228 struct snd_pcm_substream *substream)
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;
238 graph_id = cpu_dai->driver->id;
240 pdata = snd_soc_component_get_drvdata(component);
242 dev_err(dev, "Drv data not found ..\n");
246 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
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);
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;
263 /* Ensure that buffer size is a multiple of period size */
264 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
266 dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
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);
274 dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
279 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
281 dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
285 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
287 dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
291 runtime->private_data = prtd;
292 runtime->dma_bytes = BUFFER_BYTES_MAX;
294 prtd->phys = substream->dma_buffer.addr;
296 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
305 static int q6apm_dai_close(struct snd_soc_component *component,
306 struct snd_pcm_substream *substream)
308 struct snd_pcm_runtime *runtime = substream->runtime;
309 struct q6apm_dai_rtd *prtd = runtime->private_data;
311 if (prtd->state) { /* only stop graph that is started */
312 q6apm_graph_stop(prtd->graph);
313 q6apm_unmap_memory_regions(prtd->graph, substream->stream);
316 q6apm_graph_close(prtd->graph);
319 runtime->private_data = NULL;
324 static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component,
325 struct snd_pcm_substream *substream)
327 struct snd_pcm_runtime *runtime = substream->runtime;
328 struct q6apm_dai_rtd *prtd = runtime->private_data;
330 if (prtd->pos == prtd->pcm_size)
333 return bytes_to_frames(runtime, prtd->pos);
336 static int q6apm_dai_hw_params(struct snd_soc_component *component,
337 struct snd_pcm_substream *substream,
338 struct snd_pcm_hw_params *params)
340 struct snd_pcm_runtime *runtime = substream->runtime;
341 struct q6apm_dai_rtd *prtd = runtime->private_data;
343 prtd->pcm_size = params_buffer_bytes(params);
344 prtd->periods = params_periods(params);
346 switch (params_format(params)) {
347 case SNDRV_PCM_FORMAT_S16_LE:
348 prtd->bits_per_sample = 16;
350 case SNDRV_PCM_FORMAT_S24_LE:
351 prtd->bits_per_sample = 24;
360 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
362 int size = BUFFER_BYTES_MAX;
364 return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
367 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
369 .open = q6apm_dai_open,
370 .close = q6apm_dai_close,
371 .prepare = q6apm_dai_prepare,
372 .pcm_construct = q6apm_dai_pcm_new,
373 .hw_params = q6apm_dai_hw_params,
374 .pointer = q6apm_dai_pointer,
375 .trigger = q6apm_dai_trigger,
378 static int q6apm_dai_probe(struct platform_device *pdev)
380 struct device *dev = &pdev->dev;
381 struct device_node *node = dev->of_node;
382 struct q6apm_dai_data *pdata;
383 struct of_phandle_args args;
386 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
390 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
394 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
396 dev_set_drvdata(dev, pdata);
398 return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
402 static const struct of_device_id q6apm_dai_device_id[] = {
403 { .compatible = "qcom,q6apm-dais" },
406 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
409 static struct platform_driver q6apm_dai_platform_driver = {
412 .of_match_table = of_match_ptr(q6apm_dai_device_id),
414 .probe = q6apm_dai_probe,
416 module_platform_driver(q6apm_dai_platform_driver);
418 MODULE_DESCRIPTION("Q6APM dai driver");
419 MODULE_LICENSE("GPL");