Skip to content

Commit 03667e3

Browse files
Codrin Ciubotariubroonie
authored andcommitted
ASoC: atmel: mchp-i2s-mcc: Improve maxburst calculation for better performance
The period size represents the size of the DMA descriptor. To ensure all DMA descriptors start from a well-aligned address, the period size must be divided by (sample size * maxburst), not just by maxburst. This adjustment allows for computing a higher maxburst value, thereby increasing the performance of the DMA transfer. Previously, snd_pcm_lib_period_bytes() returned 0 because the runtime HW parameters are computed after the hw_params() callbacks are used. To address this, we now use params_*() functions to compute the period size accurately. This change optimizes the DMA transfer performance by ensuring proper alignment and efficient use of maxburst values. [andrei.simion@microchip.com: Reword commit message and commit title. Add macros with values for maximum DMA chunk size allowed. Add DMA_BURST_ALIGNED preprocessor function to check the alignment of the DMA burst] Signed-off-by: Codrin Ciubotariu <codrin.ciubotariu@microchip.com> Signed-off-by: Andrei Simion <andrei.simion@microchip.com> Link: https://patch.msgid.link/20240905095633.113784-1-andrei.simion@microchip.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent e328ab3 commit 03667e3

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

sound/soc/atmel/mchp-i2s-mcc.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@
221221
#define MCHP_I2SMCC_MAX_CHANNELS 8
222222
#define MCHP_I2MCC_TDM_SLOT_WIDTH 32
223223

224+
/*
225+
* ---- DMA chunk size allowed ----
226+
*/
227+
#define MCHP_I2SMCC_DMA_8_WORD_CHUNK 8
228+
#define MCHP_I2SMCC_DMA_4_WORD_CHUNK 4
229+
#define MCHP_I2SMCC_DMA_2_WORD_CHUNK 2
230+
#define MCHP_I2SMCC_DMA_1_WORD_CHUNK 1
231+
#define DMA_BURST_ALIGNED(_p, _s, _w) !(_p % (_s * _w))
232+
224233
static const struct regmap_config mchp_i2s_mcc_regmap_config = {
225234
.reg_bits = 32,
226235
.reg_stride = 4,
@@ -504,12 +513,30 @@ static int mchp_i2s_mcc_is_running(struct mchp_i2s_mcc_dev *dev)
504513
return !!(sr & (MCHP_I2SMCC_SR_TXEN | MCHP_I2SMCC_SR_RXEN));
505514
}
506515

516+
static inline int mchp_i2s_mcc_period_to_maxburst(int period_size, int sample_size)
517+
{
518+
int p_size = period_size;
519+
int s_size = sample_size;
520+
521+
if (DMA_BURST_ALIGNED(p_size, s_size, MCHP_I2SMCC_DMA_8_WORD_CHUNK))
522+
return MCHP_I2SMCC_DMA_8_WORD_CHUNK;
523+
if (DMA_BURST_ALIGNED(p_size, s_size, MCHP_I2SMCC_DMA_4_WORD_CHUNK))
524+
return MCHP_I2SMCC_DMA_4_WORD_CHUNK;
525+
if (DMA_BURST_ALIGNED(p_size, s_size, MCHP_I2SMCC_DMA_2_WORD_CHUNK))
526+
return MCHP_I2SMCC_DMA_2_WORD_CHUNK;
527+
return MCHP_I2SMCC_DMA_1_WORD_CHUNK;
528+
}
529+
507530
static int mchp_i2s_mcc_hw_params(struct snd_pcm_substream *substream,
508531
struct snd_pcm_hw_params *params,
509532
struct snd_soc_dai *dai)
510533
{
511534
unsigned long rate = 0;
512535
struct mchp_i2s_mcc_dev *dev = snd_soc_dai_get_drvdata(dai);
536+
int sample_bytes = params_physical_width(params) / 8;
537+
int period_bytes = params_period_size(params) *
538+
params_channels(params) * sample_bytes;
539+
int maxburst;
513540
u32 mra = 0;
514541
u32 mrb = 0;
515542
unsigned int channels = params_channels(params);
@@ -519,9 +546,9 @@ static int mchp_i2s_mcc_hw_params(struct snd_pcm_substream *substream,
519546
int ret;
520547
bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
521548

522-
dev_dbg(dev->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
549+
dev_dbg(dev->dev, "%s() rate=%u format=%#x width=%u channels=%u period_bytes=%d\n",
523550
__func__, params_rate(params), params_format(params),
524-
params_width(params), params_channels(params));
551+
params_width(params), params_channels(params), period_bytes);
525552

526553
switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
527554
case SND_SOC_DAIFMT_I2S:
@@ -630,11 +657,12 @@ static int mchp_i2s_mcc_hw_params(struct snd_pcm_substream *substream,
630657
* We must have the same burst size configured
631658
* in the DMA transfer and in out IP
632659
*/
633-
mrb |= MCHP_I2SMCC_MRB_DMACHUNK(channels);
660+
maxburst = mchp_i2s_mcc_period_to_maxburst(period_bytes, sample_bytes);
661+
mrb |= MCHP_I2SMCC_MRB_DMACHUNK(maxburst);
634662
if (is_playback)
635-
dev->playback.maxburst = 1 << (fls(channels) - 1);
663+
dev->playback.maxburst = maxburst;
636664
else
637-
dev->capture.maxburst = 1 << (fls(channels) - 1);
665+
dev->capture.maxburst = maxburst;
638666

639667
switch (params_format(params)) {
640668
case SNDRV_PCM_FORMAT_S8:

0 commit comments

Comments
 (0)