Skip to contentSkip to conversation

ASoC: Improve EP93xx I2S clocks management.

PATCH In review Alexander Sverdlin 2 participants 3 messages Mar 5, 2011 – Mar 7, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: Improve EP93xx I2S clocks management.

From: Alexander Sverdlin <[email protected]>

Improve EP93xx I2S clocks management.
Some freqs values are set not exact as they requested for MCLK and original code was not able to find divisors for SCLK and LRCLK. This code just picks up nearest value from 3 possible variants. This patch makes 44100 and 192000 rates working and fixes capture function.

---
 sound/soc/ep93xx/ep93xx-i2s.c |   19 +++++++++----------
 1 files changed, 9 insertions(+), 10 deletions(-)
sound/soc/ep93xx/ep93xx-i2s.c +9 -10
index 0458dd4..3d4c086 100644
@@ -242,7 +242,7 @@ static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
 {
 	struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
 	unsigned word_len, div, sdiv, lrdiv;
-	int found = 0, err;
+	int err;
 
 	switch (params_format(params)) {
 	case SNDRV_PCM_FORMAT_S16_LE:
@@ -275,15 +275,14 @@ static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
 	 * the codec uses.
 	 */
 	div = clk_get_rate(info->mclk) / params_rate(params);
-	for (sdiv = 2; sdiv <= 4; sdiv += 2)
-		for (lrdiv = 64; lrdiv <= 128; lrdiv <<= 1)
-			if (sdiv * lrdiv == div) {
-				found = 1;
-				goto out;
-			}
-out:
-	if (!found)
-		return -EINVAL;
+	sdiv = 4;
+	if (div > (256 + 512) / 2) {
+		lrdiv = 128;
+	} else {
+		lrdiv = 64;
+		if (div < (128 + 256) / 2)
+			sdiv = 2;
+	}
 
 	err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
 	if (err)
#1 lore raw
Quoted 38 lines · Alexander Sverdlin

From: Alexander Sverdlin <[email protected]>

Improve EP93xx I2S clocks management.
Some freqs values are set not exact as they requested for MCLK and original code was not able to find divisors for SCLK and LRCLK. This code just picks up nearest value from 3 possible variants. This patch makes 44100 and 192000 rates working and fixes capture function.

---
 sound/soc/ep93xx/ep93xx-i2s.c |   19 +++++++++----------
 1 files changed, 9 insertions(+), 10 deletions(-)
sound/soc/ep93xx/ep93xx-i2s.c +1 -8
index 0458dd4..3d4c086 100644
@@ -242,7 +242,7 @@ static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
 {
 	struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
 	unsigned word_len, div, sdiv, lrdiv;
-	int found = 0, err;
+	int err;
 
 	switch (params_format(params)) {
 	case SNDRV_PCM_FORMAT_S16_LE:
@@ -275,15 +275,14 @@ static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
 	 * the codec uses.
 	 */
 	div = clk_get_rate(info->mclk) / params_rate(params);
-	for (sdiv = 2; sdiv <= 4; sdiv += 2)
-		for (lrdiv = 64; lrdiv <= 128; lrdiv <<= 1)
-			if (sdiv * lrdiv == div) {
-				found = 1;
-				goto out;
-			}
-out:

sound/soc/ep93xx/ep93xx-i2s.c static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream, · line 285

Quoted 10 lines
sound/soc/ep93xx/ep93xx-i2s.c +8 -2
-	if (!found)
-		return -EINVAL;
+	sdiv = 4;
+	if (div > (256 + 512) / 2) {
+		lrdiv = 128;
+	} else {
+		lrdiv = 64;
+		if (div < (128 + 256) / 2)
+			sdiv = 2;
+	}

I don't understand where the magic numbers above come from? Does it work correctly for all rates? I think a comment is needed to explain how this works.

An alternative would be to keep the existing loop form, but try and find the closest match rather than an exact match. Something like this (untested):

	unsigned long diff, lowest_diff = ULONG_MAX;

	/* Find the closest match for sdiv/lrdiv */
	best_sdiv  = 2;
	best_lrdiv = 64;
	for (sdiv = 2; sdiv <= 4; sdiv += 2) {
		for (lrdiv = 64; lrdiv <= 128; lrdiv <<= 1) {
			diff = abs(div - (sdiv * lrdiv));
			if (diff < lowest_diff) {
				lowest_diff = diff;
				best_sdiv   = sdiv;
				best_lrdiv  = lrdiv;
			}
		}
	}

	sdiv  = best_sdiv;
	lrdiv = best_lrdiv;

~Ryan

Signature · 7 lines
Bluewater Systems Ltd - ARM Technology Solution Centre

Ryan Mallon         		5 Amuri Park, 404 Barbadoes St
[email protected]         	PO Box 13 889, Christchurch 8013
http://www.bluewatersys.com	New Zealand
Phone: +64 3 3779127		Freecall: Australia 1800 148 751
Fax:   +64 3 3779135			  USA 1800 261 2934
#2 lore raw

Dear Ryan,

Quoted 4 lines · Ryan Mallon
Quoted 4 lines
sound/soc/ep93xx/ep93xx-i2s.c +4 -0
+	sdiv = 4;
+	if (div > (256 + 512) / 2) {
+		lrdiv = 128;
+	} else {
Quoted 8 lines

sound/soc/ep93xx/ep93xx-i2s.c static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream, · line 285

Quoted 4 lines
sound/soc/ep93xx/ep93xx-i2s.c +4 -0
+		lrdiv = 64;
+		if (div < (128 + 256) / 2)
+			sdiv = 2;
+	}

I don't understand where the magic numbers above come from? Does it work correctly for all rates? I think a comment is needed to explain how this works.

The Magic is simply centers between divisors. The loop produced only 3 variants of divisors. So do this code. But it's simpler and it prefers LRDIV = 64. I do not know what do you mean by all rates, with fixed MCLK there is no way to produce all the rates. With variable MCLK I'm having full 8000-192000 range now.

Quoted 4 lines

An alternative would be to keep the existing loop form, but try and find the closest match rather than an exact match. Something like this (untested):

This is huge comparing to 2 ifs... But the main task of keeping LRCLK=64 where possible still can be achieved if we swap to lines:

Quoted 2 lines
	for (sdiv = 2; sdiv <= 4; sdiv += 2) {
		for (lrdiv = 64; lrdiv <= 128; lrdiv <<= 1) {

But I'm sure code proposed by me should work just like previous variant for you. Could you try it with your configuration please?