From 6fc6f3c493899f2ed4dc646190579a832808ff2f Mon Sep 17 00:00:00 2001
From: Jaroslav Kysela <perex@perex.cz>
Date: Fri, 28 Mar 2025 11:48:07 +0100
Subject: [PATCH] HDA: mics - improve the Jack selection

There may be different Jack controls when hardware has
different physical audio jacks or improved sensing mechanism.

Fixes: 19fc5b9 ("ucm2: HDA: HiFi-analog/mic: Refactor the analog mic discovery")
Link: https://github.com/alsa-project/alsa-ucm-conf/pull/526
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
---
 ucm2/HDA/HiFi-mic.conf | 85 ++++++++++++++++++++++++++++--------------
 1 file changed, 57 insertions(+), 28 deletions(-)

diff --git a/ucm2/HDA/HiFi-mic.conf b/ucm2/HDA/HiFi-mic.conf
index 65c351f1ecb2..f2a897fffee9 100644
--- a/ucm2/HDA/HiFi-mic.conf
+++ b/ucm2/HDA/HiFi-mic.conf
@@ -22,6 +22,8 @@
 
 Define.SourceControl ""
 Define.MicJackControl ""
+Define.MicHPJackControl ""
+Define.MicHSJackControl ""
 
 # DeviceMic is pre-configured
 Define.DeviceMicName ""
@@ -45,33 +47,60 @@ Define.DeviceMic4Priority ""
 Define.DeviceMic4Jack ""
 
 # evaluate the microphone jack name
-If.hsmicjack {
+If.micjack {
+	Condition {
+		Type ControlExists
+		Control "iface=CARD,name='Mic Jack'"
+	}
+	True.Define.MicJackControl "Mic Jack"
+}
+
+If.hsjack {
 	Condition {
 		Type ControlExists
 		Control "iface=CARD,name='Headset Mic Jack'"
 	}
-	True.Define.MicJackControl "Headset Mic Jack"
-	False.If.hpmicjack {
-		Condition {
-			Type ControlExists
-			Control "iface=CARD,name='Headphone Mic Jack'"
-		}
-		True.Define.MicJackControl "Headphone Mic Jack"
-		False.If.hpjack {
-			Condition {
-				Type ControlExists
-				Control "iface=CARD,name='Mic Jack'"
-			}
-			True.Define.MicJackControl "Mic Jack"
-			False.If.micjack {
-				Condition {
-					Type ControlExists
-					Control "iface=CARD,name='Headphone Jack'"
-				}
-				True.Define.MicJackControl "Headphone Jack"
-			}
-		}
+	True.Define.MicHSJackControl "Headset Mic Jack"
+}
+
+If.hpjack {
+	Condition {
+		Type ControlExists
+		Control "iface=CARD,name='Headphone Mic Jack'"
 	}
+	True.Define.MicHPJackControl "Headphone Mic Jack"
+}
+
+If.jacksel1 {
+	Condition {
+		Type String
+		Empty "${var:MicJackControl}"
+	}
+	True.Define.MicJackControl "${var:MicHPJackControl}"
+}
+
+If.jacksel2 {
+	Condition {
+		Type String
+		Empty "${var:MicJackControl}"
+	}
+	True.Define.MicJackControl "${var:MicHSJackControl}"
+}
+
+If.jacksel3 {
+	Condition {
+		Type String
+		Empty "${var:MicHPJackControl}"
+	}
+	True.Define.MicHPJackControl "${var:MicJackControl}"
+}
+
+If.jacksel4 {
+	Condition {
+		Type String
+		Empty "${var:MicHSJackControl}"
+	}
+	True.Define.MicHSJackControl "${var:MicJackControl}"
 }
 
 # determine the name of the source selection enum, if present
@@ -160,12 +189,12 @@ If.micsetup {
 				True.Define {
 					DeviceMicName "Headphone Mic"
 					DeviceMicComment "Headphones Stereo Microphone"
-					DeviceMicJack "${var:MicJackControl}"
+					DeviceMicJack "${var:MicHPJackControl}"
 				}
 				False.Define {
 					DeviceMic2Name "Headphone Mic"
 					DeviceMic2Comment "Headphones Stereo Microphone"
-					DeviceMic2Jack "${var:MicJackControl}"
+					DeviceMic2Jack "${var:MicHPJackControl}"
 				}
 			}
 		}
@@ -226,7 +255,7 @@ If.micsetup {
 					DeviceMic "Headset"
 					DeviceMicName "Headset Mic"
 					DeviceMicComment "Headset Mono Microphone"
-					DeviceMicJack "${var:MicJackControl}"
+					DeviceMicJack "${var:MicHSJackControl}"
 				}
 				False.If.whichdev2 {
 					Condition {
@@ -237,7 +266,7 @@ If.micsetup {
 						DeviceMic2 "Headset"
 						DeviceMic2Name "Headset Mic"
 						DeviceMic2Comment "Headset Mono Microphone"
-						DeviceMic2Jack "${var:MicJackControl}"
+						DeviceMic2Jack "${var:MicHSJackControl}"
 					}
 					False.If.whichdev3 {
 						Condition {
@@ -248,13 +277,13 @@ If.micsetup {
 							DeviceMic3 "Headset"
 							DeviceMic3Name "Headset Mic"
 							DeviceMic3Comment "Headset Mono Microphone"
-							DeviceMic3Jack "${var:MicJackControl}"
+							DeviceMic3Jack "${var:MicHSJackControl}"
 						}
 						False.Define {
 							DeviceMic4 "Headset"
 							DeviceMic4Name "Headset Mic"
 							DeviceMic4Comment "Headset Mono Microphone"
-							DeviceMic4Jack "${var:MicJackControl}"
+							DeviceMic4Jack "${var:MicHSJackControl}"
 						}
 					}
 
-- 
2.49.0

