alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: "Thomas Niederprüm" <niederp@physik.uni-kl.de>
To: broonie@kernel.org, zonque@gmail.com, brandau@gmx.de, lars@metafoo.de
Cc: alsa-devel@alsa-project.org,
	"Thomas Niederprüm" <niederp@physik.uni-kl.de>
Subject: [PATCH 05/10] ASoC: sta32x: move code to calculate mclk divider and extrapolation ratio to sta32x_hw_params()
Date: Thu, 22 Jan 2015 00:01:57 +0100	[thread overview]
Message-ID: <1421881322-27806-6-git-send-email-niederp@physik.uni-kl.de> (raw)
In-Reply-To: <1421881322-27806-1-git-send-email-niederp@physik.uni-kl.de>

Signed-off-by: Thomas Niederprüm <niederp@physik.uni-kl.de>
---
 sound/soc/codecs/sta32x.c | 87 +++++++++++++++++------------------------------
 1 file changed, 32 insertions(+), 55 deletions(-)

diff --git a/sound/soc/codecs/sta32x.c b/sound/soc/codecs/sta32x.c
index 77be160..4858194 100644
--- a/sound/soc/codecs/sta32x.c
+++ b/sound/soc/codecs/sta32x.c
@@ -555,17 +555,12 @@ static struct {
 };
 
 /* MCLK to fs clock ratios */
-static struct {
-	int ratio;
-	int mcs;
-} mclk_ratios[3][7] = {
-	{ { 768, 0 }, { 512, 1 }, { 384, 2 }, { 256, 3 },
-	  { 128, 4 }, { 576, 5 }, { 0, 0 } },
-	{ { 384, 2 }, { 256, 3 }, { 192, 4 }, { 128, 5 }, {64, 0 }, { 0, 0 } },
-	{ { 384, 2 }, { 256, 3 }, { 192, 4 }, { 128, 5 }, {64, 0 }, { 0, 0 } },
+static int mcs_ratio_table[3][7] = {
+	{ 768, 512, 384, 256, 128, 576, 0 },
+	{ 384, 256, 192, 128,  64,   0 },
+	{ 384, 256, 192, 128,  64,   0 },
 };
 
-
 /**
  * sta32x_set_dai_sysclk - configure MCLK
  * @codec_dai: the codec DAI
@@ -590,46 +585,10 @@ static int sta32x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
 {
 	struct snd_soc_codec *codec = codec_dai->codec;
 	struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec);
-	int i, j, ir, fs;
-	unsigned int rates = 0;
-	unsigned int rate_min = -1;
-	unsigned int rate_max = 0;
 
-	pr_debug("mclk=%u\n", freq);
+	dev_dbg(codec->dev, "mclk=%u\n", freq);
 	sta32x->mclk = freq;
 
-	if (sta32x->mclk) {
-		for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) {
-			ir = interpolation_ratios[i].ir;
-			fs = interpolation_ratios[i].fs;
-			for (j = 0; mclk_ratios[ir][j].ratio; j++) {
-				if (mclk_ratios[ir][j].ratio * fs == freq) {
-					rates |= snd_pcm_rate_to_rate_bit(fs);
-					if (fs < rate_min)
-						rate_min = fs;
-					if (fs > rate_max)
-						rate_max = fs;
-					break;
-				}
-			}
-		}
-		/* FIXME: soc should support a rate list */
-		rates &= ~SNDRV_PCM_RATE_KNOT;
-
-		if (!rates) {
-			dev_err(codec->dev, "could not find a valid sample rate\n");
-			return -EINVAL;
-		}
-	} else {
-		/* enable all possible rates */
-		rates = STA32X_RATES;
-		rate_min = 32000;
-		rate_max = 192000;
-	}
-
-	codec_dai->driver->playback.rates = rates;
-	codec_dai->driver->playback.rate_min = rate_min;
-	codec_dai->driver->playback.rate_max = rate_max;
 	return 0;
 }
 
@@ -695,26 +654,44 @@ static int sta32x_hw_params(struct snd_pcm_substream *substream,
 {
 	struct snd_soc_codec *codec = dai->codec;
 	struct sta32x_priv *sta32x = snd_soc_codec_get_drvdata(codec);
-	unsigned int rate;
-	int i, mcs = -1, ir = -1;
+	int i, mcs = -EINVAL, ir = -EINVAL;
 	unsigned int confa, confb;
+	unsigned int rate, ratio;
+	int ret;
+
+	if (!sta32x->mclk) {
+		dev_err(codec->dev,
+			"sta32x->mclk is unset. Unable to determine ratio\n");
+		return -EIO;
+	}
 
 	rate = params_rate(params);
-	pr_debug("rate: %u\n", rate);
-	for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++)
+	ratio = sta32x->mclk / rate;
+	dev_dbg(codec->dev, "rate: %u, ratio: %u\n", rate, ratio);
+
+	for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) {
 		if (interpolation_ratios[i].fs == rate) {
 			ir = interpolation_ratios[i].ir;
 			break;
 		}
-	if (ir < 0)
+	}
+
+	if (ir < 0) {
+		dev_err(codec->dev, "Unsupported samplerate: %u\n", rate);
 		return -EINVAL;
-	for (i = 0; mclk_ratios[ir][i].ratio; i++)
-		if (mclk_ratios[ir][i].ratio * rate == sta32x->mclk) {
-			mcs = mclk_ratios[ir][i].mcs;
+	}
+
+	for (i = 0; i < 6; i++) {
+		if (mcs_ratio_table[ir][i] == ratio) {
+			mcs = i;
 			break;
 		}
-	if (mcs < 0)
+	}
+
+	if (mcs < 0) {
+		dev_err(codec->dev, "Unresolvable ratio: %u\n", ratio);
 		return -EINVAL;
+	}
 
 	confa = (ir << STA32X_CONFA_IR_SHIFT) |
 		(mcs << STA32X_CONFA_MCS_SHIFT);
-- 
2.1.1

_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

  parent reply	other threads:[~2015-01-21 23:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-21 23:01 [PATCH 00/10] ASoC: sta32x: Add device tree binding Thomas Niederprüm
2015-01-21 23:01 ` [PATCH 01/10] ASoC: sta32x: Convert to direct regmap API usage Thomas Niederprüm
2015-01-21 23:01 ` [PATCH 02/10] ASoC: sta32x: make sta32x a gpio consumer for the reset GPIO Thomas Niederprüm
2015-01-27 17:00   ` Mark Brown
2015-01-21 23:01 ` [PATCH 03/10] ASoC: sta32x: use DECLARE_TLV_DB_RANGE macro Thomas Niederprüm
2015-01-21 23:01 ` [PATCH 04/10] ASoC: sta32x: add status register Thomas Niederprüm
2015-01-27 17:03   ` Mark Brown
2015-01-21 23:01 ` Thomas Niederprüm [this message]
2015-01-21 23:01 ` [PATCH 06/10] ASoC: sta32x: add device tree binding Thomas Niederprüm
2015-01-21 23:01 ` [PATCH 07/10] ASoC: sta32x: use dev_dbg() for debug output Thomas Niederprüm
2015-01-21 23:02 ` [PATCH 08/10] ASoC: sta32x: minor Kconfig update Thomas Niederprüm
2015-01-21 23:02 ` [PATCH 09/10] ASoC: sta32x: correct bit shift value for IDE register Thomas Niederprüm
2015-01-27 17:09   ` Mark Brown
2015-01-21 23:02 ` [PATCH 10/10] ASoC: sta32x: change dai name to be in line with the sta350 driver Thomas Niederprüm
2015-01-22 13:05 ` [PATCH 00/10] ASoC: sta32x: Add device tree binding Lars-Peter Clausen
2015-01-22 13:12   ` Daniel Mack
2015-01-22 20:46     ` Thomas Niederprüm
2015-01-27 17:16 ` Mark Brown

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=1421881322-27806-6-git-send-email-niederp@physik.uni-kl.de \
    --to=niederp@physik.uni-kl.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=brandau@gmx.de \
    --cc=broonie@kernel.org \
    --cc=lars@metafoo.de \
    --cc=zonque@gmail.com \
    /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).