1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
4 #include <linux/init.h>
6 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <sound/soc.h>
11 #include <sound/soc-dapm.h>
12 #include <linux/spinlock.h>
13 #include <sound/pcm.h>
15 #include <linux/dma-mapping.h>
16 #include <sound/pcm_params.h>
19 #define DRV_NAME "q6apm-dai"
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 COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024)
32 #define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4)
33 #define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024)
34 #define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4)
35 #define SID_MASK_DEFAULT 0xF
37 static const struct snd_compr_codec_caps q6apm_compr_caps = {
39 .descriptor[0].max_ch = 2,
40 .descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050,
41 24000, 32000, 44100, 48000, 88200,
42 96000, 176400, 192000 },
43 .descriptor[0].num_sample_rates = 13,
44 .descriptor[0].bit_rate[0] = 320,
45 .descriptor[0].bit_rate[1] = 128,
46 .descriptor[0].num_bitrates = 2,
47 .descriptor[0].profiles = 0,
48 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
49 .descriptor[0].formats = 0,
53 Q6APM_STREAM_IDLE = 0,
58 struct q6apm_dai_rtd {
59 struct snd_pcm_substream *substream;
60 struct snd_compr_stream *cstream;
61 struct snd_codec codec;
62 struct snd_compr_params codec_param;
63 struct snd_dma_buffer dma_buffer;
65 unsigned int pcm_size;
66 unsigned int pcm_count;
67 unsigned int pos; /* Buffer position */
69 unsigned int bytes_sent;
70 unsigned int bytes_received;
71 unsigned int copied_total;
72 uint16_t bits_per_sample;
73 uint16_t source; /* Encoding source bit mask */
76 enum stream_state state;
77 struct q6apm_graph *graph;
79 uint32_t initial_samples_drop;
80 uint32_t trailing_samples_drop;
84 struct q6apm_dai_data {
88 static struct snd_pcm_hardware q6apm_dai_hardware_capture = {
89 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
90 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
91 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
92 SNDRV_PCM_INFO_BATCH),
93 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
94 .rates = SNDRV_PCM_RATE_8000_48000,
99 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
100 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
101 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
102 .periods_min = CAPTURE_MIN_NUM_PERIODS,
103 .periods_max = CAPTURE_MAX_NUM_PERIODS,
107 static struct snd_pcm_hardware q6apm_dai_hardware_playback = {
108 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER |
109 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED |
110 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
111 SNDRV_PCM_INFO_BATCH),
112 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE),
113 .rates = SNDRV_PCM_RATE_8000_192000,
118 .buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE),
119 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
120 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
121 .periods_min = PLAYBACK_MIN_NUM_PERIODS,
122 .periods_max = PLAYBACK_MAX_NUM_PERIODS,
126 static void event_handler(uint32_t opcode, uint32_t token, uint32_t *payload, void *priv)
128 struct q6apm_dai_rtd *prtd = priv;
129 struct snd_pcm_substream *substream = prtd->substream;
133 case APM_CLIENT_EVENT_CMD_EOS_DONE:
134 prtd->state = Q6APM_STREAM_STOPPED;
136 case APM_CLIENT_EVENT_DATA_WRITE_DONE:
137 spin_lock_irqsave(&prtd->lock, flags);
138 prtd->pos += prtd->pcm_count;
139 spin_unlock_irqrestore(&prtd->lock, flags);
140 snd_pcm_period_elapsed(substream);
141 if (prtd->state == Q6APM_STREAM_RUNNING)
142 q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
145 case APM_CLIENT_EVENT_DATA_READ_DONE:
146 spin_lock_irqsave(&prtd->lock, flags);
147 prtd->pos += prtd->pcm_count;
148 spin_unlock_irqrestore(&prtd->lock, flags);
149 snd_pcm_period_elapsed(substream);
150 if (prtd->state == Q6APM_STREAM_RUNNING)
151 q6apm_read(prtd->graph);
159 static void event_handler_compr(uint32_t opcode, uint32_t token,
160 uint32_t *payload, void *priv)
162 struct q6apm_dai_rtd *prtd = priv;
163 struct snd_compr_stream *substream = prtd->cstream;
167 uint32_t bytes_written, bytes_to_write;
168 bool is_last_buffer = false;
171 case APM_CLIENT_EVENT_CMD_EOS_DONE:
172 spin_lock_irqsave(&prtd->lock, flags);
173 if (prtd->notify_on_drain) {
174 snd_compr_drain_notify(prtd->cstream);
175 prtd->notify_on_drain = false;
177 prtd->state = Q6APM_STREAM_STOPPED;
179 spin_unlock_irqrestore(&prtd->lock, flags);
181 case APM_CLIENT_EVENT_DATA_WRITE_DONE:
182 spin_lock_irqsave(&prtd->lock, flags);
183 bytes_written = token >> APM_WRITE_TOKEN_LEN_SHIFT;
184 prtd->copied_total += bytes_written;
185 snd_compr_fragment_elapsed(substream);
187 if (prtd->state != Q6APM_STREAM_RUNNING) {
188 spin_unlock_irqrestore(&prtd->lock, flags);
192 avail = prtd->bytes_received - prtd->bytes_sent;
194 if (avail > prtd->pcm_count) {
195 bytes_to_write = prtd->pcm_count;
197 if (substream->partial_drain || prtd->notify_on_drain)
198 is_last_buffer = true;
199 bytes_to_write = avail;
202 if (bytes_to_write) {
203 if (substream->partial_drain && is_last_buffer)
204 wflags |= APM_LAST_BUFFER_FLAG;
206 q6apm_write_async(prtd->graph,
207 bytes_to_write, 0, 0, wflags);
209 prtd->bytes_sent += bytes_to_write;
211 if (prtd->notify_on_drain && is_last_buffer)
212 audioreach_shared_memory_send_eos(prtd->graph);
215 spin_unlock_irqrestore(&prtd->lock, flags);
222 static int q6apm_dai_prepare(struct snd_soc_component *component,
223 struct snd_pcm_substream *substream)
225 struct snd_pcm_runtime *runtime = substream->runtime;
226 struct q6apm_dai_rtd *prtd = runtime->private_data;
227 struct audioreach_module_config cfg;
228 struct device *dev = component->dev;
229 struct q6apm_dai_data *pdata;
232 pdata = snd_soc_component_get_drvdata(component);
236 if (!prtd || !prtd->graph) {
237 dev_err(dev, "%s: private data null or audio client freed\n", __func__);
241 cfg.direction = substream->stream;
242 cfg.sample_rate = runtime->rate;
243 cfg.num_channels = runtime->channels;
244 cfg.bit_width = prtd->bits_per_sample;
245 cfg.fmt = SND_AUDIOCODEC_PCM;
248 /* clear the previous setup if any */
249 q6apm_graph_stop(prtd->graph);
250 q6apm_unmap_memory_regions(prtd->graph, substream->stream);
253 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
255 /* rate and channels are sent to audio driver */
256 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
258 dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
262 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
264 dev_err(dev, "%s: CMD Format block failed\n", __func__);
266 ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys,
267 (prtd->pcm_size / prtd->periods), prtd->periods);
270 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
274 ret = q6apm_graph_prepare(prtd->graph);
276 dev_err(dev, "Failed to prepare Graph %d\n", ret);
280 ret = q6apm_graph_start(prtd->graph);
282 dev_err(dev, "Failed to Start Graph %d\n", ret);
286 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
288 /* Queue the buffers for Capture ONLY after graph is started */
289 for (i = 0; i < runtime->periods; i++)
290 q6apm_read(prtd->graph);
294 /* Now that graph as been prepared and started update the internal state accordingly */
295 prtd->state = Q6APM_STREAM_RUNNING;
300 static int q6apm_dai_trigger(struct snd_soc_component *component,
301 struct snd_pcm_substream *substream, int cmd)
303 struct snd_pcm_runtime *runtime = substream->runtime;
304 struct q6apm_dai_rtd *prtd = runtime->private_data;
308 case SNDRV_PCM_TRIGGER_START:
309 case SNDRV_PCM_TRIGGER_RESUME:
310 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
311 /* start writing buffers for playback only as we already queued capture buffers */
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
313 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, 0);
315 case SNDRV_PCM_TRIGGER_STOP:
316 /* TODO support be handled via SoftPause Module */
317 prtd->state = Q6APM_STREAM_STOPPED;
319 case SNDRV_PCM_TRIGGER_SUSPEND:
320 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
330 static int q6apm_dai_open(struct snd_soc_component *component,
331 struct snd_pcm_substream *substream)
333 struct snd_pcm_runtime *runtime = substream->runtime;
334 struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
335 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0);
336 struct device *dev = component->dev;
337 struct q6apm_dai_data *pdata;
338 struct q6apm_dai_rtd *prtd;
341 graph_id = cpu_dai->driver->id;
343 pdata = snd_soc_component_get_drvdata(component);
345 dev_err(dev, "Drv data not found ..\n");
349 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
353 spin_lock_init(&prtd->lock);
354 prtd->substream = substream;
355 prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler, prtd, graph_id);
356 if (IS_ERR(prtd->graph)) {
357 dev_err(dev, "%s: Could not allocate memory\n", __func__);
358 ret = PTR_ERR(prtd->graph);
362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
363 runtime->hw = q6apm_dai_hardware_playback;
364 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
365 runtime->hw = q6apm_dai_hardware_capture;
367 /* Ensure that buffer size is a multiple of period size */
368 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
370 dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
376 BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
378 dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
383 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
385 dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
389 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
391 dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
395 runtime->private_data = prtd;
396 runtime->dma_bytes = BUFFER_BYTES_MAX;
398 prtd->phys = substream->dma_buffer.addr;
400 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
409 static int q6apm_dai_close(struct snd_soc_component *component,
410 struct snd_pcm_substream *substream)
412 struct snd_pcm_runtime *runtime = substream->runtime;
413 struct q6apm_dai_rtd *prtd = runtime->private_data;
415 if (prtd->state) { /* only stop graph that is started */
416 q6apm_graph_stop(prtd->graph);
417 q6apm_unmap_memory_regions(prtd->graph, substream->stream);
420 q6apm_graph_close(prtd->graph);
423 runtime->private_data = NULL;
428 static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component,
429 struct snd_pcm_substream *substream)
431 struct snd_pcm_runtime *runtime = substream->runtime;
432 struct q6apm_dai_rtd *prtd = runtime->private_data;
433 snd_pcm_uframes_t ptr;
436 spin_lock_irqsave(&prtd->lock, flags);
437 if (prtd->pos == prtd->pcm_size)
440 ptr = bytes_to_frames(runtime, prtd->pos);
441 spin_unlock_irqrestore(&prtd->lock, flags);
446 static int q6apm_dai_hw_params(struct snd_soc_component *component,
447 struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *params)
450 struct snd_pcm_runtime *runtime = substream->runtime;
451 struct q6apm_dai_rtd *prtd = runtime->private_data;
453 prtd->pcm_size = params_buffer_bytes(params);
454 prtd->periods = params_periods(params);
456 switch (params_format(params)) {
457 case SNDRV_PCM_FORMAT_S16_LE:
458 prtd->bits_per_sample = 16;
460 case SNDRV_PCM_FORMAT_S24_LE:
461 prtd->bits_per_sample = 24;
470 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
472 int size = BUFFER_BYTES_MAX;
474 return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
477 static int q6apm_dai_compr_open(struct snd_soc_component *component,
478 struct snd_compr_stream *stream)
480 struct snd_soc_pcm_runtime *rtd = stream->private_data;
481 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
482 struct snd_compr_runtime *runtime = stream->runtime;
483 struct q6apm_dai_rtd *prtd;
484 struct q6apm_dai_data *pdata;
485 struct device *dev = component->dev;
489 graph_id = cpu_dai->driver->id;
490 pdata = snd_soc_component_get_drvdata(component);
494 prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
498 prtd->cstream = stream;
499 prtd->graph = q6apm_graph_open(dev, (q6apm_cb)event_handler_compr, prtd, graph_id);
500 if (IS_ERR(prtd->graph)) {
501 ret = PTR_ERR(prtd->graph);
506 runtime->private_data = prtd;
507 runtime->dma_bytes = BUFFER_BYTES_MAX;
508 size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
509 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &prtd->dma_buffer);
514 prtd->phys = prtd->dma_buffer.addr;
516 prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32);
518 snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer);
519 spin_lock_init(&prtd->lock);
521 q6apm_enable_compress_module(dev, prtd->graph, true);
525 static int q6apm_dai_compr_free(struct snd_soc_component *component,
526 struct snd_compr_stream *stream)
528 struct snd_compr_runtime *runtime = stream->runtime;
529 struct q6apm_dai_rtd *prtd = runtime->private_data;
531 q6apm_graph_stop(prtd->graph);
532 q6apm_unmap_memory_regions(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK);
533 q6apm_graph_close(prtd->graph);
534 snd_dma_free_pages(&prtd->dma_buffer);
537 runtime->private_data = NULL;
542 static int q6apm_dai_compr_get_caps(struct snd_soc_component *component,
543 struct snd_compr_stream *stream,
544 struct snd_compr_caps *caps)
546 caps->direction = SND_COMPRESS_PLAYBACK;
547 caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
548 caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
549 caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
550 caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
551 caps->num_codecs = 3;
552 caps->codecs[0] = SND_AUDIOCODEC_MP3;
553 caps->codecs[1] = SND_AUDIOCODEC_AAC;
554 caps->codecs[2] = SND_AUDIOCODEC_FLAC;
559 static int q6apm_dai_compr_get_codec_caps(struct snd_soc_component *component,
560 struct snd_compr_stream *stream,
561 struct snd_compr_codec_caps *codec)
563 switch (codec->codec) {
564 case SND_AUDIOCODEC_MP3:
565 *codec = q6apm_compr_caps;
574 static int q6apm_dai_compr_pointer(struct snd_soc_component *component,
575 struct snd_compr_stream *stream,
576 struct snd_compr_tstamp *tstamp)
578 struct snd_compr_runtime *runtime = stream->runtime;
579 struct q6apm_dai_rtd *prtd = runtime->private_data;
582 spin_lock_irqsave(&prtd->lock, flags);
583 tstamp->copied_total = prtd->copied_total;
584 tstamp->byte_offset = prtd->copied_total % prtd->pcm_size;
585 spin_unlock_irqrestore(&prtd->lock, flags);
590 static int q6apm_dai_compr_trigger(struct snd_soc_component *component,
591 struct snd_compr_stream *stream, int cmd)
593 struct snd_compr_runtime *runtime = stream->runtime;
594 struct q6apm_dai_rtd *prtd = runtime->private_data;
598 case SNDRV_PCM_TRIGGER_START:
599 case SNDRV_PCM_TRIGGER_RESUME:
600 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
601 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
603 case SNDRV_PCM_TRIGGER_STOP:
605 case SNDRV_PCM_TRIGGER_SUSPEND:
606 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
608 case SND_COMPR_TRIGGER_NEXT_TRACK:
609 prtd->next_track = true;
611 case SND_COMPR_TRIGGER_DRAIN:
612 case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
613 prtd->notify_on_drain = true;
623 static int q6apm_dai_compr_ack(struct snd_soc_component *component, struct snd_compr_stream *stream,
626 struct snd_compr_runtime *runtime = stream->runtime;
627 struct q6apm_dai_rtd *prtd = runtime->private_data;
630 spin_lock_irqsave(&prtd->lock, flags);
631 prtd->bytes_received += count;
632 spin_unlock_irqrestore(&prtd->lock, flags);
637 static int q6apm_dai_compr_set_params(struct snd_soc_component *component,
638 struct snd_compr_stream *stream,
639 struct snd_compr_params *params)
641 struct snd_compr_runtime *runtime = stream->runtime;
642 struct q6apm_dai_rtd *prtd = runtime->private_data;
643 struct q6apm_dai_data *pdata;
644 struct audioreach_module_config cfg;
645 struct snd_codec *codec = ¶ms->codec;
646 int dir = stream->direction;
649 pdata = snd_soc_component_get_drvdata(component);
653 prtd->periods = runtime->fragments;
654 prtd->pcm_count = runtime->fragment_size;
655 prtd->pcm_size = runtime->fragments * runtime->fragment_size;
656 prtd->bits_per_sample = 16;
660 if (prtd->next_track != true) {
661 memcpy(&prtd->codec, codec, sizeof(*codec));
663 ret = q6apm_set_real_module_id(component->dev, prtd->graph, codec->id);
668 cfg.sample_rate = codec->sample_rate;
669 cfg.num_channels = 2;
670 cfg.bit_width = prtd->bits_per_sample;
672 memcpy(&cfg.codec, codec, sizeof(*codec));
674 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
678 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
682 ret = q6apm_map_memory_regions(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK,
683 prtd->phys, (prtd->pcm_size / prtd->periods),
688 ret = q6apm_graph_prepare(prtd->graph);
692 ret = q6apm_graph_start(prtd->graph);
698 cfg.sample_rate = codec->sample_rate;
699 cfg.num_channels = 2;
700 cfg.bit_width = prtd->bits_per_sample;
702 memcpy(&cfg.codec, codec, sizeof(*codec));
704 ret = audioreach_compr_set_param(prtd->graph, &cfg);
708 prtd->state = Q6APM_STREAM_RUNNING;
713 static int q6apm_dai_compr_set_metadata(struct snd_soc_component *component,
714 struct snd_compr_stream *stream,
715 struct snd_compr_metadata *metadata)
717 struct snd_compr_runtime *runtime = stream->runtime;
718 struct q6apm_dai_rtd *prtd = runtime->private_data;
721 switch (metadata->key) {
722 case SNDRV_COMPRESS_ENCODER_PADDING:
723 prtd->trailing_samples_drop = metadata->value[0];
724 q6apm_remove_trailing_silence(component->dev, prtd->graph,
725 prtd->trailing_samples_drop);
727 case SNDRV_COMPRESS_ENCODER_DELAY:
728 prtd->initial_samples_drop = metadata->value[0];
729 q6apm_remove_initial_silence(component->dev, prtd->graph,
730 prtd->initial_samples_drop);
740 static int q6apm_dai_compr_mmap(struct snd_soc_component *component,
741 struct snd_compr_stream *stream,
742 struct vm_area_struct *vma)
744 struct snd_compr_runtime *runtime = stream->runtime;
745 struct q6apm_dai_rtd *prtd = runtime->private_data;
746 struct device *dev = component->dev;
748 return dma_mmap_coherent(dev, vma, prtd->dma_buffer.area, prtd->dma_buffer.addr,
749 prtd->dma_buffer.bytes);
752 static int q6apm_compr_copy(struct snd_soc_component *component,
753 struct snd_compr_stream *stream, char __user *buf,
756 struct snd_compr_runtime *runtime = stream->runtime;
757 struct q6apm_dai_rtd *prtd = runtime->private_data;
764 uint32_t bytes_to_write;
765 int avail, bytes_in_flight = 0;
767 bytes_received = prtd->bytes_received;
770 * Make sure that next track data pointer is aligned at 32 bit boundary
771 * This is a Mandatory requirement from DSP data buffers alignment
773 if (prtd->next_track)
774 bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count);
776 app_pointer = bytes_received/prtd->pcm_size;
777 app_pointer = bytes_received - (app_pointer * prtd->pcm_size);
778 dstn = prtd->dma_buffer.area + app_pointer;
780 if (count < prtd->pcm_size - app_pointer) {
781 if (copy_from_user(dstn, buf, count))
784 copy = prtd->pcm_size - app_pointer;
785 if (copy_from_user(dstn, buf, copy))
787 if (copy_from_user(prtd->dma_buffer.area, buf + copy, count - copy))
791 spin_lock_irqsave(&prtd->lock, flags);
792 bytes_in_flight = prtd->bytes_received - prtd->copied_total;
794 if (prtd->next_track) {
795 prtd->next_track = false;
796 prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count);
797 prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count);
800 prtd->bytes_received = bytes_received + count;
802 /* Kick off the data to dsp if its starving!! */
803 if (prtd->state == Q6APM_STREAM_RUNNING && (bytes_in_flight == 0)) {
804 bytes_to_write = prtd->pcm_count;
805 avail = prtd->bytes_received - prtd->bytes_sent;
807 if (avail < prtd->pcm_count)
808 bytes_to_write = avail;
810 q6apm_write_async(prtd->graph, bytes_to_write, 0, 0, wflags);
811 prtd->bytes_sent += bytes_to_write;
814 spin_unlock_irqrestore(&prtd->lock, flags);
819 static const struct snd_compress_ops q6apm_dai_compress_ops = {
820 .open = q6apm_dai_compr_open,
821 .free = q6apm_dai_compr_free,
822 .get_caps = q6apm_dai_compr_get_caps,
823 .get_codec_caps = q6apm_dai_compr_get_codec_caps,
824 .pointer = q6apm_dai_compr_pointer,
825 .trigger = q6apm_dai_compr_trigger,
826 .ack = q6apm_dai_compr_ack,
827 .set_params = q6apm_dai_compr_set_params,
828 .set_metadata = q6apm_dai_compr_set_metadata,
829 .mmap = q6apm_dai_compr_mmap,
830 .copy = q6apm_compr_copy,
833 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
835 .open = q6apm_dai_open,
836 .close = q6apm_dai_close,
837 .prepare = q6apm_dai_prepare,
838 .pcm_construct = q6apm_dai_pcm_new,
839 .hw_params = q6apm_dai_hw_params,
840 .pointer = q6apm_dai_pointer,
841 .trigger = q6apm_dai_trigger,
842 .compress_ops = &q6apm_dai_compress_ops,
843 .use_dai_pcm_id = true,
846 static int q6apm_dai_probe(struct platform_device *pdev)
848 struct device *dev = &pdev->dev;
849 struct device_node *node = dev->of_node;
850 struct q6apm_dai_data *pdata;
851 struct of_phandle_args args;
854 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
858 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
862 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
864 dev_set_drvdata(dev, pdata);
866 return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
870 static const struct of_device_id q6apm_dai_device_id[] = {
871 { .compatible = "qcom,q6apm-dais" },
874 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
877 static struct platform_driver q6apm_dai_platform_driver = {
880 .of_match_table = of_match_ptr(q6apm_dai_device_id),
882 .probe = q6apm_dai_probe,
884 module_platform_driver(q6apm_dai_platform_driver);
886 MODULE_DESCRIPTION("Q6APM dai driver");
887 MODULE_LICENSE("GPL");