From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=82.195.75.108 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796139164; b=QI320JCOfNAA7pbngTVsVakyByioxZmNiHyJYW6+5APqB/ot6lH8rWjszqj+npF78r5q dSoWfAcJ0fUuBKA4k8+sE7GO9AXnybN868t1ui6HnZXeZHnHXGzeJg5K+JR86giIlbp+H hsSRTwYHdVtlE0sLT7oTBWdA8VLSkXnVH0iIERZdPv8ObZke7vXRcc4RZPXzO86zKcEPS LzuiaHkVWWfmNtlNCoNJktM0/Xz2i9bQMEo4/lrLjIkqcOoS4FwwCeHUslf51AB9qHAbn s951bAtmJ7ZZh6TFap51+cmqNDx50MY+rUK4aYVZtAaA2KPsfbc8LLblFEiu/xV0usQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139164; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Debian-User; bh=KsTxPnp3p7Fwznjp6DPdxArTQ6vxSXTVX7vYA1gaOq8=; b=jhHXBx/tDg3gO426gPWYl/HGhFDN2xcdFJn9tinfgiBhMNq5q5u8RuunRB51OTT7Bpa4 BLkUg55AoKpnAK7378SRbysa8P5E2UN3at+HA5SZUJasFE76CdtTTZORhMX6ZJMivRAUW JPpisEuKCAW0H4qESTR+Ca322aQxNW7GzAoofl7JRxnPz+GoO2OoXXfV6iaGdqlcGWW8g mC1TsiZGZgcFW1HHDCvdP1rz+aG2LACaj1KQsy4KQK1QKexp8b/k+SmpcUD5uDRgkV//e 7GW1r4Aso6nqpHtPptfFWlg2aemWFbl9jEF/wqgwIpU3gi2boiWHT5wuFQ4XnfgCjyQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=debian.org; spf=pass smtp.mailfrom=debian.org; dkim=pass header.d=debian.org header.i=@debian.org header.a=rsa-sha256 header.s=smtpauto.stravinsky header.b=tdq88i6H; arc=none smtp.remote-ip=82.195.75.108 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0421A606A33E; Tue, 1 Dec 2026 15:32:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0421A606A33E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.a=rsa-sha256 header.s=smtpauto.stravinsky header.b=tdq88i6H DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Reply-To: Content-Type:Content-ID:Content-Description; bh=KsTxPnp3p7Fwznjp6DPdxArTQ6vxSXTVX7vYA1gaOq8=; b=tdq88i6HxYfJ1uCleY2663lKav RToZ9kaoRDd6w7VUTsQJ76PuaEp1HQolBNlH9FMpEhSYtL72tMC72ql/4GA3B8KIodgM6nymE5iap cBFL6YpNlk148ygPdq6+4azuY91vmay+zGqNOs1KI0Gt9CqcJr4RFYZX21qK72aEWzNiwcxuQOpNC Sawle0Vrw4NthIcAWEOTr2WhdNBCF9znvnOStBU8oVYB1Q13x/MK4vMlxAJkI6wUXuzSWUPv0KIFO 5+kp5eCiHaAyQQ/Qg8VRX0HBcqa6maKtCUmv2q/N/mcfUvcOMmf1i88FrEcbryiB4vlHIVyxLJPss 6Dwsa3Zg==; Received: from authenticated-user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1xYPqR-000q8r-1u; Tue, 01 Dec 2026 15:32:40 +0000 From: Shih-Yuan Lee To: Takashi Iwai , Jaroslav Kysela Cc: Shih-Yuan Lee , linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 2/2] ALSA: hda/cs420x: Support headset mic type detection on MacBook8,1 Date: Tue, 1 Dec 2026 23:32:13 +0800 Message-Id: <20261201153214.25157-3-fourdollars@debian.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20261201153214.25157-1-fourdollars@debian.org> References: <20261201153214.25157-1-fourdollars@debian.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Debian-User: fourdollars 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 --- 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 */ +#include +#include #include #include #include #include +#include +#include #include #include #include @@ -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