1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
5 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <sound/soc.h>
11 #include <sound/soc.h>
12 #include <sound/soc-dapm.h>
13 #include <sound/pcm.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/of_device.h>
17 #include <sound/pcm_params.h>
19 #include "q6routing.h"
20 #include "q6dsp-errno.h"
22 #define DRV_NAME "q6asm-fe-dai"
24 #define PLAYBACK_MIN_NUM_PERIODS 2
25 #define PLAYBACK_MAX_NUM_PERIODS 8
26 #define PLAYBACK_MAX_PERIOD_SIZE 65536
27 #define PLAYBACK_MIN_PERIOD_SIZE 128
28 #define CAPTURE_MIN_NUM_PERIODS 2
29 #define CAPTURE_MAX_NUM_PERIODS 8
30 #define CAPTURE_MAX_PERIOD_SIZE 4096
31 #define CAPTURE_MIN_PERIOD_SIZE 320
32 #define SID_MASK_DEFAULT 0xF
35 Q6ASM_STREAM_IDLE = 0,
40 struct q6asm_dai_rtd {
41 struct snd_pcm_substream *substream;
43 unsigned int pcm_size;
44 unsigned int pcm_count;
45 unsigned int pcm_irq_pos; /* IRQ position */
47 uint16_t bits_per_sample;
48 uint16_t source; /* Encoding source bit mask */
49 struct audio_client *audio_client;
51 enum stream_state state;
54 struct q6asm_dai_data {
58 static struct snd_pcm_hardware q6asm_dai_hardware_capture = {
59 .info = (SNDRV_PCM_INFO_MMAP |
60 SNDRV_PCM_INFO_BLOCK_TRANSFER |
61 SNDRV_PCM_INFO_MMAP_VALID |
62 SNDRV_PCM_INFO_INTERLEAVED |
63 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
64 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
65 SNDRV_PCM_FMTBIT_S24_LE),
66 .rates = SNDRV_PCM_RATE_8000_48000,
71 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
72 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 q6asm_dai_hardware_playback = {
81 .info = (SNDRV_PCM_INFO_MMAP |
82 SNDRV_PCM_INFO_BLOCK_TRANSFER |
83 SNDRV_PCM_INFO_MMAP_VALID |
84 SNDRV_PCM_INFO_INTERLEAVED |
85 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
86 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
87 SNDRV_PCM_FMTBIT_S24_LE),
88 .rates = SNDRV_PCM_RATE_8000_192000,
93 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS *
94 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,
102 #define Q6ASM_FEDAI_DRIVER(num) { \
104 .stream_name = "MultiMedia"#num" Playback", \
105 .rates = (SNDRV_PCM_RATE_8000_192000| \
106 SNDRV_PCM_RATE_KNOT), \
107 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
108 SNDRV_PCM_FMTBIT_S24_LE), \
112 .rate_max = 192000, \
115 .stream_name = "MultiMedia"#num" Capture", \
116 .rates = (SNDRV_PCM_RATE_8000_48000| \
117 SNDRV_PCM_RATE_KNOT), \
118 .formats = (SNDRV_PCM_FMTBIT_S16_LE | \
119 SNDRV_PCM_FMTBIT_S24_LE), \
125 .name = "MultiMedia"#num, \
126 .probe = fe_dai_probe, \
127 .id = MSM_FRONTEND_DAI_MULTIMEDIA##num, \
130 /* Conventional and unconventional sample rate supported */
131 static unsigned int supported_sample_rates[] = {
132 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
133 88200, 96000, 176400, 192000
136 static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
137 .count = ARRAY_SIZE(supported_sample_rates),
138 .list = supported_sample_rates,
142 static void event_handler(uint32_t opcode, uint32_t token,
143 uint32_t *payload, void *priv)
145 struct q6asm_dai_rtd *prtd = priv;
146 struct snd_pcm_substream *substream = prtd->substream;
149 case ASM_CLIENT_EVENT_CMD_RUN_DONE:
150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
151 q6asm_write_async(prtd->audio_client,
152 prtd->pcm_count, 0, 0, NO_TIMESTAMP);
154 case ASM_CLIENT_EVENT_CMD_EOS_DONE:
155 prtd->state = Q6ASM_STREAM_STOPPED;
157 case ASM_CLIENT_EVENT_DATA_WRITE_DONE: {
158 prtd->pcm_irq_pos += prtd->pcm_count;
159 snd_pcm_period_elapsed(substream);
160 if (prtd->state == Q6ASM_STREAM_RUNNING)
161 q6asm_write_async(prtd->audio_client,
162 prtd->pcm_count, 0, 0, NO_TIMESTAMP);
166 case ASM_CLIENT_EVENT_DATA_READ_DONE:
167 prtd->pcm_irq_pos += prtd->pcm_count;
168 snd_pcm_period_elapsed(substream);
169 if (prtd->state == Q6ASM_STREAM_RUNNING)
170 q6asm_read(prtd->audio_client);
178 static int q6asm_dai_prepare(struct snd_pcm_substream *substream)
180 struct snd_pcm_runtime *runtime = substream->runtime;
181 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
182 struct q6asm_dai_rtd *prtd = runtime->private_data;
183 struct snd_soc_component *c = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
184 struct q6asm_dai_data *pdata;
187 pdata = snd_soc_component_get_drvdata(c);
191 if (!prtd || !prtd->audio_client) {
192 pr_err("%s: private data null or audio client freed\n",
197 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
198 prtd->pcm_irq_pos = 0;
199 /* rate and channels are sent to audio driver */
201 /* clear the previous setup if any */
202 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
203 q6asm_unmap_memory_regions(substream->stream,
205 q6routing_stream_close(soc_prtd->dai_link->id,
209 ret = q6asm_map_memory_regions(substream->stream, prtd->audio_client,
211 (prtd->pcm_size / prtd->periods),
215 pr_err("Audio Start: Buffer Allocation failed rc = %d\n",
220 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
221 ret = q6asm_open_write(prtd->audio_client, FORMAT_LINEAR_PCM,
222 prtd->bits_per_sample);
223 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
224 ret = q6asm_open_read(prtd->audio_client, FORMAT_LINEAR_PCM,
225 prtd->bits_per_sample);
229 pr_err("%s: q6asm_open_write failed\n", __func__);
230 q6asm_audio_client_free(prtd->audio_client);
231 prtd->audio_client = NULL;
235 prtd->session_id = q6asm_get_session_id(prtd->audio_client);
236 ret = q6routing_stream_open(soc_prtd->dai_link->id, LEGACY_PCM_MODE,
237 prtd->session_id, substream->stream);
239 pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
243 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
244 ret = q6asm_media_format_block_multi_ch_pcm(
245 prtd->audio_client, runtime->rate,
246 runtime->channels, NULL,
247 prtd->bits_per_sample);
248 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
249 ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client,
250 runtime->rate, runtime->channels,
251 prtd->bits_per_sample);
253 /* Queue the buffers */
254 for (i = 0; i < runtime->periods; i++)
255 q6asm_read(prtd->audio_client);
259 pr_info("%s: CMD Format block failed\n", __func__);
261 prtd->state = Q6ASM_STREAM_RUNNING;
266 static int q6asm_dai_trigger(struct snd_pcm_substream *substream, int cmd)
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 struct q6asm_dai_rtd *prtd = runtime->private_data;
273 case SNDRV_PCM_TRIGGER_START:
274 case SNDRV_PCM_TRIGGER_RESUME:
275 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
276 ret = q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
278 case SNDRV_PCM_TRIGGER_STOP:
279 prtd->state = Q6ASM_STREAM_STOPPED;
280 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
282 case SNDRV_PCM_TRIGGER_SUSPEND:
283 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
284 ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
294 static int q6asm_dai_open(struct snd_pcm_substream *substream)
296 struct snd_pcm_runtime *runtime = substream->runtime;
297 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
298 struct snd_soc_dai *cpu_dai = soc_prtd->cpu_dai;
299 struct snd_soc_component *c = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
300 struct q6asm_dai_rtd *prtd;
301 struct q6asm_dai_data *pdata;
302 struct device *dev = c->dev;
306 stream_id = cpu_dai->driver->id;
308 pdata = snd_soc_component_get_drvdata(c);
310 pr_err("Drv data not found ..\n");
314 prtd = kzalloc(sizeof(struct q6asm_dai_rtd), GFP_KERNEL);
318 prtd->substream = substream;
319 prtd->audio_client = q6asm_audio_client_alloc(dev,
320 (q6asm_cb)event_handler, prtd, stream_id,
322 if (IS_ERR(prtd->audio_client)) {
323 pr_info("%s: Could not allocate memory\n", __func__);
324 ret = PTR_ERR(prtd->audio_client);
329 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
330 runtime->hw = q6asm_dai_hardware_playback;
331 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
332 runtime->hw = q6asm_dai_hardware_capture;
334 ret = snd_pcm_hw_constraint_list(runtime, 0,
335 SNDRV_PCM_HW_PARAM_RATE,
336 &constraints_sample_rates);
338 pr_info("snd_pcm_hw_constraint_list failed\n");
339 /* Ensure that buffer size is a multiple of period size */
340 ret = snd_pcm_hw_constraint_integer(runtime,
341 SNDRV_PCM_HW_PARAM_PERIODS);
343 pr_info("snd_pcm_hw_constraint_integer failed\n");
345 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
346 ret = snd_pcm_hw_constraint_minmax(runtime,
347 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
348 PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE,
349 PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE);
351 pr_err("constraint for buffer bytes min max ret = %d\n",
356 ret = snd_pcm_hw_constraint_step(runtime, 0,
357 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
359 pr_err("constraint for period bytes step ret = %d\n",
362 ret = snd_pcm_hw_constraint_step(runtime, 0,
363 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
365 pr_err("constraint for buffer bytes step ret = %d\n",
369 runtime->private_data = prtd;
371 snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_playback);
373 runtime->dma_bytes = q6asm_dai_hardware_playback.buffer_bytes_max;
377 prtd->phys = substream->dma_buffer.addr;
379 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
381 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
386 static int q6asm_dai_close(struct snd_pcm_substream *substream)
388 struct snd_pcm_runtime *runtime = substream->runtime;
389 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
390 struct q6asm_dai_rtd *prtd = runtime->private_data;
392 if (prtd->audio_client) {
394 q6asm_cmd(prtd->audio_client, CMD_CLOSE);
396 q6asm_unmap_memory_regions(substream->stream,
398 q6asm_audio_client_free(prtd->audio_client);
399 prtd->audio_client = NULL;
401 q6routing_stream_close(soc_prtd->dai_link->id,
407 static snd_pcm_uframes_t q6asm_dai_pointer(struct snd_pcm_substream *substream)
410 struct snd_pcm_runtime *runtime = substream->runtime;
411 struct q6asm_dai_rtd *prtd = runtime->private_data;
413 if (prtd->pcm_irq_pos >= prtd->pcm_size)
414 prtd->pcm_irq_pos = 0;
416 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
419 static int q6asm_dai_mmap(struct snd_pcm_substream *substream,
420 struct vm_area_struct *vma)
423 struct snd_pcm_runtime *runtime = substream->runtime;
424 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
425 struct snd_soc_component *c = snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
426 struct device *dev = c->dev;
428 return dma_mmap_coherent(dev, vma,
429 runtime->dma_area, runtime->dma_addr,
433 static int q6asm_dai_hw_params(struct snd_pcm_substream *substream,
434 struct snd_pcm_hw_params *params)
436 struct snd_pcm_runtime *runtime = substream->runtime;
437 struct q6asm_dai_rtd *prtd = runtime->private_data;
439 prtd->pcm_size = params_buffer_bytes(params);
440 prtd->periods = params_periods(params);
442 switch (params_format(params)) {
443 case SNDRV_PCM_FORMAT_S16_LE:
444 prtd->bits_per_sample = 16;
446 case SNDRV_PCM_FORMAT_S24_LE:
447 prtd->bits_per_sample = 24;
454 static struct snd_pcm_ops q6asm_dai_ops = {
455 .open = q6asm_dai_open,
456 .hw_params = q6asm_dai_hw_params,
457 .close = q6asm_dai_close,
458 .ioctl = snd_pcm_lib_ioctl,
459 .prepare = q6asm_dai_prepare,
460 .trigger = q6asm_dai_trigger,
461 .pointer = q6asm_dai_pointer,
462 .mmap = q6asm_dai_mmap,
465 static int q6asm_dai_pcm_new(struct snd_soc_pcm_runtime *rtd)
467 struct snd_pcm_substream *psubstream, *csubstream;
468 struct snd_soc_component *c = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
469 struct snd_pcm *pcm = rtd->pcm;
474 size = q6asm_dai_hardware_playback.buffer_bytes_max;
475 psubstream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
477 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size,
478 &psubstream->dma_buffer);
480 dev_err(dev, "Cannot allocate buffer(s)\n");
485 csubstream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
487 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size,
488 &csubstream->dma_buffer);
490 dev_err(dev, "Cannot allocate buffer(s)\n");
492 snd_dma_free_pages(&psubstream->dma_buffer);
500 static void q6asm_dai_pcm_free(struct snd_pcm *pcm)
502 struct snd_pcm_substream *substream;
505 for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) {
506 substream = pcm->streams[i].substream;
508 snd_dma_free_pages(&substream->dma_buffer);
509 substream->dma_buffer.area = NULL;
510 substream->dma_buffer.addr = 0;
515 static const struct snd_soc_dapm_route afe_pcm_routes[] = {
516 {"MM_DL1", NULL, "MultiMedia1 Playback" },
517 {"MM_DL2", NULL, "MultiMedia2 Playback" },
518 {"MM_DL3", NULL, "MultiMedia3 Playback" },
519 {"MM_DL4", NULL, "MultiMedia4 Playback" },
520 {"MM_DL5", NULL, "MultiMedia5 Playback" },
521 {"MM_DL6", NULL, "MultiMedia6 Playback" },
522 {"MM_DL7", NULL, "MultiMedia7 Playback" },
523 {"MM_DL7", NULL, "MultiMedia8 Playback" },
524 {"MultiMedia1 Capture", NULL, "MM_UL1"},
525 {"MultiMedia2 Capture", NULL, "MM_UL2"},
526 {"MultiMedia3 Capture", NULL, "MM_UL3"},
527 {"MultiMedia4 Capture", NULL, "MM_UL4"},
528 {"MultiMedia5 Capture", NULL, "MM_UL5"},
529 {"MultiMedia6 Capture", NULL, "MM_UL6"},
530 {"MultiMedia7 Capture", NULL, "MM_UL7"},
531 {"MultiMedia8 Capture", NULL, "MM_UL8"},
535 static int fe_dai_probe(struct snd_soc_dai *dai)
537 struct snd_soc_dapm_context *dapm;
539 dapm = snd_soc_component_get_dapm(dai->component);
540 snd_soc_dapm_add_routes(dapm, afe_pcm_routes,
541 ARRAY_SIZE(afe_pcm_routes));
547 static const struct snd_soc_component_driver q6asm_fe_dai_component = {
549 .ops = &q6asm_dai_ops,
550 .pcm_new = q6asm_dai_pcm_new,
551 .pcm_free = q6asm_dai_pcm_free,
555 static struct snd_soc_dai_driver q6asm_fe_dais[] = {
556 Q6ASM_FEDAI_DRIVER(1),
557 Q6ASM_FEDAI_DRIVER(2),
558 Q6ASM_FEDAI_DRIVER(3),
559 Q6ASM_FEDAI_DRIVER(4),
560 Q6ASM_FEDAI_DRIVER(5),
561 Q6ASM_FEDAI_DRIVER(6),
562 Q6ASM_FEDAI_DRIVER(7),
563 Q6ASM_FEDAI_DRIVER(8),
566 static int q6asm_dai_probe(struct platform_device *pdev)
568 struct device *dev = &pdev->dev;
569 struct device_node *node = dev->of_node;
570 struct of_phandle_args args;
571 struct q6asm_dai_data *pdata;
574 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
578 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
582 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
584 dev_set_drvdata(dev, pdata);
586 return devm_snd_soc_register_component(dev, &q6asm_fe_dai_component,
588 ARRAY_SIZE(q6asm_fe_dais));
591 static const struct of_device_id q6asm_dai_device_id[] = {
592 { .compatible = "qcom,q6asm-dais" },
595 MODULE_DEVICE_TABLE(of, q6asm_dai_device_id);
597 static struct platform_driver q6asm_dai_platform_driver = {
600 .of_match_table = of_match_ptr(q6asm_dai_device_id),
602 .probe = q6asm_dai_probe,
604 module_platform_driver(q6asm_dai_platform_driver);
606 MODULE_DESCRIPTION("Q6ASM dai driver");
607 MODULE_LICENSE("GPL v2");