[PATCH v2] ALSA: hda/conexant: Refactor headset plug detection

songxiebing posted 1 patch 1 month ago
sound/hda/codecs/conexant.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
[PATCH v2] ALSA: hda/conexant: Refactor headset plug detection
Posted by songxiebing 1 month ago
From: Bob Song <songxiebing@kylinos.cn>

Rename cx_process_headset_plugin() to cx_process_headset_detect_plug_type()
to better describe what it does, and merge the duplicated CTIA/OMTP
branches since both select the same pin-widget control value for the mic
pin.

Move the AC_VERB_SET_PIN_WIDGET_CONTROL write into the caller
cx_update_headset_mic_vref() so that the mic pin control is applied
unconditionally while a mic is present, instead of only for CTIA/OMTP
headsets.

Signed-off-by: Bob Song <songxiebing@kylinos.cn>
---
changes in v2:
  - Optimize code logic and description
---
 sound/hda/codecs/conexant.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/sound/hda/codecs/conexant.c b/sound/hda/codecs/conexant.c
index 40da2832ba66..6ad44cd9287c 100644
--- a/sound/hda/codecs/conexant.c
+++ b/sound/hda/codecs/conexant.c
@@ -207,7 +207,7 @@ static void cx_remove(struct hda_codec *codec)
 	snd_hda_gen_remove(codec);
 }
 
-static void cx_process_headset_plugin(struct hda_codec *codec)
+static void cx_process_headset_detect_plug_type(struct hda_codec *codec)
 {
 	unsigned int val;
 	unsigned int count = 0;
@@ -223,14 +223,11 @@ static void cx_process_headset_plugin(struct hda_codec *codec)
 		count++;
 	} while (count < 3);
 	val = snd_hda_codec_read(codec, 0x1c, 0, 0xcb0, 0x0);
-	if (val & 0x800) {
-		codec_dbg(codec, "headset plugin, type is CTIA\n");
-		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
-	} else if (val & 0x400) {
-		codec_dbg(codec, "headset plugin, type is OMTP\n");
-		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
+	if (val & 0xc00) {
+		codec_dbg(codec, "headset plugin, type is %s\n",
+			  val & 0x800 ? "CTIA" : "OMTP");
 	} else {
-		codec_dbg(codec, "headphone plugin\n");
+		codec_dbg(codec, "headphone or mic plugin\n");
 	}
 }
 
@@ -243,10 +240,12 @@ static void cx_update_headset_mic_vref(struct hda_codec *codec, struct hda_jack_
 	 * Check hp&mic tag to process headset plugin & plugout.
 	 */
 	mic_present = snd_hda_codec_read(codec, 0x19, 0, AC_VERB_GET_PIN_SENSE, 0x0);
-	if (!(mic_present & AC_PINSENSE_PRESENCE)) /* mic plugout */
+	if (!(mic_present & AC_PINSENSE_PRESENCE)) { /* mic plugout */
 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20);
-	else
-		cx_process_headset_plugin(codec);
+	} else {
+		cx_process_headset_detect_plug_type(codec);
+		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
+	}
 }
 
 static int cx_suspend(struct hda_codec *codec)
-- 
2.25.1
Re: [PATCH v2] ALSA: hda/conexant: Refactor headset plug detection
Posted by Takashi Iwai 1 month ago
On Wed, 26 Aug 2026 03:45:12 +0200,
songxiebing wrote:
> 
> From: Bob Song <songxiebing@kylinos.cn>
> 
> Rename cx_process_headset_plugin() to cx_process_headset_detect_plug_type()
> to better describe what it does, and merge the duplicated CTIA/OMTP
> branches since both select the same pin-widget control value for the mic
> pin.
> 
> Move the AC_VERB_SET_PIN_WIDGET_CONTROL write into the caller
> cx_update_headset_mic_vref() so that the mic pin control is applied
> unconditionally while a mic is present, instead of only for CTIA/OMTP
> headsets.
> 
> Signed-off-by: Bob Song <songxiebing@kylinos.cn>
> ---
> changes in v2:
>   - Optimize code logic and description

This doesn't apply on the latest code, as there was already a cleanup
to achieve a part of your code changes.
Could you rebase to the latest sound.git for-linus branch and
resubmit?


thanks,

Takashi