ALSA: hda/conexant: Fix pop noise on conexant codec
PATCH v2 1/1 ALSA: hda/conexant: Fix pop noise on conexant codec
From: wangdicheng <[email protected]>
Pop noise mitigation: When headphones are unplugged during playback,mute speaker DAC(0x17)immediately and restore after 20ms delay to avoid audible popping.
--- sound/hda/codecs/conexant.c | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+)
sound/hda/codecs/conexant.c +65 -0
@@ -43,6 +43,9 @@ struct conexant_spec {unsigned int gpio_mute_led_mask;unsigned int gpio_mic_led_mask;bool is_cx11880_sn6140; + +struct hda_codec *conexant_codec; +struct delayed_work change_pinctl_work; };
@@ -216,6 +219,63 @@ static void cx_remove(struct hda_codec *codec)snd_hda_gen_remove (codec); } +static void mute_unmute_speaker (struct hda_codec *codec,hda_nid_t nid,bool mute) +{ +unsigned int conn_sel, dac, conn_list, gain_left, gain_right; + + conn_sel =snd_hda_codec_read (codec, nid,0 ,0xf01 ,0x0 ); + conn_list =snd_hda_codec_read (codec, nid,0 ,0xf02 ,0x0 ); + + dac = ((conn_list >> (conn_sel *8 )) &0xff ); +if (dac ==0 ) +return ; + + gain_left =snd_hda_codec_read (codec, dac,0 ,0xba0 ,0x0 ); + gain_right =snd_hda_codec_read (codec, dac,0 ,0xb80 ,0x0 ); + +if (mute) { + gain_left |=0x80 ; + gain_right |=0x80 ; + }else { + gain_left &= (~(0x80 )); + gain_right &= (~(0x80 )); + } + +snd_hda_codec_write (codec, dac,0 ,0x3a0 , gain_left); +snd_hda_codec_write (codec, dac,0 ,0x390 , gain_right); + +if (mute) { +snd_hda_codec_write (codec, nid,0 ,0x707 ,0 ); +codec_dbg (codec,"mute_speaker, set 0x%x PinCtrl to 0.\n" , nid); + }else { +snd_hda_codec_write (codec, nid,0 ,0x707 ,0x40 ); +codec_dbg (codec,"unmute_speaker, set 0x%x PinCtrl to 0x40.\n" , nid); + } +} + +static void change_pinctl_worker (struct work_struct *work) +{ +struct hda_codec *codec; +struct conexant_spec *spec; + + spec = container_of(work, struct conexant_spec, change_pinctl_work.work); + codec = spec->conexant_codec; + +return mute_unmute_speaker (codec,0x17 , false); +} + +static void cx_auto_mute_unmute_speaker (struct hda_codec *codec,struct hda_jack_callback *event) +{ +struct conexant_spec *spec = codec->spec; +int phone_present; + + phone_present =snd_hda_codec_read (codec,0x16 ,0 ,0xf09 ,0x0 ); +if (!(phone_present &0x80000000 )) { +mute_unmute_speaker (codec,0x17 , true); +schedule_delayed_work (&spec->change_pinctl_work,20 ); + } +} +static void cx_process_headset_plugin (struct hda_codec *codec) {unsigned int val;
@@ -1178,6 +1238,10 @@ static int cx_probe(struct hda_codec *codec, const struct hda_device_id *id) spec =kzalloc (sizeof (*spec),GFP_KERNEL );if (!spec)return -ENOMEM ; + +INIT_DELAYED_WORK (&spec->change_pinctl_work, change_pinctl_worker); + spec->conexant_codec = codec; +snd_hda_gen_spec_init (&spec->gen); codec->spec = spec;
@@ -1187,6 +1251,7 @@ static int cx_probe(struct hda_codec *codec, const struct hda_device_id *id)case 0x14f11f87 : spec->is_cx11880_sn6140 = true;snd_hda_jack_detect_enable_callback (codec,0x19 , cx_update_headset_mic_vref); +snd_hda_jack_detect_enable_callback (codec,0x16 , cx_auto_mute_unmute_speaker);break ; }
Signature · 1 line
2.25.1
Quoted 5 lines · On 2025-11-05 3:25 AM, [email protected] wrote:
From: wangdicheng <[email protected]>
Pop noise mitigation: When headphones are unplugged during playback,mute speaker DAC(0x17)immediately and restore after 20ms delay to avoid audible popping.
What's the device ID for the "conexant codec"? I doubt all of them share the problem. Once obtained, mention the ID within the title.
Now, the problem you're aiming to address seem generic - addressing pop noises during plug/unplug is a common subject in sound/ domain. I'd check if sound/hda/codecs doesn't have something for such occasion already. Takashi is more fluent in sound/hda, perhaps he could help here.
If such code exists, you could get rid of ugly:
codec->spec <-> spec->codec
relation which you're adding with:
struct hda_codec *conexant_codec;
Quoted 11 lines
Signed-off-by wangdicheng <[email protected]>--- sound/hda/codecs/conexant.c | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+)sound/hda/codecs/conexant.c +0
-0
sound/hda/codecs/conexant.c struct conexant_spec { · line 48
Quoted 6 lines
sound/hda/codecs/conexant.c +3
-0@@ -43,6 +43,9 @@ struct conexant_spec {unsigned int gpio_mute_led_mask;unsigned int gpio_mic_led_mask;bool is_cx11880_sn6140; + +struct hda_codec *conexant_codec; +struct delayed_work change_pinctl_work;
Not fan of adding 'struct hda_codec *' as part of the spec. As stated above, I'd first check if something pop noise -related doesn't exist in sound/hda already. If it does, hook into it.
Quoted 40 lines
sound/hda/codecs/conexant.c +33
-0};@@ -216,6 +219,63 @@ static void cx_remove(struct hda_codec *codec)snd_hda_gen_remove (codec); } +static void mute_unmute_speaker (struct hda_codec *codec,hda_nid_t nid,bool mute) +{ +unsigned int conn_sel, dac, conn_list, gain_left, gain_right; + + conn_sel =snd_hda_codec_read (codec, nid,0 ,0xf01 ,0x0 ); + conn_list =snd_hda_codec_read (codec, nid,0 ,0xf02 ,0x0 ); + + dac = ((conn_list >> (conn_sel *8 )) &0xff ); +if (dac ==0 ) +return ; + + gain_left =snd_hda_codec_read (codec, dac,0 ,0xba0 ,0x0 ); + gain_right =snd_hda_codec_read (codec, dac,0 ,0xb80 ,0x0 ); + +if (mute) { + gain_left |=0x80 ; + gain_right |=0x80 ; + }else { + gain_left &= (~(0x80 )); + gain_right &= (~(0x80 )); + } + +snd_hda_codec_write (codec, dac,0 ,0x3a0 , gain_left); +snd_hda_codec_write (codec, dac,0 ,0x390 , gain_right); + +if (mute) { +snd_hda_codec_write (codec, nid,0 ,0x707 ,0 ); +codec_dbg (codec,"mute_speaker, set 0x%x PinCtrl to 0.\n" , nid); + }else { +snd_hda_codec_write (codec, nid,0 ,0x707 ,0x40 ); +codec_dbg (codec,"unmute_speaker, set 0x%x PinCtrl to 0x40.\n" , nid); + } +}
sound/hda/codecs/conexant.c static void cx_remove(struct hda_codec *codec) · line 264
Quoted 10 lines
sound/hda/codecs/conexant.c +10
-0+ +static void change_pinctl_worker (struct work_struct *work) +{ +struct hda_codec *codec; +struct conexant_spec *spec; + + spec = container_of(work, struct conexant_spec, change_pinctl_work.work); + codec = spec->conexant_codec; + +return mute_unmute_speaker (codec,0x17 , false);
return-statement in void? The code won't compile. Was the patch tested at all?
Quoted 1 line
sound/hda/codecs/conexant.c +1
-0+}