Lines Matching +full:dma +full:- +full:channel
1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/dma-mapping.h>
6 #include <linux/dma/sprd-dma.h>
14 #include "sprd-pcm-dma.h"
31 #define SPRD_COMPR_IRAM_LINKLIST_SIZE (1024 - SPRD_COMPR_IRAM_INFO_SIZE)
52 * The Spreadtrum Audio compress offload mode will use 2-stage DMA transfer to
53 * save power. That means we can request 2 dma channels, one for source channel,
54 * and another one for destination channel. Once the source channel's transaction
55 * is done, it will trigger the destination channel's transaction automatically
58 * For 2-stage DMA transfer, we can allocate 2 buffers: IRAM buffer (always
59 * power-on) and DDR buffer. The source channel will transfer data from IRAM
61 * transferring done, the destination channel will start to transfer data from
72 struct sprd_compr_dma dma[SPRD_COMPR_DMA_CHANS];
74 /* DMA engine channel number */
106 struct snd_compr_runtime *runtime = cstream->runtime;
107 struct sprd_compr_stream *stream = runtime->private_data;
109 memset(stream->info_area, 0, sizeof(struct sprd_compr_playinfo));
117 struct snd_compr_runtime *runtime = cstream->runtime;
118 struct sprd_compr_stream *stream = runtime->private_data;
119 struct sprd_compr_dma *dma = &stream->dma[1];
122 stream->copied_total += dma->trans_len;
123 if (stream->copied_total > stream->received_total)
124 stream->copied_total = stream->received_total;
132 int channel)
134 struct snd_compr_runtime *runtime = cstream->runtime;
135 struct sprd_compr_stream *stream = runtime->private_data;
136 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
137 struct device *dev = component->dev;
139 struct sprd_pcm_dma_params *dma_params = data->dma_params;
140 struct sprd_compr_dma *dma = &stream->dma[channel];
152 dev_err(dev, "no dma parameters setting\n");
153 return -EINVAL;
156 dma->chan = dma_request_slave_channel(dev,
157 dma_params->chan_name[channel]);
158 if (!dma->chan) {
159 dev_err(dev, "failed to request dma channel\n");
160 return -ENODEV;
165 ret = -ENOMEM;
169 switch (channel) {
172 period = (SPRD_COMPR_MCDT_FIFO_SIZE - SPRD_COMPR_MCDT_EMPTY_WMK) * 4;
173 period_cnt = params->buffer.fragment_size / period;
174 src_addr = stream->iram_buffer.addr;
175 dst_addr = dma_params->dev_phys[channel];
184 period = params->buffer.fragment_size;
185 period_cnt = params->buffer.fragments;
186 src_addr = stream->compr_buffer.addr;
187 dst_addr = stream->iram_buffer.addr;
195 ret = -EINVAL;
199 dma->trans_len = period * period_cnt;
204 if (cstream->direction == SND_COMPRESS_PLAYBACK) {
216 sg_dma_len(sgt) = dma->trans_len;
221 * Configure the link-list address for the DMA engine link-list
224 link.virt_addr = (unsigned long)dma->virt;
225 link.phy_addr = dma->phys;
227 ret = dmaengine_slave_config(dma->chan, &config);
235 * We configure the DMA request mode, interrupt mode, channel
236 * mode and channel trigger mode by the flags.
238 dma->desc = dma->chan->device->device_prep_slave_sg(dma->chan, sg,
241 if (!dma->desc) {
243 ret = -ENOMEM;
247 /* Only channel 1 transfer can wake up the AP system. */
248 if (!params->no_wake_mode && channel == 1) {
249 dma->desc->callback = sprd_platform_compr_dma_complete;
250 dma->desc->callback_param = cstream;
260 dma_release_channel(dma->chan);
268 struct snd_compr_runtime *runtime = cstream->runtime;
269 struct sprd_compr_stream *stream = runtime->private_data;
270 struct device *dev = component->dev;
275 * Configure the DMA engine 2-stage transfer mode. Channel 1 set as the
276 * destination channel, and channel 0 set as the source channel, that
277 * means once the source channel's transaction is done, it will trigger
278 * the destination channel's transaction automatically.
282 dev_err(dev, "failed to config stage 1 DMA: %d\n", ret);
288 dev_err(dev, "failed to config stage 0 DMA: %d\n", ret);
292 compr_params.direction = cstream->direction;
293 compr_params.sample_rate = params->codec.sample_rate;
294 compr_params.channels = stream->num_channels;
295 compr_params.info_phys = stream->info_phys;
296 compr_params.info_size = stream->info_size;
297 compr_params.rate = params->codec.bit_rate;
298 compr_params.format = params->codec.id;
300 ret = stream->compr_ops->set_params(cstream->direction, &compr_params);
309 dma_release_channel(stream->dma[0].chan);
311 dma_release_channel(stream->dma[1].chan);
318 struct snd_compr_runtime *runtime = cstream->runtime;
319 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
320 struct device *dev = component->dev;
324 int stream_id = cstream->direction, ret;
332 return -ENOMEM;
334 stream->cstream = cstream;
335 stream->num_channels = 2;
336 stream->compr_ops = data->ops;
339 * Allocate the stage 0 IRAM buffer size, including the DMA 0
340 * link-list size and play information of DSP address size.
343 SPRD_COMPR_IRAM_SIZE, &stream->iram_buffer);
347 /* Use to save link-list configuration for DMA 0. */
348 stream->dma[0].virt = stream->iram_buffer.area + SPRD_COMPR_IRAM_SIZE;
349 stream->dma[0].phys = stream->iram_buffer.addr + SPRD_COMPR_IRAM_SIZE;
352 stream->info_phys = stream->iram_buffer.addr + SPRD_COMPR_IRAM_SIZE +
354 stream->info_area = stream->iram_buffer.area + SPRD_COMPR_IRAM_SIZE +
356 stream->info_size = SPRD_COMPR_IRAM_INFO_SIZE;
359 * Allocate the stage 1 DDR buffer size, including the DMA 1 link-list
363 SPRD_COMPR_AREA_SIZE, &stream->compr_buffer);
367 /* Use to save link-list configuration for DMA 1. */
368 stream->dma[1].virt = stream->compr_buffer.area + SPRD_COMPR_AREA_SIZE;
369 stream->dma[1].phys = stream->compr_buffer.addr + SPRD_COMPR_AREA_SIZE;
373 ret = stream->compr_ops->open(stream_id, &cb);
379 runtime->private_data = stream;
383 snd_dma_free_pages(&stream->compr_buffer);
385 snd_dma_free_pages(&stream->iram_buffer);
395 struct snd_compr_runtime *runtime = cstream->runtime;
396 struct sprd_compr_stream *stream = runtime->private_data;
397 struct device *dev = component->dev;
398 int stream_id = cstream->direction, i;
400 for (i = 0; i < stream->num_channels; i++) {
401 struct sprd_compr_dma *dma = &stream->dma[i];
403 if (dma->chan) {
404 dma_release_channel(dma->chan);
405 dma->chan = NULL;
409 snd_dma_free_pages(&stream->compr_buffer);
410 snd_dma_free_pages(&stream->iram_buffer);
412 stream->compr_ops->close(stream_id);
422 struct snd_compr_runtime *runtime = cstream->runtime;
423 struct sprd_compr_stream *stream = runtime->private_data;
424 struct device *dev = component->dev;
425 int channels = stream->num_channels, ret = 0, i;
426 int stream_id = cstream->direction;
428 if (cstream->direction != SND_COMPRESS_PLAYBACK) {
430 return -EINVAL;
435 for (i = channels - 1; i >= 0; i--) {
436 struct sprd_compr_dma *dma = &stream->dma[i];
438 if (!dma->desc)
441 dma->cookie = dmaengine_submit(dma->desc);
442 ret = dma_submit_error(dma->cookie);
450 for (i = channels - 1; i >= 0; i--) {
451 struct sprd_compr_dma *dma = &stream->dma[i];
453 if (dma->chan)
454 dma_async_issue_pending(dma->chan);
457 ret = stream->compr_ops->start(stream_id);
461 for (i = channels - 1; i >= 0; i--) {
462 struct sprd_compr_dma *dma = &stream->dma[i];
464 if (dma->chan)
465 dmaengine_terminate_async(dma->chan);
468 stream->copied_total = 0;
469 stream->stage1_pointer = 0;
470 stream->received_total = 0;
471 stream->received_stage0 = 0;
472 stream->received_stage1 = 0;
474 ret = stream->compr_ops->stop(stream_id);
479 for (i = channels - 1; i >= 0; i--) {
480 struct sprd_compr_dma *dma = &stream->dma[i];
482 if (dma->chan)
483 dmaengine_pause(dma->chan);
486 ret = stream->compr_ops->pause(stream_id);
491 for (i = channels - 1; i >= 0; i--) {
492 struct sprd_compr_dma *dma = &stream->dma[i];
494 if (dma->chan)
495 dmaengine_resume(dma->chan);
498 ret = stream->compr_ops->pause_release(stream_id);
503 ret = stream->compr_ops->drain(stream->received_total);
507 ret = -EINVAL;
518 struct snd_compr_runtime *runtime = cstream->runtime;
519 struct sprd_compr_stream *stream = runtime->private_data;
521 (struct sprd_compr_playinfo *)stream->info_area;
523 tstamp->copied_total = stream->copied_total;
524 tstamp->pcm_io_frames = info->current_data_offset;
533 struct snd_compr_runtime *runtime = cstream->runtime;
534 struct sprd_compr_stream *stream = runtime->private_data;
544 if (stream->received_stage0 < runtime->fragment_size) {
545 avail_bytes = runtime->fragment_size - stream->received_stage0;
546 dst = stream->iram_buffer.area + stream->received_stage0;
554 return -EFAULT;
556 stream->received_stage0 += data_count;
557 stream->copied_total += data_count;
567 return -EFAULT;
569 data_count -= avail_bytes;
570 stream->received_stage0 += avail_bytes;
571 stream->copied_total += avail_bytes;
580 dst = stream->compr_buffer.area + stream->stage1_pointer;
581 if (data_count < stream->compr_buffer.bytes - stream->stage1_pointer) {
583 return -EFAULT;
585 stream->stage1_pointer += data_count;
587 avail_bytes = stream->compr_buffer.bytes - stream->stage1_pointer;
590 return -EFAULT;
592 if (copy_from_user(stream->compr_buffer.area, buf + avail_bytes,
593 data_count - avail_bytes))
594 return -EFAULT;
596 stream->stage1_pointer = data_count - avail_bytes;
599 stream->received_stage1 += data_count;
603 stream->received_total += count;
611 caps->direction = cstream->direction;
612 caps->min_fragment_size = SPRD_COMPR_MIN_FRAGMENT_SIZE;
613 caps->max_fragment_size = SPRD_COMPR_MAX_FRAGMENT_SIZE;
614 caps->min_fragments = SPRD_COMPR_MIN_NUM_FRAGMENTS;
615 caps->max_fragments = SPRD_COMPR_MAX_NUM_FRAGMENTS;
616 caps->num_codecs = 2;
617 caps->codecs[0] = SND_AUDIOCODEC_MP3;
618 caps->codecs[1] = SND_AUDIOCODEC_AAC;
628 switch (codec->codec) {
630 codec->num_descriptors = 2;
631 codec->descriptor[0].max_ch = 2;
632 codec->descriptor[0].bit_rate[0] = 320;
633 codec->descriptor[0].bit_rate[1] = 128;
634 codec->descriptor[0].num_bitrates = 2;
635 codec->descriptor[0].profiles = 0;
636 codec->descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO;
637 codec->descriptor[0].formats = 0;
641 codec->num_descriptors = 2;
642 codec->descriptor[1].max_ch = 2;
643 codec->descriptor[1].bit_rate[0] = 320;
644 codec->descriptor[1].bit_rate[1] = 128;
645 codec->descriptor[1].num_bitrates = 2;
646 codec->descriptor[1].profiles = 0;
647 codec->descriptor[1].modes = 0;
648 codec->descriptor[1].formats = 0;
652 return -EINVAL;
671 MODULE_ALIAS("platform:compress-platform");