Skip to contentSkip to conversation

ASoC: rt5659: cancel jack_detect_work on component remove

PATCH New Tristan Madani 1 participant 1 message Oct 29

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 ASoC: rt5659: cancel jack_detect_work on component remove

From: Tristan Madani <[email protected]>

rt5659_remove() resets the codec via regmap_write() but does not cancel the pending jack_detect_work. Since the IRQ is registered with devm_request_threaded_irq(), it remains active until after the component remove callback returns. The IRQ handler (rt5659_irq) schedules jack_detect_work, so a late interrupt can queue work that runs against a reset codec.

Track successful IRQ acquisition with a flag so that disable_irq() only quiesces a line this driver actually owns. Guard the cancel_delayed_work_sync() call with a flag that tracks whether the work was initialized, since INIT_DELAYED_WORK is only called for specific jd_src configurations (JD3, JD_HDA_HEADER) and cancelling an uninitialized work is undefined behavior.

On component remove, disable the IRQ to synchronize with any running handler and prevent new enqueues, then drain the residual jack_detect_work synchronously before resetting the hardware. On the next component probe (card rebind), re-enable the IRQ to restore jack detection.

Fixes d3cb2de2479b ("ASoC: rt5659: add rt5659 codec driver")

---
Changes in v9:
- Guard cancel_delayed_work_sync() with jack_detect_work_inited flag since INIT_DELAYED_WORK is conditional on jd_src (JD3 or HDA_HEADER only), per Mark Brown

Changes in v8:
- Split codec series into individual patches
- Track IRQ acquisition to guard disable_irq/enable_irq

 sound/soc/codecs/rt5659.c | 19 ++++++++++++++++++-
 sound/soc/codecs/rt5659.h |  3 +++
 2 files changed, 21 insertions(+), 1 deletion(-)
sound/soc/codecs/rt5659.c +18 -1
index a061028a16d82..255552af59f7e 100644
@@ -3668,6 +3668,11 @@ static int rt5659_probe(struct snd_soc_component *component)
 
 	rt5659->component = component;
 
+	if (rt5659->irq_disabled) {
+		enable_irq(to_i2c_client(component->dev)->irq);
+		rt5659->irq_disabled = false;
+	}
+
 	switch (rt5659->pdata.jd_src) {
 	case RT5659_JD_HDA_HEADER:
 		break;
@@ -3685,7 +3690,15 @@ static int rt5659_probe(struct snd_soc_component *component)
 static void rt5659_remove(struct snd_soc_component *component)
 {
 	struct rt5659_priv *rt5659 = snd_soc_component_get_drvdata(component);
+	struct i2c_client *i2c = to_i2c_client(component->dev);
+
+	if (rt5659->irq_acquired) {
+		disable_irq(i2c->irq);
+		rt5659->irq_disabled = true;
+	}
 
+	if (rt5659->jack_detect_work_inited)
+		cancel_delayed_work_sync(&rt5659->jack_detect_work);
 	regmap_write(rt5659->regmap, RT5659_RESET, 0);
 }
 
@@ -4272,6 +4285,7 @@ static int rt5659_i2c_probe(struct i2c_client *i2c)
 		regmap_write(rt5659->regmap, RT5659_IRQ_CTRL_2, 0x0040);
 		INIT_DELAYED_WORK(&rt5659->jack_detect_work,
 			rt5659_jack_detect_work);
+		rt5659->jack_detect_work_inited = true;
 		break;
 	case RT5659_JD_HDA_HEADER:
 		regmap_write(rt5659->regmap, RT5659_GPIO_CTRL_3, 0x8000);
@@ -4281,6 +4295,7 @@ static int rt5659_i2c_probe(struct i2c_client *i2c)
 		regmap_write(rt5659->regmap, RT5659_IRQ_CTRL_1,  0x0040);
 		INIT_DELAYED_WORK(&rt5659->jack_detect_work,
 			rt5659_jack_detect_intel_hd_header);
+		rt5659->jack_detect_work_inited = true;
 		rt5659_intel_hd_header_probe_setup(rt5659);
 		break;
 	default:
@@ -4291,7 +4306,9 @@ static int rt5659_i2c_probe(struct i2c_client *i2c)
 		ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
 			rt5659_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
 			| IRQF_ONESHOT, "rt5659", rt5659);
-		if (ret)
+		if (!ret)
+			rt5659->irq_acquired = true;
+		else
 			dev_err(&i2c->dev, "Failed to reguest IRQ: %d\n", ret);
 
 		/* Enable IRQ output for GPIO1 pin any way */
sound/soc/codecs/rt5659.h +3 -0
index b49fd8baf4e7e..e10b3844501b9 100644
@@ -1813,6 +1813,9 @@ struct rt5659_priv {
 	int jack_type;
 	bool hda_hp_plugged;
 	bool hda_mic_plugged;
+	bool irq_acquired;
+	bool irq_disabled;
+	bool jack_detect_work_inited;
 };
 
 int rt5659_set_jack_detect(struct snd_soc_component *component,
Signature · 1 line
2.47.3