Repository navigation
Conversation
Move the bodies of the tip plug and unplug cases of cs42l84_irq_thread() into cs42l84_handle_plug() and cs42l84_handle_unplug(), so that the following changes can run them outside the interrupt as well. The register writes, their order and the debug messages are unchanged. No functional change. Signed-off-by: LiamRay11 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
When the tip sense no longer reads plugged after headset detection, the plug path sets tip_state back to unplugged and turns HSBIAS off, but keeps hs_type and the jack report that detection just made, and then unmasks the ring sense interrupts. If the tip then settles unplugged, the next tip interrupt sees no change against tip_state and does nothing, so the jack stays reported as plugged with nothing in it. A ring interrupt in that state, or the no-change tip interrupt falling through to the ring check, runs headset detection again with the jack empty. Treat the invalidated detection as an unplug: turn HSBIAS off, clear hs_type, report the jack empty and leave the ring interrupts masked. If the tip then settles plugged, its interrupt is a change and detects again, as before. Also run the ring check only while the tip is plugged: with the tip unplugged or in transition there is nothing to detect. Signed-off-by: LiamRay11 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
The driver learns of a plug only from its plug interrupt, which acts
on a change of the tip sense against the state saved at probe. That
leaves the jack reported wrongly in two cases:
- Headphones already in at boot. The probe saves the tip as plugged,
no headset detection runs and hs_type stays 0, so set_jack reports
the jack empty until the plug is pulled and pushed in again.
- A plug or unplug during a sleep that leaves the codec powered
(suspend-to-idle). The interrupt is not a wake source and nothing
reads the tip sense again on resume, so headphones pulled out
while asleep still read plugged afterwards. A sleep that puts the
codec in reset already detects again on resume.
Add cs42l84_sync_plug(), which reads the tip sense once and runs the
interrupt's own plug or unplug path when it disagrees with the
driver's state. It runs under irq_lock, like the interrupt thread, so
an interrupt for the same change finds the state already updated.
Call it from set_jack before the first report, and from a work item
that resume queues on system_freezable_wq when the codec kept its
power, so it runs after every device has resumed without adding the
headset detection to the resume path. Suspend cancels a check that is
still pending.
On a machine booted without headphones this adds one register read
at set_jack and after each such sleep.
Tested on a MacBook Pro (M6, J804), on an earlier base
(integration/aurora f5fa539) with an earlier version of this change:
booted without headphones, the attach check reads the tip unplugged and
does nothing more.
Signed-off-by: LiamRay11 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sithglan
left a comment
There was a problem hiding this comment.
Test report, 2026-11-17: Apple Mac mini M6 (J873g/T8152, board 0x24). PR revision tested: 9396cd421efd94f9573103e5d9597dda86ac6d4f.
Host validation used the combined #2105–2108/#2110–2112 series (54742c617db4 on 677be395b9d2) with Clang 22. All changed driver objects built with W=1 and no new warnings; two pre-existing DART unused-helper warnings were unchanged.
cs42l84.o built with Clang 22 and W=1 with no new warnings. Hardware smoke used this patch plus its existing upstream sleep prerequisite b912083 on the separate M6 test kernel. CS42L84 revision 36 bound successfully at 0-004b; 25 ALSA controls were readable and both jack controls reported off.
Physical insertion/removal, plugged-at-boot detection and jack changes across sleep were not exercised. This is attach/control smoke coverage, not full qualification of the jack-state fix.
|
Blockers: 2.
Code checked and sound: HW row: MacBook Pro J804 (M6), plus one M2/M3 CS42L84 laptop, on this head with default bootargs. In each case Assisted-by: LLM (Claude:Opus 5.5) |
|
Since this branch was cut, #1895 (2f122e3) added ring-sense plug detection selected by |
|
The jack state refresh at attach and resume is still needed. Head |
RyanTheTide
left a comment
There was a problem hiding this comment.
cs42l84_handle_plug() expands the post-detection status read from the IRQ path to jack attach and resume. That read can fail, and in that case its output does not give a tip state. Please handle the read error explicitly. The driver should unwind the tentative headset detection, keep ring IRQs masked and preserve an unsettled tip state so the next valid event can reconcile it.
A host test of the exact helper passed stable plug and unplug but failed the read-error case. With an explicit error branch that reports the tentative detection as cleared and leaves the tip in transition, all three passed. This exercises the source-level state decision. A jack inserted before probe and a plug/unplug during retained s2idle still need physical CS42L84 rows.
|
@LiamRay11 checking open drafts today. To come out of draft, this still needs:
If it's no longer needed, say so here and we'll close it. Otherwise it stays open as a draft until these are done. |
|
I prepared a port for this draft on current main The draft still needs the physical plugged-at-attach, retained-wake plug/unplug and normal replug rows. The J413/J473 tip rows and a ring-detect row are queued for a staffed test. Here is the source diff for the existing PR. Patch against current maindiff --git a/sound/soc/codecs/cs42l84.c b/sound/soc/codecs/cs42l84.c
--- a/sound/soc/codecs/cs42l84.c
+++ b/sound/soc/codecs/cs42l84.c
@@ -61,6 +61,7 @@
unsigned int remote_btn1_prev;
unsigned int remote_short_prev;
struct delayed_work remote_poll_work;
+ struct work_struct resume_work;
bool sleep_reset;
};
@@ -301,6 +302,8 @@
{"SDOUT1", NULL, "BCLK"},
};
+static void cs42l84_sync_plug(struct cs42l84_private *cs42l84);
+
static int cs42l84_set_jack(struct snd_soc_component *component, struct snd_soc_jack *jk, void *d)
{
struct cs42l84_private *cs42l84 = snd_soc_component_get_drvdata(component);
@@ -308,6 +311,8 @@
/* Prevent race with interrupt handler */
guard(mutex)(&cs42l84->irq_lock);
cs42l84->jack = jk;
+ if (jk && !cs42l84->sleep_reset)
+ cs42l84_sync_plug(cs42l84);
snd_soc_jack_report(jk, cs42l84->hs_type, SND_JACK_HEADSET);
return 0;
@@ -991,6 +996,81 @@
val);
}
+/* The caller holds irq_lock. Ring-detect boards leave tip sense masked. */
+static u8 cs42l84_plug_state(struct cs42l84_private *cs42l84, unsigned int reg)
+{
+ if (cs42l84->ring_detect)
+ return FIELD_GET(CS42L84_RS_PLUG | CS42L84_RS_UNPLUG, reg);
+
+ return FIELD_GET(CS42L84_TS_PLUG | CS42L84_TS_UNPLUG, reg);
+}
+
+static void cs42l84_clear_jack(struct cs42l84_private *cs42l84)
+{
+ cs42l84_revert_hs(cs42l84);
+ cs42l84->hs_type = 0;
+ snd_soc_jack_report(cs42l84->jack, 0, SND_JACK_HEADSET);
+ cs42l84_set_interrupt_masks(cs42l84, cs42l84->ring_detect ?
+ CS42L84_TS_PLUG | CS42L84_TS_UNPLUG :
+ CS42L84_RS_PLUG | CS42L84_RS_UNPLUG);
+}
+
+static void cs42l84_detect_stable_plug(struct cs42l84_private *cs42l84)
+{
+ unsigned int reg;
+ u8 state = CS42L84_TRANS;
+
+ cs42l84_detect_hs(cs42l84);
+ if (!regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®))
+ state = cs42l84_plug_state(cs42l84, reg);
+
+ if (state != CS42L84_PLUG) {
+ cs42l84->tip_state = state;
+ cs42l84->ring_state = cs42l84->ring_detect ? state : CS42L84_UNPLUG;
+ cs42l84_clear_jack(cs42l84);
+ return;
+ }
+
+ if (!cs42l84->ring_detect)
+ cs42l84_set_interrupt_masks(cs42l84, 0);
+}
+
+/* Reconcile an attach or wake even when no interrupt was delivered. */
+static void cs42l84_sync_plug(struct cs42l84_private *cs42l84)
+{
+ unsigned int reg;
+ u8 state;
+
+ if (regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®))
+ return;
+
+ state = cs42l84_plug_state(cs42l84, reg);
+ if (state == CS42L84_TRANS)
+ return;
+
+ if (cs42l84->ring_detect)
+ cs42l84->ring_state = state;
+ if (state == CS42L84_PLUG &&
+ (cs42l84->tip_state != state || !cs42l84->hs_type)) {
+ cs42l84->tip_state = state;
+ cs42l84_detect_stable_plug(cs42l84);
+ } else if (state == CS42L84_UNPLUG &&
+ (cs42l84->tip_state != state || cs42l84->hs_type)) {
+ cs42l84->tip_state = state;
+ cs42l84->ring_state = state;
+ cs42l84_clear_jack(cs42l84);
+ }
+}
+
+static void cs42l84_resume_work(struct work_struct *work)
+{
+ struct cs42l84_private *cs42l84 =
+ container_of(work, struct cs42l84_private, resume_work);
+
+ guard(mutex)(&cs42l84->irq_lock);
+ cs42l84_sync_plug(cs42l84);
+}
+
static irqreturn_t cs42l84_irq_thread(int irq, void *data)
{
struct cs42l84_private *cs42l84 = (struct cs42l84_private *)data;
@@ -1020,7 +1100,8 @@
* asserted for good.
*/
regmap_read(cs42l84->regmap, CS42L84_REMOTE_BUTTON_STATUS1, ®);
- regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®);
+ if (regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®))
+ return IRQ_HANDLED;
current_ring_state = (((char)reg) &
(CS42L84_RS_PLUG | CS42L84_RS_UNPLUG)) >>
CS42L84_RS_PLUG_SHIFT;
@@ -1031,7 +1112,7 @@
case CS42L84_PLUG:
dev_dbg(cs42l84->dev, "Plug event (ring sense)\n");
cs42l84->tip_state = CS42L84_PLUG;
- cs42l84_detect_hs(cs42l84);
+ cs42l84_detect_stable_plug(cs42l84);
break;
case CS42L84_UNPLUG:
dev_dbg(cs42l84->dev, "Unplug event (ring sense)\n");
@@ -1054,7 +1135,8 @@
*/
if ((~masks[0]) & irq_params_table[0].mask) {
- regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®);
+ if (regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®))
+ return IRQ_HANDLED;
current_tip_state = (((char) reg) &
(CS42L84_TS_PLUG | CS42L84_TS_UNPLUG)) >>
@@ -1065,28 +1147,7 @@
switch (current_tip_state) {
case CS42L84_PLUG:
dev_dbg(cs42l84->dev, "Plug event\n");
-
- cs42l84_detect_hs(cs42l84);
-
- /*
- * Check the tip sense status again, and possibly invalidate
- * the detection result
- *
- * Thanks to debounce, this should reliably indicate if the tip
- * was disconnected at any point during the detection procedure.
- */
- regmap_read(cs42l84->regmap, CS42L84_TSRS_PLUG_STATUS, ®);
- current_tip_state = (((char) reg) &
- (CS42L84_TS_PLUG | CS42L84_TS_UNPLUG)) >>
- CS42L84_TS_PLUG_SHIFT;
- if (current_tip_state != CS42L84_PLUG) {
- dev_dbg(cs42l84->dev, "Wobbly connection, detection invalidated\n");
- cs42l84->tip_state = CS42L84_UNPLUG;
- cs42l84_revert_hs(cs42l84);
- }
-
- /* Unmask ring sense interrupts */
- cs42l84_set_interrupt_masks(cs42l84, 0);
+ cs42l84_detect_stable_plug(cs42l84);
break;
case CS42L84_UNPLUG:
cs42l84->ring_state = CS42L84_UNPLUG;
@@ -1114,7 +1175,8 @@
(CS42L84_RS_PLUG | CS42L84_RS_UNPLUG)) >>
CS42L84_RS_PLUG_SHIFT;
- if (current_ring_state != cs42l84->ring_state) {
+ if (cs42l84->tip_state == CS42L84_PLUG &&
+ current_ring_state != cs42l84->ring_state) {
cs42l84->ring_state = current_ring_state;
if (current_ring_state == CS42L84_PLUG)
cs42l84_detect_hs(cs42l84);
@@ -1254,6 +1316,11 @@
ret = devm_delayed_work_autocancel(&i2c_client->dev,
&cs42l84->remote_poll_work,
cs42l84_remote_poll);
+ if (ret)
+ return ret;
+
+ ret = devm_work_autocancel(&i2c_client->dev, &cs42l84->resume_work,
+ cs42l84_resume_work);
if (ret)
return ret;
@@ -1389,6 +1456,7 @@
if (i2c_client->irq)
free_irq(i2c_client->irq, cs42l84);
+ cancel_work_sync(&cs42l84->resume_work);
cancel_delayed_work_sync(&cs42l84->remote_poll_work);
gpiod_set_value_cansleep(cs42l84->reset_gpio, 0);
@@ -1477,6 +1545,7 @@
struct i2c_client *i2c_client = to_i2c_client(dev);
struct cs42l84_private *cs42l84 = i2c_get_clientdata(i2c_client);
+ cancel_work_sync(&cs42l84->resume_work);
if (!pm_suspend_via_firmware())
return 0;
@@ -1506,8 +1575,10 @@
unsigned int reg;
int ret, devid;
- if (!cs42l84->sleep_reset)
+ if (!cs42l84->sleep_reset) {
+ queue_work(system_freezable_wq, &cs42l84->resume_work);
return 0;
+ }
mutex_lock(&cs42l84->irq_lock);
|
Summary
cs42l84 learns of a plug only from a plug-sense interrupt that acts on a change. That leaves the jack reported wrong in three cases:
b91208392("Bring the codec back after a firmware sleep") already re-detects after a firmware sleep that resets the codec. This PR covers the other cases. It is generic, with no machine gate.Changes
Move the plug/unplug bodies of the IRQ thread into
cs42l84_handle_plug()/cs42l84_handle_unplug(). No functional change.If detection is invalidated because the tip did not stay plugged, treat it as an unplug: HSBIAS off,
hs_type0, jack reported empty, ring IRQs stay masked. Previously the report stayed "plugged" with nothing in the jack. The ring check now also runs only while the tip is plugged.cs42l84_sync_plug()reads the tip sense once and runs the IRQ's own path if it disagrees with the driver state. It is called:set_jackbefore the first reportsystem_freezable_wqthat resume queues when the codec kept powerSuspend cancels a pending check. Everything runs under
irq_lock.Testing
On hardware: MacBook Pro 14-inch (M6, J804), 2026-11-16, tested by LiamRay11.
bf423bf0ewith all nine J804 linux PRs merged (t8152-mailboxes, dart-gen3, dt-board, centauri, g18-fast-retire, cfg80211-stale-scan, smc, cs42l84-jack, and an NVMe PR that is not sent yet), plus retest-only device-tree changes that are not part of these PRs: SSD nodes (ANS mailbox, SART, NVMe), PCIe port 0 with the N1 Wi-Fi, the USB0 gadget console, and aread-onlyNVRAM partition. Those nodes were needed to reach the root filesystem and SSH.systemctl rebootfrom this kernel (watchdog restart) returned to the installed system.Results for this PR (draft): not run. The change was built into the retest kernel, but the jack codec is disabled in that tree and the plug test needs the owner at the machine, so it was not run. This stays a draft until it is.
sound/soc/codecs/cs42l84.owithW=1builds without warnings.integration/aurora has since moved to
677be395b; this branch still merges into it without conflicts.Notes
This affects every Mac with a CS42L84. A test on another machine (for example an M2/M3 laptop) would be welcome.
Dependencies
None.
🤖 Generated with Claude Code