diff mbox series

[3/4] ASoC: codecs: wsa883x: mute/unmute PA in correct sequence

Message ID 20230323164403.6654-4-srinivas.kandagatla@linaro.org
State New
Headers show
Series ASoC: qcom: fixes for Click/Pop Noise | expand

Commit Message

Srinivas Kandagatla March 23, 2023, 4:44 p.m. UTC
In the current setup the PA is left unmuted even when the
Soundwire ports are not started streaming. This can lead to click
and pop sounds during start.
There is a same issue in the reverse order where in the PA is
left unmute even after the data stream is stopped, the time
between data stream stopping and port closing is long enough
to accumulate DC on the line resulting in Click/Pop noise
during end of stream.

Moving the mute/unmute to trigger stop/start respectively seems to
help a lot with this Click/Pop issues reported on this Codec.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 sound/soc/codecs/wsa883x.c | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

Comments

Mark Brown March 23, 2023, 5:07 p.m. UTC | #1
On Thu, Mar 23, 2023 at 04:44:02PM +0000, Srinivas Kandagatla wrote:
> In the current setup the PA is left unmuted even when the
> Soundwire ports are not started streaming. This can lead to click
> and pop sounds during start.
> There is a same issue in the reverse order where in the PA is
> left unmute even after the data stream is stopped, the time
> between data stream stopping and port closing is long enough
> to accumulate DC on the line resulting in Click/Pop noise
> during end of stream.

Wow, that hardware sounds *super* fragile.

> Moving the mute/unmute to trigger stop/start respectively seems to
> help a lot with this Click/Pop issues reported on this Codec.

> +static int wsa883x_trigger(struct snd_pcm_substream *s, int cmd,
> +			   struct snd_soc_dai *dai)
> +{
> +	switch (cmd) {
> +	case SNDRV_PCM_TRIGGER_START:
> +	case SNDRV_PCM_TRIGGER_RESUME:
> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> +		wsa883x_digital_mute(dai, false, 0);
> +		break;

>  static const struct snd_soc_dai_ops wsa883x_dai_ops = {
> +	.startup = wsa883x_startup,
>  	.hw_params = wsa883x_hw_params,
>  	.hw_free = wsa883x_hw_free,
> -	.mute_stream = wsa883x_digital_mute,
> +	.trigger = wsa883x_trigger,

The trigger is run in atomic context, can you really write safely to a
SoundWire device there?

This feels like we should be doing it at the framework level, either
tightening up where the mute happens in general or having some option
that devices can select if they really need it.
Pierre-Louis Bossart March 23, 2023, 6:11 p.m. UTC | #2
>> +static int wsa883x_trigger(struct snd_pcm_substream *s, int cmd,
>> +			   struct snd_soc_dai *dai)
>> +{
>> +	switch (cmd) {
>> +	case SNDRV_PCM_TRIGGER_START:
>> +	case SNDRV_PCM_TRIGGER_RESUME:
>> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>> +		wsa883x_digital_mute(dai, false, 0);
>> +		break;
> 
>>  static const struct snd_soc_dai_ops wsa883x_dai_ops = {
>> +	.startup = wsa883x_startup,
>>  	.hw_params = wsa883x_hw_params,
>>  	.hw_free = wsa883x_hw_free,
>> -	.mute_stream = wsa883x_digital_mute,
>> +	.trigger = wsa883x_trigger,
> 
> The trigger is run in atomic context, can you really write safely to a
> SoundWire device there?

Mark, I've seen that comment from you several times, and I wonder if I
am missing something: the triggers for SoundWire managers and dailinks
are typically nonatomic - at least for the Cadence-based solution the
trigger is based on a bank switch that may happen with a delay and with
a wait_for_completion(). Sending a command over the SoundWire channel is
also typically not atomic, there's usually a wait_for_completion() as well.
Mark Brown March 24, 2023, 12:14 a.m. UTC | #3
On Thu, Mar 23, 2023 at 01:11:11PM -0500, Pierre-Louis Bossart wrote:

> > The trigger is run in atomic context, can you really write safely to a
> > SoundWire device there?

> Mark, I've seen that comment from you several times, and I wonder if I
> am missing something: the triggers for SoundWire managers and dailinks
> are typically nonatomic - at least for the Cadence-based solution the
> trigger is based on a bank switch that may happen with a delay and with
> a wait_for_completion(). Sending a command over the SoundWire channel is
> also typically not atomic, there's usually a wait_for_completion() as well.

Ah, you're setting the nonatomic flag on your links to disable the
locking.  The default for trigger operations is to run them with local
interrupts disabled.  It looks like at least some of the Qualcomm stuff
does that too.
Srinivas Kandagatla March 24, 2023, 6:43 a.m. UTC | #4
On 24/03/2023 00:14, Mark Brown wrote:
> On Thu, Mar 23, 2023 at 01:11:11PM -0500, Pierre-Louis Bossart wrote:
> 
>>> The trigger is run in atomic context, can you really write safely to a
>>> SoundWire device there?
> 
>> Mark, I've seen that comment from you several times, and I wonder if I
>> am missing something: the triggers for SoundWire managers and dailinks
>> are typically nonatomic - at least for the Cadence-based solution the
>> trigger is based on a bank switch that may happen with a delay and with
>> a wait_for_completion(). Sending a command over the SoundWire channel is
>> also typically not atomic, there's usually a wait_for_completion() as well.
> 
> Ah, you're setting the nonatomic flag on your links to disable the
> locking.  The default for trigger operations is to run them with local
> interrupts disabled.  It looks like at least some of the Qualcomm stuff
> does that too.
Yes, by default dailinks are marked as nonatomic in Qualcomm case aswell.

--srini
Srinivas Kandagatla March 24, 2023, 6:44 a.m. UTC | #5
On 23/03/2023 17:07, Mark Brown wrote:
> On Thu, Mar 23, 2023 at 04:44:02PM +0000, Srinivas Kandagatla wrote:
>> In the current setup the PA is left unmuted even when the
>> Soundwire ports are not started streaming. This can lead to click
>> and pop sounds during start.
>> There is a same issue in the reverse order where in the PA is
>> left unmute even after the data stream is stopped, the time
>> between data stream stopping and port closing is long enough
>> to accumulate DC on the line resulting in Click/Pop noise
>> during end of stream.
> 
> Wow, that hardware sounds *super* fragile.
> 
>> Moving the mute/unmute to trigger stop/start respectively seems to
>> help a lot with this Click/Pop issues reported on this Codec.
> 
>> +static int wsa883x_trigger(struct snd_pcm_substream *s, int cmd,
>> +			   struct snd_soc_dai *dai)
>> +{
>> +	switch (cmd) {
>> +	case SNDRV_PCM_TRIGGER_START:
>> +	case SNDRV_PCM_TRIGGER_RESUME:
>> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>> +		wsa883x_digital_mute(dai, false, 0);
>> +		break;
> 
>>   static const struct snd_soc_dai_ops wsa883x_dai_ops = {
>> +	.startup = wsa883x_startup,
>>   	.hw_params = wsa883x_hw_params,
>>   	.hw_free = wsa883x_hw_free,
>> -	.mute_stream = wsa883x_digital_mute,
>> +	.trigger = wsa883x_trigger,
> 
> The trigger is run in atomic context, can you really write safely to a
> SoundWire device there?
> 
> This feels like we should be doing it at the framework level, either
> tightening up where the mute happens in general or having some option
> that devices can select if they really need it.
That makes more sense, I can give that a try.

--srini
Johan Hovold Oct. 25, 2023, 7:57 a.m. UTC | #6
Hi Mark,

On Fri, Mar 24, 2023 at 06:44:40AM +0000, Srinivas Kandagatla wrote:
> On 23/03/2023 17:07, Mark Brown wrote:
> > On Thu, Mar 23, 2023 at 04:44:02PM +0000, Srinivas Kandagatla wrote:
> >> In the current setup the PA is left unmuted even when the
> >> Soundwire ports are not started streaming. This can lead to click
> >> and pop sounds during start.
> >> There is a same issue in the reverse order where in the PA is
> >> left unmute even after the data stream is stopped, the time
> >> between data stream stopping and port closing is long enough
> >> to accumulate DC on the line resulting in Click/Pop noise
> >> during end of stream.
> > 
> > Wow, that hardware sounds *super* fragile.
> > 
> >> Moving the mute/unmute to trigger stop/start respectively seems to
> >> help a lot with this Click/Pop issues reported on this Codec.
> > 
> >> +static int wsa883x_trigger(struct snd_pcm_substream *s, int cmd,
> >> +			   struct snd_soc_dai *dai)
> >> +{
> >> +	switch (cmd) {
> >> +	case SNDRV_PCM_TRIGGER_START:
> >> +	case SNDRV_PCM_TRIGGER_RESUME:
> >> +	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
> >> +		wsa883x_digital_mute(dai, false, 0);
> >> +		break;
> > 
> >>   static const struct snd_soc_dai_ops wsa883x_dai_ops = {
> >> +	.startup = wsa883x_startup,
> >>   	.hw_params = wsa883x_hw_params,
> >>   	.hw_free = wsa883x_hw_free,
> >> -	.mute_stream = wsa883x_digital_mute,
> >> +	.trigger = wsa883x_trigger,

> > This feels like we should be doing it at the framework level, either
> > tightening up where the mute happens in general or having some option
> > that devices can select if they really need it.

> That makes more sense, I can give that a try.

I understand Srini has looked at this but has not yet been able to come
up with a generic implementation. Would it be possible to merge the two
codec fixes as an interim workaround for 6.7?

Without the wsa883x patch there's a loud crackling scary noise when
starting a stream on the Lenovo ThinkPad X13s which users will hit now
that they can run mainline on this machine.

	https://lore.kernel.org/lkml/20230323164403.6654-4-srinivas.kandagatla@linaro.org/

I've been using this one myself for the past seven months without any
issues (even if there's still a faint click when stopping a stream):

Tested-by: Johan Hovold <johan+linaro@kernel.org>

Getting this backported at least to 6.5 where sound support for the X13s
was added would be great too.

Johan
Mark Brown Oct. 25, 2023, 12:36 p.m. UTC | #7
On Wed, Oct 25, 2023 at 09:57:12AM +0200, Johan Hovold wrote:

> I understand Srini has looked at this but has not yet been able to come
> up with a generic implementation. Would it be possible to merge the two
> codec fixes as an interim workaround for 6.7?

You're talking about two fixes here but this is a 4 patch series...
Johan Hovold Oct. 25, 2023, 12:43 p.m. UTC | #8
On Wed, Oct 25, 2023 at 01:36:14PM +0100, Mark Brown wrote:
> On Wed, Oct 25, 2023 at 09:57:12AM +0200, Johan Hovold wrote:
> 
> > I understand Srini has looked at this but has not yet been able to come
> > up with a generic implementation. Would it be possible to merge the two
> > codec fixes as an interim workaround for 6.7?
> 
> You're talking about two fixes here but this is a 4 patch series...

Yes, sorry, I should have been more clear. I was talking about the codec
fixes so that's patch 3 and 4.

I believe the first two have already been applied.

Johan
Mark Brown Oct. 26, 2023, 1:05 p.m. UTC | #9
On Wed, Oct 25, 2023 at 02:43:28PM +0200, Johan Hovold wrote:
> On Wed, Oct 25, 2023 at 01:36:14PM +0100, Mark Brown wrote:
> > On Wed, Oct 25, 2023 at 09:57:12AM +0200, Johan Hovold wrote:

> > > I understand Srini has looked at this but has not yet been able to come
> > > up with a generic implementation. Would it be possible to merge the two
> > > codec fixes as an interim workaround for 6.7?

> > You're talking about two fixes here but this is a 4 patch series...

> Yes, sorry, I should have been more clear. I was talking about the codec
> fixes so that's patch 3 and 4.

> I believe the first two have already been applied.

So, having gone and fished the series out of lore to look at what the
patches are I'm pretty surprised here, it's been about six months since
the original discussion and I'd not have expected this to be such a
difficult thing to do, or at least that any issues would have been
flagged up by now.  What are the issues that have been encountered here?
Johan Hovold Oct. 26, 2023, 1:55 p.m. UTC | #10
On Thu, Oct 26, 2023 at 02:05:25PM +0100, Mark Brown wrote:
> On Wed, Oct 25, 2023 at 02:43:28PM +0200, Johan Hovold wrote:
> > On Wed, Oct 25, 2023 at 01:36:14PM +0100, Mark Brown wrote:
> > > On Wed, Oct 25, 2023 at 09:57:12AM +0200, Johan Hovold wrote:
> 
> > > > I understand Srini has looked at this but has not yet been able to come
> > > > up with a generic implementation. Would it be possible to merge the two
> > > > codec fixes as an interim workaround for 6.7?

> So, having gone and fished the series out of lore to look at what the
> patches are I'm pretty surprised here, it's been about six months since
> the original discussion and I'd not have expected this to be such a
> difficult thing to do, or at least that any issues would have been
> flagged up by now.  What are the issues that have been encountered here?

I'm also surprised that this has not yet been resolved, especially given
that I've been reminding Srini about the need to get this fixed
repeatedly for the past six months.

I haven't looked at this myself yet and I don't know what the technical
issues he hinted about having run into would be.

Srini, can you provide some details?

Johan
Srinivas Kandagatla Oct. 27, 2023, 10:51 a.m. UTC | #11
On 26/10/2023 14:05, Mark Brown wrote:
> On Wed, Oct 25, 2023 at 02:43:28PM +0200, Johan Hovold wrote:
>> On Wed, Oct 25, 2023 at 01:36:14PM +0100, Mark Brown wrote:
>>> On Wed, Oct 25, 2023 at 09:57:12AM +0200, Johan Hovold wrote:
> 
>>>> I understand Srini has looked at this but has not yet been able to come
>>>> up with a generic implementation. Would it be possible to merge the two
>>>> codec fixes as an interim workaround for 6.7?
> 
>>> You're talking about two fixes here but this is a 4 patch series...
> 
>> Yes, sorry, I should have been more clear. I was talking about the codec
>> fixes so that's patch 3 and 4.
> 
>> I believe the first two have already been applied.
> 
> So, having gone and fished the series out of lore to look at what the
> patches are I'm pretty surprised here, it's been about six months since
> the original discussion and I'd not have expected this to be such a
> difficult thing to do, or at least that any issues would have been
> flagged up by now.  What are the issues that have been encountered here?

The approach I tried was to reuse the existing 
snd_soc_dai_digital_mute() from generic snd_soc_pcm_dai_trigger()

The issue is that prepare also unmutes the stream which is why moving 
this to generic code is not helping in this setup.

To overcome this I have added a flag in snd_soc_dai_ops which seems to 
have helped

Let me send that patch as RFC for more discussion.

thanks,
Srini
diff mbox series

Patch

diff --git a/sound/soc/codecs/wsa883x.c b/sound/soc/codecs/wsa883x.c
index c609cb63dae6..b83b5b0d4bab 100644
--- a/sound/soc/codecs/wsa883x.c
+++ b/sound/soc/codecs/wsa883x.c
@@ -1204,9 +1204,6 @@  static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
 			break;
 		}
 
-		snd_soc_component_write_field(component, WSA883X_DRE_CTL_1,
-					      WSA883X_DRE_GAIN_EN_MASK,
-					      WSA883X_DRE_GAIN_FROM_CSR);
 		if (wsa883x->port_enable[WSA883X_PORT_COMP])
 			snd_soc_component_write_field(component, WSA883X_DRE_CTL_0,
 						      WSA883X_DRE_OFFSET_MASK,
@@ -1219,9 +1216,6 @@  static int wsa883x_spkr_event(struct snd_soc_dapm_widget *w,
 		snd_soc_component_write_field(component, WSA883X_PDM_WD_CTL,
 					      WSA883X_PDM_EN_MASK,
 					      WSA883X_PDM_ENABLE);
-		snd_soc_component_write_field(component, WSA883X_PA_FSM_CTL,
-					      WSA883X_GLOBAL_PA_EN_MASK,
-					      WSA883X_GLOBAL_PA_ENABLE);
 
 		break;
 	case SND_SOC_DAPM_PRE_PMD:
@@ -1341,10 +1335,38 @@  static int wsa883x_digital_mute(struct snd_soc_dai *dai, int mute, int stream)
 	return 0;
 }
 
+static int wsa883x_trigger(struct snd_pcm_substream *s, int cmd,
+			   struct snd_soc_dai *dai)
+{
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_RESUME:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+		wsa883x_digital_mute(dai, false, 0);
+		break;
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		wsa883x_digital_mute(dai, true, 0);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static int wsa883x_startup(struct snd_pcm_substream *stream,
+			   struct snd_soc_dai *dai)
+{
+	return wsa883x_digital_mute(dai, true, 0);
+}
+
 static const struct snd_soc_dai_ops wsa883x_dai_ops = {
+	.startup = wsa883x_startup,
 	.hw_params = wsa883x_hw_params,
 	.hw_free = wsa883x_hw_free,
-	.mute_stream = wsa883x_digital_mute,
+	.trigger = wsa883x_trigger,
 	.set_stream = wsa883x_set_sdw_stream,
 };