public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes
       [not found] <20240116093903.19403-1-johan+linaro@kernel.org>
@ 2024-01-16  9:38 ` Johan Hovold
  2024-01-16 11:11   ` Srinivas Kandagatla
  2024-01-16  9:38 ` [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack Johan Hovold
  1 sibling, 1 reply; 7+ messages in thread
From: Johan Hovold @ 2024-01-16  9:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Srinivas Kandagatla, Banajit Goswami, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-sound,
	linux-kernel, Johan Hovold, stable

The current UCM configuration sets the speaker PA volume to 15 dB when
enabling the speakers but this does not prevent the user from increasing
the volume further.

Limit the PA volume to 15 dB in the machine driver to reduce the risk of
speaker damage until we have active speaker protection in place.

Note that this will probably need to be generalised using
machine-specific limits, but a common limit should do for now.

Cc: stable@vger.kernel.org	# 6.5
Signed-off-by: Johan Hovold <johan+linaro@kernel.org>
---
 sound/soc/qcom/sc8280xp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/sound/soc/qcom/sc8280xp.c b/sound/soc/qcom/sc8280xp.c
index ed4bb551bfbb..aa43903421f5 100644
--- a/sound/soc/qcom/sc8280xp.c
+++ b/sound/soc/qcom/sc8280xp.c
@@ -32,12 +32,14 @@ static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
 	case WSA_CODEC_DMA_RX_0:
 	case WSA_CODEC_DMA_RX_1:
 		/*
-		 * set limit of 0dB on Digital Volume for Speakers,
-		 * this can prevent damage of speakers to some extent without
-		 * active speaker protection
+		 * Set limit of 0 dB on Digital Volume and 15 dB on PA Volume
+		 * to reduce the risk of speaker damage until we have active
+		 * speaker protection in place.
 		 */
 		snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 84);
 		snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 84);
+		snd_soc_limit_volume(card, "SpkrLeft PA Volume", 12);
+		snd_soc_limit_volume(card, "SpkrRight PA Volume", 12);
 		break;
 	default:
 		break;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack
       [not found] <20240116093903.19403-1-johan+linaro@kernel.org>
  2024-01-16  9:38 ` [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes Johan Hovold
@ 2024-01-16  9:38 ` Johan Hovold
  2024-01-16 11:10   ` Srinivas Kandagatla
  1 sibling, 1 reply; 7+ messages in thread
From: Johan Hovold @ 2024-01-16  9:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Srinivas Kandagatla, Banajit Goswami, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-sound,
	linux-kernel, Johan Hovold, stable

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(-)

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);
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack
  2024-01-16  9:38 ` [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack Johan Hovold
@ 2024-01-16 11:10   ` Srinivas Kandagatla
  2024-01-16 13:10     ` Johan Hovold
  0 siblings, 1 reply; 7+ messages in thread
From: Srinivas Kandagatla @ 2024-01-16 11:10 UTC (permalink / raw)
  To: Johan Hovold, Mark Brown
  Cc: Banajit Goswami, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	alsa-devel, linux-sound, linux-kernel, stable

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);

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes
  2024-01-16  9:38 ` [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes Johan Hovold
@ 2024-01-16 11:11   ` Srinivas Kandagatla
  2024-01-16 15:10     ` Mark Brown
  0 siblings, 1 reply; 7+ messages in thread
From: Srinivas Kandagatla @ 2024-01-16 11:11 UTC (permalink / raw)
  To: Johan Hovold, Mark Brown
  Cc: Banajit Goswami, Liam Girdwood, Jaroslav Kysela, Takashi Iwai,
	alsa-devel, linux-sound, linux-kernel, stable



On 16/01/2024 09:38, Johan Hovold wrote:
> The current UCM configuration sets the speaker PA volume to 15 dB when
> enabling the speakers but this does not prevent the user from increasing
> the volume further.
> 
> Limit the PA volume to 15 dB in the machine driver to reduce the risk of
> speaker damage until we have active speaker protection in place.
> 
> Note that this will probably need to be generalised using
> machine-specific limits, but a common limit should do for now.
> 
> Cc: stable@vger.kernel.org	# 6.5
> Signed-off-by: Johan Hovold <johan+linaro@kernel.org>

LGTM, We can get rid of this limit once we have Speaker protection inplace.


Reviewed-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

--srini

> ---
>   sound/soc/qcom/sc8280xp.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/sound/soc/qcom/sc8280xp.c b/sound/soc/qcom/sc8280xp.c
> index ed4bb551bfbb..aa43903421f5 100644
> --- a/sound/soc/qcom/sc8280xp.c
> +++ b/sound/soc/qcom/sc8280xp.c
> @@ -32,12 +32,14 @@ static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
>   	case WSA_CODEC_DMA_RX_0:
>   	case WSA_CODEC_DMA_RX_1:
>   		/*
> -		 * set limit of 0dB on Digital Volume for Speakers,
> -		 * this can prevent damage of speakers to some extent without
> -		 * active speaker protection
> +		 * Set limit of 0 dB on Digital Volume and 15 dB on PA Volume
> +		 * to reduce the risk of speaker damage until we have active
> +		 * speaker protection in place.
>   		 */
>   		snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 84);
>   		snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 84);
> +		snd_soc_limit_volume(card, "SpkrLeft PA Volume", 12);
> +		snd_soc_limit_volume(card, "SpkrRight PA Volume", 12);
>   		break;
>   	default:
>   		break;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack
  2024-01-16 11:10   ` Srinivas Kandagatla
@ 2024-01-16 13:10     ` Johan Hovold
  2024-01-17  9:08       ` Johan Hovold
  0 siblings, 1 reply; 7+ messages in thread
From: Johan Hovold @ 2024-01-16 13:10 UTC (permalink / raw)
  To: Srinivas Kandagatla
  Cc: Johan Hovold, Mark Brown, Banajit Goswami, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-sound,
	linux-kernel, stable

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes
  2024-01-16 11:11   ` Srinivas Kandagatla
@ 2024-01-16 15:10     ` Mark Brown
  0 siblings, 0 replies; 7+ messages in thread
From: Mark Brown @ 2024-01-16 15:10 UTC (permalink / raw)
  To: Srinivas Kandagatla
  Cc: Johan Hovold, Banajit Goswami, Liam Girdwood, Jaroslav Kysela,
	Takashi Iwai, alsa-devel, linux-sound, linux-kernel, stable

[-- Attachment #1: Type: text/plain, Size: 649 bytes --]

On Tue, Jan 16, 2024 at 11:11:47AM +0000, Srinivas Kandagatla wrote:
> On 16/01/2024 09:38, Johan Hovold wrote:
> > The current UCM configuration sets the speaker PA volume to 15 dB when
> > enabling the speakers but this does not prevent the user from increasing
> > the volume further.
> > 
> > Limit the PA volume to 15 dB in the machine driver to reduce the risk of
> > speaker damage until we have active speaker protection in place.

> LGTM, We can get rid of this limit once we have Speaker protection inplace.

There should be a userspace component for speaker protection so you'll
need to limit things when that's not running.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack
  2024-01-16 13:10     ` Johan Hovold
@ 2024-01-17  9:08       ` Johan Hovold
  0 siblings, 0 replies; 7+ messages in thread
From: Johan Hovold @ 2024-01-17  9:08 UTC (permalink / raw)
  To: Srinivas Kandagatla
  Cc: Johan Hovold, Mark Brown, Banajit Goswami, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-sound,
	linux-kernel, stable

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-01-17  9:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20240116093903.19403-1-johan+linaro@kernel.org>
2024-01-16  9:38 ` [PATCH 1/7] ASoC: qcom: sc8280xp: limit speaker volumes Johan Hovold
2024-01-16 11:11   ` Srinivas Kandagatla
2024-01-16 15:10     ` Mark Brown
2024-01-16  9:38 ` [PATCH 2/7] ASoC: codecs: lpass-wsa-macro: fix compander volume hack Johan Hovold
2024-01-16 11:10   ` Srinivas Kandagatla
2024-01-16 13:10     ` Johan Hovold
2024-01-17  9:08       ` Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox