alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: Ian Minett <ian_minett@creativelabs.com>
To: patch@alsa-project.org
Cc: alsa-devel@alsa-project.org, Ian Minett <ian_minett@creativelabs.com>
Subject: [PATCH 3/4] ALSA: CA0132: Add latency monitoring
Date: Fri, 8 Feb 2013 18:31:44 -0800	[thread overview]
Message-ID: <1360377105-2480-4-git-send-email-ian_minett@creativelabs.com> (raw)
In-Reply-To: <1360377105-2480-1-git-send-email-ian_minett@creativelabs.com>

From: Ian Minett <ian_minett@creativelabs.com>

Add latency monitoring for playback, capture and DSP effects.

Signed-off-by: Ian Minett <ian_minett@creativelabs.com>

diff --git a/sound/pci/hda/patch_ca0132.c b/sound/pci/hda/patch_ca0132.c
index b52f00c..c797f65 100644
--- a/sound/pci/hda/patch_ca0132.c
+++ b/sound/pci/hda/patch_ca0132.c
@@ -133,6 +133,12 @@ enum {
 /* Effects values size*/
 #define EFFECT_VALS_MAX_COUNT 12
 
+#define DSP_CAPTURE_INIT_LATENCY        0
+#define DSP_CRYSTAL_VOICE_LATENCY       124
+#define DSP_PLAYBACK_INIT_LATENCY       13
+#define DSP_PLAY_ENHANCEMENT_LATENCY    30
+#define DSP_SPEAKER_OUT_LATENCY         7
+
 struct ct_effect {
 	char name[44];
 	hda_nid_t nid;
@@ -2761,8 +2767,27 @@ static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
 }
 
 /*
- * PCM callbacks
+ * PCM playbacks
  */
+static unsigned int ca0132_get_playback_latency(struct hda_codec *codec)
+{
+	struct ca0132_spec *spec = codec->spec;
+	unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
+
+	if (!dspload_is_loaded(codec))
+		return 0;
+
+	if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
+		if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
+		    (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
+			latency += DSP_PLAY_ENHANCEMENT_LATENCY;
+	}
+
+	if (spec->cur_out_type == SPEAKER_OUT)
+		latency += DSP_SPEAKER_OUT_LATENCY;
+	return latency;
+}
+
 static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
 			struct hda_codec *codec,
 			unsigned int stream_tag,
@@ -2770,6 +2795,16 @@ static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
 			struct snd_pcm_substream *substream)
 {
 	struct ca0132_spec *spec = codec->spec;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	unsigned int latency = ca0132_get_playback_latency(codec);
+
+	if (spec->dsp_state == DSP_DOWNLOADING) {
+		spec->dsp_stream_id = stream_tag;
+		return 0;
+	}
+
+	runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
+					runtime->byte_align) / 1000);
 
 	ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
 
@@ -2834,8 +2869,22 @@ static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
 }
 
 /*
- * Analog capture
+ * PCM capture
  */
+
+static unsigned int ca0132_get_capture_latency(struct hda_codec *codec)
+{
+	struct ca0132_spec *spec = codec->spec;
+	unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
+
+	if (!dspload_is_loaded(codec))
+		return 0;
+
+	if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
+		latency += DSP_CRYSTAL_VOICE_LATENCY;
+	return latency;
+}
+
 static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
 					struct hda_codec *codec,
 					unsigned int stream_tag,
@@ -2843,9 +2892,16 @@ static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
 					struct snd_pcm_substream *substream)
 {
 	struct ca0132_spec *spec = codec->spec;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	unsigned int latency = ca0132_get_capture_latency(codec);
 
-	ca0132_setup_stream(codec, spec->adcs[substream->number],
-			    stream_tag, 0, format);
+	if (spec->dsp_state == DSP_DOWNLOADING)
+		return 0;
+
+	runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
+					runtime->byte_align) / 1000);
+
+	ca0132_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
 
 	return 0;
 }
@@ -3429,6 +3485,33 @@ static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
 	return 1;
 }
 
+static void ca0132_update_latency(struct hda_codec *codec,
+				  unsigned int direction)
+{
+	struct ca0132_spec *spec = codec->spec;
+	unsigned int latency;
+	struct snd_pcm_substream *substr;
+	struct snd_pcm_runtime *runtime;
+
+	if (spec->dsp_state == DSP_DOWNLOADING)
+		return;
+
+	latency = (direction == SNDRV_PCM_STREAM_PLAYBACK)
+			? ca0132_get_playback_latency(codec)
+			: ca0132_get_capture_latency(codec);
+
+	substr = codec->pcm_info->pcm->streams[direction].substream;
+	while (substr) {
+		runtime = substr->runtime;
+		/* update latency only when frame_bits is set */
+		if (runtime && runtime->frame_bits)
+			runtime->delay = bytes_to_frames(runtime,
+					(latency * runtime->rate *
+					runtime->byte_align) / 1000);
+		substr = substr->next;
+	}
+}
+
 /*
  * Set the effects parameters
  */
@@ -3468,6 +3551,9 @@ static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
 	err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
 				   ca0132_effects[idx].reqs[0], on);
 
+	ca0132_update_latency(codec, SNDRV_PCM_STREAM_PLAYBACK);
+	ca0132_update_latency(codec, SNDRV_PCM_STREAM_CAPTURE);
+
 	if (err < 0)
 		return 0; /* no changed */
 
-- 
1.7.4.1

  parent reply	other threads:[~2013-02-09  2:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-09  2:31 [PATCH 0/4] ALSA: Updates for the CA0132 codec Ian Minett
2013-02-09  2:31 ` [PATCH 1/4] ALSA: CA0132: Add SpeakerEQ feature firmware loading Ian Minett
2013-02-10 10:47   ` Takashi Iwai
2013-02-09  2:31 ` [PATCH 2/4] ALSA: CA0132: Improve the DSP transfer timeout calculations Ian Minett
2013-02-10 10:51   ` Takashi Iwai
2013-02-09  2:31 ` Ian Minett [this message]
2013-02-10 10:51   ` [PATCH 3/4] ALSA: CA0132: Add latency monitoring Takashi Iwai
2013-03-25 17:47     ` Dylan Reid
2013-04-02  9:30       ` Takashi Iwai
2013-04-02 20:24         ` Dylan Reid
2013-04-03  5:19           ` Takashi Iwai
2013-02-09  2:31 ` [PATCH 4/4] ALSA: CA0132: Update hp unsol event handler Ian Minett
2013-02-10 10:52   ` Takashi Iwai
2013-02-10 10:57 ` [PATCH 0/4] ALSA: Updates for the CA0132 codec Takashi Iwai
2013-03-15  0:34   ` Dylan Reid
2013-03-15  8:25     ` Takashi Iwai
2013-03-15 18:39       ` Dylan Reid
2013-03-15 20:23         ` Takashi Iwai
2013-03-20 17:21           ` Dylan Reid
2013-03-20 17:39             ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1360377105-2480-4-git-send-email-ian_minett@creativelabs.com \
    --to=ian_minett@creativelabs.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=patch@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).