Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v1 0/2] ALSA: hda/cs420x: MacBook8,1 speakers and headset mic
@ 2026-12-01 15:32 Shih-Yuan Lee
  2026-12-01 15:32 ` [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1 Shih-Yuan Lee
  2026-12-01 15:32 ` [PATCH v1 2/2] ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1 Shih-Yuan Lee
  0 siblings, 2 replies; 4+ messages in thread
From: Shih-Yuan Lee @ 2026-12-01 15:32 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: Shih-Yuan Lee, linux-sound, linux-kernel

This series adds audio support for the 12-inch MacBook (MacBook8,1,
codec SSID 106b:6400, Cirrus Logic CS4208), which currently has no
working internal speakers or headset microphone on mainline.

Patch 1 adds a CS4208_MB81 fixup for the four MAX98357B TDM speaker
amplifiers (digital pin 0x1d / converter 0x0a, enabled via GPIO0), with
a { FL, FR, NA, NA } channel map and GPIO0 automute on headphone
plug/unplug.

Patch 2 adds 4-pole TRRS headset mic support.  Pin 0x18 has no
mechanical jack sense, so CTIA/OMTP/3-pole polarity is detected with the
CS4208 HS3/HS4 impedance detector and routed via NID 0x24 CIR 0x1a.

Userspace needs a matching alsa-ucm-conf profile (SplitPCM to expose the
4-channel TDM PCM as a stereo sink); I will send that once this is
settled.  Background and reverse-engineering credits are in
https://bugzilla.kernel.org/show_bug.cgi?id=110561 (Leif Liddy,
Thomas Shirley, Zach Robinson and others).

Questions for review:

 - Patch 1 locks the "IEC958 Default PCM Playback Switch" read-only in
   HDA_FIXUP_ACT_BUILD to stop analog playback from being mirrored onto
   the 4-channel TDM converter.  Would you prefer a flag in
   hda_gen_spec (e.g. no_share_spdif) that skips
   snd_hda_create_spdif_share_sw() instead?
 - Patch 2 calls snd_hda_gen_fix_pin_power() for pin 0x18, because
   dyn_adc_switch does not propagate stream_enabled to the newly
   selected input pin when switching an already open capture stream.
   Should this be fixed in generic.c rather than worked around here?

Known limitations: speaker protection (pin 0x22 IV-sense) is not
implemented, so keep volume moderate.  Only MacBook8,1 was tested;
MacBook9,1/10,1 share the hardware family but are not added.

Testing, on a real MacBook8,1 (Ubuntu 7.0.0-44-generic, built as an
out-of-tree module of this cs420x.c since no mainline kernel with this
base is installed there):

 - Cold boot matches the quirk; speaker playback (4ch/44.1kHz/S16_LE
   via UCM SplitPCM), headphone plug/unplug with GPIO0 automute,
   internal mic, and TRRS headset mic hotplug all work.
 - Runtime PM D3 -> D0 resume and S3 suspend/resume keep working.  On
   this machine the codec does not wake the HDA link from D3 on jack
   plug/unplug, hence cs_init() re-runs debounced detection on resume.
 - Caveat for S3: on this MacBook8,1 suspend/resume is only reliable
   with three platform workarounds that are unrelated to this series:
   the ACPI LID0 wakeup source is disabled (otherwise the machine
   wakes again about 2 seconds after suspending with the lid open),
   the WiFi/Bluetooth kernel modules are unloaded before suspending,
   and my separate spi-pxa2xx patchset (currently under review) is
   applied.  Without those, suspend/resume fails or wakes up
   immediately regardless of this driver.
 - Analog headphone playback (hw:PCH,0, 48/96 kHz S32_LE) leaves
   converter 0x0a idle (stream=0).
 - checkpatch.pl --strict is clean, W=1 and sparse report nothing, and
   each patch builds on its own on top of v7.4-rc5-era master.
 - The locking and runtime-PM interaction of the headset detection
   (hs_mutex vs. runtime PM, unplug vs. resume, unsol vs. resume) was
   also modelled in SPIN and checked exhaustively.

Tool disclosure: I used an AI coding assistant for review feedback and
iteration on the code and for drafting parts of the changelogs, and the
SPIN model above was written with its help.  I reviewed and tested
everything myself and take responsibility for the submission.

Shih-Yuan Lee (2):
  ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1
  ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1

 Documentation/sound/hd-audio/models.rst |   2 +
 sound/hda/codecs/cirrus/cs420x.c        | 751 +++++++++++++++++++++++-
 2 files changed, 747 insertions(+), 6 deletions(-)

-- 
2.39.5


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

* [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1
  2026-12-01 15:32 [PATCH v1 0/2] ALSA: hda/cs420x: MacBook8,1 speakers and headset mic Shih-Yuan Lee
@ 2026-12-01 15:32 ` Shih-Yuan Lee
  2026-12-01 16:04   ` sashiko-bot
  2026-12-01 15:32 ` [PATCH v1 2/2] ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1 Shih-Yuan Lee
  1 sibling, 1 reply; 4+ messages in thread
From: Shih-Yuan Lee @ 2026-12-01 15:32 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: Shih-Yuan Lee, linux-sound, linux-kernel

The 12-inch MacBook (MacBook8,1, codec SSID 0x106b:0x6400) drives four
Maxim MAX98357B Class-D TDM digital speaker amplifiers via CS4208 digital
pin 0x1d (converter 0x0a) enabled through codec GPIO0.  The two left
amplifiers latch TDM slot 4 (ch 0) and the two right amplifiers latch
slot 12 (ch 1), while slots 0 and 16 (ch 2/ch 3) are unused.

Add a CS4208_MB81 fixup that:
- Applies explicit MacBook8,1 pin defaults with pin 0x1d retasked as a
  fixed internal digital output (0x904600f0).
- Constrains the digital playback PCM (paired with ALSA UCM2 SplitPCM
  for stereo speaker routing) to the 4-channel, 44.1 kHz, S16_LE TDM
  frame format (0x4013) with a { FL, FR, NA, NA } channel map so
  userspace does not treat it as 4.0 surround.
- Programs the Boot Camp CONF_0807 NID 0x24 TDM and A1 combo-jack
  coefficients directly on init/resume (instead of cs4208_coef_init_verbs)
  and sets the converter channel count (AC_VERB_SET_CVT_CHAN_COUNT) on
  stream prepare.
- Enables the parsed digital speaker converter before control creation so
  IEC958 starts unmuted, and disables analog-to-SPDIF stream sharing so
  analog headphone playback cannot cross-route onto or clamp against the
  4ch TDM converter.
- Serializes GPIO0 speaker amplifier power updates under gpio_mutex
  across init, resume, and headphone jack plug/unplug events.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=110561
Signed-off-by: Shih-Yuan Lee <fourdollars@debian.org>
---
 Documentation/sound/hd-audio/models.rst |   2 +
 sound/hda/codecs/cirrus/cs420x.c        | 273 +++++++++++++++++++++++-
 2 files changed, 270 insertions(+), 5 deletions(-)

diff --git a/Documentation/sound/hd-audio/models.rst b/Documentation/sound/hd-audio/models.rst
index 120430450014..edcc71f9b956 100644
--- a/Documentation/sound/hd-audio/models.rst
+++ b/Documentation/sound/hd-audio/models.rst
@@ -800,6 +800,8 @@ mbp11
     MacBookPro 11,2
 macmini
     MacMini 7,1
+mb81
+    MacBook 8,1 (12-inch Retina)
 auto
     BIOS setup (default)
 
diff --git a/sound/hda/codecs/cirrus/cs420x.c b/sound/hda/codecs/cirrus/cs420x.c
index 85c2ecf46d38..236a624735ed 100644
--- a/sound/hda/codecs/cirrus/cs420x.c
+++ b/sound/hda/codecs/cirrus/cs420x.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/init.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <sound/core.h>
@@ -17,6 +18,10 @@
 #include "hda_jack.h"
 #include "../generic.h"
 
+struct cs4208_mb81_data {
+	struct mutex gpio_mutex; /* serializes jack-to-GPIO updates */
+};
+
 struct cs_spec {
 	struct hda_gen_spec gen;
 
@@ -31,6 +36,9 @@ struct cs_spec {
 	/* for MBP SPDIF control */
 	int (*spdif_sw_put)(struct snd_kcontrol *kcontrol,
 			    struct snd_ctl_elem_value *ucontrol);
+
+	/* for MacBook 8,1 TDM digital speakers (NULL on other models) */
+	struct cs4208_mb81_data *mb81;
 };
 
 /* available models with CS420x */
@@ -118,12 +126,55 @@ static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  * HP/SPK/SPDIF
  */
 
+static hda_nid_t cs4208_mb81_hp_pin(struct hda_codec *codec)
+{
+	struct cs_spec *spec = codec->spec;
+	const struct auto_pin_cfg *cfg = &spec->gen.autocfg;
+
+	if (cfg->hp_outs)
+		return cfg->hp_pins[0];
+	if (cfg->line_outs && cfg->line_out_type == AUTO_PIN_HP_OUT)
+		return cfg->line_out_pins[0];
+	return 0;
+}
+
+static bool cs4208_mb81_hp_present(struct hda_codec *codec)
+{
+	hda_nid_t hp_pin = cs4208_mb81_hp_pin(codec);
+
+	return hp_pin && snd_hda_jack_detect(codec, hp_pin);
+}
+
+static void cs4208_mb81_update_gpio(struct hda_codec *codec, bool write_mask)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+	bool hp_present;
+
+	mutex_lock(&mb81->gpio_mutex);
+	hp_present = cs4208_mb81_hp_present(codec);
+	spec->gpio_data = hp_present ?
+		spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
+	if (write_mask)
+		snd_hda_codec_set_gpio(codec, spec->gpio_mask, spec->gpio_dir,
+				       spec->gpio_data, 0);
+	else
+		snd_hda_codec_write(codec, 0x01, 0,
+				    AC_VERB_SET_GPIO_DATA, spec->gpio_data);
+	mutex_unlock(&mb81->gpio_mutex);
+}
+
 static void cs_automute(struct hda_codec *codec)
 {
 	struct cs_spec *spec = codec->spec;
 
 	snd_hda_gen_update_outputs(codec);
 
+	if (spec->mb81) {
+		cs4208_mb81_update_gpio(codec, false);
+		return;
+	}
+
 	if (spec->gpio_eapd_hp || spec->gpio_eapd_speaker) {
 		if (spec->gen.automute_speaker)
 			spec->gpio_data = spec->gen.hp_jack_present ?
@@ -193,6 +244,34 @@ static const struct hda_verb cs4208_coef_init_verbs[] = {
 	{} /* terminator */
 };
 
+/*
+ * MacBook 8,1 NID 0x24 vendor coefficients (from Apple Boot Camp CONF_0807):
+ * configures the 4-channel TDM speaker link on TX1 (CIR 0x00/0x03/0x04/0x05/
+ * 0x1f/0x36) and board-specific A1 combo-jack sense parameters (CIR 0x33/0x34)
+ * in place of the generic cs4208_coef_init_verbs sequence.
+ */
+static const struct hda_verb cs4208_mb81_coef_verbs[] = {
+	{0x01, AC_VERB_SET_POWER_STATE, 0x00}, /* AFG: D0 */
+	{0x24, AC_VERB_SET_PROC_STATE, 0x01},  /* VPW: processing on */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0000},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x00c4}, /* ASP/TDM master & frame config */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0003},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0baa}, /* TX1/RX1 TDM slot format */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0004},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0c04}, /* TX1 ch 0: slot 4, ch 1: slot 12 */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0005},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x1000}, /* TX1 ch 2: slot 0, ch 3: slot 16 */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x001f},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0000}, /* ASP/digital routing control */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0821}, /* A1 ICS (MB8,1 combo jack) */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0034},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x3b21}, /* A1 Enable/threshold (MB8,1) */
+	{0x24, AC_VERB_SET_COEF_INDEX, 0x0036},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0034}, /* ASP clock/interface config */
+	{} /* terminator */
+};
+
 /* Errata: CS4207 rev C0/C1/C2 Silicon
  *
  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
@@ -259,12 +338,17 @@ static int cs_init(struct hda_codec *codec)
 		snd_hda_sequence_write(codec, cs_errata_init_verbs);
 		snd_hda_sequence_write(codec, cs_coef_init_verbs);
 	} else if (spec->vendor_nid == CS4208_VENDOR_NID) {
-		snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
+		if (spec->mb81)
+			snd_hda_sequence_write(codec, cs4208_mb81_coef_verbs);
+		else
+			snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
 	}
 
 	snd_hda_gen_init(codec);
 
-	if (spec->gpio_mask)
+	if (spec->mb81)
+		cs4208_mb81_update_gpio(codec, true);
+	else if (spec->gpio_mask)
 		snd_hda_codec_set_gpio(codec, spec->gpio_mask, spec->gpio_dir,
 				       spec->gpio_data, 0);
 
@@ -558,6 +642,7 @@ enum {
 	CS4208_MBA6,
 	CS4208_MBP11,
 	CS4208_MACMINI,
+	CS4208_MB81,
 	CS4208_GPIO0,
 };
 
@@ -566,6 +651,7 @@ static const struct hda_model_fixup cs4208_models[] = {
 	{ .id = CS4208_MBA6, .name = "mba6" },
 	{ .id = CS4208_MBP11, .name = "mbp11" },
 	{ .id = CS4208_MACMINI, .name = "macmini" },
+	{ .id = CS4208_MB81, .name = "mb81" },
 	{}
 };
 
@@ -577,6 +663,7 @@ static const struct hda_quirk cs4208_fixup_tbl[] = {
 /* codec SSID matching */
 static const struct hda_quirk cs4208_mac_fixup_tbl[] = {
 	SND_PCI_QUIRK(0x106b, 0x5e00, "MacBookPro 11,2", CS4208_MBP11),
+	SND_PCI_QUIRK(0x106b, 0x6400, "MacBook 8,1", CS4208_MB81),
 	SND_PCI_QUIRK(0x106b, 0x6c00, "MacMini 7,1", CS4208_MACMINI),
 	SND_PCI_QUIRK(0x106b, 0x7100, "MacBookAir 6,1", CS4208_MBA6),
 	SND_PCI_QUIRK(0x106b, 0x7200, "MacBookAir 6,2", CS4208_MBA6),
@@ -663,6 +750,167 @@ static void cs4208_fixup_spdif_switch(struct hda_codec *codec,
 	}
 }
 
+static const struct hda_pcm_stream cs4208_mb81_dig_playback = {
+	.channels_min = 4,
+	.channels_max = 4,
+	.rates = SNDRV_PCM_RATE_44100,
+	.formats = SNDRV_PCM_FMTBIT_S16_LE,
+	.maxbps = 16,
+};
+
+/*
+ * Although the TDM frame carries 4 slots, the two left-side MAX98357B amps
+ * both latch slot 4 (ch 0 = FL) and the two right-side amps latch slot 12
+ * (ch 1 = FR); slots 0 and 16 (ch 2/ch 3) are unused.  Mark ch 2/ch 3 NA so
+ * userspace treats the speakers as stereo rather than 4.0 surround.
+ */
+static const struct snd_pcm_chmap_elem cs4208_mb81_chmaps[] = {
+	{ .channels = 4,
+	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
+		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA } },
+	{ }
+};
+
+static int cs4208_mb81_dig_playback_prepare(struct hda_pcm_stream *hinfo,
+					    struct hda_codec *codec,
+					    unsigned int stream_tag,
+					    unsigned int format,
+					    struct snd_pcm_substream *substream)
+{
+	struct cs_spec *spec = codec->spec;
+	int err;
+
+	err = snd_hda_multi_out_dig_prepare(codec, &spec->gen.multiout,
+					    stream_tag, format, substream);
+	if (err < 0)
+		return err;
+
+	snd_hda_codec_write(codec, hinfo->nid, 0, AC_VERB_SET_CVT_CHAN_COUNT,
+			    substream->runtime->channels - 1);
+	return 0;
+}
+
+static void cs4208_mb81_hp_cb(struct hda_codec *codec,
+			      struct hda_jack_callback *cb)
+{
+	cs_automute(codec);
+}
+
+/*
+ * MacBook 8,1 drives 4x MAX98357B TDM digital speaker amplifiers via
+ * digital pin 0x1d (converter 0x0a) enabled by GPIO0.
+ */
+static void cs4208_fixup_mb81(struct hda_codec *codec,
+			      const struct hda_fixup *fix, int action)
+{
+	static const struct hda_pintbl pincfgs[] = {
+		{ 0x10, 0x002b4020 }, /* Headphone */
+		{ 0x11, 0x400000f0 },
+		{ 0x12, 0x400000f0 },
+		{ 0x13, 0x400000f0 },
+		{ 0x14, 0x400000f0 },
+		{ 0x15, 0x400000f0 },
+		{ 0x16, 0x400000f0 },
+		{ 0x17, 0x400000f0 },
+		{ 0x18, 0x00ab9030 }, /* Headset Mic */
+		{ 0x19, 0x90a60100 }, /* Internal Mic */
+		{ 0x1a, 0x400000f0 },
+		{ 0x1b, 0x400000f0 },
+		{ 0x1c, 0x400000f0 },
+		{ 0x1d, 0x904600f0 }, /* Internal TDM speaker digital out */
+		{ 0x1e, 0x400000f0 },
+		{ 0x1f, 0x400000f0 },
+		{ 0x20, 0x400000f0 },
+		{ 0x21, 0x400000f0 },
+		{ 0x22, 0x400000f0 },
+		{ }
+	};
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81;
+	struct hda_jack_callback *cb;
+	struct snd_kcontrol *kctl;
+	hda_nid_t hp_pin, dig_nid;
+
+	switch (action) {
+	case HDA_FIXUP_ACT_PRE_PROBE:
+		mb81 = kzalloc_obj(*mb81);
+		if (!mb81) {
+			codec_err(codec,
+				  "Failed to allocate MacBook 8,1 fixup data\n");
+			return;
+		}
+		mutex_init(&mb81->gpio_mutex);
+		spec->mb81 = mb81;
+		snd_hda_apply_pincfgs(codec, pincfgs);
+		spec->gen.stream_digital_playback = &cs4208_mb81_dig_playback;
+		spec->gpio_eapd_hp = 0;
+		spec->gpio_eapd_speaker = 1;
+		spec->gpio_mask = 0x01; /* GPIO0 (speaker amp enable) */
+		spec->gpio_dir = 0x01;  /* GPIO0 output */
+		spec->gpio_data = spec->gpio_eapd_speaker;
+		break;
+	case HDA_FIXUP_ACT_PROBE:
+		if (!spec->mb81)
+			return;
+		hp_pin = cs4208_mb81_hp_pin(codec);
+		if (hp_pin) {
+			cb = snd_hda_jack_detect_enable_callback(codec, hp_pin,
+								 cs4208_mb81_hp_cb);
+			if (IS_ERR(cb))
+				codec_warn(codec,
+					   "Failed to register HP jack callback: %pe\n",
+					   cb);
+		}
+		/*
+		 * Enable the digital speaker converter before control build so
+		 * IEC958 starts unmuted; upper byte sets DIGI_CONVERT_2 (0x01)
+		 * matching Apple firmware.
+		 */
+		dig_nid = spec->gen.multiout.dig_out_nid;
+		if (dig_nid)
+			snd_hda_codec_write_cache(codec, dig_nid, 0,
+						  AC_VERB_SET_DIGI_CONVERT_1,
+						  AC_DIG1_ENABLE |
+						  AC_DIG1_COPYRIGHT |
+						  (0x01 << 8));
+		break;
+	case HDA_FIXUP_ACT_BUILD:
+		if (!spec->mb81)
+			return;
+		/*
+		 * Never mirror 2-channel analog HP playback onto the 4-channel
+		 * TDM speaker converter or clamp HP stream capabilities to it.
+		 */
+		spec->gen.multiout.share_spdif = 0;
+		kctl = snd_hda_find_mixer_ctl(codec,
+					      "IEC958 Default PCM Playback Switch");
+		if (kctl)
+			kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
+		break;
+	}
+}
+
+static int cs_build_pcms(struct hda_codec *codec)
+{
+	struct cs_spec *spec = codec->spec;
+	int err;
+
+	err = snd_hda_gen_build_pcms(codec);
+	if (err < 0)
+		return err;
+
+	if (spec->mb81 && spec->gen.pcm_rec[1] &&
+	    spec->gen.multiout.dig_out_nid) {
+		struct hda_pcm_stream *hinfo =
+			&spec->gen.pcm_rec[1]->stream[SNDRV_PCM_STREAM_PLAYBACK];
+
+		hinfo->chmap = cs4208_mb81_chmaps;
+		hinfo->ops.prepare = cs4208_mb81_dig_playback_prepare;
+	}
+
+	return 0;
+}
+
 static const struct hda_fixup cs4208_fixups[] = {
 	[CS4208_MBA6] = {
 		.type = HDA_FIXUP_PINS,
@@ -682,6 +930,10 @@ static const struct hda_fixup cs4208_fixups[] = {
 		.chained = true,
 		.chain_id = CS4208_GPIO0,
 	},
+	[CS4208_MB81] = {
+		.type = HDA_FIXUP_FUNC,
+		.v.func = cs4208_fixup_mb81,
+	},
 	[CS4208_GPIO0] = {
 		.type = HDA_FIXUP_FUNC,
 		.v.func = cs4208_fixup_gpio0,
@@ -730,6 +982,17 @@ static int cs4208_probe(struct hda_codec *codec)
 	return 0;
 }
 
+static void cs_remove(struct hda_codec *codec)
+{
+	struct cs_spec *spec = codec->spec;
+
+	if (spec) {
+		kfree(spec->mb81);
+		spec->mb81 = NULL;
+	}
+	snd_hda_gen_remove(codec);
+}
+
 static int cs_codec_probe(struct hda_codec *codec,
 			  const struct hda_device_id *id)
 {
@@ -746,15 +1009,15 @@ static int cs_codec_probe(struct hda_codec *codec,
 	else
 		err = cs420x_probe(codec);
 	if (err < 0)
-		snd_hda_gen_remove(codec);
+		cs_remove(codec);
 	return err;
 }
 
 static const struct hda_codec_ops cs_codec_ops = {
 	.probe = cs_codec_probe,
-	.remove = snd_hda_gen_remove,
+	.remove = cs_remove,
 	.build_controls = cs_build_controls,
-	.build_pcms = snd_hda_gen_build_pcms,
+	.build_pcms = cs_build_pcms,
 	.init = cs_init,
 	.unsol_event = snd_hda_jack_unsol_event,
 	.stream_pm = snd_hda_gen_stream_pm,
-- 
2.39.5


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

* [PATCH v1 2/2] ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1
  2026-12-01 15:32 [PATCH v1 0/2] ALSA: hda/cs420x: MacBook8,1 speakers and headset mic Shih-Yuan Lee
  2026-12-01 15:32 ` [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1 Shih-Yuan Lee
@ 2026-12-01 15:32 ` Shih-Yuan Lee
  1 sibling, 0 replies; 4+ messages in thread
From: Shih-Yuan Lee @ 2026-12-01 15:32 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: Shih-Yuan Lee, linux-sound, linux-kernel

On MacBook 8,1 (CS4208 SSID 106b:6400), the 3.5 mm combo jack headset
microphone (NID 0x18 on ADC2 0x07) does not work out of the box for four
hardware reasons:

1. CS4208 ADC input amp indices share a single hardware gain/mute
   register, so generic auto-mic muting unused index 2 after unmuting
   index 1 (NID 0x18) silently mutes ADC2.  Set codec->single_adc_amp = 1
   for MacBook 8,1.
2. External HS3/HS4 DFETs on the combo jack require GPIO4 and GPIO5 high
   while AFG is active (matching Boot Camp CONF_0807.Gpio), and NID 0x24
   CIR 0x1d needs DC detect level 0x0bf6.
3. Pin 0x18 has no mechanical jack-sense switch and CTIA/OMTP polarity
   must be detected via the CS4208 HS3/HS4 impedance detector
   (CIR 0x19/0x1a/0x1b, with manual comparator fallback) before routing
   HSBIAS/MIC and GND via CIR 0x1a (0x00a6 CTIA, 0x0059 OMTP, 0x00f3
   3-pole headphone).
4. Hot-plugging a headset while capture is already open leaves pin 0x18
   in D3 because dyn_adc_switch does not propagate stream_enabled to the
   newly selected input pin; keep pin 0x18 powered via
   snd_hda_gen_fix_pin_power().

Run type detection asynchronously on system_freezable_power_efficient_wq
with a 500 ms insertion debounce (hardware auto-detect then takes ~120 ms),
releasing hs_mutex across settling/polling sleeps so jack callbacks and
suspend never stall.  Because MacBook 8,1 CS4208 does not wake the HDA link
from runtime D3 on jack plug/unplug, cs_init() re-runs debounced detection
on resume so D3 swaps are always caught while keeping hs_mic_present across
resume to avoid flapping userspace routing.  On unplug, cancel pending
detection and clear pin 0x18 synchronously; set block_report and disable
hardware unsolicited events on pin 0x18, and wrap jack unsol handling in
snd_hda_power_up()/snd_hda_power_down() so runtime resume and unsol jack
updates never race on pin 0x18 jack state.  All MacBook 8,1 state stays
isolated in struct cs4208_mb81_data.

Signed-off-by: Shih-Yuan Lee <fourdollars@debian.org>
---
 sound/hda/codecs/cirrus/cs420x.c | 506 ++++++++++++++++++++++++++++++-
 1 file changed, 491 insertions(+), 15 deletions(-)

diff --git a/sound/hda/codecs/cirrus/cs420x.c b/sound/hda/codecs/cirrus/cs420x.c
index 236a624735ed..cc7e92646952 100644
--- a/sound/hda/codecs/cirrus/cs420x.c
+++ b/sound/hda/codecs/cirrus/cs420x.c
@@ -5,10 +5,14 @@
  * Copyright (c) 2009 Takashi Iwai <tiwai@suse.de>
  */
 
+#include <linux/atomic.h>
+#include <linux/completion.h>
 #include <linux/init.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/workqueue.h>
 #include <sound/core.h>
 #include <linux/pci.h>
 #include <sound/tlv.h>
@@ -19,7 +23,16 @@
 #include "../generic.h"
 
 struct cs4208_mb81_data {
+	struct hda_codec *codec;
 	struct mutex gpio_mutex; /* serializes jack-to-GPIO updates */
+	struct mutex hs_mutex; /* serializes NID 0x24 COEFs and HS detection */
+	struct delayed_work hs_detect_work;
+	struct completion hs_stop_comp;
+	unsigned int hs_seq; /* incremented on plug/unplug/suspend to abort stale runs */
+	atomic_t hs_detecting;
+	u8 pm_retries;
+	bool hs_stopping;
+	bool hs_mic_present;
 };
 
 struct cs_spec {
@@ -99,6 +112,28 @@ enum {
 /* Cirrus Logic CS4208 */
 #define CS4208_VENDOR_NID	0x24
 
+/* CS4208 vendor COEFs for headset type detection (NID 0x24) */
+#define CS4208_COEF_HSDET_CTL		0x0019 /* (HSDET_CTL2 << 8) | CTL1 */
+#define CS4208_COEF_HS_SWITCH_CTL	0x001a
+#define CS4208_COEF_HS_DET_STATUS	0x001b
+#define CS4208_COEF_DC_DET_LVL		0x001d
+
+#define CS4208_HSDET_MANUAL_DIS		0x0088
+#define CS4208_HSDET_MANUAL_ACT		0x402c
+#define CS4208_HSDET_AUTO_ACT		0xe388
+
+#define CS4208_HS_SW_CTIA		0x00a6 /* HS3=GND, HS4=HSBIAS+MIC */
+#define CS4208_HS_SW_OMTP		0x0059 /* HS4=GND, HS3=HSBIAS+MIC */
+#define CS4208_HS_SW_HEADPHONE		0x00f3 /* HS3+HS4=GND, no MIC */
+
+#define CS4208_HSDET_TYPE_MASK		0x0003
+#define CS4208_HSDET_TYPE_CTIA		0
+#define CS4208_HSDET_TYPE_OMTP		1
+#define CS4208_HSDET_TYPE_HP		2
+#define CS4208_HSDET_AUTO_DONE		0x0004
+#define CS4208_HSDET_COMP1_OUT		0x0040
+#define CS4208_HSDET_COMP2_OUT		0x0080
+
 static inline int cs_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
 {
 	struct cs_spec *spec = codec->spec;
@@ -145,7 +180,7 @@ static bool cs4208_mb81_hp_present(struct hda_codec *codec)
 	return hp_pin && snd_hda_jack_detect(codec, hp_pin);
 }
 
-static void cs4208_mb81_update_gpio(struct hda_codec *codec, bool write_mask)
+static bool cs4208_mb81_update_gpio(struct hda_codec *codec, bool write_mask)
 {
 	struct cs_spec *spec = codec->spec;
 	struct cs4208_mb81_data *mb81 = spec->mb81;
@@ -162,6 +197,8 @@ static void cs4208_mb81_update_gpio(struct hda_codec *codec, bool write_mask)
 		snd_hda_codec_write(codec, 0x01, 0,
 				    AC_VERB_SET_GPIO_DATA, spec->gpio_data);
 	mutex_unlock(&mb81->gpio_mutex);
+
+	return hp_present;
 }
 
 static void cs_automute(struct hda_codec *codec)
@@ -261,6 +298,8 @@ static const struct hda_verb cs4208_mb81_coef_verbs[] = {
 	{0x24, AC_VERB_SET_PROC_COEF, 0x0c04}, /* TX1 ch 0: slot 4, ch 1: slot 12 */
 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0005},
 	{0x24, AC_VERB_SET_PROC_COEF, 0x1000}, /* TX1 ch 2: slot 0, ch 3: slot 16 */
+	{0x24, AC_VERB_SET_COEF_INDEX, CS4208_COEF_DC_DET_LVL},
+	{0x24, AC_VERB_SET_PROC_COEF, 0x0bf6}, /* DC detect level = 0x36 */
 	{0x24, AC_VERB_SET_COEF_INDEX, 0x001f},
 	{0x24, AC_VERB_SET_PROC_COEF, 0x0000}, /* ASP/digital routing control */
 	{0x24, AC_VERB_SET_COEF_INDEX, 0x0033},
@@ -272,6 +311,286 @@ static const struct hda_verb cs4208_mb81_coef_verbs[] = {
 	{} /* terminator */
 };
 
+/* Reset headset mic state; must be called with mb81->hs_mutex held. */
+static void cs4208_mb81_reset_hs_state(struct hda_codec *codec, bool write_hw)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+
+	WRITE_ONCE(mb81->hs_mic_present, false);
+	if (write_hw) {
+		cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+				   CS4208_HSDET_MANUAL_DIS);
+		cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL,
+				   CS4208_HS_SW_HEADPHONE);
+	}
+}
+
+/*
+ * Release mb81->hs_mutex across detection sleeps so hp_cb(),
+ * mic_autoswitch_hook, and cs_suspend() never stall on the 300-700 ms
+ * settling/polling windows.  Re-checking hs_stopping and hs_seq after
+ * re-acquiring hs_mutex aborts superseded or shutdown-interrupted runs
+ * before any further verbs run.
+ */
+static bool cs4208_mb81_hs_sleep(struct cs4208_mb81_data *mb81,
+				 unsigned int seq, unsigned int msecs)
+{
+	unsigned long rem;
+
+	if (READ_ONCE(mb81->hs_stopping) || mb81->hs_seq != seq)
+		return true;
+	mutex_unlock(&mb81->hs_mutex);
+	rem = wait_for_completion_timeout(&mb81->hs_stop_comp,
+					  msecs_to_jiffies(msecs));
+	mutex_lock(&mb81->hs_mutex);
+	return rem != 0 || READ_ONCE(mb81->hs_stopping) || mb81->hs_seq != seq;
+}
+
+static void cs4208_mb81_restore_mic_pin(struct hda_codec *codec,
+					unsigned int pinctl, unsigned int pwr)
+{
+	unsigned int target = snd_hda_codec_get_pin_target(codec, 0x18);
+
+	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
+			    target ? target : pinctl);
+	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, pwr);
+}
+
+/*
+ * Run CS4208 HS3/HS4 headset type detection under mb81->hs_mutex (temporarily
+ * dropped across settling sleeps).  Returns true if the caller should sync and
+ * report pin 0x18, or false if the run was aborted by shutdown or superseded
+ * by a newer jack/suspend event.
+ */
+static bool cs4208_mb81_hs_detect_locked(struct hda_codec *codec,
+					 bool hp_present, unsigned int seq)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+	hda_nid_t hp_pin = cs4208_mb81_hp_pin(codec);
+	unsigned int pwr, pinctl, status = 0, hs_type, hs_switch;
+	int i;
+
+	if (!hp_present || !hp_pin) {
+		cs4208_mb81_reset_hs_state(codec, true);
+		return true;
+	}
+
+	pwr = snd_hda_codec_read(codec, 0x18, 0, AC_VERB_GET_POWER_STATE, 0) &
+	      AC_PWRST_SETTING;
+	pinctl = snd_hda_codec_read(codec, 0x18, 0,
+				    AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
+
+	/* Enable HSBIAS via pin 0x18 VREF_80 for type detection */
+	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
+			    AC_PWRST_D0);
+	snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
+			    PIN_IN | AC_PINCTL_VREF_80);
+	if (cs4208_mb81_hs_sleep(mb81, seq, 100))
+		goto out_abort;
+
+	/* Re-verify plug presence after bias ramp */
+	if (!(snd_hda_codec_read(codec, hp_pin, 0,
+				 AC_VERB_GET_PIN_SENSE, 0) &
+	      AC_PINSENSE_PRESENCE))
+		goto out_abort;
+
+	/* Run hardware auto headset type detection */
+	cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL,
+			   CS4208_HS_SW_HEADPHONE);
+	cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+			   CS4208_HSDET_MANUAL_DIS);
+	cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+			   CS4208_HSDET_AUTO_ACT);
+
+	for (i = 0; i < 20; i++) {
+		if (cs4208_mb81_hs_sleep(mb81, seq, 20))
+			goto out_abort;
+		status = cs_vendor_coef_get(codec, CS4208_COEF_HS_DET_STATUS);
+		if (status & CS4208_HSDET_AUTO_DONE)
+			break;
+	}
+
+	hs_type = (status & CS4208_HSDET_AUTO_DONE) ?
+		  (status & CS4208_HSDET_TYPE_MASK) : CS4208_HSDET_TYPE_HP;
+
+	/* Manual comparator fallback if auto detect reports headphone/invalid */
+	if (hs_type >= CS4208_HSDET_TYPE_HP) {
+		unsigned int c_ctia, c_omtp, comp1, comp2;
+
+		cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+				   CS4208_HSDET_MANUAL_ACT);
+		cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL,
+				   CS4208_HS_SW_CTIA);
+		if (cs4208_mb81_hs_sleep(mb81, seq, 100))
+			goto out_abort;
+		c_ctia = cs_vendor_coef_get(codec, CS4208_COEF_HS_DET_STATUS);
+
+		cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL,
+				   CS4208_HS_SW_OMTP);
+		if (cs4208_mb81_hs_sleep(mb81, seq, 100))
+			goto out_abort;
+		c_omtp = cs_vendor_coef_get(codec, CS4208_COEF_HS_DET_STATUS);
+
+		comp1 = ((c_ctia & CS4208_HSDET_COMP1_OUT) ? 1 : 0) |
+			((c_omtp & CS4208_HSDET_COMP1_OUT) ? 2 : 0);
+		comp2 = ((c_ctia & CS4208_HSDET_COMP2_OUT) ? 1 : 0) |
+			((c_omtp & CS4208_HSDET_COMP2_OUT) ? 2 : 0);
+
+		if (comp1 == 1 || (comp1 != 2 && comp2 == 1))
+			hs_type = CS4208_HSDET_TYPE_CTIA;
+		else if (comp1 == 2 || comp2 == 2)
+			hs_type = CS4208_HSDET_TYPE_OMTP;
+		else
+			hs_type = CS4208_HSDET_TYPE_HP;
+	}
+
+	/* Re-verify plug presence right before committing detection result */
+	if (!(snd_hda_codec_read(codec, hp_pin, 0,
+				 AC_VERB_GET_PIN_SENSE, 0) &
+	      AC_PINSENSE_PRESENCE))
+		goto out_abort;
+
+	switch (hs_type) {
+	case CS4208_HSDET_TYPE_CTIA:
+		hs_switch = CS4208_HS_SW_CTIA;
+		WRITE_ONCE(mb81->hs_mic_present, true);
+		break;
+	case CS4208_HSDET_TYPE_OMTP:
+		hs_switch = CS4208_HS_SW_OMTP;
+		WRITE_ONCE(mb81->hs_mic_present, true);
+		break;
+	default:
+		hs_switch = CS4208_HS_SW_HEADPHONE;
+		WRITE_ONCE(mb81->hs_mic_present, false);
+		break;
+	}
+
+	cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+			   CS4208_HSDET_MANUAL_DIS);
+	cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL, hs_switch);
+
+	/* Restore pin 0x18; auto-mic will activate it if a mic was detected */
+	cs4208_mb81_restore_mic_pin(codec, pinctl, pwr);
+
+	codec_dbg(codec, "CS4208 HS type detect: type=%u sw=0x%04x mic=%d\n",
+		  hs_type, hs_switch, READ_ONCE(mb81->hs_mic_present));
+	return true;
+
+out_abort:
+	if (!READ_ONCE(mb81->hs_stopping)) {
+		cs4208_mb81_restore_mic_pin(codec, pinctl, pwr);
+		cs4208_mb81_reset_hs_state(codec, true);
+	} else {
+		cs4208_mb81_reset_hs_state(codec, false);
+	}
+	return !READ_ONCE(mb81->hs_stopping) && mb81->hs_seq == seq;
+}
+
+/*
+ * Pin 0x18 (headset mic) has no independent mechanical jack-sense switch.
+ * block_report stays set on pin 0x18 so snd_hda_jack_report_sync() never
+ * reports it concurrently; all pin 0x18 state updates and snd_jack_report()
+ * calls are serialized here under mb81->hs_mutex.
+ */
+static void cs4208_mb81_sync_mic_jack(struct hda_codec *codec, bool report)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+	struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, 0x18);
+	bool present = READ_ONCE(mb81->hs_mic_present);
+
+	if (jack) {
+		jack->pin_sense = present ? AC_PINSENSE_PRESENCE : 0;
+		jack->jack_dirty = 0;
+	}
+	snd_hda_gen_mic_autoswitch(codec, NULL);
+	if (report && jack && jack->jack)
+		snd_jack_report(jack->jack, present ? jack->type : 0);
+}
+
+static bool cs_pm_ready(struct hda_codec *codec)
+{
+	return !IS_ENABLED(CONFIG_PM) ||
+	       pm_runtime_enabled(hda_codec_dev(codec));
+}
+
+static bool cs_is_shutting_down(void)
+{
+	return system_state == SYSTEM_HALT ||
+	       system_state == SYSTEM_POWER_OFF ||
+	       system_state == SYSTEM_RESTART;
+}
+
+static void cs4208_mb81_mic_autoswitch(struct hda_codec *codec,
+				       struct hda_jack_callback *cb)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+
+	if (!mb81)
+		return;
+
+	mutex_lock(&mb81->hs_mutex);
+	cs4208_mb81_sync_mic_jack(codec, false);
+	mutex_unlock(&mb81->hs_mutex);
+}
+
+static void cs4208_mb81_hs_detect_work(struct work_struct *work)
+{
+	struct cs4208_mb81_data *mb81 =
+		container_of(work, struct cs4208_mb81_data,
+			     hs_detect_work.work);
+	struct hda_codec *codec = mb81->codec;
+	hda_nid_t hp_pin = cs4208_mb81_hp_pin(codec);
+	unsigned int seq;
+	bool hp_present, report;
+	int err;
+
+	if (READ_ONCE(mb81->hs_stopping) || cs_is_shutting_down())
+		return;
+
+	atomic_inc(&mb81->hs_detecting);
+
+	/*
+	 * During boot probe, cs_init() runs before snd_hda_codec_register()
+	 * enables runtime PM; defer until runtime PM is enabled.
+	 */
+	if (!cs_pm_ready(codec)) {
+		atomic_dec(&mb81->hs_detecting);
+		if (++mb81->pm_retries <= 50)
+			queue_delayed_work(system_freezable_power_efficient_wq,
+					   &mb81->hs_detect_work,
+					   msecs_to_jiffies(100));
+		else
+			codec_warn(codec,
+				   "Timed out waiting for runtime PM before HS detect\n");
+		return;
+	}
+	mb81->pm_retries = 0;
+
+	err = snd_hda_power_up(codec);
+	if (err < 0)
+		goto out;
+
+	mutex_lock(&mb81->hs_mutex);
+	if (!READ_ONCE(mb81->hs_stopping)) {
+		seq = mb81->hs_seq;
+		hp_present = hp_pin &&
+			!!(snd_hda_codec_read(codec, hp_pin, 0,
+					      AC_VERB_GET_PIN_SENSE, 0) &
+			   AC_PINSENSE_PRESENCE);
+		report = cs4208_mb81_hs_detect_locked(codec, hp_present, seq);
+		if (report && !READ_ONCE(mb81->hs_stopping))
+			cs4208_mb81_sync_mic_jack(codec, true);
+	}
+	mutex_unlock(&mb81->hs_mutex);
+out:
+	atomic_dec(&mb81->hs_detecting);
+	snd_hda_power_down(codec);
+}
+
 /* Errata: CS4207 rev C0/C1/C2 Silicon
  *
  * http://www.cirrus.com/en/pubs/errata/ER880C3.pdf
@@ -332,25 +651,62 @@ static void init_digital_coef(struct hda_codec *codec)
 static int cs_init(struct hda_codec *codec)
 {
 	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
 
 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
 		/* init_verb sequence for C0/C1/C2 errata*/
 		snd_hda_sequence_write(codec, cs_errata_init_verbs);
 		snd_hda_sequence_write(codec, cs_coef_init_verbs);
 	} else if (spec->vendor_nid == CS4208_VENDOR_NID) {
-		if (spec->mb81)
+		if (mb81) {
+			mutex_lock(&mb81->hs_mutex);
 			snd_hda_sequence_write(codec, cs4208_mb81_coef_verbs);
-		else
+			mutex_unlock(&mb81->hs_mutex);
+		} else {
 			snd_hda_sequence_write(codec, cs4208_coef_init_verbs);
+		}
 	}
 
 	snd_hda_gen_init(codec);
 
-	if (spec->mb81)
-		cs4208_mb81_update_gpio(codec, true);
-	else if (spec->gpio_mask)
+	if (mb81) {
+		bool hp_present = cs4208_mb81_update_gpio(codec, true);
+
+		mutex_lock(&mb81->hs_mutex);
+		if (!hp_present) {
+			mb81->hs_seq++;
+			cs4208_mb81_reset_hs_state(codec, true);
+			cs4208_mb81_sync_mic_jack(codec, true);
+		} else {
+			/*
+			 * Put the HS switch in the safe 3-pole default and
+			 * re-verify the headset type after 500 ms debounce so
+			 * swaps or insertions during D3 are always detected,
+			 * while keeping hs_mic_present across resume to avoid
+			 * flapping userspace audio routing on runtime idle.
+			 */
+			cs_vendor_coef_set(codec, CS4208_COEF_HSDET_CTL,
+					   CS4208_HSDET_MANUAL_DIS);
+			cs_vendor_coef_set(codec, CS4208_COEF_HS_SWITCH_CTL,
+					   CS4208_HS_SW_HEADPHONE);
+			cs4208_mb81_sync_mic_jack(codec, true);
+			if (!READ_ONCE(mb81->hs_stopping) &&
+			    !atomic_read(&mb81->hs_detecting)) {
+				unsigned long delay = cs_pm_ready(codec) ?
+					msecs_to_jiffies(500) :
+					msecs_to_jiffies(100);
+
+				mb81->hs_seq++;
+				queue_delayed_work(system_freezable_power_efficient_wq,
+						   &mb81->hs_detect_work,
+						   delay);
+			}
+		}
+		mutex_unlock(&mb81->hs_mutex);
+	} else if (spec->gpio_mask) {
 		snd_hda_codec_set_gpio(codec, spec->gpio_mask, spec->gpio_dir,
 				       spec->gpio_data, 0);
+	}
 
 	if (spec->vendor_nid == CS420X_VENDOR_NID) {
 		init_input_coef(codec);
@@ -793,12 +1149,44 @@ static int cs4208_mb81_dig_playback_prepare(struct hda_pcm_stream *hinfo,
 static void cs4208_mb81_hp_cb(struct hda_codec *codec,
 			      struct hda_jack_callback *cb)
 {
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec->mb81;
+
 	cs_automute(codec);
+	if (!mb81 || READ_ONCE(mb81->hs_stopping))
+		return;
+
+	if (cs4208_mb81_hp_present(codec)) {
+		mutex_lock(&mb81->hs_mutex);
+		mb81->hs_seq++;
+		mutex_unlock(&mb81->hs_mutex);
+		/*
+		 * Debounce 3.5 mm TRRS insertion (matches Cirrus INF
+		 * URSettlingTime = 500 ms) so HS3/HS4 contacts are fully
+		 * seated before running impedance/type detection.
+		 */
+		mod_delayed_work(system_freezable_power_efficient_wq,
+				 &mb81->hs_detect_work,
+				 msecs_to_jiffies(500));
+	} else {
+		/*
+		 * On unplug, cancel pending detection and clear pin 0x18
+		 * synchronously under hs_mutex (which is never held across
+		 * sleeps) so userspace sees the disconnect immediately.
+		 */
+		cancel_delayed_work(&mb81->hs_detect_work);
+		mutex_lock(&mb81->hs_mutex);
+		mb81->hs_seq++;
+		cs4208_mb81_reset_hs_state(codec, true);
+		cs4208_mb81_sync_mic_jack(codec, true);
+		mutex_unlock(&mb81->hs_mutex);
+	}
 }
 
 /*
  * MacBook 8,1 drives 4x MAX98357B TDM digital speaker amplifiers via
- * digital pin 0x1d (converter 0x0a) enabled by GPIO0.
+ * digital pin 0x1d (converter 0x0a) enabled by GPIO0, and gates the
+ * combo-jack HS3/HS4 DFETs via GPIO4 and GPIO5.
  */
 static void cs4208_fixup_mb81(struct hda_codec *codec,
 			      const struct hda_fixup *fix, int action)
@@ -828,8 +1216,10 @@ static void cs4208_fixup_mb81(struct hda_codec *codec,
 	struct cs_spec *spec = codec->spec;
 	struct cs4208_mb81_data *mb81;
 	struct hda_jack_callback *cb;
+	struct hda_jack_tbl *jack;
 	struct snd_kcontrol *kctl;
 	hda_nid_t hp_pin, dig_nid;
+	int err;
 
 	switch (action) {
 	case HDA_FIXUP_ACT_PRE_PROBE:
@@ -839,19 +1229,36 @@ static void cs4208_fixup_mb81(struct hda_codec *codec,
 				  "Failed to allocate MacBook 8,1 fixup data\n");
 			return;
 		}
+		mb81->codec = codec;
 		mutex_init(&mb81->gpio_mutex);
+		mutex_init(&mb81->hs_mutex);
+		INIT_DELAYED_WORK(&mb81->hs_detect_work,
+				  cs4208_mb81_hs_detect_work);
+		init_completion(&mb81->hs_stop_comp);
 		spec->mb81 = mb81;
 		snd_hda_apply_pincfgs(codec, pincfgs);
+		codec->single_adc_amp = 1;
+		spec->gen.mic_autoswitch_hook = cs4208_mb81_mic_autoswitch;
 		spec->gen.stream_digital_playback = &cs4208_mb81_dig_playback;
-		spec->gpio_eapd_hp = 0;
-		spec->gpio_eapd_speaker = 1;
-		spec->gpio_mask = 0x01; /* GPIO0 (speaker amp enable) */
-		spec->gpio_dir = 0x01;  /* GPIO0 output */
+		spec->gpio_eapd_hp = 0x30;      /* GPIO4 + GPIO5 (HS3/HS4 DFETs) */
+		spec->gpio_eapd_speaker = 0x31; /* GPIO0 (amp) + GPIO4 + GPIO5 */
+		spec->gpio_mask = 0x31;         /* GPIO0 + GPIO4 + GPIO5 */
+		spec->gpio_dir = 0x31;          /* GPIO0, GPIO4, GPIO5 outputs */
 		spec->gpio_data = spec->gpio_eapd_speaker;
 		break;
 	case HDA_FIXUP_ACT_PROBE:
 		if (!spec->mb81)
 			return;
+		/*
+		 * Keep headset mic pin 0x18 powered in D0 while AFG is active:
+		 * dyn_adc_switch hot-swapping an already-open capture stream
+		 * from 0x06/0x19 to 0x07/0x18 does not update stream_enabled
+		 * on the newly selected input pin, leaving 0x18 in D3.
+		 */
+		err = snd_hda_gen_fix_pin_power(codec, 0x18);
+		if (err < 0)
+			codec_warn(codec,
+				   "Failed to fix pin 0x18 power: %d\n", err);
 		hp_pin = cs4208_mb81_hp_pin(codec);
 		if (hp_pin) {
 			cb = snd_hda_jack_detect_enable_callback(codec, hp_pin,
@@ -877,6 +1284,17 @@ static void cs4208_fixup_mb81(struct hda_codec *codec,
 	case HDA_FIXUP_ACT_BUILD:
 		if (!spec->mb81)
 			return;
+		/*
+		 * Pin 0x18 has no mechanical jack-sense switch; prevent
+		 * snd_hda_jack_report_sync() from reporting it concurrently
+		 * with cs4208_mb81_sync_mic_jack() and disable its hardware
+		 * unsol response (cached in regmap so resume keeps it off).
+		 */
+		jack = snd_hda_jack_tbl_get(codec, 0x18);
+		if (jack)
+			jack->block_report = 1;
+		snd_hda_codec_write_cache(codec, 0x18, 0,
+					  AC_VERB_SET_UNSOLICITED_ENABLE, 0);
 		/*
 		 * Never mirror 2-channel analog HP playback onto the 4-channel
 		 * TDM speaker converter or clamp HP stream capabilities to it.
@@ -982,17 +1400,74 @@ static int cs4208_probe(struct hda_codec *codec)
 	return 0;
 }
 
-static void cs_remove(struct hda_codec *codec)
+static int cs_suspend(struct hda_codec *codec)
 {
 	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec ? spec->mb81 : NULL;
+	bool shutting_down = cs_is_shutting_down();
 
-	if (spec) {
-		kfree(spec->mb81);
+	if (!mb81)
+		return 0;
+
+	if (shutting_down) {
+		/*
+		 * On shutdown, abort any in-flight detection and drain
+		 * hs_mutex without waiting on runtime PM (lock order is
+		 * always runtime PM -> hs_mutex).
+		 */
+		WRITE_ONCE(mb81->hs_stopping, true);
+		complete_all(&mb81->hs_stop_comp);
+	}
+
+	cancel_delayed_work(&mb81->hs_detect_work);
+	mutex_lock(&mb81->hs_mutex);
+	mb81->hs_seq++;
+	if (shutting_down)
+		cs4208_mb81_reset_hs_state(codec, false);
+	mutex_unlock(&mb81->hs_mutex);
+	return 0;
+}
+
+static void cs_remove(struct hda_codec *codec)
+{
+	struct cs_spec *spec = codec->spec;
+	struct cs4208_mb81_data *mb81 = spec ? spec->mb81 : NULL;
+
+	/*
+	 * hda_codec_driver_remove() disables/flushes unsol and jackpoll before
+	 * calling ->remove(); probe failure runs with core.registered == false.
+	 */
+	if (mb81) {
+		WRITE_ONCE(mb81->hs_stopping, true);
+		complete_all(&mb81->hs_stop_comp);
+		disable_delayed_work_sync(&mb81->hs_detect_work);
+		mutex_destroy(&mb81->hs_mutex);
+		mutex_destroy(&mb81->gpio_mutex);
+		kfree(mb81);
 		spec->mb81 = NULL;
 	}
 	snd_hda_gen_remove(codec);
 }
 
+static void cs_unsol_event(struct hda_codec *codec, unsigned int res)
+{
+	struct cs_spec *spec = codec->spec;
+
+	if (!spec || !spec->mb81) {
+		snd_hda_jack_unsol_event(codec, res);
+		return;
+	}
+
+	/*
+	 * Pin runtime PM across jack unsol handling so it cannot overlap
+	 * runtime resume (snd_hda_jack_set_dirty_all -> cs_init) and the
+	 * lock order remains strictly runtime PM -> hs_mutex.
+	 */
+	if (snd_hda_power_up(codec) >= 0)
+		snd_hda_jack_unsol_event(codec, res);
+	snd_hda_power_down(codec);
+}
+
 static int cs_codec_probe(struct hda_codec *codec,
 			  const struct hda_device_id *id)
 {
@@ -1016,10 +1491,11 @@ static int cs_codec_probe(struct hda_codec *codec,
 static const struct hda_codec_ops cs_codec_ops = {
 	.probe = cs_codec_probe,
 	.remove = cs_remove,
+	.suspend = cs_suspend,
 	.build_controls = cs_build_controls,
 	.build_pcms = cs_build_pcms,
 	.init = cs_init,
-	.unsol_event = snd_hda_jack_unsol_event,
+	.unsol_event = cs_unsol_event,
 	.stream_pm = snd_hda_gen_stream_pm,
 };
 
-- 
2.39.5


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

* Re: [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1
  2026-12-01 15:32 ` [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1 Shih-Yuan Lee
@ 2026-12-01 16:04   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-01 16:04 UTC (permalink / raw)
  To: Shih-Yuan Lee; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: hda/cs420x: lock inversion between gpio_mutex and
  runtime PM
--

commit fa5307267e50e6ff89619572855f03b4f32adc6e
Author: Shih-Yuan Lee <fourdollars@debian.org>

ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1

This commit adds a CS4208 fixup for the 12-inch MacBook (MacBook8,1) to
configure its 4-channel TDM digital speaker link, pin defaults, vendor
coefficients, and GPIO0 speaker amplifier control.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=110561

> diff --git a/sound/hda/codecs/cirrus/cs420x.c b/sound/hda/codecs/cirrus/cs420x.c
> index 6cba01228c271..d389fd8961d60 100644
> --- a/sound/hda/codecs/cirrus/cs420x.c
> +++ b/sound/hda/codecs/cirrus/cs420x.c
[ ... ]
> @@ -118,12 +126,55 @@ static inline void cs_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
>   * HP/SPK/SPDIF
>   */
>  
> +static hda_nid_t cs4208_mb81_hp_pin(struct hda_codec *codec)
> +{
> +	struct cs_spec *spec = codec->spec;
> +	const struct auto_pin_cfg *cfg = &spec->gen.autocfg;
> +
> +	if (cfg->hp_outs)
> +		return cfg->hp_pins[0];
> +	if (cfg->line_outs && cfg->line_out_type == AUTO_PIN_HP_OUT)
> +		return cfg->line_out_pins[0];
> +	return 0;
> +}
> +
> +static bool cs4208_mb81_hp_present(struct hda_codec *codec)
> +{
> +	hda_nid_t hp_pin = cs4208_mb81_hp_pin(codec);
> +
> +	return hp_pin && snd_hda_jack_detect(codec, hp_pin);
> +}
> +
> +static void cs4208_mb81_update_gpio(struct hda_codec *codec, bool write_mask)
> +{
> +	struct cs_spec *spec = codec->spec;
> +	struct cs4208_mb81_data *mb81 = spec->mb81;
> +	bool hp_present;
> +
> +	mutex_lock(&mb81->gpio_mutex);
> +	hp_present = cs4208_mb81_hp_present(codec);
> +	spec->gpio_data = hp_present ?
> +		spec->gpio_eapd_hp : spec->gpio_eapd_speaker;
> +	if (write_mask)
> +		snd_hda_codec_set_gpio(codec, spec->gpio_mask, spec->gpio_dir,
> +				       spec->gpio_data, 0);
> +	else
> +		snd_hda_codec_write(codec, 0x01, 0,
> +				    AC_VERB_SET_GPIO_DATA, spec->gpio_data);
> +	mutex_unlock(&mb81->gpio_mutex);
> +}

[Severity: Medium]
Can cs4208_mb81_update_gpio() deadlock between mb81->gpio_mutex and codec
runtime PM?

During runtime resume, hda_codec_runtime_resume() calls cs_init() ->
cs4208_mb81_update_gpio(codec, true), acquiring mb81->gpio_mutex inside
the runtime resume callback.  However, cs4208_mb81_update_gpio() also
issues HDA verbs while holding mb81->gpio_mutex, and each verb in
call_exec_verb() calls snd_hda_power_up_pm(codec) ->
pm_runtime_get_sync():

cs_automute()
  cs4208_mb81_update_gpio(codec, false)
    mutex_lock(&mb81->gpio_mutex)
    snd_hda_codec_write()
      ...
        call_exec_verb()
          snd_hda_power_up_pm()
            pm_runtime_get_sync()
              hda_codec_runtime_resume()
                ...
                  cs_init()
                    cs4208_mb81_update_gpio(codec, true)
                      mutex_lock(&mb81->gpio_mutex)  <-- deadlocks

When cs_automute() runs without the caller pinning runtime PM (such as
from snd_hda_jack_unsol_event(), or from mixer control callbacks like
automute_mode_put() even after commit "ALSA: hda/cs420x: Support headset
mic type detection on MacBook8,1" wraps unsol_event in
snd_hda_power_up()), snd_hda_gen_update_outputs() releases any temporary
per-verb runtime PM reference before returning.  If the codec is runtime
suspended or concurrently resuming when cs4208_mb81_update_gpio() enters
snd_hda_codec_write(), pm_runtime_get_sync() will run or wait for
hda_codec_runtime_resume() and deadlock on mb81->gpio_mutex.

Calling snd_hda_power_up_pm(codec) before mutex_lock(&mb81->gpio_mutex)
and snd_hda_power_down_pm(codec) after mutex_unlock(&mb81->gpio_mutex)
would keep the lock ordering consistent across all paths.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201153214.25157-1-fourdollars@debian.org?part=1

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

end of thread, other threads:[~2026-12-01 16:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:32 [PATCH v1 0/2] ALSA: hda/cs420x: MacBook8,1 speakers and headset mic Shih-Yuan Lee
2026-12-01 15:32 ` [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1 Shih-Yuan Lee
2026-12-01 16:04   ` sashiko-bot
2026-12-01 15:32 ` [PATCH v1 2/2] ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1 Shih-Yuan Lee

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