diff mbox series

[2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack

Message ID 20240116093903.19403-3-johan+linaro@kernel.org (mailing list archive)
State Superseded
Headers show
Series ASoC: qcom: volume fixes and codec cleanups | expand

Commit Message

Johan Hovold Jan. 16, 2024, 9:38 a.m. UTC
The LPASS WSA macro codec driver is updating the digital gain settings
behind the back of user space on DAPM events if companding has been
enabled.

As compander control is exported to user space, this can result in the
digital gain setting being incremented (or decremented) every time the
sound server is started and the codec suspended depending on what the
UCM configuration looks like.

Soon enough playback will become distorted (or too quiet).

This is specifically a problem on the Lenovo ThinkPad X13s as this
bypasses the limit for the digital gain setting that has been set by the
machine driver.

Fix this by simply dropping the compander gain hack. If someone cares
about modelling the impact of the compander setting this can possibly be
done by exporting it as a volume control later.

Fixes: 2c4066e5d428 ("ASoC: codecs: lpass-wsa-macro: add dapm widgets and route")
Cc: stable@vger.kernel.org      # 5.11
Cc: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
---
 sound/soc/codecs/lpass-wsa-macro.c | 10 ----------
 1 file changed, 10 deletions(-)

Comments

Srinivas Kandagatla Jan. 16, 2024, 11:10 a.m. UTC | #1
Thanks Johan for this patch,

On 16/01/2024 09:38, Johan Hovold wrote:
> The LPASS WSA macro codec driver is updating the digital gain settings
> behind the back of user space on DAPM events if companding has been
> enabled.
> 
> As compander control is exported to user space, this can result in the
> digital gain setting being incremented (or decremented) every time the
> sound server is started and the codec suspended depending on what the
> UCM configuration looks like.
> 
> Soon enough playback will become distorted (or too quiet).
> 
> This is specifically a problem on the Lenovo ThinkPad X13s as this
> bypasses the limit for the digital gain setting that has been set by the
> machine driver.
> 
> Fix this by simply dropping the compander gain hack. If someone cares
> about modelling the impact of the compander setting this can possibly be
> done by exporting it as a volume control later.
> 
This is not a hack, wsa codec requires gain to be programmed after the 
clk is enabled for that particular interpolator.

However I agree with you on programming the gain that is different to 
what user set it.

This is because of unimplemented or half baked implementation of half-db 
feature of gain control in this codec.

We can clean that half baked implementation for now and still continue 
to program the gain values after the clks are enabled.

lets remove spkr_gain_offset and associated code paths in this codec, 
which should fix the issue that you have reported and still do the right 
thing of programming gain after clks are enabled.

thanks,
Srini


> Fixes: 2c4066e5d428 ("ASoC: codecs: lpass-wsa-macro: add dapm widgets and route")
> Cc: stable@vger.kernel.org      # 5.11
> Cc: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
> Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
> ---
>   sound/soc/codecs/lpass-wsa-macro.c | 10 ----------
>   1 file changed, 10 deletions(-)
> 
> diff --git a/sound/soc/codecs/lpass-wsa-macro.c b/sound/soc/codecs/lpass-wsa-macro.c
> index 7e21cec3c2fb..7de221464d47 100644
> --- a/sound/soc/codecs/lpass-wsa-macro.c
> +++ b/sound/soc/codecs/lpass-wsa-macro.c
> @@ -1583,8 +1583,6 @@ static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
>   	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
>   	u16 gain_reg;
>   	u16 reg;
> -	int val;
> -	int offset_val = 0;
>   	struct wsa_macro *wsa = snd_soc_component_get_drvdata(component);
>   
>   	if (w->shift == WSA_MACRO_COMP1) {
> @@ -1623,11 +1621,7 @@ static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
>   					CDC_WSA_RX1_RX_PATH_MIX_SEC0,
>   					CDC_WSA_RX_PGA_HALF_DB_MASK,
>   					CDC_WSA_RX_PGA_HALF_DB_ENABLE);
> -			offset_val = -2;
>   		}
> -		val = snd_soc_component_read(component, gain_reg);
> -		val += offset_val;
> -		snd_soc_component_write(component, gain_reg, val);
>   		wsa_macro_config_ear_spkr_gain(component, wsa,
>   						event, gain_reg);
>   		break;
> @@ -1654,10 +1648,6 @@ static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
>   					CDC_WSA_RX1_RX_PATH_MIX_SEC0,
>   					CDC_WSA_RX_PGA_HALF_DB_MASK,
>   					CDC_WSA_RX_PGA_HALF_DB_DISABLE);
> -			offset_val = 2;
> -			val = snd_soc_component_read(component, gain_reg);
> -			val += offset_val;
> -			snd_soc_component_write(component, gain_reg, val);
>   		}
>   		wsa_macro_config_ear_spkr_gain(component, wsa,
>   						event, gain_reg);
Johan Hovold Jan. 16, 2024, 1:10 p.m. UTC | #2
On Tue, Jan 16, 2024 at 11:10:21AM +0000, Srinivas Kandagatla wrote:
> Thanks Johan for this patch,
> 
> On 16/01/2024 09:38, Johan Hovold wrote:
> > The LPASS WSA macro codec driver is updating the digital gain settings
> > behind the back of user space on DAPM events if companding has been
> > enabled.
> > 
> > As compander control is exported to user space, this can result in the
> > digital gain setting being incremented (or decremented) every time the
> > sound server is started and the codec suspended depending on what the
> > UCM configuration looks like.
> > 
> > Soon enough playback will become distorted (or too quiet).
> > 
> > This is specifically a problem on the Lenovo ThinkPad X13s as this
> > bypasses the limit for the digital gain setting that has been set by the
> > machine driver.
> > 
> > Fix this by simply dropping the compander gain hack. If someone cares
> > about modelling the impact of the compander setting this can possibly be
> > done by exporting it as a volume control later.
> > 
> This is not a hack, wsa codec requires gain to be programmed after the 
> clk is enabled for that particular interpolator.

Ok, but then it's also broken as, as I mentioned off-list, these
registers are cached so unless companding is enabled, the write on
enable will have no effect.

> However I agree with you on programming the gain that is different to 
> what user set it.
> 
> This is because of unimplemented or half baked implementation of half-db 
> feature of gain control in this codec.
> 
> We can clean that half baked implementation for now and still continue 
> to program the gain values after the clks are enabled.
> 
> lets remove spkr_gain_offset and associated code paths in this codec, 
> which should fix the issue that you have reported and still do the right 
> thing of programming gain after clks are enabled.

Removing the offset which can alter the gain, will cause both of these
writes to become no-ops as the registers are cached (e.g. just as for
the follow-on codec cleanups). So then we might as well just remove
this too.

How is the half-dB feature supposed to work?

And are you sure that you need to reprogram the gain value after
enabling the clock? Everything seems to work without doing so.

Johan
Johan Hovold Jan. 17, 2024, 9:08 a.m. UTC | #3
On Tue, Jan 16, 2024 at 02:10:19PM +0100, Johan Hovold wrote:
> On Tue, Jan 16, 2024 at 11:10:21AM +0000, Srinivas Kandagatla wrote:

> > This is not a hack, wsa codec requires gain to be programmed after the 
> > clk is enabled for that particular interpolator.
> 
> Ok, but then it's also broken as, as I mentioned off-list, these
> registers are cached so unless companding is enabled, the write on
> enable will have no effect.

I was obviously confused here, and indeed tests reveal that those writes
are needed for any prior updates to take effect (e.g. volume changes
while the codec is runtime suspended).

I've just sent a v2 here:

	https://lore.kernel.org/lkml/20240117090331.31111-1-johan+linaro@kernel.org/

Johan
diff mbox series

Patch

diff --git a/sound/soc/codecs/lpass-wsa-macro.c b/sound/soc/codecs/lpass-wsa-macro.c
index 7e21cec3c2fb..7de221464d47 100644
--- a/sound/soc/codecs/lpass-wsa-macro.c
+++ b/sound/soc/codecs/lpass-wsa-macro.c
@@ -1583,8 +1583,6 @@  static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 	u16 gain_reg;
 	u16 reg;
-	int val;
-	int offset_val = 0;
 	struct wsa_macro *wsa = snd_soc_component_get_drvdata(component);
 
 	if (w->shift == WSA_MACRO_COMP1) {
@@ -1623,11 +1621,7 @@  static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
 					CDC_WSA_RX1_RX_PATH_MIX_SEC0,
 					CDC_WSA_RX_PGA_HALF_DB_MASK,
 					CDC_WSA_RX_PGA_HALF_DB_ENABLE);
-			offset_val = -2;
 		}
-		val = snd_soc_component_read(component, gain_reg);
-		val += offset_val;
-		snd_soc_component_write(component, gain_reg, val);
 		wsa_macro_config_ear_spkr_gain(component, wsa,
 						event, gain_reg);
 		break;
@@ -1654,10 +1648,6 @@  static int wsa_macro_enable_interpolator(struct snd_soc_dapm_widget *w,
 					CDC_WSA_RX1_RX_PATH_MIX_SEC0,
 					CDC_WSA_RX_PGA_HALF_DB_MASK,
 					CDC_WSA_RX_PGA_HALF_DB_DISABLE);
-			offset_val = 2;
-			val = snd_soc_component_read(component, gain_reg);
-			val += offset_val;
-			snd_soc_component_write(component, gain_reg, val);
 		}
 		wsa_macro_config_ear_spkr_gain(component, wsa,
 						event, gain_reg);