linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode
@ 2020-06-29 13:58 Shengjiu Wang
  2020-06-29 20:08 ` Nicolin Chen
  2020-06-30 12:36 ` Fabio Estevam
  0 siblings, 2 replies; 5+ messages in thread
From: Shengjiu Wang @ 2020-06-29 13:58 UTC (permalink / raw)
  To: timur, nicoleotsuka, Xiubo.Lee, festevam, broonie, alsa-devel,
	lgirdwood, perex, tiwai
  Cc: linuxppc-dev, linux-kernel

The ASRC not only supports ideal ratio mode, but also supports
internal ratio mode.

For internal rato mode, the rate of clock source should be divided
with no remainder by sample rate, otherwise there is sound
distortion.

Add function fsl_asrc_select_clk() to find proper clock source for
internal ratio mode, if the clock source is available then internal
ratio mode will be selected.

With change, the ideal ratio mode is not the only option for user.

Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
---
 sound/soc/fsl/fsl_asrc.c | 58 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)

diff --git a/sound/soc/fsl/fsl_asrc.c b/sound/soc/fsl/fsl_asrc.c
index 95f6a9617b0b..fcafc8ecb131 100644
--- a/sound/soc/fsl/fsl_asrc.c
+++ b/sound/soc/fsl/fsl_asrc.c
@@ -582,11 +582,59 @@ static int fsl_asrc_dai_startup(struct snd_pcm_substream *substream,
 			SNDRV_PCM_HW_PARAM_RATE, &fsl_asrc_rate_constraints);
 }
 
+/**
+ * Select proper clock source for internal ratio mode
+ */
+static int fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
+			       struct fsl_asrc_pair *pair,
+			       int in_rate,
+			       int out_rate)
+{
+	struct fsl_asrc_pair_priv *pair_priv = pair->private;
+	struct asrc_config *config = pair_priv->config;
+	int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
+	int clk_rate, clk_index;
+	int i = 0, j = 0;
+	bool clk_sel[2];
+
+	rate[0] = in_rate;
+	rate[1] = out_rate;
+
+	/* Select proper clock source for internal ratio mode */
+	for (j = 0; j < 2; j++) {
+		for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
+			clk_index = asrc_priv->clk_map[j][i];
+			clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
+			if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
+			    (clk_rate % rate[j]) == 0)
+				break;
+		}
+
+		if (i == ASRC_CLK_MAP_LEN) {
+			select_clk[j] = OUTCLK_ASRCK1_CLK;
+			clk_sel[j] = false;
+		} else {
+			select_clk[j] = i;
+			clk_sel[j] = true;
+		}
+	}
+
+	/* Switch to ideal ratio mode if there is no proper clock source */
+	if (!clk_sel[IN] || !clk_sel[OUT])
+		select_clk[IN] = INCLK_NONE;
+
+	config->inclk = select_clk[IN];
+	config->outclk = select_clk[OUT];
+
+	return 0;
+}
+
 static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
 				  struct snd_pcm_hw_params *params,
 				  struct snd_soc_dai *dai)
 {
 	struct fsl_asrc *asrc = snd_soc_dai_get_drvdata(dai);
+	struct fsl_asrc_priv *asrc_priv = asrc->private;
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct fsl_asrc_pair *pair = runtime->private_data;
 	struct fsl_asrc_pair_priv *pair_priv = pair->private;
@@ -605,8 +653,6 @@ static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
 
 	config.pair = pair->index;
 	config.channel_num = channels;
-	config.inclk = INCLK_NONE;
-	config.outclk = OUTCLK_ASRCK1_CLK;
 
 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 		config.input_format   = params_format(params);
@@ -620,6 +666,14 @@ static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
 		config.output_sample_rate = rate;
 	}
 
+	ret = fsl_asrc_select_clk(asrc_priv, pair,
+				  config.input_sample_rate,
+				  config.output_sample_rate);
+	if (ret) {
+		dev_err(dai->dev, "fail to select clock\n");
+		return ret;
+	}
+
 	ret = fsl_asrc_config_pair(pair, false);
 	if (ret) {
 		dev_err(dai->dev, "fail to config asrc pair\n");
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode
  2020-06-29 13:58 [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode Shengjiu Wang
@ 2020-06-29 20:08 ` Nicolin Chen
  2020-06-30  5:31   ` Shengjiu Wang
  2020-06-30 12:36 ` Fabio Estevam
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolin Chen @ 2020-06-29 20:08 UTC (permalink / raw)
  To: Shengjiu Wang
  Cc: timur, Xiubo.Lee, festevam, broonie, alsa-devel, lgirdwood,
	perex, tiwai, linuxppc-dev, linux-kernel

On Mon, Jun 29, 2020 at 09:58:35PM +0800, Shengjiu Wang wrote:
> The ASRC not only supports ideal ratio mode, but also supports
> internal ratio mode.
> 
> For internal rato mode, the rate of clock source should be divided
> with no remainder by sample rate, otherwise there is sound
> distortion.
> 
> Add function fsl_asrc_select_clk() to find proper clock source for
> internal ratio mode, if the clock source is available then internal
> ratio mode will be selected.
> 
> With change, the ideal ratio mode is not the only option for user.
> 
> Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
> ---

> +static int fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
> +			       struct fsl_asrc_pair *pair,
> +			       int in_rate,
> +			       int out_rate)
> +{
> +	struct fsl_asrc_pair_priv *pair_priv = pair->private;
> +	struct asrc_config *config = pair_priv->config;
> +	int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
> +	int clk_rate, clk_index;
> +	int i = 0, j = 0;
> +	bool clk_sel[2];
> +
> +	rate[0] = in_rate;
> +	rate[1] = out_rate;
> +
> +	/* Select proper clock source for internal ratio mode */
> +	for (j = 0; j < 2; j++) {
> +		for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
> +			clk_index = asrc_priv->clk_map[j][i];
> +			clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);

+			/* Only match a perfect clock source with no remainder */

> +			if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
> +			    (clk_rate % rate[j]) == 0)
> +				break;
> +		}
> +
> +		if (i == ASRC_CLK_MAP_LEN) {
> +			select_clk[j] = OUTCLK_ASRCK1_CLK;
> +			clk_sel[j] = false;
> +		} else {
> +			select_clk[j] = i;
> +			clk_sel[j] = true;
> +		}
> +	}
> +
> +	/* Switch to ideal ratio mode if there is no proper clock source */
> +	if (!clk_sel[IN] || !clk_sel[OUT])
> +		select_clk[IN] = INCLK_NONE;

Could get rid of clk_set:

	for (j) {
		for (i) {
			if (match)
				break;
		}

		clk[j] = i;
	}

	if (clk[IN] == ASRC_CLK_MAP_LEN || clk[OUT] == ASRC_CLK_MAP_LEN)

And it only overrides clk[IN] setting but leaving clk[OUT] to
to the searching result. This means that clk[OUT] may be using
a clock source other than OUTCLK_ASRCK1_CLK if sel[IN] happens
to be false while sel[OUT] happens to be true. Not sure if it
is intended...but I feel it would probably be safer to use the
previous settings: INCLK_NONE + OUTCLK_ASRCK1_CLK?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode
  2020-06-29 20:08 ` Nicolin Chen
@ 2020-06-30  5:31   ` Shengjiu Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Shengjiu Wang @ 2020-06-30  5:31 UTC (permalink / raw)
  To: Nicolin Chen
  Cc: Shengjiu Wang, Linux-ALSA, Timur Tabi, Xiubo Li, linuxppc-dev,
	Takashi Iwai, Liam Girdwood, Mark Brown, Fabio Estevam,
	linux-kernel

On Tue, Jun 30, 2020 at 4:09 AM Nicolin Chen <nicoleotsuka@gmail.com> wrote:
>
> On Mon, Jun 29, 2020 at 09:58:35PM +0800, Shengjiu Wang wrote:
> > The ASRC not only supports ideal ratio mode, but also supports
> > internal ratio mode.
> >
> > For internal rato mode, the rate of clock source should be divided
> > with no remainder by sample rate, otherwise there is sound
> > distortion.
> >
> > Add function fsl_asrc_select_clk() to find proper clock source for
> > internal ratio mode, if the clock source is available then internal
> > ratio mode will be selected.
> >
> > With change, the ideal ratio mode is not the only option for user.
> >
> > Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
> > ---
>
> > +static int fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
> > +                            struct fsl_asrc_pair *pair,
> > +                            int in_rate,
> > +                            int out_rate)
> > +{
> > +     struct fsl_asrc_pair_priv *pair_priv = pair->private;
> > +     struct asrc_config *config = pair_priv->config;
> > +     int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
> > +     int clk_rate, clk_index;
> > +     int i = 0, j = 0;
> > +     bool clk_sel[2];
> > +
> > +     rate[0] = in_rate;
> > +     rate[1] = out_rate;
> > +
> > +     /* Select proper clock source for internal ratio mode */
> > +     for (j = 0; j < 2; j++) {
> > +             for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
> > +                     clk_index = asrc_priv->clk_map[j][i];
> > +                     clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
>
> +                       /* Only match a perfect clock source with no remainder */
>
> > +                     if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
> > +                         (clk_rate % rate[j]) == 0)
> > +                             break;
> > +             }
> > +
> > +             if (i == ASRC_CLK_MAP_LEN) {
> > +                     select_clk[j] = OUTCLK_ASRCK1_CLK;
> > +                     clk_sel[j] = false;
> > +             } else {
> > +                     select_clk[j] = i;
> > +                     clk_sel[j] = true;
> > +             }
> > +     }
> > +
> > +     /* Switch to ideal ratio mode if there is no proper clock source */
> > +     if (!clk_sel[IN] || !clk_sel[OUT])
> > +             select_clk[IN] = INCLK_NONE;
>
> Could get rid of clk_set:
>
>         for (j) {
>                 for (i) {
>                         if (match)
>                                 break;
>                 }
>
>                 clk[j] = i;
>         }
>
>         if (clk[IN] == ASRC_CLK_MAP_LEN || clk[OUT] == ASRC_CLK_MAP_LEN)
>
> And it only overrides clk[IN] setting but leaving clk[OUT] to
> to the searching result. This means that clk[OUT] may be using
> a clock source other than OUTCLK_ASRCK1_CLK if sel[IN] happens
> to be false while sel[OUT] happens to be true. Not sure if it
> is intended...but I feel it would probably be safer to use the
> previous settings: INCLK_NONE + OUTCLK_ASRCK1_CLK?

ok, will update the patch.

best regards
wang shengjiu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode
  2020-06-29 13:58 [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode Shengjiu Wang
  2020-06-29 20:08 ` Nicolin Chen
@ 2020-06-30 12:36 ` Fabio Estevam
  2020-06-30 13:30   ` Shengjiu Wang
  1 sibling, 1 reply; 5+ messages in thread
From: Fabio Estevam @ 2020-06-30 12:36 UTC (permalink / raw)
  To: Shengjiu Wang
  Cc: Timur Tabi, Nicolin Chen, Xiubo Li, Mark Brown, Linux-ALSA,
	Liam Girdwood, Jaroslav Kysela, Takashi Iwai, linuxppc-dev,
	linux-kernel

Hi Shengjiu,

On Mon, Jun 29, 2020 at 11:10 AM Shengjiu Wang <shengjiu.wang@nxp.com> wrote:

> +/**

"/**" notation may confuse 'make htmldocs". Since this is a single
line comment you could do:

/* Select proper clock source for internal ratio mode */


> + * Select proper clock source for internal ratio mode
> + */
> +static int fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
> +                              struct fsl_asrc_pair *pair,
> +                              int in_rate,
> +                              int out_rate)
> +{
> +       struct fsl_asrc_pair_priv *pair_priv = pair->private;
> +       struct asrc_config *config = pair_priv->config;
> +       int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
> +       int clk_rate, clk_index;
> +       int i = 0, j = 0;
> +       bool clk_sel[2];
> +
> +       rate[0] = in_rate;
> +       rate[1] = out_rate;
> +
> +       /* Select proper clock source for internal ratio mode */
> +       for (j = 0; j < 2; j++) {
> +               for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
> +                       clk_index = asrc_priv->clk_map[j][i];
> +                       clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
> +                       if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
> +                           (clk_rate % rate[j]) == 0)
> +                               break;
> +               }
> +
> +               if (i == ASRC_CLK_MAP_LEN) {
> +                       select_clk[j] = OUTCLK_ASRCK1_CLK;
> +                       clk_sel[j] = false;
> +               } else {
> +                       select_clk[j] = i;
> +                       clk_sel[j] = true;
> +               }
> +       }
> +
> +       /* Switch to ideal ratio mode if there is no proper clock source */
> +       if (!clk_sel[IN] || !clk_sel[OUT])
> +               select_clk[IN] = INCLK_NONE;
> +
> +       config->inclk = select_clk[IN];
> +       config->outclk = select_clk[OUT];
> +
> +       return 0;

This new function always returns 0. Should it be converted to 'void'
type instead?

> +       ret = fsl_asrc_select_clk(asrc_priv, pair,
> +                                 config.input_sample_rate,
> +                                 config.output_sample_rate);
> +       if (ret) {
> +               dev_err(dai->dev, "fail to select clock\n");

fsl_asrc_select_clk() does not return error, so you could skip the
error checking.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode
  2020-06-30 12:36 ` Fabio Estevam
@ 2020-06-30 13:30   ` Shengjiu Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Shengjiu Wang @ 2020-06-30 13:30 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: Shengjiu Wang, Linux-ALSA, Timur Tabi, Xiubo Li, linux-kernel,
	Takashi Iwai, Liam Girdwood, Nicolin Chen, Mark Brown,
	linuxppc-dev

On Tue, Jun 30, 2020 at 8:38 PM Fabio Estevam <festevam@gmail.com> wrote:
>
> Hi Shengjiu,
>
> On Mon, Jun 29, 2020 at 11:10 AM Shengjiu Wang <shengjiu.wang@nxp.com> wrote:
>
> > +/**
>
> "/**" notation may confuse 'make htmldocs". Since this is a single
> line comment you could do:
>
> /* Select proper clock source for internal ratio mode */
>
>
> > + * Select proper clock source for internal ratio mode
> > + */
> > +static int fsl_asrc_select_clk(struct fsl_asrc_priv *asrc_priv,
> > +                              struct fsl_asrc_pair *pair,
> > +                              int in_rate,
> > +                              int out_rate)
> > +{
> > +       struct fsl_asrc_pair_priv *pair_priv = pair->private;
> > +       struct asrc_config *config = pair_priv->config;
> > +       int rate[2], select_clk[2]; /* Array size 2 means IN and OUT */
> > +       int clk_rate, clk_index;
> > +       int i = 0, j = 0;
> > +       bool clk_sel[2];
> > +
> > +       rate[0] = in_rate;
> > +       rate[1] = out_rate;
> > +
> > +       /* Select proper clock source for internal ratio mode */
> > +       for (j = 0; j < 2; j++) {
> > +               for (i = 0; i < ASRC_CLK_MAP_LEN; i++) {
> > +                       clk_index = asrc_priv->clk_map[j][i];
> > +                       clk_rate = clk_get_rate(asrc_priv->asrck_clk[clk_index]);
> > +                       if (clk_rate != 0 && (clk_rate / rate[j]) <= 1024 &&
> > +                           (clk_rate % rate[j]) == 0)
> > +                               break;
> > +               }
> > +
> > +               if (i == ASRC_CLK_MAP_LEN) {
> > +                       select_clk[j] = OUTCLK_ASRCK1_CLK;
> > +                       clk_sel[j] = false;
> > +               } else {
> > +                       select_clk[j] = i;
> > +                       clk_sel[j] = true;
> > +               }
> > +       }
> > +
> > +       /* Switch to ideal ratio mode if there is no proper clock source */
> > +       if (!clk_sel[IN] || !clk_sel[OUT])
> > +               select_clk[IN] = INCLK_NONE;
> > +
> > +       config->inclk = select_clk[IN];
> > +       config->outclk = select_clk[OUT];
> > +
> > +       return 0;
>
> This new function always returns 0. Should it be converted to 'void'
> type instead?
>
> > +       ret = fsl_asrc_select_clk(asrc_priv, pair,
> > +                                 config.input_sample_rate,
> > +                                 config.output_sample_rate);
> > +       if (ret) {
> > +               dev_err(dai->dev, "fail to select clock\n");
>
> fsl_asrc_select_clk() does not return error, so you could skip the
> error checking.

ok, will update the patch

best regards
wang shengjiu

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-30 13:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-29 13:58 [PATCH] ASoC: fsl_asrc: Add an option to select internal ratio mode Shengjiu Wang
2020-06-29 20:08 ` Nicolin Chen
2020-06-30  5:31   ` Shengjiu Wang
2020-06-30 12:36 ` Fabio Estevam
2020-06-30 13:30   ` Shengjiu Wang

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).