From f0a501b81926f3cd91415681feb22a3b8f6f0d83 Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.com>
Date: Fri, 1 Mar 2024 09:46:23 +0000
Subject: [PATCH 0932/1085] ASoC: bcm: Use the correct sample width value

ALSA's concept of the physical width of a sample is how much memory it
occupies, including any padding. This not the same as the count of bits
of actual sample content. In particular, S24_LE has a width of 24 bits
but a physical width of 32 bits because there is a byte of padding with
each sample.

When calculating bclk_ratio, etc., it is width that matters, not
physical width. Correct the error that has been replicated across the
drivers for many Raspberry Pi-compatible soundcards.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
---
 sound/soc/bcm/allo-boss-dac.c           | 2 +-
 sound/soc/bcm/dionaudio_loco.c          | 2 +-
 sound/soc/bcm/hifiberry_dacplus.c       | 2 +-
 sound/soc/bcm/hifiberry_dacplusadc.c    | 2 +-
 sound/soc/bcm/hifiberry_dacplusadcpro.c | 2 +-
 sound/soc/bcm/i-sabre-q2m.c             | 2 +-
 sound/soc/bcm/pifi-40.c                 | 2 --
 sound/soc/bcm/pisound.c                 | 2 +-
 sound/soc/bcm/rpi-cirrus.c              | 3 +--
 sound/soc/bcm/rpi-simple-soundcard.c    | 2 +-
 10 files changed, 9 insertions(+), 12 deletions(-)

--- a/sound/soc/bcm/allo-boss-dac.c
+++ b/sound/soc/bcm/allo-boss-dac.c
@@ -274,7 +274,7 @@ static int snd_allo_boss_hw_params(
 	int ret = 0;
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	int channels = params_channels(params);
-	int width = snd_pcm_format_physical_width(params_format(params));
+	int width = snd_pcm_format_width(params_format(params));
 
 	if (snd_soc_allo_boss_master) {
 		struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
--- a/sound/soc/bcm/dionaudio_loco.c
+++ b/sound/soc/bcm/dionaudio_loco.c
@@ -32,7 +32,7 @@ static int snd_rpi_dionaudio_loco_hw_par
 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
 
 	unsigned int sample_bits =
-		snd_pcm_format_physical_width(params_format(params));
+		snd_pcm_format_width(params_format(params));
 
 	return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
 }
--- a/sound/soc/bcm/hifiberry_dacplus.c
+++ b/sound/soc/bcm/hifiberry_dacplus.c
@@ -295,7 +295,7 @@ static int snd_rpi_hifiberry_dacplus_hw_
 	int ret = 0;
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	int channels = params_channels(params);
-	int width = snd_pcm_format_physical_width(params_format(params));
+	int width = snd_pcm_format_width(params_format(params));
 
 	if (snd_rpi_hifiberry_is_dacpro) {
 		struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
--- a/sound/soc/bcm/hifiberry_dacplusadc.c
+++ b/sound/soc/bcm/hifiberry_dacplusadc.c
@@ -234,7 +234,7 @@ static int snd_rpi_hifiberry_dacplusadc_
 	if (snd_rpi_hifiberry_is_dacpro) {
 		struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
 
-		width = snd_pcm_format_physical_width(params_format(params));
+		width =  snd_pcm_format_width(params_format(params));
 
 		snd_rpi_hifiberry_dacplusadc_set_sclk(component,
 			params_rate(params));
--- a/sound/soc/bcm/hifiberry_dacplusadcpro.c
+++ b/sound/soc/bcm/hifiberry_dacplusadcpro.c
@@ -383,7 +383,7 @@ static int snd_rpi_hifiberry_dacplusadcp
 	int ret = 0;
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	int channels = params_channels(params);
-	int width = snd_pcm_format_physical_width(params_format(params));
+	int width =  snd_pcm_format_width(params_format(params));
 	struct snd_soc_component *dac = asoc_rtd_to_codec(rtd, 0)->component;
 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
 	struct snd_soc_dai_driver *drv = dai->driver;
--- a/sound/soc/bcm/i-sabre-q2m.c
+++ b/sound/soc/bcm/i-sabre-q2m.c
@@ -53,7 +53,7 @@ static int snd_rpi_i_sabre_q2m_hw_params
 	struct snd_soc_dai         *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
 	int bclk_ratio;
 
-	bclk_ratio = snd_pcm_format_physical_width(
+	bclk_ratio = snd_pcm_format_width(
 			params_format(params)) * params_channels(params);
 	return snd_soc_dai_set_bclk_ratio(cpu_dai, bclk_ratio);
 }
--- a/sound/soc/bcm/pifi-40.c
+++ b/sound/soc/bcm/pifi-40.c
@@ -140,9 +140,7 @@ static int snd_pifi_40_hw_params(struct
 {
 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
-	unsigned int sample_bits;
 
-	sample_bits = snd_pcm_format_physical_width(params_format(params));
 	return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
 }
 
--- a/sound/soc/bcm/pisound.c
+++ b/sound/soc/bcm/pisound.c
@@ -895,7 +895,7 @@ static int pisnd_hw_params(
 	printd("rate   = %d\n", params_rate(params));
 	printd("ch     = %d\n", params_channels(params));
 	printd("bits   = %u\n",
-		snd_pcm_format_physical_width(params_format(params)));
+		snd_pcm_format_width(params_format(params)));
 	printd("format = %d\n", params_format(params));
 
 	gpiod_set_value(reset, false);
--- a/sound/soc/bcm/rpi-cirrus.c
+++ b/sound/soc/bcm/rpi-cirrus.c
@@ -704,8 +704,7 @@ static int rpi_cirrus_hw_params(struct s
 
 	int ret;
 
-	unsigned int width = snd_pcm_format_physical_width(
-		params_format(params));
+	unsigned int width = snd_pcm_format_width(params_format(params));
 	unsigned int rate = params_rate(params);
 	unsigned int clk_freq = calc_sysclk(rate);
 
--- a/sound/soc/bcm/rpi-simple-soundcard.c
+++ b/sound/soc/bcm/rpi-simple-soundcard.c
@@ -137,7 +137,7 @@ static int snd_rpi_simple_hw_params(stru
 	 * hard-code this for now. More complex drivers could just replace
 	 * the hw_params routine.
 	 */
-	sample_bits = snd_pcm_format_physical_width(params_format(params));
+	sample_bits = snd_pcm_format_width(params_format(params));
 	return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
 }