All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shih-Yuan Lee <fourdollars@debian.org>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: Shih-Yuan Lee <fourdollars@debian.org>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1
Date: Tue,  1 Dec 2026 23:32:12 +0800	[thread overview]
Message-ID: <20261201153214.25157-2-fourdollars@debian.org> (raw)
In-Reply-To: <20261201153214.25157-1-fourdollars@debian.org>

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


  reply	other threads:[~2026-12-01 15:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-01 16:04   ` [PATCH v1 1/2] ALSA: hda/cs420x: Add CS4208 TDM speaker fixup for MacBook8,1 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201153214.25157-2-fourdollars@debian.org \
    --to=fourdollars@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.