Commit ee941a33 authored by Srinivas Kandagatla's avatar Srinivas Kandagatla Committed by Mark Brown

ASoC: q6asm-dai: add gapless support

Add support to gapless playback by implementing metadata,
next_track, drain and partial drain support.

Gapless on Q6ASM is implemented by opening 2 streams in a single
q6asm stream and toggling them on next track.
Signed-off-by: default avatarSrinivas Kandagatla <srinivas.kandagatla@linaro.org>
Tested-by: default avatarVinod Koul <vkoul@kernel.org>
Reviewed-by: default avatarPierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: default avatarVinod Koul <vkoul@kernel.org>
Link: https://lore.kernel.org/r/20200727093806.17089-10-srinivas.kandagatla@linaro.orgSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 5b39363e
...@@ -64,11 +64,14 @@ struct q6asm_dai_rtd { ...@@ -64,11 +64,14 @@ struct q6asm_dai_rtd {
uint16_t bits_per_sample; uint16_t bits_per_sample;
uint16_t source; /* Encoding source bit mask */ uint16_t source; /* Encoding source bit mask */
struct audio_client *audio_client; struct audio_client *audio_client;
uint32_t next_track_stream_id;
bool next_track;
uint32_t stream_id; uint32_t stream_id;
uint16_t session_id; uint16_t session_id;
enum stream_state state; enum stream_state state;
uint32_t initial_samples_drop; uint32_t initial_samples_drop;
uint32_t trailing_samples_drop; uint32_t trailing_samples_drop;
bool notify_on_drain;
}; };
struct q6asm_dai_data { struct q6asm_dai_data {
...@@ -507,13 +510,19 @@ static void compress_event_handler(uint32_t opcode, uint32_t token, ...@@ -507,13 +510,19 @@ static void compress_event_handler(uint32_t opcode, uint32_t token,
struct q6asm_dai_rtd *prtd = priv; struct q6asm_dai_rtd *prtd = priv;
struct snd_compr_stream *substream = prtd->cstream; struct snd_compr_stream *substream = prtd->cstream;
unsigned long flags; unsigned long flags;
u32 wflags = 0;
uint64_t avail; uint64_t avail;
uint32_t bytes_written; uint32_t bytes_written, bytes_to_write;
bool is_last_buffer = false;
switch (opcode) { switch (opcode) {
case ASM_CLIENT_EVENT_CMD_RUN_DONE: case ASM_CLIENT_EVENT_CMD_RUN_DONE:
spin_lock_irqsave(&prtd->lock, flags); spin_lock_irqsave(&prtd->lock, flags);
if (!prtd->bytes_sent) { if (!prtd->bytes_sent) {
q6asm_stream_remove_initial_silence(prtd->audio_client,
prtd->stream_id,
prtd->initial_samples_drop);
q6asm_write_async(prtd->audio_client, prtd->stream_id, q6asm_write_async(prtd->audio_client, prtd->stream_id,
prtd->pcm_count, 0, 0, 0); prtd->pcm_count, 0, 0, 0);
prtd->bytes_sent += prtd->pcm_count; prtd->bytes_sent += prtd->pcm_count;
...@@ -523,7 +532,30 @@ static void compress_event_handler(uint32_t opcode, uint32_t token, ...@@ -523,7 +532,30 @@ static void compress_event_handler(uint32_t opcode, uint32_t token,
break; break;
case ASM_CLIENT_EVENT_CMD_EOS_DONE: case ASM_CLIENT_EVENT_CMD_EOS_DONE:
prtd->state = Q6ASM_STREAM_STOPPED; spin_lock_irqsave(&prtd->lock, flags);
if (prtd->notify_on_drain) {
if (substream->partial_drain) {
/*
* Close old stream and make it stale, switch
* the active stream now!
*/
q6asm_cmd_nowait(prtd->audio_client,
prtd->stream_id,
CMD_CLOSE);
/*
* vaild stream ids start from 1, So we are
* toggling this between 1 and 2.
*/
prtd->stream_id = (prtd->stream_id == 1 ? 2 : 1);
}
snd_compr_drain_notify(prtd->cstream);
prtd->notify_on_drain = false;
} else {
prtd->state = Q6ASM_STREAM_STOPPED;
}
spin_unlock_irqrestore(&prtd->lock, flags);
break; break;
case ASM_CLIENT_EVENT_DATA_WRITE_DONE: case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
...@@ -539,13 +571,32 @@ static void compress_event_handler(uint32_t opcode, uint32_t token, ...@@ -539,13 +571,32 @@ static void compress_event_handler(uint32_t opcode, uint32_t token,
} }
avail = prtd->bytes_received - prtd->bytes_sent; avail = prtd->bytes_received - prtd->bytes_sent;
if (avail > prtd->pcm_count) {
bytes_to_write = prtd->pcm_count;
} else {
if (substream->partial_drain || prtd->notify_on_drain)
is_last_buffer = true;
bytes_to_write = avail;
}
if (bytes_to_write) {
if (substream->partial_drain && is_last_buffer) {
wflags |= ASM_LAST_BUFFER_FLAG;
q6asm_stream_remove_trailing_silence(prtd->audio_client,
prtd->stream_id,
prtd->trailing_samples_drop);
}
if (avail >= prtd->pcm_count) {
q6asm_write_async(prtd->audio_client, prtd->stream_id, q6asm_write_async(prtd->audio_client, prtd->stream_id,
prtd->pcm_count, 0, 0, 0); bytes_to_write, 0, 0, wflags);
prtd->bytes_sent += prtd->pcm_count;
prtd->bytes_sent += bytes_to_write;
} }
if (prtd->notify_on_drain && is_last_buffer)
q6asm_cmd_nowait(prtd->audio_client,
prtd->stream_id, CMD_EOS);
spin_unlock_irqrestore(&prtd->lock, flags); spin_unlock_irqrestore(&prtd->lock, flags);
break; break;
...@@ -625,9 +676,15 @@ static int q6asm_dai_compr_free(struct snd_soc_component *component, ...@@ -625,9 +676,15 @@ static int q6asm_dai_compr_free(struct snd_soc_component *component,
struct snd_soc_pcm_runtime *rtd = stream->private_data; struct snd_soc_pcm_runtime *rtd = stream->private_data;
if (prtd->audio_client) { if (prtd->audio_client) {
if (prtd->state) if (prtd->state) {
q6asm_cmd(prtd->audio_client, prtd->stream_id, q6asm_cmd(prtd->audio_client, prtd->stream_id,
CMD_CLOSE); CMD_CLOSE);
if (prtd->next_track_stream_id) {
q6asm_cmd(prtd->audio_client,
prtd->next_track_stream_id,
CMD_CLOSE);
}
}
snd_dma_free_pages(&prtd->dma_buffer); snd_dma_free_pages(&prtd->dma_buffer);
q6asm_unmap_memory_regions(stream->direction, q6asm_unmap_memory_regions(stream->direction,
...@@ -902,6 +959,32 @@ static int q6asm_dai_compr_set_metadata(struct snd_soc_component *component, ...@@ -902,6 +959,32 @@ static int q6asm_dai_compr_set_metadata(struct snd_soc_component *component,
break; break;
case SNDRV_COMPRESS_ENCODER_DELAY: case SNDRV_COMPRESS_ENCODER_DELAY:
prtd->initial_samples_drop = metadata->value[0]; prtd->initial_samples_drop = metadata->value[0];
if (prtd->next_track_stream_id) {
ret = q6asm_open_write(prtd->audio_client,
prtd->next_track_stream_id,
prtd->codec.id,
prtd->codec.profile,
prtd->bits_per_sample,
true);
if (ret < 0) {
dev_err(component->dev, "q6asm_open_write failed\n");
return ret;
}
ret = __q6asm_dai_compr_set_codec_params(component, stream,
&prtd->codec,
prtd->next_track_stream_id);
if (ret < 0) {
dev_err(component->dev, "q6asm_open_write failed\n");
return ret;
}
ret = q6asm_stream_remove_initial_silence(prtd->audio_client,
prtd->next_track_stream_id,
prtd->initial_samples_drop);
prtd->next_track_stream_id = 0;
}
break; break;
default: default:
ret = -EINVAL; ret = -EINVAL;
...@@ -935,6 +1018,14 @@ static int q6asm_dai_compr_trigger(struct snd_soc_component *component, ...@@ -935,6 +1018,14 @@ static int q6asm_dai_compr_trigger(struct snd_soc_component *component,
ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id, ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
CMD_PAUSE); CMD_PAUSE);
break; break;
case SND_COMPR_TRIGGER_NEXT_TRACK:
prtd->next_track = true;
prtd->next_track_stream_id = (prtd->stream_id == 1 ? 2 : 1);
break;
case SND_COMPR_TRIGGER_DRAIN:
case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
prtd->notify_on_drain = true;
break;
default: default:
ret = -EINVAL; ret = -EINVAL;
break; break;
......
...@@ -33,6 +33,7 @@ enum { ...@@ -33,6 +33,7 @@ enum {
#define MAX_SESSIONS 8 #define MAX_SESSIONS 8
#define FORMAT_LINEAR_PCM 0x0000 #define FORMAT_LINEAR_PCM 0x0000
#define ASM_LAST_BUFFER_FLAG BIT(30)
struct q6asm_flac_cfg { struct q6asm_flac_cfg {
u32 sample_rate; u32 sample_rate;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment