Commit b39eb1e2 authored by Shengjiu Wang's avatar Shengjiu Wang Committed by Mark Brown

ASoC: fsl_asrc: refine the setting of internal clock divider

The output divider should align with the output sample
rate, if use ideal sample rate, there will be a lot of overload,
which would cause underrun.

The maximum divider of asrc clock is 1024, but there is no
judgement for this limitation in driver, which may cause the divider
setting not correct.

For non-ideal ratio mode, the clock rate should divide the sample
rate with no remainder, and the quotient should be less than 1024.
Signed-off-by: default avatarShengjiu Wang <shengjiu.wang@nxp.com>
Acked-by: default avatarNicolin Chen <nicoleotsuka@gmail.com>
Link: https://lore.kernel.org/r/23c634e4bf58afce5b3ae67f5f42e8d1cae2639a.1572252307.git.shengjiu.wang@nxp.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 15747a80
...@@ -259,8 +259,15 @@ static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair, ...@@ -259,8 +259,15 @@ static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair,
* It configures those ASRC registers according to a configuration instance * It configures those ASRC registers according to a configuration instance
* of struct asrc_config which includes in/output sample rate, width, channel * of struct asrc_config which includes in/output sample rate, width, channel
* and clock settings. * and clock settings.
*
* Note:
* The ideal ratio configuration can work with a flexible clock rate setting.
* Using IDEAL_RATIO_RATE gives a faster converting speed but overloads ASRC.
* For a regular audio playback, the clock rate should not be slower than an
* clock rate aligning with the output sample rate; For a use case requiring
* faster conversion, set use_ideal_rate to have the faster speed.
*/ */
static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair, bool use_ideal_rate)
{ {
struct asrc_config *config = pair->config; struct asrc_config *config = pair->config;
struct fsl_asrc *asrc_priv = pair->asrc_priv; struct fsl_asrc *asrc_priv = pair->asrc_priv;
...@@ -268,7 +275,8 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) ...@@ -268,7 +275,8 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair)
enum asrc_word_width input_word_width; enum asrc_word_width input_word_width;
enum asrc_word_width output_word_width; enum asrc_word_width output_word_width;
u32 inrate, outrate, indiv, outdiv; u32 inrate, outrate, indiv, outdiv;
u32 clk_index[2], div[2]; u32 clk_index[2], div[2], rem[2];
u64 clk_rate;
int in, out, channels; int in, out, channels;
int pre_proc, post_proc; int pre_proc, post_proc;
struct clk *clk; struct clk *clk;
...@@ -351,27 +359,42 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) ...@@ -351,27 +359,42 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair)
/* We only have output clock for ideal ratio mode */ /* We only have output clock for ideal ratio mode */
clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]]; clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]];
div[IN] = clk_get_rate(clk) / inrate; clk_rate = clk_get_rate(clk);
if (div[IN] == 0) { rem[IN] = do_div(clk_rate, inrate);
div[IN] = (u32)clk_rate;
/*
* The divider range is [1, 1024], defined by the hardware. For non-
* ideal ratio configuration, clock rate has to be strictly aligned
* with the sample rate. For ideal ratio configuration, clock rates
* only result in different converting speeds. So remainder does not
* matter, as long as we keep the divider within its valid range.
*/
if (div[IN] == 0 || (!ideal && (div[IN] > 1024 || rem[IN] != 0))) {
pair_err("failed to support input sample rate %dHz by asrck_%x\n", pair_err("failed to support input sample rate %dHz by asrck_%x\n",
inrate, clk_index[ideal ? OUT : IN]); inrate, clk_index[ideal ? OUT : IN]);
return -EINVAL; return -EINVAL;
} }
clk = asrc_priv->asrck_clk[clk_index[OUT]]; div[IN] = min_t(u32, 1024, div[IN]);
/* Use fixed output rate for Ideal Ratio mode (INCLK_NONE) */ clk = asrc_priv->asrck_clk[clk_index[OUT]];
if (ideal) clk_rate = clk_get_rate(clk);
div[OUT] = clk_get_rate(clk) / IDEAL_RATIO_RATE; if (ideal && use_ideal_rate)
rem[OUT] = do_div(clk_rate, IDEAL_RATIO_RATE);
else else
div[OUT] = clk_get_rate(clk) / outrate; rem[OUT] = do_div(clk_rate, outrate);
div[OUT] = clk_rate;
if (div[OUT] == 0) { /* Output divider has the same limitation as the input one */
if (div[OUT] == 0 || (!ideal && (div[OUT] > 1024 || rem[OUT] != 0))) {
pair_err("failed to support output sample rate %dHz by asrck_%x\n", pair_err("failed to support output sample rate %dHz by asrck_%x\n",
outrate, clk_index[OUT]); outrate, clk_index[OUT]);
return -EINVAL; return -EINVAL;
} }
div[OUT] = min_t(u32, 1024, div[OUT]);
/* Set the channel number */ /* Set the channel number */
channels = config->channel_num; channels = config->channel_num;
...@@ -560,7 +583,7 @@ static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream, ...@@ -560,7 +583,7 @@ static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream,
config.output_sample_rate = rate; config.output_sample_rate = rate;
} }
ret = fsl_asrc_config_pair(pair); ret = fsl_asrc_config_pair(pair, false);
if (ret) { if (ret) {
dev_err(dai->dev, "fail to config asrc pair\n"); dev_err(dai->dev, "fail to config asrc pair\n");
return ret; return ret;
......
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