From dcab368ab69a82521c7652f64a88550b828b69c9 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 17 Apr 2024 17:20:05 +0100 Subject: [PATCH] ASoC: bcm: Use power-of-2 bclk_ratios The soundcard drivers originally used snd_pcm_format_physical_width, but a later commit changed that to snd_pcm_format_width because the in-memory sample storage width should not be a factor in determining the bclk_ratio. However, the physical width rounds the sample bits up to the nearest power of 2, which makes it easier to find integer clock divisors. Restore the old behaviour, but with an implementation that makes it clear what is going on. See: https://github.com/raspberrypi/linux/issues/6104 Signed-off-by: Phil Elwell --- sound/soc/bcm/allo-boss-dac.c | 3 +++ sound/soc/bcm/dionaudio_loco.c | 3 +++ sound/soc/bcm/hifiberry_dacplus.c | 3 +++ sound/soc/bcm/hifiberry_dacplusadc.c | 3 +++ sound/soc/bcm/hifiberry_dacplusadcpro.c | 3 +++ sound/soc/bcm/i-sabre-q2m.c | 5 +++-- sound/soc/bcm/rpi-cirrus.c | 3 +++ sound/soc/bcm/rpi-simple-soundcard.c | 5 ++++- 8 files changed, 25 insertions(+), 3 deletions(-) diff --git a/sound/soc/bcm/allo-boss-dac.c b/sound/soc/bcm/allo-boss-dac.c index 1a1135130a7144..85fd5a2f9ae80f 100644 --- a/sound/soc/bcm/allo-boss-dac.c +++ b/sound/soc/bcm/allo-boss-dac.c @@ -278,6 +278,9 @@ static int snd_allo_boss_hw_params( struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; struct snd_soc_card *card = rtd->card; + /* Using powers of 2 allows for an integer clock divisor */ + width = width <= 16 ? 16 : 32; + /* Mute before changing sample rate */ snd_allo_boss_gpio_mute(card); diff --git a/sound/soc/bcm/dionaudio_loco.c b/sound/soc/bcm/dionaudio_loco.c index 55ea4f234decdc..03e3e6d185e4a0 100644 --- a/sound/soc/bcm/dionaudio_loco.c +++ b/sound/soc/bcm/dionaudio_loco.c @@ -34,6 +34,9 @@ static int snd_rpi_dionaudio_loco_hw_params( unsigned int sample_bits = snd_pcm_format_width(params_format(params)); + /* Using powers of 2 allows for an integer clock divisor */ + sample_bits = sample_bits <= 16 ? 16 : 32; + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2); } diff --git a/sound/soc/bcm/hifiberry_dacplus.c b/sound/soc/bcm/hifiberry_dacplus.c index 451c4d7ddf37cc..a6dacdced79a74 100644 --- a/sound/soc/bcm/hifiberry_dacplus.c +++ b/sound/soc/bcm/hifiberry_dacplus.c @@ -297,6 +297,9 @@ static int snd_rpi_hifiberry_dacplus_hw_params( int channels = params_channels(params); int width = snd_pcm_format_width(params_format(params)); + /* Using powers of 2 allows for an integer clock divisor */ + width = width <= 16 ? 16 : 32; + if (snd_rpi_hifiberry_is_dacpro) { struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; diff --git a/sound/soc/bcm/hifiberry_dacplusadc.c b/sound/soc/bcm/hifiberry_dacplusadc.c index 7e227ab57e5ebe..d21b006aad7d05 100644 --- a/sound/soc/bcm/hifiberry_dacplusadc.c +++ b/sound/soc/bcm/hifiberry_dacplusadc.c @@ -231,6 +231,9 @@ static int snd_rpi_hifiberry_dacplusadc_hw_params( int channels = params_channels(params); int width = snd_pcm_format_width(params_format(params)); + /* Using powers of 2 allows for an integer clock divisor */ + width = width <= 16 ? 16 : 32; + if (snd_rpi_hifiberry_is_dacpro) { struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component; diff --git a/sound/soc/bcm/hifiberry_dacplusadcpro.c b/sound/soc/bcm/hifiberry_dacplusadcpro.c index daeb83fe0d5cd3..9cb8692caf1a35 100644 --- a/sound/soc/bcm/hifiberry_dacplusadcpro.c +++ b/sound/soc/bcm/hifiberry_dacplusadcpro.c @@ -389,6 +389,9 @@ static int snd_rpi_hifiberry_dacplusadcpro_hw_params( struct snd_soc_dai_driver *drv = dai->driver; const struct snd_soc_dai_ops *ops = drv->ops; + /* Using powers of 2 allows for an integer clock divisor */ + width = width <= 16 ? 16 : 32; + if (snd_rpi_hifiberry_is_dacpro) { snd_rpi_hifiberry_dacplusadcpro_set_sclk(dac, params_rate(params)); diff --git a/sound/soc/bcm/i-sabre-q2m.c b/sound/soc/bcm/i-sabre-q2m.c index b674de1d3f06e2..d18a536f8fefb0 100644 --- a/sound/soc/bcm/i-sabre-q2m.c +++ b/sound/soc/bcm/i-sabre-q2m.c @@ -53,8 +53,9 @@ static int snd_rpi_i_sabre_q2m_hw_params( struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); int bclk_ratio; - bclk_ratio = snd_pcm_format_width( - params_format(params)) * params_channels(params); + /* Using powers of 2 allows for an integer clock divisor */ + bclk_ratio = (snd_pcm_format_width(params_format(params)) <= 16 ? 16 : 32) * + params_channels(params); return snd_soc_dai_set_bclk_ratio(cpu_dai, bclk_ratio); } diff --git a/sound/soc/bcm/rpi-cirrus.c b/sound/soc/bcm/rpi-cirrus.c index 38739b5ba0eb33..b04c1bf38f6bbf 100644 --- a/sound/soc/bcm/rpi-cirrus.c +++ b/sound/soc/bcm/rpi-cirrus.c @@ -708,6 +708,9 @@ static int rpi_cirrus_hw_params(struct snd_pcm_substream *substream, unsigned int rate = params_rate(params); unsigned int clk_freq = calc_sysclk(rate); + /* Using powers of 2 allows for an integer clock divisor */ + width = width <= 16 ? 16 : 32; + mutex_lock(&priv->lock); dev_dbg(card->dev, "hw_params: setting rate to %d\n", rate); diff --git a/sound/soc/bcm/rpi-simple-soundcard.c b/sound/soc/bcm/rpi-simple-soundcard.c index 40d4be42bc4b7b..3bf8653913f614 100644 --- a/sound/soc/bcm/rpi-simple-soundcard.c +++ b/sound/soc/bcm/rpi-simple-soundcard.c @@ -134,10 +134,13 @@ static int snd_rpi_simple_hw_params(struct snd_pcm_substream *substream, return 0; // BCLK is configured in .init /* The simple drivers just set the bclk_ratio to sample_bits * 2 so - * hard-code this for now. More complex drivers could just replace + * hard-code this for now, but sticking to powers of 2 to allow for + * integer clock divisors. More complex drivers could just replace * the hw_params routine. */ sample_bits = snd_pcm_format_width(params_format(params)); + sample_bits = sample_bits <= 16 ? 16 : 32; + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2); }