Commit bed41de0 authored by Takashi Iwai's avatar Takashi Iwai Committed by Mark Brown

ASoC: nau8825: Implement hw constraint for rates

nau8825 driver restricts the sample rate with over sampling rate, but
currently it barely bails out at hw_params with -EINVAL error (with a
kernel message); this doesn't help for user-space to recognize which
rate can be actually used.

This patch introduces the proper hw constraint for adjusting the
available range of the sample rate depending on the OSR setup, as well
as some code cleanup, for improving the communication with
user-space.  Now applications can know the valid rate beforehand and
reduces the rate appropriately without errors.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20220823081000.2965-5-tiwai@suse.deSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 92283c86
...@@ -1247,27 +1247,42 @@ static const struct snd_soc_dapm_route nau8825_dapm_routes[] = { ...@@ -1247,27 +1247,42 @@ static const struct snd_soc_dapm_route nau8825_dapm_routes[] = {
{"HPOR", NULL, "Class G"}, {"HPOR", NULL, "Class G"},
}; };
static int nau8825_clock_check(struct nau8825 *nau8825, static const struct nau8825_osr_attr *
int stream, int rate, int osr) nau8825_get_osr(struct nau8825 *nau8825, int stream)
{ {
int osrate; unsigned int osr;
if (stream == SNDRV_PCM_STREAM_PLAYBACK) { if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
regmap_read(nau8825->regmap,
NAU8825_REG_DAC_CTRL1, &osr);
osr &= NAU8825_DAC_OVERSAMPLE_MASK;
if (osr >= ARRAY_SIZE(osr_dac_sel)) if (osr >= ARRAY_SIZE(osr_dac_sel))
return -EINVAL; return NULL;
osrate = osr_dac_sel[osr].osr; return &osr_dac_sel[osr];
} else { } else {
regmap_read(nau8825->regmap,
NAU8825_REG_ADC_RATE, &osr);
osr &= NAU8825_ADC_SYNC_DOWN_MASK;
if (osr >= ARRAY_SIZE(osr_adc_sel)) if (osr >= ARRAY_SIZE(osr_adc_sel))
return -EINVAL; return NULL;
osrate = osr_adc_sel[osr].osr; return &osr_adc_sel[osr];
} }
}
if (!osrate || rate * osr > CLK_DA_AD_MAX) { static int nau8825_dai_startup(struct snd_pcm_substream *substream,
dev_err(nau8825->dev, "exceed the maximum frequency of CLK_ADC or CLK_DAC\n"); struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
const struct nau8825_osr_attr *osr;
osr = nau8825_get_osr(nau8825, substream->stream);
if (!osr || !osr->osr)
return -EINVAL; return -EINVAL;
}
return 0; return snd_pcm_hw_constraint_minmax(substream->runtime,
SNDRV_PCM_HW_PARAM_RATE,
0, CLK_DA_AD_MAX / osr->osr);
} }
static int nau8825_hw_params(struct snd_pcm_substream *substream, static int nau8825_hw_params(struct snd_pcm_substream *substream,
...@@ -1276,7 +1291,9 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream, ...@@ -1276,7 +1291,9 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream,
{ {
struct snd_soc_component *component = dai->component; struct snd_soc_component *component = dai->component;
struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component); struct nau8825 *nau8825 = snd_soc_component_get_drvdata(component);
unsigned int val_len = 0, osr, ctrl_val, bclk_fs, bclk_div; unsigned int val_len = 0, ctrl_val, bclk_fs, bclk_div;
const struct nau8825_osr_attr *osr;
int err = -EINVAL;
nau8825_sema_acquire(nau8825, 3 * HZ); nau8825_sema_acquire(nau8825, 3 * HZ);
...@@ -1286,29 +1303,19 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream, ...@@ -1286,29 +1303,19 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream,
* values must be selected such that the maximum frequency is less * values must be selected such that the maximum frequency is less
* than 6.144 MHz. * than 6.144 MHz.
*/ */
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { osr = nau8825_get_osr(nau8825, substream->stream);
regmap_read(nau8825->regmap, NAU8825_REG_DAC_CTRL1, &osr); if (!osr || !osr->osr)
osr &= NAU8825_DAC_OVERSAMPLE_MASK; goto error;
if (nau8825_clock_check(nau8825, substream->stream, if (params_rate(params) * osr->osr > CLK_DA_AD_MAX)
params_rate(params), osr)) { goto error;
nau8825_sema_release(nau8825); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
return -EINVAL;
}
regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER, regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
NAU8825_CLK_DAC_SRC_MASK, NAU8825_CLK_DAC_SRC_MASK,
osr_dac_sel[osr].clk_src << NAU8825_CLK_DAC_SRC_SFT); osr->clk_src << NAU8825_CLK_DAC_SRC_SFT);
} else { else
regmap_read(nau8825->regmap, NAU8825_REG_ADC_RATE, &osr);
osr &= NAU8825_ADC_SYNC_DOWN_MASK;
if (nau8825_clock_check(nau8825, substream->stream,
params_rate(params), osr)) {
nau8825_sema_release(nau8825);
return -EINVAL;
}
regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER, regmap_update_bits(nau8825->regmap, NAU8825_REG_CLK_DIVIDER,
NAU8825_CLK_ADC_SRC_MASK, NAU8825_CLK_ADC_SRC_MASK,
osr_adc_sel[osr].clk_src << NAU8825_CLK_ADC_SRC_SFT); osr->clk_src << NAU8825_CLK_ADC_SRC_SFT);
}
/* make BCLK and LRC divde configuration if the codec as master. */ /* make BCLK and LRC divde configuration if the codec as master. */
regmap_read(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, &ctrl_val); regmap_read(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, &ctrl_val);
...@@ -1321,10 +1328,8 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream, ...@@ -1321,10 +1328,8 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream,
bclk_div = 1; bclk_div = 1;
else if (bclk_fs <= 128) else if (bclk_fs <= 128)
bclk_div = 0; bclk_div = 0;
else { else
nau8825_sema_release(nau8825); goto error;
return -EINVAL;
}
regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2, regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL2,
NAU8825_I2S_LRC_DIV_MASK | NAU8825_I2S_BLK_DIV_MASK, NAU8825_I2S_LRC_DIV_MASK | NAU8825_I2S_BLK_DIV_MASK,
((bclk_div + 1) << NAU8825_I2S_LRC_DIV_SFT) | bclk_div); ((bclk_div + 1) << NAU8825_I2S_LRC_DIV_SFT) | bclk_div);
...@@ -1344,17 +1349,18 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream, ...@@ -1344,17 +1349,18 @@ static int nau8825_hw_params(struct snd_pcm_substream *substream,
val_len |= NAU8825_I2S_DL_32; val_len |= NAU8825_I2S_DL_32;
break; break;
default: default:
nau8825_sema_release(nau8825); goto error;
return -EINVAL;
} }
regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1, regmap_update_bits(nau8825->regmap, NAU8825_REG_I2S_PCM_CTRL1,
NAU8825_I2S_DL_MASK, val_len); NAU8825_I2S_DL_MASK, val_len);
err = 0;
error:
/* Release the semaphore. */ /* Release the semaphore. */
nau8825_sema_release(nau8825); nau8825_sema_release(nau8825);
return 0; return err;
} }
static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
...@@ -1420,6 +1426,7 @@ static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) ...@@ -1420,6 +1426,7 @@ static int nau8825_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
} }
static const struct snd_soc_dai_ops nau8825_dai_ops = { static const struct snd_soc_dai_ops nau8825_dai_ops = {
.startup = nau8825_dai_startup,
.hw_params = nau8825_hw_params, .hw_params = nau8825_hw_params,
.set_fmt = nau8825_set_dai_fmt, .set_fmt = nau8825_set_dai_fmt,
}; };
......
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